]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/combine.c
Merge in gcc2-ss-010999
[thirdparty/gcc.git] / gcc / combine.c
CommitLineData
230d793d 1/* Optimize by combining instructions for GNU compiler.
1bf27b5b 2 Copyright (C) 1987, 88, 92-98, 1999 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"
670ee920 78#include "system.h"
c5c76735 79#include "rtl.h"
230d793d
RS
80#include "flags.h"
81#include "regs.h"
55310dad 82#include "hard-reg-set.h"
230d793d
RS
83#include "basic-block.h"
84#include "insn-config.h"
49ad7cfa 85#include "function.h"
d6f4ec51
KG
86/* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
87#include "expr.h"
230d793d
RS
88#include "insn-flags.h"
89#include "insn-codes.h"
90#include "insn-attr.h"
91#include "recog.h"
92#include "real.h"
2e107e9e 93#include "toplev.h"
230d793d
RS
94
95/* It is not safe to use ordinary gen_lowpart in combine.
96 Use gen_lowpart_for_combine instead. See comments there. */
97#define gen_lowpart dont_use_gen_lowpart_you_dummy
98
99/* Number of attempts to combine instructions in this function. */
100
101static int combine_attempts;
102
103/* Number of attempts that got as far as substitution in this function. */
104
105static int combine_merges;
106
107/* Number of instructions combined with added SETs in this function. */
108
109static int combine_extras;
110
111/* Number of instructions combined in this function. */
112
113static int combine_successes;
114
115/* Totals over entire compilation. */
116
117static int total_attempts, total_merges, total_extras, total_successes;
9210df58 118
ddd5a7c1 119/* Define a default value for REVERSIBLE_CC_MODE.
9210df58
RK
120 We can never assume that a condition code mode is safe to reverse unless
121 the md tells us so. */
122#ifndef REVERSIBLE_CC_MODE
123#define REVERSIBLE_CC_MODE(MODE) 0
124#endif
230d793d
RS
125\f
126/* Vector mapping INSN_UIDs to cuids.
5089e22e 127 The cuids are like uids but increase monotonically always.
230d793d
RS
128 Combine always uses cuids so that it can compare them.
129 But actually renumbering the uids, which we used to do,
130 proves to be a bad idea because it makes it hard to compare
131 the dumps produced by earlier passes with those from later passes. */
132
133static int *uid_cuid;
4255220d 134static int max_uid_cuid;
230d793d
RS
135
136/* Get the cuid of an insn. */
137
1427d6d2
RK
138#define INSN_CUID(INSN) \
139(INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
230d793d
RS
140
141/* Maximum register number, which is the size of the tables below. */
142
143static int combine_max_regno;
144
145/* Record last point of death of (hard or pseudo) register n. */
146
147static rtx *reg_last_death;
148
149/* Record last point of modification of (hard or pseudo) register n. */
150
151static rtx *reg_last_set;
152
153/* Record the cuid of the last insn that invalidated memory
154 (anything that writes memory, and subroutine calls, but not pushes). */
155
156static int mem_last_set;
157
158/* Record the cuid of the last CALL_INSN
159 so we can tell whether a potential combination crosses any calls. */
160
161static int last_call_cuid;
162
163/* When `subst' is called, this is the insn that is being modified
164 (by combining in a previous insn). The PATTERN of this insn
165 is still the old pattern partially modified and it should not be
166 looked at, but this may be used to examine the successors of the insn
167 to judge whether a simplification is valid. */
168
169static rtx subst_insn;
170
0d9641d1
JW
171/* This is an insn that belongs before subst_insn, but is not currently
172 on the insn chain. */
173
174static rtx subst_prev_insn;
175
230d793d
RS
176/* This is the lowest CUID that `subst' is currently dealing with.
177 get_last_value will not return a value if the register was set at or
178 after this CUID. If not for this mechanism, we could get confused if
179 I2 or I1 in try_combine were an insn that used the old value of a register
180 to obtain a new value. In that case, we might erroneously get the
181 new value of the register when we wanted the old one. */
182
183static int subst_low_cuid;
184
6e25d159
RK
185/* This contains any hard registers that are used in newpat; reg_dead_at_p
186 must consider all these registers to be always live. */
187
188static HARD_REG_SET newpat_used_regs;
189
abe6e52f
RK
190/* This is an insn to which a LOG_LINKS entry has been added. If this
191 insn is the earlier than I2 or I3, combine should rescan starting at
192 that location. */
193
194static rtx added_links_insn;
195
0d4d42c3
RK
196/* Basic block number of the block in which we are performing combines. */
197static int this_basic_block;
230d793d
RS
198\f
199/* The next group of arrays allows the recording of the last value assigned
200 to (hard or pseudo) register n. We use this information to see if a
5089e22e 201 operation being processed is redundant given a prior operation performed
230d793d
RS
202 on the register. For example, an `and' with a constant is redundant if
203 all the zero bits are already known to be turned off.
204
205 We use an approach similar to that used by cse, but change it in the
206 following ways:
207
208 (1) We do not want to reinitialize at each label.
209 (2) It is useful, but not critical, to know the actual value assigned
210 to a register. Often just its form is helpful.
211
212 Therefore, we maintain the following arrays:
213
214 reg_last_set_value the last value assigned
215 reg_last_set_label records the value of label_tick when the
216 register was assigned
217 reg_last_set_table_tick records the value of label_tick when a
218 value using the register is assigned
219 reg_last_set_invalid set to non-zero when it is not valid
220 to use the value of this register in some
221 register's value
222
223 To understand the usage of these tables, it is important to understand
224 the distinction between the value in reg_last_set_value being valid
225 and the register being validly contained in some other expression in the
226 table.
227
228 Entry I in reg_last_set_value is valid if it is non-zero, and either
229 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
230
231 Register I may validly appear in any expression returned for the value
232 of another register if reg_n_sets[i] is 1. It may also appear in the
233 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
234 reg_last_set_invalid[j] is zero.
235
236 If an expression is found in the table containing a register which may
237 not validly appear in an expression, the register is replaced by
238 something that won't match, (clobber (const_int 0)).
239
240 reg_last_set_invalid[i] is set non-zero when register I is being assigned
241 to and reg_last_set_table_tick[i] == label_tick. */
242
0f41302f 243/* Record last value assigned to (hard or pseudo) register n. */
230d793d
RS
244
245static rtx *reg_last_set_value;
246
247/* Record the value of label_tick when the value for register n is placed in
248 reg_last_set_value[n]. */
249
568356af 250static int *reg_last_set_label;
230d793d
RS
251
252/* Record the value of label_tick when an expression involving register n
0f41302f 253 is placed in reg_last_set_value. */
230d793d 254
568356af 255static int *reg_last_set_table_tick;
230d793d
RS
256
257/* Set non-zero if references to register n in expressions should not be
258 used. */
259
260static char *reg_last_set_invalid;
261
0f41302f 262/* Incremented for each label. */
230d793d 263
568356af 264static int label_tick;
230d793d
RS
265
266/* Some registers that are set more than once and used in more than one
267 basic block are nevertheless always set in similar ways. For example,
268 a QImode register may be loaded from memory in two places on a machine
269 where byte loads zero extend.
270
951553af 271 We record in the following array what we know about the nonzero
230d793d
RS
272 bits of a register, specifically which bits are known to be zero.
273
274 If an entry is zero, it means that we don't know anything special. */
275
55310dad 276static unsigned HOST_WIDE_INT *reg_nonzero_bits;
230d793d 277
951553af 278/* Mode used to compute significance in reg_nonzero_bits. It is the largest
5f4f0e22 279 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
230d793d 280
951553af 281static enum machine_mode nonzero_bits_mode;
230d793d 282
d0ab8cd3
RK
283/* Nonzero if we know that a register has some leading bits that are always
284 equal to the sign bit. */
285
286static char *reg_sign_bit_copies;
287
951553af 288/* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
1a26b032
RK
289 It is zero while computing them and after combine has completed. This
290 former test prevents propagating values based on previously set values,
291 which can be incorrect if a variable is modified in a loop. */
230d793d 292
951553af 293static int nonzero_sign_valid;
55310dad
RK
294
295/* These arrays are maintained in parallel with reg_last_set_value
296 and are used to store the mode in which the register was last set,
297 the bits that were known to be zero when it was last set, and the
298 number of sign bits copies it was known to have when it was last set. */
299
300static enum machine_mode *reg_last_set_mode;
301static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
302static char *reg_last_set_sign_bit_copies;
230d793d
RS
303\f
304/* Record one modification to rtl structure
305 to be undone by storing old_contents into *where.
306 is_int is 1 if the contents are an int. */
307
308struct undo
309{
241cea85 310 struct undo *next;
230d793d 311 int is_int;
f5393ab9
RS
312 union {rtx r; int i;} old_contents;
313 union {rtx *r; int *i;} where;
230d793d
RS
314};
315
316/* Record a bunch of changes to be undone, up to MAX_UNDO of them.
317 num_undo says how many are currently recorded.
318
319 storage is nonzero if we must undo the allocation of new storage.
320 The value of storage is what to pass to obfree.
321
322 other_insn is nonzero if we have modified some other insn in the process
241cea85 323 of working on subst_insn. It must be verified too.
230d793d 324
241cea85
RK
325 previous_undos is the value of undobuf.undos when we started processing
326 this substitution. This will prevent gen_rtx_combine from re-used a piece
327 from the previous expression. Doing so can produce circular rtl
328 structures. */
230d793d
RS
329
330struct undobuf
331{
230d793d 332 char *storage;
241cea85
RK
333 struct undo *undos;
334 struct undo *frees;
335 struct undo *previous_undos;
230d793d
RS
336 rtx other_insn;
337};
338
339static struct undobuf undobuf;
340
cc876596 341/* Substitute NEWVAL, an rtx expression, into INTO, a place in some
230d793d 342 insn. The substitution can be undone by undo_all. If INTO is already
cc876596
RK
343 set to NEWVAL, do not record this change. Because computing NEWVAL might
344 also call SUBST, we have to compute it before we put anything into
345 the undo table. */
230d793d
RS
346
347#define SUBST(INTO, NEWVAL) \
241cea85
RK
348 do { rtx _new = (NEWVAL); \
349 struct undo *_buf; \
350 \
351 if (undobuf.frees) \
352 _buf = undobuf.frees, undobuf.frees = _buf->next; \
353 else \
354 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
355 \
356 _buf->is_int = 0; \
357 _buf->where.r = &INTO; \
358 _buf->old_contents.r = INTO; \
359 INTO = _new; \
360 if (_buf->old_contents.r == INTO) \
361 _buf->next = undobuf.frees, undobuf.frees = _buf; \
362 else \
363 _buf->next = undobuf.undos, undobuf.undos = _buf; \
230d793d
RS
364 } while (0)
365
241cea85
RK
366/* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
367 for the value of a HOST_WIDE_INT value (including CONST_INT) is
368 not safe. */
230d793d
RS
369
370#define SUBST_INT(INTO, NEWVAL) \
241cea85
RK
371 do { struct undo *_buf; \
372 \
373 if (undobuf.frees) \
374 _buf = undobuf.frees, undobuf.frees = _buf->next; \
375 else \
376 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
377 \
378 _buf->is_int = 1; \
379 _buf->where.i = (int *) &INTO; \
380 _buf->old_contents.i = INTO; \
381 INTO = NEWVAL; \
382 if (_buf->old_contents.i == INTO) \
383 _buf->next = undobuf.frees, undobuf.frees = _buf; \
384 else \
385 _buf->next = undobuf.undos, undobuf.undos = _buf; \
230d793d
RS
386 } while (0)
387
388/* Number of times the pseudo being substituted for
389 was found and replaced. */
390
391static int n_occurrences;
392
c5ad722c
RK
393static void init_reg_last_arrays PROTO((void));
394static void setup_incoming_promotions PROTO((void));
fe2db4fb
RK
395static void set_nonzero_bits_and_sign_copies PROTO((rtx, rtx));
396static int can_combine_p PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
e009aaf3 397static int sets_function_arg_p PROTO((rtx));
fe2db4fb
RK
398static int combinable_i3pat PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
399static rtx try_combine PROTO((rtx, rtx, rtx));
400static void undo_all PROTO((void));
401static rtx *find_split_point PROTO((rtx *, rtx));
402static rtx subst PROTO((rtx, rtx, rtx, int, int));
8079805d
RK
403static rtx simplify_rtx PROTO((rtx, enum machine_mode, int, int));
404static rtx simplify_if_then_else PROTO((rtx));
405static rtx simplify_set PROTO((rtx));
406static rtx simplify_logical PROTO((rtx, int));
fe2db4fb
RK
407static rtx expand_compound_operation PROTO((rtx));
408static rtx expand_field_assignment PROTO((rtx));
409static rtx make_extraction PROTO((enum machine_mode, rtx, int, rtx, int,
410 int, int, int));
71923da7 411static rtx extract_left_shift PROTO((rtx, int));
fe2db4fb
RK
412static rtx make_compound_operation PROTO((rtx, enum rtx_code));
413static int get_pos_from_mask PROTO((unsigned HOST_WIDE_INT, int *));
6139ff20 414static rtx force_to_mode PROTO((rtx, enum machine_mode,
e3d616e3 415 unsigned HOST_WIDE_INT, rtx, int));
abe6e52f 416static rtx if_then_else_cond PROTO((rtx, rtx *, rtx *));
fe2db4fb 417static rtx known_cond PROTO((rtx, enum rtx_code, rtx, rtx));
e11fa86f 418static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
fe2db4fb
RK
419static rtx make_field_assignment PROTO((rtx));
420static rtx apply_distributive_law PROTO((rtx));
421static rtx simplify_and_const_int PROTO((rtx, enum machine_mode, rtx,
422 unsigned HOST_WIDE_INT));
423static unsigned HOST_WIDE_INT nonzero_bits PROTO((rtx, enum machine_mode));
424static int num_sign_bit_copies PROTO((rtx, enum machine_mode));
425static int merge_outer_ops PROTO((enum rtx_code *, HOST_WIDE_INT *,
426 enum rtx_code, HOST_WIDE_INT,
427 enum machine_mode, int *));
428static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
429 rtx, int));
8e2f6e35 430static int recog_for_combine PROTO((rtx *, rtx, rtx *));
fe2db4fb 431static rtx gen_lowpart_for_combine PROTO((enum machine_mode, rtx));
d18225c4 432static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
4f90e4a0 433 ...));
fe2db4fb
RK
434static rtx gen_binary PROTO((enum rtx_code, enum machine_mode,
435 rtx, rtx));
0c1c8ea6
RK
436static rtx gen_unary PROTO((enum rtx_code, enum machine_mode,
437 enum machine_mode, rtx));
fe2db4fb
RK
438static enum rtx_code simplify_comparison PROTO((enum rtx_code, rtx *, rtx *));
439static int reversible_comparison_p PROTO((rtx));
440static void update_table_tick PROTO((rtx));
441static void record_value_for_reg PROTO((rtx, rtx, rtx));
442static void record_dead_and_set_regs_1 PROTO((rtx, rtx));
443static void record_dead_and_set_regs PROTO((rtx));
9a893315 444static int get_last_value_validate PROTO((rtx *, rtx, int, int));
fe2db4fb
RK
445static rtx get_last_value PROTO((rtx));
446static int use_crosses_set_p PROTO((rtx, int));
447static void reg_dead_at_p_1 PROTO((rtx, rtx));
448static int reg_dead_at_p PROTO((rtx, rtx));
6eb12cef 449static void move_deaths PROTO((rtx, rtx, int, rtx, rtx *));
fe2db4fb
RK
450static int reg_bitfield_target_p PROTO((rtx, rtx));
451static void distribute_notes PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
452static void distribute_links PROTO((rtx));
6e25d159 453static void mark_used_regs_combine PROTO((rtx));
1427d6d2 454static int insn_cuid PROTO((rtx));
230d793d
RS
455\f
456/* Main entry point for combiner. F is the first insn of the function.
457 NREGS is the first unused pseudo-reg number. */
458
459void
460combine_instructions (f, nregs)
461 rtx f;
462 int nregs;
463{
b729186a
JL
464 register rtx insn, next;
465#ifdef HAVE_cc0
466 register rtx prev;
467#endif
230d793d
RS
468 register int i;
469 register rtx links, nextlinks;
470
471 combine_attempts = 0;
472 combine_merges = 0;
473 combine_extras = 0;
474 combine_successes = 0;
241cea85 475 undobuf.undos = undobuf.previous_undos = 0;
230d793d
RS
476
477 combine_max_regno = nregs;
478
ef026f91
RS
479 reg_nonzero_bits
480 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
481 reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
482
4c9a05bc 483 bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
ef026f91
RS
484 bzero (reg_sign_bit_copies, nregs * sizeof (char));
485
230d793d
RS
486 reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
487 reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
488 reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
568356af
RK
489 reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
490 reg_last_set_label = (int *) alloca (nregs * sizeof (int));
5f4f0e22 491 reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
55310dad
RK
492 reg_last_set_mode
493 = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
494 reg_last_set_nonzero_bits
495 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
496 reg_last_set_sign_bit_copies
497 = (char *) alloca (nregs * sizeof (char));
498
ef026f91 499 init_reg_last_arrays ();
230d793d
RS
500
501 init_recog_no_volatile ();
502
503 /* Compute maximum uid value so uid_cuid can be allocated. */
504
505 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
506 if (INSN_UID (insn) > i)
507 i = INSN_UID (insn);
508
509 uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
4255220d 510 max_uid_cuid = i;
230d793d 511
951553af 512 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
230d793d 513
951553af 514 /* Don't use reg_nonzero_bits when computing it. This can cause problems
230d793d
RS
515 when, for example, we have j <<= 1 in a loop. */
516
951553af 517 nonzero_sign_valid = 0;
230d793d
RS
518
519 /* Compute the mapping from uids to cuids.
520 Cuids are numbers assigned to insns, like uids,
521 except that cuids increase monotonically through the code.
522
523 Scan all SETs and see if we can deduce anything about what
951553af 524 bits are known to be zero for some registers and how many copies
d79f08e0
RK
525 of the sign bit are known to exist for those registers.
526
527 Also set any known values so that we can use it while searching
528 for what bits are known to be set. */
529
530 label_tick = 1;
230d793d 531
bcd49eb7
JW
532 /* We need to initialize it here, because record_dead_and_set_regs may call
533 get_last_value. */
534 subst_prev_insn = NULL_RTX;
535
7988fd36
RK
536 setup_incoming_promotions ();
537
230d793d
RS
538 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
539 {
4255220d 540 uid_cuid[INSN_UID (insn)] = ++i;
d79f08e0
RK
541 subst_low_cuid = i;
542 subst_insn = insn;
543
230d793d 544 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
d79f08e0
RK
545 {
546 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
547 record_dead_and_set_regs (insn);
2dab894a
RK
548
549#ifdef AUTO_INC_DEC
550 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
551 if (REG_NOTE_KIND (links) == REG_INC)
552 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
553#endif
d79f08e0
RK
554 }
555
556 if (GET_CODE (insn) == CODE_LABEL)
557 label_tick++;
230d793d
RS
558 }
559
951553af 560 nonzero_sign_valid = 1;
230d793d
RS
561
562 /* Now scan all the insns in forward order. */
563
0d4d42c3 564 this_basic_block = -1;
230d793d
RS
565 label_tick = 1;
566 last_call_cuid = 0;
567 mem_last_set = 0;
ef026f91 568 init_reg_last_arrays ();
7988fd36
RK
569 setup_incoming_promotions ();
570
230d793d
RS
571 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
572 {
573 next = 0;
574
0d4d42c3 575 /* If INSN starts a new basic block, update our basic block number. */
f085c9cd 576 if (this_basic_block + 1 < n_basic_blocks
3b413743 577 && BLOCK_HEAD (this_basic_block + 1) == insn)
0d4d42c3
RK
578 this_basic_block++;
579
230d793d
RS
580 if (GET_CODE (insn) == CODE_LABEL)
581 label_tick++;
582
0d4d42c3 583 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
230d793d
RS
584 {
585 /* Try this insn with each insn it links back to. */
586
587 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
5f4f0e22 588 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
230d793d
RS
589 goto retry;
590
591 /* Try each sequence of three linked insns ending with this one. */
592
593 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
594 for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
595 nextlinks = XEXP (nextlinks, 1))
596 if ((next = try_combine (insn, XEXP (links, 0),
597 XEXP (nextlinks, 0))) != 0)
598 goto retry;
599
600#ifdef HAVE_cc0
601 /* Try to combine a jump insn that uses CC0
602 with a preceding insn that sets CC0, and maybe with its
603 logical predecessor as well.
604 This is how we make decrement-and-branch insns.
605 We need this special code because data flow connections
606 via CC0 do not get entered in LOG_LINKS. */
607
608 if (GET_CODE (insn) == JUMP_INSN
609 && (prev = prev_nonnote_insn (insn)) != 0
610 && GET_CODE (prev) == INSN
611 && sets_cc0_p (PATTERN (prev)))
612 {
5f4f0e22 613 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
230d793d
RS
614 goto retry;
615
616 for (nextlinks = LOG_LINKS (prev); nextlinks;
617 nextlinks = XEXP (nextlinks, 1))
618 if ((next = try_combine (insn, prev,
619 XEXP (nextlinks, 0))) != 0)
620 goto retry;
621 }
622
623 /* Do the same for an insn that explicitly references CC0. */
624 if (GET_CODE (insn) == INSN
625 && (prev = prev_nonnote_insn (insn)) != 0
626 && GET_CODE (prev) == INSN
627 && sets_cc0_p (PATTERN (prev))
628 && GET_CODE (PATTERN (insn)) == SET
629 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
630 {
5f4f0e22 631 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
230d793d
RS
632 goto retry;
633
634 for (nextlinks = LOG_LINKS (prev); nextlinks;
635 nextlinks = XEXP (nextlinks, 1))
636 if ((next = try_combine (insn, prev,
637 XEXP (nextlinks, 0))) != 0)
638 goto retry;
639 }
640
641 /* Finally, see if any of the insns that this insn links to
642 explicitly references CC0. If so, try this insn, that insn,
5089e22e 643 and its predecessor if it sets CC0. */
230d793d
RS
644 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
645 if (GET_CODE (XEXP (links, 0)) == INSN
646 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
647 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
648 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
649 && GET_CODE (prev) == INSN
650 && sets_cc0_p (PATTERN (prev))
651 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
652 goto retry;
653#endif
654
655 /* Try combining an insn with two different insns whose results it
656 uses. */
657 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
658 for (nextlinks = XEXP (links, 1); nextlinks;
659 nextlinks = XEXP (nextlinks, 1))
660 if ((next = try_combine (insn, XEXP (links, 0),
661 XEXP (nextlinks, 0))) != 0)
662 goto retry;
663
664 if (GET_CODE (insn) != NOTE)
665 record_dead_and_set_regs (insn);
666
667 retry:
668 ;
669 }
670 }
671
672 total_attempts += combine_attempts;
673 total_merges += combine_merges;
674 total_extras += combine_extras;
675 total_successes += combine_successes;
1a26b032 676
951553af 677 nonzero_sign_valid = 0;
972b320c
R
678
679 /* Make recognizer allow volatile MEMs again. */
680 init_recog ();
230d793d 681}
ef026f91
RS
682
683/* Wipe the reg_last_xxx arrays in preparation for another pass. */
684
685static void
686init_reg_last_arrays ()
687{
688 int nregs = combine_max_regno;
689
4c9a05bc
RK
690 bzero ((char *) reg_last_death, nregs * sizeof (rtx));
691 bzero ((char *) reg_last_set, nregs * sizeof (rtx));
692 bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
693 bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
694 bzero ((char *) reg_last_set_label, nregs * sizeof (int));
ef026f91 695 bzero (reg_last_set_invalid, nregs * sizeof (char));
4c9a05bc
RK
696 bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
697 bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
ef026f91
RS
698 bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
699}
230d793d 700\f
7988fd36
RK
701/* Set up any promoted values for incoming argument registers. */
702
ee791cc3 703static void
7988fd36
RK
704setup_incoming_promotions ()
705{
706#ifdef PROMOTE_FUNCTION_ARGS
707 int regno;
708 rtx reg;
709 enum machine_mode mode;
710 int unsignedp;
711 rtx first = get_insns ();
712
713 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
714 if (FUNCTION_ARG_REGNO_P (regno)
715 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
38a448ca
RH
716 {
717 record_value_for_reg
718 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
719 : SIGN_EXTEND),
720 GET_MODE (reg),
721 gen_rtx_CLOBBER (mode, const0_rtx)));
722 }
7988fd36
RK
723#endif
724}
725\f
91102d5a
RK
726/* Called via note_stores. If X is a pseudo that is narrower than
727 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
230d793d
RS
728
729 If we are setting only a portion of X and we can't figure out what
730 portion, assume all bits will be used since we don't know what will
d0ab8cd3
RK
731 be happening.
732
733 Similarly, set how many bits of X are known to be copies of the sign bit
734 at all locations in the function. This is the smallest number implied
735 by any set of X. */
230d793d
RS
736
737static void
951553af 738set_nonzero_bits_and_sign_copies (x, set)
230d793d
RS
739 rtx x;
740 rtx set;
741{
d0ab8cd3
RK
742 int num;
743
230d793d
RS
744 if (GET_CODE (x) == REG
745 && REGNO (x) >= FIRST_PSEUDO_REGISTER
e8095e80
RK
746 /* If this register is undefined at the start of the file, we can't
747 say what its contents were. */
e881bb1b 748 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, REGNO (x))
5f4f0e22 749 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
230d793d 750 {
2dab894a 751 if (set == 0 || GET_CODE (set) == CLOBBER)
e8095e80
RK
752 {
753 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
88306d12 754 reg_sign_bit_copies[REGNO (x)] = 1;
e8095e80
RK
755 return;
756 }
230d793d
RS
757
758 /* If this is a complex assignment, see if we can convert it into a
5089e22e 759 simple assignment. */
230d793d 760 set = expand_field_assignment (set);
d79f08e0
RK
761
762 /* If this is a simple assignment, or we have a paradoxical SUBREG,
763 set what we know about X. */
764
765 if (SET_DEST (set) == x
766 || (GET_CODE (SET_DEST (set)) == SUBREG
705c7b3b
JW
767 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
768 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
d79f08e0 769 && SUBREG_REG (SET_DEST (set)) == x))
d0ab8cd3 770 {
9afa3d54
RK
771 rtx src = SET_SRC (set);
772
773#ifdef SHORT_IMMEDIATES_SIGN_EXTEND
774 /* If X is narrower than a word and SRC is a non-negative
775 constant that would appear negative in the mode of X,
776 sign-extend it for use in reg_nonzero_bits because some
777 machines (maybe most) will actually do the sign-extension
778 and this is the conservative approach.
779
780 ??? For 2.5, try to tighten up the MD files in this regard
781 instead of this kludge. */
782
783 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
784 && GET_CODE (src) == CONST_INT
785 && INTVAL (src) > 0
786 && 0 != (INTVAL (src)
787 & ((HOST_WIDE_INT) 1
9e69be8c 788 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9afa3d54
RK
789 src = GEN_INT (INTVAL (src)
790 | ((HOST_WIDE_INT) (-1)
791 << GET_MODE_BITSIZE (GET_MODE (x))));
792#endif
793
951553af 794 reg_nonzero_bits[REGNO (x)]
9afa3d54 795 |= nonzero_bits (src, nonzero_bits_mode);
d0ab8cd3
RK
796 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
797 if (reg_sign_bit_copies[REGNO (x)] == 0
798 || reg_sign_bit_copies[REGNO (x)] > num)
799 reg_sign_bit_copies[REGNO (x)] = num;
800 }
230d793d 801 else
d0ab8cd3 802 {
951553af 803 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
88306d12 804 reg_sign_bit_copies[REGNO (x)] = 1;
d0ab8cd3 805 }
230d793d
RS
806 }
807}
808\f
809/* See if INSN can be combined into I3. PRED and SUCC are optionally
810 insns that were previously combined into I3 or that will be combined
811 into the merger of INSN and I3.
812
813 Return 0 if the combination is not allowed for any reason.
814
815 If the combination is allowed, *PDEST will be set to the single
816 destination of INSN and *PSRC to the single source, and this function
817 will return 1. */
818
819static int
820can_combine_p (insn, i3, pred, succ, pdest, psrc)
821 rtx insn;
822 rtx i3;
e51712db
KG
823 rtx pred ATTRIBUTE_UNUSED;
824 rtx succ;
230d793d
RS
825 rtx *pdest, *psrc;
826{
827 int i;
828 rtx set = 0, src, dest;
b729186a
JL
829 rtx p;
830#ifdef AUTO_INC_DEC
76d31c63 831 rtx link;
b729186a 832#endif
230d793d
RS
833 int all_adjacent = (succ ? (next_active_insn (insn) == succ
834 && next_active_insn (succ) == i3)
835 : next_active_insn (insn) == i3);
836
837 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
838 or a PARALLEL consisting of such a SET and CLOBBERs.
839
840 If INSN has CLOBBER parallel parts, ignore them for our processing.
841 By definition, these happen during the execution of the insn. When it
842 is merged with another insn, all bets are off. If they are, in fact,
843 needed and aren't also supplied in I3, they may be added by
844 recog_for_combine. Otherwise, it won't match.
845
846 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
847 note.
848
849 Get the source and destination of INSN. If more than one, can't
850 combine. */
851
852 if (GET_CODE (PATTERN (insn)) == SET)
853 set = PATTERN (insn);
854 else if (GET_CODE (PATTERN (insn)) == PARALLEL
855 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
856 {
857 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
858 {
859 rtx elt = XVECEXP (PATTERN (insn), 0, i);
860
861 switch (GET_CODE (elt))
862 {
e3258cef
R
863 /* This is important to combine floating point insns
864 for the SH4 port. */
865 case USE:
866 /* Combining an isolated USE doesn't make sense.
867 We depend here on combinable_i3_pat to reject them. */
868 /* The code below this loop only verifies that the inputs of
869 the SET in INSN do not change. We call reg_set_between_p
870 to verify that the REG in the USE does not change betweeen
871 I3 and INSN.
872 If the USE in INSN was for a pseudo register, the matching
873 insn pattern will likely match any register; combining this
874 with any other USE would only be safe if we knew that the
875 used registers have identical values, or if there was
876 something to tell them apart, e.g. different modes. For
877 now, we forgo such compilcated tests and simply disallow
878 combining of USES of pseudo registers with any other USE. */
879 if (GET_CODE (XEXP (elt, 0)) == REG
880 && GET_CODE (PATTERN (i3)) == PARALLEL)
881 {
882 rtx i3pat = PATTERN (i3);
883 int i = XVECLEN (i3pat, 0) - 1;
884 int regno = REGNO (XEXP (elt, 0));
885 do
886 {
887 rtx i3elt = XVECEXP (i3pat, 0, i);
888 if (GET_CODE (i3elt) == USE
889 && GET_CODE (XEXP (i3elt, 0)) == REG
890 && (REGNO (XEXP (i3elt, 0)) == regno
891 ? reg_set_between_p (XEXP (elt, 0),
892 PREV_INSN (insn), i3)
893 : regno >= FIRST_PSEUDO_REGISTER))
894 return 0;
895 }
896 while (--i >= 0);
897 }
898 break;
899
230d793d
RS
900 /* We can ignore CLOBBERs. */
901 case CLOBBER:
902 break;
903
904 case SET:
905 /* Ignore SETs whose result isn't used but not those that
906 have side-effects. */
907 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
908 && ! side_effects_p (elt))
909 break;
910
911 /* If we have already found a SET, this is a second one and
912 so we cannot combine with this insn. */
913 if (set)
914 return 0;
915
916 set = elt;
917 break;
918
919 default:
920 /* Anything else means we can't combine. */
921 return 0;
922 }
923 }
924
925 if (set == 0
926 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
927 so don't do anything with it. */
928 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
929 return 0;
930 }
931 else
932 return 0;
933
934 if (set == 0)
935 return 0;
936
937 set = expand_field_assignment (set);
938 src = SET_SRC (set), dest = SET_DEST (set);
939
940 /* Don't eliminate a store in the stack pointer. */
941 if (dest == stack_pointer_rtx
230d793d
RS
942 /* If we couldn't eliminate a field assignment, we can't combine. */
943 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
944 /* Don't combine with an insn that sets a register to itself if it has
945 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
5f4f0e22 946 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
230d793d
RS
947 /* Can't merge a function call. */
948 || GET_CODE (src) == CALL
cd5e8f1f 949 /* Don't eliminate a function call argument. */
4dca5ec5
RK
950 || (GET_CODE (i3) == CALL_INSN
951 && (find_reg_fusage (i3, USE, dest)
952 || (GET_CODE (dest) == REG
953 && REGNO (dest) < FIRST_PSEUDO_REGISTER
954 && global_regs[REGNO (dest)])))
230d793d
RS
955 /* Don't substitute into an incremented register. */
956 || FIND_REG_INC_NOTE (i3, dest)
957 || (succ && FIND_REG_INC_NOTE (succ, dest))
ec35104c 958#if 0
230d793d 959 /* Don't combine the end of a libcall into anything. */
ec35104c
JL
960 /* ??? This gives worse code, and appears to be unnecessary, since no
961 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
962 use REG_RETVAL notes for noconflict blocks, but other code here
963 makes sure that those insns don't disappear. */
5f4f0e22 964 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
ec35104c 965#endif
230d793d
RS
966 /* Make sure that DEST is not used after SUCC but before I3. */
967 || (succ && ! all_adjacent
968 && reg_used_between_p (dest, succ, i3))
969 /* Make sure that the value that is to be substituted for the register
970 does not use any registers whose values alter in between. However,
971 If the insns are adjacent, a use can't cross a set even though we
972 think it might (this can happen for a sequence of insns each setting
973 the same destination; reg_last_set of that register might point to
d81481d3
RK
974 a NOTE). If INSN has a REG_EQUIV note, the register is always
975 equivalent to the memory so the substitution is valid even if there
976 are intervening stores. Also, don't move a volatile asm or
977 UNSPEC_VOLATILE across any other insns. */
230d793d 978 || (! all_adjacent
d81481d3
RK
979 && (((GET_CODE (src) != MEM
980 || ! find_reg_note (insn, REG_EQUIV, src))
981 && use_crosses_set_p (src, INSN_CUID (insn)))
a66a10c7
RS
982 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
983 || GET_CODE (src) == UNSPEC_VOLATILE))
230d793d
RS
984 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
985 better register allocation by not doing the combine. */
986 || find_reg_note (i3, REG_NO_CONFLICT, dest)
987 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
988 /* Don't combine across a CALL_INSN, because that would possibly
989 change whether the life span of some REGs crosses calls or not,
990 and it is a pain to update that information.
991 Exception: if source is a constant, moving it later can't hurt.
992 Accept that special case, because it helps -fforce-addr a lot. */
993 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
994 return 0;
995
996 /* DEST must either be a REG or CC0. */
997 if (GET_CODE (dest) == REG)
998 {
999 /* If register alignment is being enforced for multi-word items in all
1000 cases except for parameters, it is possible to have a register copy
1001 insn referencing a hard register that is not allowed to contain the
1002 mode being copied and which would not be valid as an operand of most
1003 insns. Eliminate this problem by not combining with such an insn.
1004
1005 Also, on some machines we don't want to extend the life of a hard
4d2c432d
RK
1006 register.
1007
1008 This is the same test done in can_combine except that we don't test
1009 if SRC is a CALL operation to permit a hard register with
1010 SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
1011 into account. */
230d793d
RS
1012
1013 if (GET_CODE (src) == REG
1014 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1015 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
c448a43e
RK
1016 /* Don't extend the life of a hard register unless it is
1017 user variable (if we have few registers) or it can't
1018 fit into the desired register (meaning something special
ecd40809
RK
1019 is going on).
1020 Also avoid substituting a return register into I3, because
1021 reload can't handle a conflict with constraints of other
1022 inputs. */
230d793d 1023 || (REGNO (src) < FIRST_PSEUDO_REGISTER
c448a43e 1024 && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
f95182a4
ILT
1025 || (SMALL_REGISTER_CLASSES
1026 && ((! all_adjacent && ! REG_USERVAR_P (src))
1027 || (FUNCTION_VALUE_REGNO_P (REGNO (src))
e9a25f70 1028 && ! REG_USERVAR_P (src))))))))
230d793d
RS
1029 return 0;
1030 }
1031 else if (GET_CODE (dest) != CC0)
1032 return 0;
1033
5f96750d
RS
1034 /* Don't substitute for a register intended as a clobberable operand.
1035 Similarly, don't substitute an expression containing a register that
1036 will be clobbered in I3. */
230d793d
RS
1037 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1038 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1039 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
5f96750d
RS
1040 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1041 src)
1042 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
230d793d
RS
1043 return 0;
1044
1045 /* If INSN contains anything volatile, or is an `asm' (whether volatile
d276f2bb 1046 or not), reject, unless nothing volatile comes between it and I3 */
230d793d
RS
1047
1048 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
d276f2bb
CM
1049 {
1050 /* Make sure succ doesn't contain a volatile reference. */
1051 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1052 return 0;
1053
1054 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1055 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1056 && p != succ && volatile_refs_p (PATTERN (p)))
1057 return 0;
1058 }
230d793d 1059
b79ee7eb
RH
1060 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1061 to be an explicit register variable, and was chosen for a reason. */
1062
1063 if (GET_CODE (src) == ASM_OPERANDS
1064 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1065 return 0;
1066
4b2cb4a2
RS
1067 /* If there are any volatile insns between INSN and I3, reject, because
1068 they might affect machine state. */
1069
1070 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1071 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1072 && p != succ && volatile_insn_p (PATTERN (p)))
1073 return 0;
1074
230d793d
RS
1075 /* If INSN or I2 contains an autoincrement or autodecrement,
1076 make sure that register is not used between there and I3,
1077 and not already used in I3 either.
1078 Also insist that I3 not be a jump; if it were one
1079 and the incremented register were spilled, we would lose. */
1080
1081#ifdef AUTO_INC_DEC
1082 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1083 if (REG_NOTE_KIND (link) == REG_INC
1084 && (GET_CODE (i3) == JUMP_INSN
1085 || reg_used_between_p (XEXP (link, 0), insn, i3)
1086 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1087 return 0;
1088#endif
1089
1090#ifdef HAVE_cc0
1091 /* Don't combine an insn that follows a CC0-setting insn.
1092 An insn that uses CC0 must not be separated from the one that sets it.
1093 We do, however, allow I2 to follow a CC0-setting insn if that insn
1094 is passed as I1; in that case it will be deleted also.
1095 We also allow combining in this case if all the insns are adjacent
1096 because that would leave the two CC0 insns adjacent as well.
1097 It would be more logical to test whether CC0 occurs inside I1 or I2,
1098 but that would be much slower, and this ought to be equivalent. */
1099
1100 p = prev_nonnote_insn (insn);
1101 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1102 && ! all_adjacent)
1103 return 0;
1104#endif
1105
1106 /* If we get here, we have passed all the tests and the combination is
1107 to be allowed. */
1108
1109 *pdest = dest;
1110 *psrc = src;
1111
1112 return 1;
1113}
1114\f
956d6950
JL
1115/* Check if PAT is an insn - or a part of it - used to set up an
1116 argument for a function in a hard register. */
1117
1118static int
1119sets_function_arg_p (pat)
1120 rtx pat;
1121{
1122 int i;
1123 rtx inner_dest;
1124
1125 switch (GET_CODE (pat))
1126 {
1127 case INSN:
1128 return sets_function_arg_p (PATTERN (pat));
1129
1130 case PARALLEL:
1131 for (i = XVECLEN (pat, 0); --i >= 0;)
1132 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1133 return 1;
1134
1135 break;
1136
1137 case SET:
1138 inner_dest = SET_DEST (pat);
1139 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1140 || GET_CODE (inner_dest) == SUBREG
1141 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1142 inner_dest = XEXP (inner_dest, 0);
1143
1144 return (GET_CODE (inner_dest) == REG
1145 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1146 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1d300e19
KG
1147
1148 default:
1149 break;
956d6950
JL
1150 }
1151
1152 return 0;
1153}
1154
230d793d
RS
1155/* LOC is the location within I3 that contains its pattern or the component
1156 of a PARALLEL of the pattern. We validate that it is valid for combining.
1157
1158 One problem is if I3 modifies its output, as opposed to replacing it
1159 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1160 so would produce an insn that is not equivalent to the original insns.
1161
1162 Consider:
1163
1164 (set (reg:DI 101) (reg:DI 100))
1165 (set (subreg:SI (reg:DI 101) 0) <foo>)
1166
1167 This is NOT equivalent to:
1168
1169 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1170 (set (reg:DI 101) (reg:DI 100))])
1171
1172 Not only does this modify 100 (in which case it might still be valid
1173 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1174
1175 We can also run into a problem if I2 sets a register that I1
1176 uses and I1 gets directly substituted into I3 (not via I2). In that
1177 case, we would be getting the wrong value of I2DEST into I3, so we
1178 must reject the combination. This case occurs when I2 and I1 both
1179 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1180 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1181 of a SET must prevent combination from occurring.
1182
e9a25f70 1183 On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
c448a43e
RK
1184 if the destination of a SET is a hard register that isn't a user
1185 variable.
230d793d
RS
1186
1187 Before doing the above check, we first try to expand a field assignment
1188 into a set of logical operations.
1189
1190 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1191 we place a register that is both set and used within I3. If more than one
1192 such register is detected, we fail.
1193
1194 Return 1 if the combination is valid, zero otherwise. */
1195
1196static int
1197combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1198 rtx i3;
1199 rtx *loc;
1200 rtx i2dest;
1201 rtx i1dest;
1202 int i1_not_in_src;
1203 rtx *pi3dest_killed;
1204{
1205 rtx x = *loc;
1206
1207 if (GET_CODE (x) == SET)
1208 {
1209 rtx set = expand_field_assignment (x);
1210 rtx dest = SET_DEST (set);
1211 rtx src = SET_SRC (set);
29a82058
JL
1212 rtx inner_dest = dest;
1213
1214#if 0
1215 rtx inner_src = src;
1216#endif
230d793d
RS
1217
1218 SUBST (*loc, set);
1219
1220 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1221 || GET_CODE (inner_dest) == SUBREG
1222 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1223 inner_dest = XEXP (inner_dest, 0);
1224
1225 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1226 was added. */
1227#if 0
1228 while (GET_CODE (inner_src) == STRICT_LOW_PART
1229 || GET_CODE (inner_src) == SUBREG
1230 || GET_CODE (inner_src) == ZERO_EXTRACT)
1231 inner_src = XEXP (inner_src, 0);
1232
1233 /* If it is better that two different modes keep two different pseudos,
1234 avoid combining them. This avoids producing the following pattern
1235 on a 386:
1236 (set (subreg:SI (reg/v:QI 21) 0)
1237 (lshiftrt:SI (reg/v:SI 20)
1238 (const_int 24)))
1239 If that were made, reload could not handle the pair of
1240 reg 20/21, since it would try to get any GENERAL_REGS
1241 but some of them don't handle QImode. */
1242
1243 if (rtx_equal_p (inner_src, i2dest)
1244 && GET_CODE (inner_dest) == REG
1245 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1246 return 0;
1247#endif
1248
1249 /* Check for the case where I3 modifies its output, as
1250 discussed above. */
1251 if ((inner_dest != dest
1252 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1253 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
956d6950 1254
3f508eca
RK
1255 /* This is the same test done in can_combine_p except that we
1256 allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
956d6950
JL
1257 CALL operation. Moreover, we can't test all_adjacent; we don't
1258 have to, since this instruction will stay in place, thus we are
1259 not considering increasing the lifetime of INNER_DEST.
1260
1261 Also, if this insn sets a function argument, combining it with
1262 something that might need a spill could clobber a previous
1263 function argument; the all_adjacent test in can_combine_p also
1264 checks this; here, we do a more specific test for this case. */
1265
230d793d 1266 || (GET_CODE (inner_dest) == REG
dfbe1b2f 1267 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
c448a43e
RK
1268 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1269 GET_MODE (inner_dest))
e9a25f70
JL
1270 || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1271 && ! REG_USERVAR_P (inner_dest)
956d6950
JL
1272 && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1273 || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1274 && i3 != 0
1275 && sets_function_arg_p (prev_nonnote_insn (i3)))))))
230d793d
RS
1276 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1277 return 0;
1278
1279 /* If DEST is used in I3, it is being killed in this insn,
36a9c2e9
JL
1280 so record that for later.
1281 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1282 STACK_POINTER_REGNUM, since these are always considered to be
1283 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
230d793d 1284 if (pi3dest_killed && GET_CODE (dest) == REG
36a9c2e9
JL
1285 && reg_referenced_p (dest, PATTERN (i3))
1286 && REGNO (dest) != FRAME_POINTER_REGNUM
6d7096b0
DE
1287#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1288 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1289#endif
36a9c2e9
JL
1290#if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1291 && (REGNO (dest) != ARG_POINTER_REGNUM
1292 || ! fixed_regs [REGNO (dest)])
1293#endif
1294 && REGNO (dest) != STACK_POINTER_REGNUM)
230d793d
RS
1295 {
1296 if (*pi3dest_killed)
1297 return 0;
1298
1299 *pi3dest_killed = dest;
1300 }
1301 }
1302
1303 else if (GET_CODE (x) == PARALLEL)
1304 {
1305 int i;
1306
1307 for (i = 0; i < XVECLEN (x, 0); i++)
1308 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1309 i1_not_in_src, pi3dest_killed))
1310 return 0;
1311 }
1312
1313 return 1;
1314}
1315\f
1316/* Try to combine the insns I1 and I2 into I3.
1317 Here I1 and I2 appear earlier than I3.
1318 I1 can be zero; then we combine just I2 into I3.
1319
1320 It we are combining three insns and the resulting insn is not recognized,
1321 try splitting it into two insns. If that happens, I2 and I3 are retained
1322 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1323 are pseudo-deleted.
1324
abe6e52f
RK
1325 Return 0 if the combination does not work. Then nothing is changed.
1326 If we did the combination, return the insn at which combine should
1327 resume scanning. */
230d793d
RS
1328
1329static rtx
1330try_combine (i3, i2, i1)
1331 register rtx i3, i2, i1;
1332{
1333 /* New patterns for I3 and I3, respectively. */
1334 rtx newpat, newi2pat = 0;
1335 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1336 int added_sets_1, added_sets_2;
1337 /* Total number of SETs to put into I3. */
1338 int total_sets;
1339 /* Nonzero is I2's body now appears in I3. */
1340 int i2_is_used;
1341 /* INSN_CODEs for new I3, new I2, and user of condition code. */
6a651371 1342 int insn_code_number, i2_code_number = 0, other_code_number = 0;
230d793d
RS
1343 /* Contains I3 if the destination of I3 is used in its source, which means
1344 that the old life of I3 is being killed. If that usage is placed into
1345 I2 and not in I3, a REG_DEAD note must be made. */
1346 rtx i3dest_killed = 0;
1347 /* SET_DEST and SET_SRC of I2 and I1. */
1348 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1349 /* PATTERN (I2), or a copy of it in certain cases. */
1350 rtx i2pat;
1351 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
c4e861e8 1352 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
230d793d
RS
1353 int i1_feeds_i3 = 0;
1354 /* Notes that must be added to REG_NOTES in I3 and I2. */
1355 rtx new_i3_notes, new_i2_notes;
176c9e6b
JW
1356 /* Notes that we substituted I3 into I2 instead of the normal case. */
1357 int i3_subst_into_i2 = 0;
df7d75de
RK
1358 /* Notes that I1, I2 or I3 is a MULT operation. */
1359 int have_mult = 0;
230d793d
RS
1360
1361 int maxreg;
1362 rtx temp;
1363 register rtx link;
1364 int i;
1365
1366 /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1367 This can occur when flow deletes an insn that it has merged into an
1368 auto-increment address. We also can't do anything if I3 has a
1369 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1370 libcall. */
1371
1372 if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1373 || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1374 || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
ec35104c
JL
1375#if 0
1376 /* ??? This gives worse code, and appears to be unnecessary, since no
1377 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1378 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1379#endif
1380)
230d793d
RS
1381 return 0;
1382
1383 combine_attempts++;
1384
241cea85 1385 undobuf.undos = undobuf.previous_undos = 0;
230d793d
RS
1386 undobuf.other_insn = 0;
1387
1388 /* Save the current high-water-mark so we can free storage if we didn't
1389 accept this combination. */
1390 undobuf.storage = (char *) oballoc (0);
1391
6e25d159
RK
1392 /* Reset the hard register usage information. */
1393 CLEAR_HARD_REG_SET (newpat_used_regs);
1394
230d793d
RS
1395 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1396 code below, set I1 to be the earlier of the two insns. */
1397 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1398 temp = i1, i1 = i2, i2 = temp;
1399
abe6e52f 1400 added_links_insn = 0;
137e889e 1401
230d793d
RS
1402 /* First check for one important special-case that the code below will
1403 not handle. Namely, the case where I1 is zero, I2 has multiple sets,
1404 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1405 we may be able to replace that destination with the destination of I3.
1406 This occurs in the common code where we compute both a quotient and
1407 remainder into a structure, in which case we want to do the computation
1408 directly into the structure to avoid register-register copies.
1409
1410 We make very conservative checks below and only try to handle the
1411 most common cases of this. For example, we only handle the case
1412 where I2 and I3 are adjacent to avoid making difficult register
1413 usage tests. */
1414
1415 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1416 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1417 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
f95182a4 1418 && (! SMALL_REGISTER_CLASSES
e9a25f70
JL
1419 || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1420 || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1421 || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
230d793d
RS
1422 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1423 && GET_CODE (PATTERN (i2)) == PARALLEL
1424 && ! side_effects_p (SET_DEST (PATTERN (i3)))
5089e22e
RS
1425 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1426 below would need to check what is inside (and reg_overlap_mentioned_p
1427 doesn't support those codes anyway). Don't allow those destinations;
1428 the resulting insn isn't likely to be recognized anyway. */
1429 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1430 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
230d793d
RS
1431 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1432 SET_DEST (PATTERN (i3)))
1433 && next_real_insn (i2) == i3)
5089e22e
RS
1434 {
1435 rtx p2 = PATTERN (i2);
1436
1437 /* Make sure that the destination of I3,
1438 which we are going to substitute into one output of I2,
1439 is not used within another output of I2. We must avoid making this:
1440 (parallel [(set (mem (reg 69)) ...)
1441 (set (reg 69) ...)])
1442 which is not well-defined as to order of actions.
1443 (Besides, reload can't handle output reloads for this.)
1444
1445 The problem can also happen if the dest of I3 is a memory ref,
1446 if another dest in I2 is an indirect memory ref. */
1447 for (i = 0; i < XVECLEN (p2, 0); i++)
7ca919b7
RK
1448 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1449 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
5089e22e
RS
1450 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1451 SET_DEST (XVECEXP (p2, 0, i))))
1452 break;
230d793d 1453
5089e22e
RS
1454 if (i == XVECLEN (p2, 0))
1455 for (i = 0; i < XVECLEN (p2, 0); i++)
1456 if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1457 {
1458 combine_merges++;
230d793d 1459
5089e22e
RS
1460 subst_insn = i3;
1461 subst_low_cuid = INSN_CUID (i2);
230d793d 1462
c4e861e8 1463 added_sets_2 = added_sets_1 = 0;
5089e22e 1464 i2dest = SET_SRC (PATTERN (i3));
230d793d 1465
5089e22e
RS
1466 /* Replace the dest in I2 with our dest and make the resulting
1467 insn the new pattern for I3. Then skip to where we
1468 validate the pattern. Everything was set up above. */
1469 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1470 SET_DEST (PATTERN (i3)));
1471
1472 newpat = p2;
176c9e6b 1473 i3_subst_into_i2 = 1;
5089e22e
RS
1474 goto validate_replacement;
1475 }
1476 }
230d793d
RS
1477
1478#ifndef HAVE_cc0
1479 /* If we have no I1 and I2 looks like:
1480 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1481 (set Y OP)])
1482 make up a dummy I1 that is
1483 (set Y OP)
1484 and change I2 to be
1485 (set (reg:CC X) (compare:CC Y (const_int 0)))
1486
1487 (We can ignore any trailing CLOBBERs.)
1488
1489 This undoes a previous combination and allows us to match a branch-and-
1490 decrement insn. */
1491
1492 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1493 && XVECLEN (PATTERN (i2), 0) >= 2
1494 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1495 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1496 == MODE_CC)
1497 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1498 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1499 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1500 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1501 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1502 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1503 {
1504 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1505 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1506 break;
1507
1508 if (i == 1)
1509 {
1510 /* We make I1 with the same INSN_UID as I2. This gives it
1511 the same INSN_CUID for value tracking. Our fake I1 will
1512 never appear in the insn stream so giving it the same INSN_UID
1513 as I2 will not cause a problem. */
1514
0d9641d1 1515 subst_prev_insn = i1
38a448ca
RH
1516 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1517 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1518 NULL_RTX);
230d793d
RS
1519
1520 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1521 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1522 SET_DEST (PATTERN (i1)));
1523 }
1524 }
1525#endif
1526
1527 /* Verify that I2 and I1 are valid for combining. */
5f4f0e22
CH
1528 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1529 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
230d793d
RS
1530 {
1531 undo_all ();
1532 return 0;
1533 }
1534
1535 /* Record whether I2DEST is used in I2SRC and similarly for the other
1536 cases. Knowing this will help in register status updating below. */
1537 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1538 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1539 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1540
916f14f1 1541 /* See if I1 directly feeds into I3. It does if I1DEST is not used
230d793d
RS
1542 in I2SRC. */
1543 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1544
1545 /* Ensure that I3's pattern can be the destination of combines. */
1546 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1547 i1 && i2dest_in_i1src && i1_feeds_i3,
1548 &i3dest_killed))
1549 {
1550 undo_all ();
1551 return 0;
1552 }
1553
df7d75de
RK
1554 /* See if any of the insns is a MULT operation. Unless one is, we will
1555 reject a combination that is, since it must be slower. Be conservative
1556 here. */
1557 if (GET_CODE (i2src) == MULT
1558 || (i1 != 0 && GET_CODE (i1src) == MULT)
1559 || (GET_CODE (PATTERN (i3)) == SET
1560 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1561 have_mult = 1;
1562
230d793d
RS
1563 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1564 We used to do this EXCEPT in one case: I3 has a post-inc in an
1565 output operand. However, that exception can give rise to insns like
1566 mov r3,(r3)+
1567 which is a famous insn on the PDP-11 where the value of r3 used as the
5089e22e 1568 source was model-dependent. Avoid this sort of thing. */
230d793d
RS
1569
1570#if 0
1571 if (!(GET_CODE (PATTERN (i3)) == SET
1572 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1573 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1574 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1575 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1576 /* It's not the exception. */
1577#endif
1578#ifdef AUTO_INC_DEC
1579 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1580 if (REG_NOTE_KIND (link) == REG_INC
1581 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1582 || (i1 != 0
1583 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1584 {
1585 undo_all ();
1586 return 0;
1587 }
1588#endif
1589
1590 /* See if the SETs in I1 or I2 need to be kept around in the merged
1591 instruction: whenever the value set there is still needed past I3.
1592 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1593
1594 For the SET in I1, we have two cases: If I1 and I2 independently
1595 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1596 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1597 in I1 needs to be kept around unless I1DEST dies or is set in either
1598 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1599 I1DEST. If so, we know I1 feeds into I2. */
1600
1601 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1602
1603 added_sets_1
1604 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1605 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1606
1607 /* If the set in I2 needs to be kept around, we must make a copy of
1608 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
5089e22e 1609 PATTERN (I2), we are only substituting for the original I1DEST, not into
230d793d
RS
1610 an already-substituted copy. This also prevents making self-referential
1611 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1612 I2DEST. */
1613
1614 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
38a448ca 1615 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
230d793d
RS
1616 : PATTERN (i2));
1617
1618 if (added_sets_2)
1619 i2pat = copy_rtx (i2pat);
1620
1621 combine_merges++;
1622
1623 /* Substitute in the latest insn for the regs set by the earlier ones. */
1624
1625 maxreg = max_reg_num ();
1626
1627 subst_insn = i3;
230d793d
RS
1628
1629 /* It is possible that the source of I2 or I1 may be performing an
1630 unneeded operation, such as a ZERO_EXTEND of something that is known
1631 to have the high part zero. Handle that case by letting subst look at
1632 the innermost one of them.
1633
1634 Another way to do this would be to have a function that tries to
1635 simplify a single insn instead of merging two or more insns. We don't
1636 do this because of the potential of infinite loops and because
1637 of the potential extra memory required. However, doing it the way
1638 we are is a bit of a kludge and doesn't catch all cases.
1639
1640 But only do this if -fexpensive-optimizations since it slows things down
1641 and doesn't usually win. */
1642
1643 if (flag_expensive_optimizations)
1644 {
1645 /* Pass pc_rtx so no substitutions are done, just simplifications.
1646 The cases that we are interested in here do not involve the few
1647 cases were is_replaced is checked. */
1648 if (i1)
d0ab8cd3
RK
1649 {
1650 subst_low_cuid = INSN_CUID (i1);
1651 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1652 }
230d793d 1653 else
d0ab8cd3
RK
1654 {
1655 subst_low_cuid = INSN_CUID (i2);
1656 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1657 }
230d793d 1658
241cea85 1659 undobuf.previous_undos = undobuf.undos;
230d793d
RS
1660 }
1661
1662#ifndef HAVE_cc0
1663 /* Many machines that don't use CC0 have insns that can both perform an
1664 arithmetic operation and set the condition code. These operations will
1665 be represented as a PARALLEL with the first element of the vector
1666 being a COMPARE of an arithmetic operation with the constant zero.
1667 The second element of the vector will set some pseudo to the result
1668 of the same arithmetic operation. If we simplify the COMPARE, we won't
1669 match such a pattern and so will generate an extra insn. Here we test
1670 for this case, where both the comparison and the operation result are
1671 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1672 I2SRC. Later we will make the PARALLEL that contains I2. */
1673
1674 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1675 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1676 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1677 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1678 {
081f5e7e 1679#ifdef EXTRA_CC_MODES
230d793d
RS
1680 rtx *cc_use;
1681 enum machine_mode compare_mode;
081f5e7e 1682#endif
230d793d
RS
1683
1684 newpat = PATTERN (i3);
1685 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1686
1687 i2_is_used = 1;
1688
1689#ifdef EXTRA_CC_MODES
1690 /* See if a COMPARE with the operand we substituted in should be done
1691 with the mode that is currently being used. If not, do the same
1692 processing we do in `subst' for a SET; namely, if the destination
1693 is used only once, try to replace it with a register of the proper
1694 mode and also replace the COMPARE. */
1695 if (undobuf.other_insn == 0
1696 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1697 &undobuf.other_insn))
77fa0940
RK
1698 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1699 i2src, const0_rtx))
230d793d
RS
1700 != GET_MODE (SET_DEST (newpat))))
1701 {
1702 int regno = REGNO (SET_DEST (newpat));
38a448ca 1703 rtx new_dest = gen_rtx_REG (compare_mode, regno);
230d793d
RS
1704
1705 if (regno < FIRST_PSEUDO_REGISTER
b1f21e0a 1706 || (REG_N_SETS (regno) == 1 && ! added_sets_2
230d793d
RS
1707 && ! REG_USERVAR_P (SET_DEST (newpat))))
1708 {
1709 if (regno >= FIRST_PSEUDO_REGISTER)
1710 SUBST (regno_reg_rtx[regno], new_dest);
1711
1712 SUBST (SET_DEST (newpat), new_dest);
1713 SUBST (XEXP (*cc_use, 0), new_dest);
1714 SUBST (SET_SRC (newpat),
1715 gen_rtx_combine (COMPARE, compare_mode,
1716 i2src, const0_rtx));
1717 }
1718 else
1719 undobuf.other_insn = 0;
1720 }
1721#endif
1722 }
1723 else
1724#endif
1725 {
1726 n_occurrences = 0; /* `subst' counts here */
1727
1728 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1729 need to make a unique copy of I2SRC each time we substitute it
1730 to avoid self-referential rtl. */
1731
d0ab8cd3 1732 subst_low_cuid = INSN_CUID (i2);
230d793d
RS
1733 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1734 ! i1_feeds_i3 && i1dest_in_i1src);
241cea85 1735 undobuf.previous_undos = undobuf.undos;
230d793d
RS
1736
1737 /* Record whether i2's body now appears within i3's body. */
1738 i2_is_used = n_occurrences;
1739 }
1740
1741 /* If we already got a failure, don't try to do more. Otherwise,
1742 try to substitute in I1 if we have it. */
1743
1744 if (i1 && GET_CODE (newpat) != CLOBBER)
1745 {
1746 /* Before we can do this substitution, we must redo the test done
1747 above (see detailed comments there) that ensures that I1DEST
0f41302f 1748 isn't mentioned in any SETs in NEWPAT that are field assignments. */
230d793d 1749
5f4f0e22
CH
1750 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1751 0, NULL_PTR))
230d793d
RS
1752 {
1753 undo_all ();
1754 return 0;
1755 }
1756
1757 n_occurrences = 0;
d0ab8cd3 1758 subst_low_cuid = INSN_CUID (i1);
230d793d 1759 newpat = subst (newpat, i1dest, i1src, 0, 0);
241cea85 1760 undobuf.previous_undos = undobuf.undos;
230d793d
RS
1761 }
1762
916f14f1
RK
1763 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1764 to count all the ways that I2SRC and I1SRC can be used. */
5f4f0e22 1765 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
916f14f1 1766 && i2_is_used + added_sets_2 > 1)
5f4f0e22 1767 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
916f14f1
RK
1768 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1769 > 1))
230d793d
RS
1770 /* Fail if we tried to make a new register (we used to abort, but there's
1771 really no reason to). */
1772 || max_reg_num () != maxreg
1773 /* Fail if we couldn't do something and have a CLOBBER. */
df7d75de
RK
1774 || GET_CODE (newpat) == CLOBBER
1775 /* Fail if this new pattern is a MULT and we didn't have one before
1776 at the outer level. */
1777 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1778 && ! have_mult))
230d793d
RS
1779 {
1780 undo_all ();
1781 return 0;
1782 }
1783
1784 /* If the actions of the earlier insns must be kept
1785 in addition to substituting them into the latest one,
1786 we must make a new PARALLEL for the latest insn
1787 to hold additional the SETs. */
1788
1789 if (added_sets_1 || added_sets_2)
1790 {
1791 combine_extras++;
1792
1793 if (GET_CODE (newpat) == PARALLEL)
1794 {
1795 rtvec old = XVEC (newpat, 0);
1796 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
38a448ca 1797 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
59888de2 1798 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
230d793d
RS
1799 sizeof (old->elem[0]) * old->num_elem);
1800 }
1801 else
1802 {
1803 rtx old = newpat;
1804 total_sets = 1 + added_sets_1 + added_sets_2;
38a448ca 1805 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
230d793d
RS
1806 XVECEXP (newpat, 0, 0) = old;
1807 }
1808
1809 if (added_sets_1)
1810 XVECEXP (newpat, 0, --total_sets)
1811 = (GET_CODE (PATTERN (i1)) == PARALLEL
38a448ca 1812 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
230d793d
RS
1813
1814 if (added_sets_2)
c5c76735
JL
1815 {
1816 /* If there is no I1, use I2's body as is. We used to also not do
1817 the subst call below if I2 was substituted into I3,
1818 but that could lose a simplification. */
1819 if (i1 == 0)
1820 XVECEXP (newpat, 0, --total_sets) = i2pat;
1821 else
1822 /* See comment where i2pat is assigned. */
1823 XVECEXP (newpat, 0, --total_sets)
1824 = subst (i2pat, i1dest, i1src, 0, 0);
1825 }
230d793d
RS
1826 }
1827
1828 /* We come here when we are replacing a destination in I2 with the
1829 destination of I3. */
1830 validate_replacement:
1831
6e25d159
RK
1832 /* Note which hard regs this insn has as inputs. */
1833 mark_used_regs_combine (newpat);
1834
230d793d 1835 /* Is the result of combination a valid instruction? */
8e2f6e35 1836 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
230d793d
RS
1837
1838 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1839 the second SET's destination is a register that is unused. In that case,
1840 we just need the first SET. This can occur when simplifying a divmod
1841 insn. We *must* test for this case here because the code below that
1842 splits two independent SETs doesn't handle this case correctly when it
1843 updates the register status. Also check the case where the first
1844 SET's destination is unused. That would not cause incorrect code, but
1845 does cause an unneeded insn to remain. */
1846
1847 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1848 && XVECLEN (newpat, 0) == 2
1849 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1850 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1851 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1852 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1853 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1854 && asm_noperands (newpat) < 0)
1855 {
1856 newpat = XVECEXP (newpat, 0, 0);
8e2f6e35 1857 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
230d793d
RS
1858 }
1859
1860 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1861 && XVECLEN (newpat, 0) == 2
1862 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1863 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1864 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1865 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1866 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1867 && asm_noperands (newpat) < 0)
1868 {
1869 newpat = XVECEXP (newpat, 0, 1);
8e2f6e35 1870 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
230d793d
RS
1871 }
1872
1873 /* If we were combining three insns and the result is a simple SET
1874 with no ASM_OPERANDS that wasn't recognized, try to split it into two
916f14f1
RK
1875 insns. There are two ways to do this. It can be split using a
1876 machine-specific method (like when you have an addition of a large
1877 constant) or by combine in the function find_split_point. */
1878
230d793d
RS
1879 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1880 && asm_noperands (newpat) < 0)
1881 {
916f14f1 1882 rtx m_split, *split;
42495ca0 1883 rtx ni2dest = i2dest;
916f14f1
RK
1884
1885 /* See if the MD file can split NEWPAT. If it can't, see if letting it
42495ca0
RK
1886 use I2DEST as a scratch register will help. In the latter case,
1887 convert I2DEST to the mode of the source of NEWPAT if we can. */
916f14f1
RK
1888
1889 m_split = split_insns (newpat, i3);
a70c61d9
JW
1890
1891 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1892 inputs of NEWPAT. */
1893
1894 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1895 possible to try that as a scratch reg. This would require adding
1896 more code to make it work though. */
1897
1898 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
42495ca0
RK
1899 {
1900 /* If I2DEST is a hard register or the only use of a pseudo,
1901 we can change its mode. */
1902 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
02f4ada4 1903 && GET_MODE (SET_DEST (newpat)) != VOIDmode
60654f77 1904 && GET_CODE (i2dest) == REG
42495ca0 1905 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
b1f21e0a 1906 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
42495ca0 1907 && ! REG_USERVAR_P (i2dest))))
38a448ca 1908 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
c5c76735
JL
1909 REGNO (i2dest));
1910
1911 m_split = split_insns (gen_rtx_PARALLEL
1912 (VOIDmode,
1913 gen_rtvec (2, newpat,
1914 gen_rtx_CLOBBER (VOIDmode,
1915 ni2dest))),
1916 i3);
42495ca0 1917 }
916f14f1
RK
1918
1919 if (m_split && GET_CODE (m_split) == SEQUENCE
3f508eca
RK
1920 && XVECLEN (m_split, 0) == 2
1921 && (next_real_insn (i2) == i3
1922 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1923 INSN_CUID (i2))))
916f14f1 1924 {
1a26b032 1925 rtx i2set, i3set;
d0ab8cd3 1926 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
916f14f1 1927 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
916f14f1 1928
e4ba89be
RK
1929 i3set = single_set (XVECEXP (m_split, 0, 1));
1930 i2set = single_set (XVECEXP (m_split, 0, 0));
1a26b032 1931
42495ca0
RK
1932 /* In case we changed the mode of I2DEST, replace it in the
1933 pseudo-register table here. We can't do it above in case this
1934 code doesn't get executed and we do a split the other way. */
1935
1936 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1937 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1938
8e2f6e35 1939 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1a26b032
RK
1940
1941 /* If I2 or I3 has multiple SETs, we won't know how to track
9cc96794
RK
1942 register status, so don't use these insns. If I2's destination
1943 is used between I2 and I3, we also can't use these insns. */
1a26b032 1944
9cc96794
RK
1945 if (i2_code_number >= 0 && i2set && i3set
1946 && (next_real_insn (i2) == i3
1947 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
8e2f6e35
BS
1948 insn_code_number = recog_for_combine (&newi3pat, i3,
1949 &new_i3_notes);
d0ab8cd3
RK
1950 if (insn_code_number >= 0)
1951 newpat = newi3pat;
1952
c767f54b 1953 /* It is possible that both insns now set the destination of I3.
22609cbf 1954 If so, we must show an extra use of it. */
c767f54b 1955
393de53f
RK
1956 if (insn_code_number >= 0)
1957 {
1958 rtx new_i3_dest = SET_DEST (i3set);
1959 rtx new_i2_dest = SET_DEST (i2set);
1960
1961 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1962 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1963 || GET_CODE (new_i3_dest) == SUBREG)
1964 new_i3_dest = XEXP (new_i3_dest, 0);
1965
d4096689
RK
1966 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1967 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1968 || GET_CODE (new_i2_dest) == SUBREG)
1969 new_i2_dest = XEXP (new_i2_dest, 0);
1970
393de53f
RK
1971 if (GET_CODE (new_i3_dest) == REG
1972 && GET_CODE (new_i2_dest) == REG
1973 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
b1f21e0a 1974 REG_N_SETS (REGNO (new_i2_dest))++;
393de53f 1975 }
916f14f1 1976 }
230d793d
RS
1977
1978 /* If we can split it and use I2DEST, go ahead and see if that
1979 helps things be recognized. Verify that none of the registers
1980 are set between I2 and I3. */
d0ab8cd3 1981 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
230d793d
RS
1982#ifdef HAVE_cc0
1983 && GET_CODE (i2dest) == REG
1984#endif
1985 /* We need I2DEST in the proper mode. If it is a hard register
1986 or the only use of a pseudo, we can change its mode. */
1987 && (GET_MODE (*split) == GET_MODE (i2dest)
1988 || GET_MODE (*split) == VOIDmode
1989 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
b1f21e0a 1990 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
230d793d
RS
1991 && ! REG_USERVAR_P (i2dest)))
1992 && (next_real_insn (i2) == i3
1993 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1994 /* We can't overwrite I2DEST if its value is still used by
1995 NEWPAT. */
1996 && ! reg_referenced_p (i2dest, newpat))
1997 {
1998 rtx newdest = i2dest;
df7d75de
RK
1999 enum rtx_code split_code = GET_CODE (*split);
2000 enum machine_mode split_mode = GET_MODE (*split);
230d793d
RS
2001
2002 /* Get NEWDEST as a register in the proper mode. We have already
2003 validated that we can do this. */
df7d75de 2004 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
230d793d 2005 {
38a448ca 2006 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
230d793d
RS
2007
2008 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2009 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2010 }
2011
2012 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2013 an ASHIFT. This can occur if it was inside a PLUS and hence
2014 appeared to be a memory address. This is a kludge. */
df7d75de 2015 if (split_code == MULT
230d793d
RS
2016 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2017 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
1dc8a823
JW
2018 {
2019 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2020 XEXP (*split, 0), GEN_INT (i)));
2021 /* Update split_code because we may not have a multiply
2022 anymore. */
2023 split_code = GET_CODE (*split);
2024 }
230d793d
RS
2025
2026#ifdef INSN_SCHEDULING
2027 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2028 be written as a ZERO_EXTEND. */
df7d75de
RK
2029 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2030 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
230d793d
RS
2031 XEXP (*split, 0)));
2032#endif
2033
2034 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2035 SUBST (*split, newdest);
8e2f6e35 2036 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
df7d75de
RK
2037
2038 /* If the split point was a MULT and we didn't have one before,
2039 don't use one now. */
2040 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
8e2f6e35 2041 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
230d793d
RS
2042 }
2043 }
2044
2045 /* Check for a case where we loaded from memory in a narrow mode and
2046 then sign extended it, but we need both registers. In that case,
2047 we have a PARALLEL with both loads from the same memory location.
2048 We can split this into a load from memory followed by a register-register
2049 copy. This saves at least one insn, more if register allocation can
f0343c74
RK
2050 eliminate the copy.
2051
2052 We cannot do this if the destination of the second assignment is
2053 a register that we have already assumed is zero-extended. Similarly
2054 for a SUBREG of such a register. */
230d793d
RS
2055
2056 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2057 && GET_CODE (newpat) == PARALLEL
2058 && XVECLEN (newpat, 0) == 2
2059 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2060 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2061 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2062 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2063 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2064 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2065 INSN_CUID (i2))
2066 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2067 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
f0343c74
RK
2068 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2069 (GET_CODE (temp) == REG
2070 && reg_nonzero_bits[REGNO (temp)] != 0
2071 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2072 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2073 && (reg_nonzero_bits[REGNO (temp)]
2074 != GET_MODE_MASK (word_mode))))
2075 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2076 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2077 (GET_CODE (temp) == REG
2078 && reg_nonzero_bits[REGNO (temp)] != 0
2079 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2080 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2081 && (reg_nonzero_bits[REGNO (temp)]
2082 != GET_MODE_MASK (word_mode)))))
230d793d
RS
2083 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2084 SET_SRC (XVECEXP (newpat, 0, 1)))
2085 && ! find_reg_note (i3, REG_UNUSED,
2086 SET_DEST (XVECEXP (newpat, 0, 0))))
2087 {
472fbdd1
RK
2088 rtx ni2dest;
2089
230d793d 2090 newi2pat = XVECEXP (newpat, 0, 0);
472fbdd1 2091 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
230d793d
RS
2092 newpat = XVECEXP (newpat, 0, 1);
2093 SUBST (SET_SRC (newpat),
472fbdd1 2094 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
8e2f6e35 2095 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
a29ca9db 2096
230d793d 2097 if (i2_code_number >= 0)
8e2f6e35 2098 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
5089e22e
RS
2099
2100 if (insn_code_number >= 0)
2101 {
2102 rtx insn;
2103 rtx link;
2104
2105 /* If we will be able to accept this, we have made a change to the
2106 destination of I3. This can invalidate a LOG_LINKS pointing
2107 to I3. No other part of combine.c makes such a transformation.
2108
2109 The new I3 will have a destination that was previously the
2110 destination of I1 or I2 and which was used in i2 or I3. Call
2111 distribute_links to make a LOG_LINK from the next use of
2112 that destination. */
2113
2114 PATTERN (i3) = newpat;
38a448ca 2115 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
5089e22e
RS
2116
2117 /* I3 now uses what used to be its destination and which is
2118 now I2's destination. That means we need a LOG_LINK from
2119 I3 to I2. But we used to have one, so we still will.
2120
2121 However, some later insn might be using I2's dest and have
2122 a LOG_LINK pointing at I3. We must remove this link.
2123 The simplest way to remove the link is to point it at I1,
2124 which we know will be a NOTE. */
2125
2126 for (insn = NEXT_INSN (i3);
0d4d42c3 2127 insn && (this_basic_block == n_basic_blocks - 1
3b413743 2128 || insn != BLOCK_HEAD (this_basic_block + 1));
5089e22e
RS
2129 insn = NEXT_INSN (insn))
2130 {
2131 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
472fbdd1 2132 && reg_referenced_p (ni2dest, PATTERN (insn)))
5089e22e
RS
2133 {
2134 for (link = LOG_LINKS (insn); link;
2135 link = XEXP (link, 1))
2136 if (XEXP (link, 0) == i3)
2137 XEXP (link, 0) = i1;
2138
2139 break;
2140 }
2141 }
2142 }
230d793d
RS
2143 }
2144
2145 /* Similarly, check for a case where we have a PARALLEL of two independent
2146 SETs but we started with three insns. In this case, we can do the sets
2147 as two separate insns. This case occurs when some SET allows two
2148 other insns to combine, but the destination of that SET is still live. */
2149
2150 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2151 && GET_CODE (newpat) == PARALLEL
2152 && XVECLEN (newpat, 0) == 2
2153 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2154 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2155 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2156 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2157 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2158 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2159 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2160 INSN_CUID (i2))
2161 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2162 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2163 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2164 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2165 XVECEXP (newpat, 0, 0))
2166 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2167 XVECEXP (newpat, 0, 1)))
2168 {
e9a25f70
JL
2169 /* Normally, it doesn't matter which of the two is done first,
2170 but it does if one references cc0. In that case, it has to
2171 be first. */
2172#ifdef HAVE_cc0
2173 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2174 {
2175 newi2pat = XVECEXP (newpat, 0, 0);
2176 newpat = XVECEXP (newpat, 0, 1);
2177 }
2178 else
2179#endif
2180 {
2181 newi2pat = XVECEXP (newpat, 0, 1);
2182 newpat = XVECEXP (newpat, 0, 0);
2183 }
230d793d 2184
8e2f6e35 2185 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
a29ca9db 2186
230d793d 2187 if (i2_code_number >= 0)
8e2f6e35 2188 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
230d793d
RS
2189 }
2190
2191 /* If it still isn't recognized, fail and change things back the way they
2192 were. */
2193 if ((insn_code_number < 0
2194 /* Is the result a reasonable ASM_OPERANDS? */
2195 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2196 {
2197 undo_all ();
2198 return 0;
2199 }
2200
2201 /* If we had to change another insn, make sure it is valid also. */
2202 if (undobuf.other_insn)
2203 {
230d793d
RS
2204 rtx other_pat = PATTERN (undobuf.other_insn);
2205 rtx new_other_notes;
2206 rtx note, next;
2207
6e25d159
RK
2208 CLEAR_HARD_REG_SET (newpat_used_regs);
2209
8e2f6e35
BS
2210 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2211 &new_other_notes);
230d793d
RS
2212
2213 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2214 {
2215 undo_all ();
2216 return 0;
2217 }
2218
2219 PATTERN (undobuf.other_insn) = other_pat;
2220
2221 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2222 are still valid. Then add any non-duplicate notes added by
2223 recog_for_combine. */
2224 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2225 {
2226 next = XEXP (note, 1);
2227
2228 if (REG_NOTE_KIND (note) == REG_UNUSED
2229 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
1a26b032
RK
2230 {
2231 if (GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 2232 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
1a26b032
RK
2233
2234 remove_note (undobuf.other_insn, note);
2235 }
230d793d
RS
2236 }
2237
1a26b032
RK
2238 for (note = new_other_notes; note; note = XEXP (note, 1))
2239 if (GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 2240 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
1a26b032 2241
230d793d 2242 distribute_notes (new_other_notes, undobuf.other_insn,
5f4f0e22 2243 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
230d793d
RS
2244 }
2245
2246 /* We now know that we can do this combination. Merge the insns and
2247 update the status of registers and LOG_LINKS. */
2248
2249 {
2250 rtx i3notes, i2notes, i1notes = 0;
2251 rtx i3links, i2links, i1links = 0;
2252 rtx midnotes = 0;
230d793d 2253 register int regno;
ff3467a9
JW
2254 /* Compute which registers we expect to eliminate. newi2pat may be setting
2255 either i3dest or i2dest, so we must check it. Also, i1dest may be the
2256 same as i3dest, in which case newi2pat may be setting i1dest. */
2257 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2258 || i2dest_in_i2src || i2dest_in_i1src
230d793d 2259 ? 0 : i2dest);
ff3467a9
JW
2260 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2261 || (newi2pat && reg_set_p (i1dest, newi2pat))
2262 ? 0 : i1dest);
230d793d
RS
2263
2264 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2265 clear them. */
2266 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2267 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2268 if (i1)
2269 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2270
2271 /* Ensure that we do not have something that should not be shared but
2272 occurs multiple times in the new insns. Check this by first
5089e22e 2273 resetting all the `used' flags and then copying anything is shared. */
230d793d
RS
2274
2275 reset_used_flags (i3notes);
2276 reset_used_flags (i2notes);
2277 reset_used_flags (i1notes);
2278 reset_used_flags (newpat);
2279 reset_used_flags (newi2pat);
2280 if (undobuf.other_insn)
2281 reset_used_flags (PATTERN (undobuf.other_insn));
2282
2283 i3notes = copy_rtx_if_shared (i3notes);
2284 i2notes = copy_rtx_if_shared (i2notes);
2285 i1notes = copy_rtx_if_shared (i1notes);
2286 newpat = copy_rtx_if_shared (newpat);
2287 newi2pat = copy_rtx_if_shared (newi2pat);
2288 if (undobuf.other_insn)
2289 reset_used_flags (PATTERN (undobuf.other_insn));
2290
2291 INSN_CODE (i3) = insn_code_number;
2292 PATTERN (i3) = newpat;
2293 if (undobuf.other_insn)
2294 INSN_CODE (undobuf.other_insn) = other_code_number;
2295
2296 /* We had one special case above where I2 had more than one set and
2297 we replaced a destination of one of those sets with the destination
2298 of I3. In that case, we have to update LOG_LINKS of insns later
176c9e6b
JW
2299 in this basic block. Note that this (expensive) case is rare.
2300
2301 Also, in this case, we must pretend that all REG_NOTEs for I2
2302 actually came from I3, so that REG_UNUSED notes from I2 will be
2303 properly handled. */
2304
2305 if (i3_subst_into_i2)
2306 {
2307 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2308 if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2309 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2310 && ! find_reg_note (i2, REG_UNUSED,
2311 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2312 for (temp = NEXT_INSN (i2);
2313 temp && (this_basic_block == n_basic_blocks - 1
3b413743 2314 || BLOCK_HEAD (this_basic_block) != temp);
176c9e6b
JW
2315 temp = NEXT_INSN (temp))
2316 if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2317 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2318 if (XEXP (link, 0) == i2)
2319 XEXP (link, 0) = i3;
2320
2321 if (i3notes)
2322 {
2323 rtx link = i3notes;
2324 while (XEXP (link, 1))
2325 link = XEXP (link, 1);
2326 XEXP (link, 1) = i2notes;
2327 }
2328 else
2329 i3notes = i2notes;
2330 i2notes = 0;
2331 }
230d793d
RS
2332
2333 LOG_LINKS (i3) = 0;
2334 REG_NOTES (i3) = 0;
2335 LOG_LINKS (i2) = 0;
2336 REG_NOTES (i2) = 0;
2337
2338 if (newi2pat)
2339 {
2340 INSN_CODE (i2) = i2_code_number;
2341 PATTERN (i2) = newi2pat;
2342 }
2343 else
2344 {
2345 PUT_CODE (i2, NOTE);
2346 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2347 NOTE_SOURCE_FILE (i2) = 0;
2348 }
2349
2350 if (i1)
2351 {
2352 LOG_LINKS (i1) = 0;
2353 REG_NOTES (i1) = 0;
2354 PUT_CODE (i1, NOTE);
2355 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2356 NOTE_SOURCE_FILE (i1) = 0;
2357 }
2358
2359 /* Get death notes for everything that is now used in either I3 or
6eb12cef
RK
2360 I2 and used to die in a previous insn. If we built two new
2361 patterns, move from I1 to I2 then I2 to I3 so that we get the
2362 proper movement on registers that I2 modifies. */
230d793d 2363
230d793d 2364 if (newi2pat)
6eb12cef
RK
2365 {
2366 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2367 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2368 }
2369 else
2370 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2371 i3, &midnotes);
230d793d
RS
2372
2373 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2374 if (i3notes)
5f4f0e22
CH
2375 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2376 elim_i2, elim_i1);
230d793d 2377 if (i2notes)
5f4f0e22
CH
2378 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2379 elim_i2, elim_i1);
230d793d 2380 if (i1notes)
5f4f0e22
CH
2381 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2382 elim_i2, elim_i1);
230d793d 2383 if (midnotes)
5f4f0e22
CH
2384 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2385 elim_i2, elim_i1);
230d793d
RS
2386
2387 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2388 know these are REG_UNUSED and want them to go to the desired insn,
1a26b032
RK
2389 so we always pass it as i3. We have not counted the notes in
2390 reg_n_deaths yet, so we need to do so now. */
2391
230d793d 2392 if (newi2pat && new_i2_notes)
1a26b032
RK
2393 {
2394 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2395 if (GET_CODE (XEXP (temp, 0)) == REG)
b1f21e0a 2396 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
1a26b032
RK
2397
2398 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2399 }
2400
230d793d 2401 if (new_i3_notes)
1a26b032
RK
2402 {
2403 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2404 if (GET_CODE (XEXP (temp, 0)) == REG)
b1f21e0a 2405 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
1a26b032
RK
2406
2407 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2408 }
230d793d
RS
2409
2410 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
e9a25f70
JL
2411 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2412 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2413 in that case, it might delete I2. Similarly for I2 and I1.
1a26b032
RK
2414 Show an additional death due to the REG_DEAD note we make here. If
2415 we discard it in distribute_notes, we will decrement it again. */
d0ab8cd3 2416
230d793d 2417 if (i3dest_killed)
1a26b032
RK
2418 {
2419 if (GET_CODE (i3dest_killed) == REG)
b1f21e0a 2420 REG_N_DEATHS (REGNO (i3dest_killed))++;
1a26b032 2421
e9a25f70 2422 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
38a448ca
RH
2423 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2424 NULL_RTX),
ff3467a9 2425 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
e9a25f70 2426 else
38a448ca
RH
2427 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2428 NULL_RTX),
e9a25f70 2429 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
ff3467a9 2430 elim_i2, elim_i1);
1a26b032 2431 }
58c8c593 2432
230d793d 2433 if (i2dest_in_i2src)
58c8c593 2434 {
1a26b032 2435 if (GET_CODE (i2dest) == REG)
b1f21e0a 2436 REG_N_DEATHS (REGNO (i2dest))++;
1a26b032 2437
58c8c593 2438 if (newi2pat && reg_set_p (i2dest, newi2pat))
38a448ca 2439 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
58c8c593
RK
2440 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2441 else
38a448ca 2442 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
58c8c593
RK
2443 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2444 NULL_RTX, NULL_RTX);
2445 }
2446
230d793d 2447 if (i1dest_in_i1src)
58c8c593 2448 {
1a26b032 2449 if (GET_CODE (i1dest) == REG)
b1f21e0a 2450 REG_N_DEATHS (REGNO (i1dest))++;
1a26b032 2451
58c8c593 2452 if (newi2pat && reg_set_p (i1dest, newi2pat))
38a448ca 2453 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
58c8c593
RK
2454 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2455 else
38a448ca 2456 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
58c8c593
RK
2457 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2458 NULL_RTX, NULL_RTX);
2459 }
230d793d
RS
2460
2461 distribute_links (i3links);
2462 distribute_links (i2links);
2463 distribute_links (i1links);
2464
2465 if (GET_CODE (i2dest) == REG)
2466 {
d0ab8cd3
RK
2467 rtx link;
2468 rtx i2_insn = 0, i2_val = 0, set;
2469
2470 /* The insn that used to set this register doesn't exist, and
2471 this life of the register may not exist either. See if one of
2472 I3's links points to an insn that sets I2DEST. If it does,
2473 that is now the last known value for I2DEST. If we don't update
2474 this and I2 set the register to a value that depended on its old
230d793d
RS
2475 contents, we will get confused. If this insn is used, thing
2476 will be set correctly in combine_instructions. */
d0ab8cd3
RK
2477
2478 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2479 if ((set = single_set (XEXP (link, 0))) != 0
2480 && rtx_equal_p (i2dest, SET_DEST (set)))
2481 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2482
2483 record_value_for_reg (i2dest, i2_insn, i2_val);
230d793d
RS
2484
2485 /* If the reg formerly set in I2 died only once and that was in I3,
2486 zero its use count so it won't make `reload' do any work. */
538fe8cd
ILT
2487 if (! added_sets_2
2488 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2489 && ! i2dest_in_i2src)
230d793d
RS
2490 {
2491 regno = REGNO (i2dest);
b1f21e0a
MM
2492 REG_N_SETS (regno)--;
2493 if (REG_N_SETS (regno) == 0
e881bb1b
RH
2494 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2495 regno))
b1f21e0a 2496 REG_N_REFS (regno) = 0;
230d793d
RS
2497 }
2498 }
2499
2500 if (i1 && GET_CODE (i1dest) == REG)
2501 {
d0ab8cd3
RK
2502 rtx link;
2503 rtx i1_insn = 0, i1_val = 0, set;
2504
2505 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2506 if ((set = single_set (XEXP (link, 0))) != 0
2507 && rtx_equal_p (i1dest, SET_DEST (set)))
2508 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2509
2510 record_value_for_reg (i1dest, i1_insn, i1_val);
2511
230d793d 2512 regno = REGNO (i1dest);
5af91171 2513 if (! added_sets_1 && ! i1dest_in_i1src)
230d793d 2514 {
b1f21e0a
MM
2515 REG_N_SETS (regno)--;
2516 if (REG_N_SETS (regno) == 0
e881bb1b
RH
2517 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2518 regno))
b1f21e0a 2519 REG_N_REFS (regno) = 0;
230d793d
RS
2520 }
2521 }
2522
951553af 2523 /* Update reg_nonzero_bits et al for any changes that may have been made
22609cbf
RK
2524 to this insn. */
2525
951553af 2526 note_stores (newpat, set_nonzero_bits_and_sign_copies);
22609cbf 2527 if (newi2pat)
951553af 2528 note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
22609cbf 2529
230d793d
RS
2530 /* If I3 is now an unconditional jump, ensure that it has a
2531 BARRIER following it since it may have initially been a
381ee8af 2532 conditional jump. It may also be the last nonnote insn. */
230d793d
RS
2533
2534 if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
381ee8af
TW
2535 && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2536 || GET_CODE (temp) != BARRIER))
230d793d
RS
2537 emit_barrier_after (i3);
2538 }
2539
2540 combine_successes++;
2541
bcd49eb7
JW
2542 /* Clear this here, so that subsequent get_last_value calls are not
2543 affected. */
2544 subst_prev_insn = NULL_RTX;
2545
abe6e52f
RK
2546 if (added_links_insn
2547 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2548 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2549 return added_links_insn;
2550 else
2551 return newi2pat ? i2 : i3;
230d793d
RS
2552}
2553\f
2554/* Undo all the modifications recorded in undobuf. */
2555
2556static void
2557undo_all ()
2558{
241cea85
RK
2559 struct undo *undo, *next;
2560
2561 for (undo = undobuf.undos; undo; undo = next)
7c046e4e 2562 {
241cea85
RK
2563 next = undo->next;
2564 if (undo->is_int)
2565 *undo->where.i = undo->old_contents.i;
7c046e4e 2566 else
241cea85
RK
2567 *undo->where.r = undo->old_contents.r;
2568
2569 undo->next = undobuf.frees;
2570 undobuf.frees = undo;
7c046e4e 2571 }
230d793d
RS
2572
2573 obfree (undobuf.storage);
845fc875 2574 undobuf.undos = undobuf.previous_undos = 0;
bcd49eb7
JW
2575
2576 /* Clear this here, so that subsequent get_last_value calls are not
2577 affected. */
2578 subst_prev_insn = NULL_RTX;
230d793d
RS
2579}
2580\f
2581/* Find the innermost point within the rtx at LOC, possibly LOC itself,
d0ab8cd3
RK
2582 where we have an arithmetic expression and return that point. LOC will
2583 be inside INSN.
230d793d
RS
2584
2585 try_combine will call this function to see if an insn can be split into
2586 two insns. */
2587
2588static rtx *
d0ab8cd3 2589find_split_point (loc, insn)
230d793d 2590 rtx *loc;
d0ab8cd3 2591 rtx insn;
230d793d
RS
2592{
2593 rtx x = *loc;
2594 enum rtx_code code = GET_CODE (x);
2595 rtx *split;
6a651371
KG
2596 int len = 0, pos = 0, unsignedp = 0;
2597 rtx inner = NULL_RTX;
230d793d
RS
2598
2599 /* First special-case some codes. */
2600 switch (code)
2601 {
2602 case SUBREG:
2603#ifdef INSN_SCHEDULING
2604 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2605 point. */
2606 if (GET_CODE (SUBREG_REG (x)) == MEM)
2607 return loc;
2608#endif
d0ab8cd3 2609 return find_split_point (&SUBREG_REG (x), insn);
230d793d 2610
230d793d 2611 case MEM:
916f14f1 2612#ifdef HAVE_lo_sum
230d793d
RS
2613 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2614 using LO_SUM and HIGH. */
2615 if (GET_CODE (XEXP (x, 0)) == CONST
2616 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2617 {
2618 SUBST (XEXP (x, 0),
2619 gen_rtx_combine (LO_SUM, Pmode,
2620 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2621 XEXP (x, 0)));
2622 return &XEXP (XEXP (x, 0), 0);
2623 }
230d793d
RS
2624#endif
2625
916f14f1
RK
2626 /* If we have a PLUS whose second operand is a constant and the
2627 address is not valid, perhaps will can split it up using
2628 the machine-specific way to split large constants. We use
ddd5a7c1 2629 the first pseudo-reg (one of the virtual regs) as a placeholder;
916f14f1
RK
2630 it will not remain in the result. */
2631 if (GET_CODE (XEXP (x, 0)) == PLUS
2632 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2633 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2634 {
2635 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
38a448ca 2636 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
916f14f1
RK
2637 subst_insn);
2638
2639 /* This should have produced two insns, each of which sets our
2640 placeholder. If the source of the second is a valid address,
2641 we can make put both sources together and make a split point
2642 in the middle. */
2643
2644 if (seq && XVECLEN (seq, 0) == 2
2645 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2646 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2647 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2648 && ! reg_mentioned_p (reg,
2649 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2650 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2651 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2652 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2653 && memory_address_p (GET_MODE (x),
2654 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2655 {
2656 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2657 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2658
2659 /* Replace the placeholder in SRC2 with SRC1. If we can
2660 find where in SRC2 it was placed, that can become our
2661 split point and we can replace this address with SRC2.
2662 Just try two obvious places. */
2663
2664 src2 = replace_rtx (src2, reg, src1);
2665 split = 0;
2666 if (XEXP (src2, 0) == src1)
2667 split = &XEXP (src2, 0);
2668 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2669 && XEXP (XEXP (src2, 0), 0) == src1)
2670 split = &XEXP (XEXP (src2, 0), 0);
2671
2672 if (split)
2673 {
2674 SUBST (XEXP (x, 0), src2);
2675 return split;
2676 }
2677 }
1a26b032
RK
2678
2679 /* If that didn't work, perhaps the first operand is complex and
2680 needs to be computed separately, so make a split point there.
2681 This will occur on machines that just support REG + CONST
2682 and have a constant moved through some previous computation. */
2683
2684 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2685 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2686 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2687 == 'o')))
2688 return &XEXP (XEXP (x, 0), 0);
916f14f1
RK
2689 }
2690 break;
2691
230d793d
RS
2692 case SET:
2693#ifdef HAVE_cc0
2694 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2695 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2696 we need to put the operand into a register. So split at that
2697 point. */
2698
2699 if (SET_DEST (x) == cc0_rtx
2700 && GET_CODE (SET_SRC (x)) != COMPARE
2701 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2702 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2703 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2704 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2705 return &SET_SRC (x);
2706#endif
2707
2708 /* See if we can split SET_SRC as it stands. */
d0ab8cd3 2709 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2710 if (split && split != &SET_SRC (x))
2711 return split;
2712
041d7180
JL
2713 /* See if we can split SET_DEST as it stands. */
2714 split = find_split_point (&SET_DEST (x), insn);
2715 if (split && split != &SET_DEST (x))
2716 return split;
2717
230d793d
RS
2718 /* See if this is a bitfield assignment with everything constant. If
2719 so, this is an IOR of an AND, so split it into that. */
2720 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2721 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
5f4f0e22 2722 <= HOST_BITS_PER_WIDE_INT)
230d793d
RS
2723 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2724 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2725 && GET_CODE (SET_SRC (x)) == CONST_INT
2726 && ((INTVAL (XEXP (SET_DEST (x), 1))
2727 + INTVAL (XEXP (SET_DEST (x), 2)))
2728 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2729 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2730 {
2731 int pos = INTVAL (XEXP (SET_DEST (x), 2));
2732 int len = INTVAL (XEXP (SET_DEST (x), 1));
2733 int src = INTVAL (SET_SRC (x));
2734 rtx dest = XEXP (SET_DEST (x), 0);
2735 enum machine_mode mode = GET_MODE (dest);
5f4f0e22 2736 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
230d793d 2737
f76b9db2
ILT
2738 if (BITS_BIG_ENDIAN)
2739 pos = GET_MODE_BITSIZE (mode) - len - pos;
230d793d 2740
e51712db 2741 if ((unsigned HOST_WIDE_INT) src == mask)
230d793d 2742 SUBST (SET_SRC (x),
5f4f0e22 2743 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
230d793d
RS
2744 else
2745 SUBST (SET_SRC (x),
2746 gen_binary (IOR, mode,
2747 gen_binary (AND, mode, dest,
5f4f0e22
CH
2748 GEN_INT (~ (mask << pos)
2749 & GET_MODE_MASK (mode))),
2750 GEN_INT (src << pos)));
230d793d
RS
2751
2752 SUBST (SET_DEST (x), dest);
2753
d0ab8cd3 2754 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2755 if (split && split != &SET_SRC (x))
2756 return split;
2757 }
2758
2759 /* Otherwise, see if this is an operation that we can split into two.
2760 If so, try to split that. */
2761 code = GET_CODE (SET_SRC (x));
2762
2763 switch (code)
2764 {
d0ab8cd3
RK
2765 case AND:
2766 /* If we are AND'ing with a large constant that is only a single
2767 bit and the result is only being used in a context where we
2768 need to know if it is zero or non-zero, replace it with a bit
2769 extraction. This will avoid the large constant, which might
2770 have taken more than one insn to make. If the constant were
2771 not a valid argument to the AND but took only one insn to make,
2772 this is no worse, but if it took more than one insn, it will
2773 be better. */
2774
2775 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2776 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2777 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2778 && GET_CODE (SET_DEST (x)) == REG
2779 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2780 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2781 && XEXP (*split, 0) == SET_DEST (x)
2782 && XEXP (*split, 1) == const0_rtx)
2783 {
76184def
DE
2784 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2785 XEXP (SET_SRC (x), 0),
2786 pos, NULL_RTX, 1, 1, 0, 0);
2787 if (extraction != 0)
2788 {
2789 SUBST (SET_SRC (x), extraction);
2790 return find_split_point (loc, insn);
2791 }
d0ab8cd3
RK
2792 }
2793 break;
2794
1a6ec070
RK
2795 case NE:
2796 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2797 is known to be on, this can be converted into a NEG of a shift. */
2798 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2799 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4eb2cb10 2800 && 1 <= (pos = exact_log2
1a6ec070
RK
2801 (nonzero_bits (XEXP (SET_SRC (x), 0),
2802 GET_MODE (XEXP (SET_SRC (x), 0))))))
2803 {
2804 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2805
2806 SUBST (SET_SRC (x),
2807 gen_rtx_combine (NEG, mode,
2808 gen_rtx_combine (LSHIFTRT, mode,
2809 XEXP (SET_SRC (x), 0),
4eb2cb10 2810 GEN_INT (pos))));
1a6ec070
RK
2811
2812 split = find_split_point (&SET_SRC (x), insn);
2813 if (split && split != &SET_SRC (x))
2814 return split;
2815 }
2816 break;
2817
230d793d
RS
2818 case SIGN_EXTEND:
2819 inner = XEXP (SET_SRC (x), 0);
101c1a3d
JL
2820
2821 /* We can't optimize if either mode is a partial integer
2822 mode as we don't know how many bits are significant
2823 in those modes. */
2824 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2825 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2826 break;
2827
230d793d
RS
2828 pos = 0;
2829 len = GET_MODE_BITSIZE (GET_MODE (inner));
2830 unsignedp = 0;
2831 break;
2832
2833 case SIGN_EXTRACT:
2834 case ZERO_EXTRACT:
2835 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2836 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2837 {
2838 inner = XEXP (SET_SRC (x), 0);
2839 len = INTVAL (XEXP (SET_SRC (x), 1));
2840 pos = INTVAL (XEXP (SET_SRC (x), 2));
2841
f76b9db2
ILT
2842 if (BITS_BIG_ENDIAN)
2843 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
230d793d
RS
2844 unsignedp = (code == ZERO_EXTRACT);
2845 }
2846 break;
e9a25f70
JL
2847
2848 default:
2849 break;
230d793d
RS
2850 }
2851
2852 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2853 {
2854 enum machine_mode mode = GET_MODE (SET_SRC (x));
2855
d0ab8cd3
RK
2856 /* For unsigned, we have a choice of a shift followed by an
2857 AND or two shifts. Use two shifts for field sizes where the
2858 constant might be too large. We assume here that we can
2859 always at least get 8-bit constants in an AND insn, which is
2860 true for every current RISC. */
2861
2862 if (unsignedp && len <= 8)
230d793d
RS
2863 {
2864 SUBST (SET_SRC (x),
2865 gen_rtx_combine
2866 (AND, mode,
2867 gen_rtx_combine (LSHIFTRT, mode,
2868 gen_lowpart_for_combine (mode, inner),
5f4f0e22
CH
2869 GEN_INT (pos)),
2870 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
230d793d 2871
d0ab8cd3 2872 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2873 if (split && split != &SET_SRC (x))
2874 return split;
2875 }
2876 else
2877 {
2878 SUBST (SET_SRC (x),
2879 gen_rtx_combine
d0ab8cd3 2880 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
230d793d
RS
2881 gen_rtx_combine (ASHIFT, mode,
2882 gen_lowpart_for_combine (mode, inner),
5f4f0e22
CH
2883 GEN_INT (GET_MODE_BITSIZE (mode)
2884 - len - pos)),
2885 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
230d793d 2886
d0ab8cd3 2887 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2888 if (split && split != &SET_SRC (x))
2889 return split;
2890 }
2891 }
2892
2893 /* See if this is a simple operation with a constant as the second
2894 operand. It might be that this constant is out of range and hence
2895 could be used as a split point. */
2896 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2897 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2898 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2899 && CONSTANT_P (XEXP (SET_SRC (x), 1))
2900 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2901 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2902 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2903 == 'o'))))
2904 return &XEXP (SET_SRC (x), 1);
2905
2906 /* Finally, see if this is a simple operation with its first operand
2907 not in a register. The operation might require this operand in a
2908 register, so return it as a split point. We can always do this
2909 because if the first operand were another operation, we would have
2910 already found it as a split point. */
2911 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2912 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2913 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2914 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2915 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2916 return &XEXP (SET_SRC (x), 0);
2917
2918 return 0;
2919
2920 case AND:
2921 case IOR:
2922 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2923 it is better to write this as (not (ior A B)) so we can split it.
2924 Similarly for IOR. */
2925 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2926 {
2927 SUBST (*loc,
2928 gen_rtx_combine (NOT, GET_MODE (x),
2929 gen_rtx_combine (code == IOR ? AND : IOR,
2930 GET_MODE (x),
2931 XEXP (XEXP (x, 0), 0),
2932 XEXP (XEXP (x, 1), 0))));
d0ab8cd3 2933 return find_split_point (loc, insn);
230d793d
RS
2934 }
2935
2936 /* Many RISC machines have a large set of logical insns. If the
2937 second operand is a NOT, put it first so we will try to split the
2938 other operand first. */
2939 if (GET_CODE (XEXP (x, 1)) == NOT)
2940 {
2941 rtx tem = XEXP (x, 0);
2942 SUBST (XEXP (x, 0), XEXP (x, 1));
2943 SUBST (XEXP (x, 1), tem);
2944 }
2945 break;
e9a25f70
JL
2946
2947 default:
2948 break;
230d793d
RS
2949 }
2950
2951 /* Otherwise, select our actions depending on our rtx class. */
2952 switch (GET_RTX_CLASS (code))
2953 {
2954 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
2955 case '3':
d0ab8cd3 2956 split = find_split_point (&XEXP (x, 2), insn);
230d793d
RS
2957 if (split)
2958 return split;
0f41302f 2959 /* ... fall through ... */
230d793d
RS
2960 case '2':
2961 case 'c':
2962 case '<':
d0ab8cd3 2963 split = find_split_point (&XEXP (x, 1), insn);
230d793d
RS
2964 if (split)
2965 return split;
0f41302f 2966 /* ... fall through ... */
230d793d
RS
2967 case '1':
2968 /* Some machines have (and (shift ...) ...) insns. If X is not
2969 an AND, but XEXP (X, 0) is, use it as our split point. */
2970 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2971 return &XEXP (x, 0);
2972
d0ab8cd3 2973 split = find_split_point (&XEXP (x, 0), insn);
230d793d
RS
2974 if (split)
2975 return split;
2976 return loc;
2977 }
2978
2979 /* Otherwise, we don't have a split point. */
2980 return 0;
2981}
2982\f
2983/* Throughout X, replace FROM with TO, and return the result.
2984 The result is TO if X is FROM;
2985 otherwise the result is X, but its contents may have been modified.
2986 If they were modified, a record was made in undobuf so that
2987 undo_all will (among other things) return X to its original state.
2988
2989 If the number of changes necessary is too much to record to undo,
2990 the excess changes are not made, so the result is invalid.
2991 The changes already made can still be undone.
2992 undobuf.num_undo is incremented for such changes, so by testing that
2993 the caller can tell whether the result is valid.
2994
2995 `n_occurrences' is incremented each time FROM is replaced.
2996
2997 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
2998
5089e22e 2999 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
230d793d
RS
3000 by copying if `n_occurrences' is non-zero. */
3001
3002static rtx
3003subst (x, from, to, in_dest, unique_copy)
3004 register rtx x, from, to;
3005 int in_dest;
3006 int unique_copy;
3007{
f24ad0e4 3008 register enum rtx_code code = GET_CODE (x);
230d793d 3009 enum machine_mode op0_mode = VOIDmode;
6f7d635c 3010 register const char *fmt;
8079805d
RK
3011 register int len, i;
3012 rtx new;
230d793d
RS
3013
3014/* Two expressions are equal if they are identical copies of a shared
3015 RTX or if they are both registers with the same register number
3016 and mode. */
3017
3018#define COMBINE_RTX_EQUAL_P(X,Y) \
3019 ((X) == (Y) \
3020 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3021 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3022
3023 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3024 {
3025 n_occurrences++;
3026 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3027 }
3028
3029 /* If X and FROM are the same register but different modes, they will
3030 not have been seen as equal above. However, flow.c will make a
3031 LOG_LINKS entry for that case. If we do nothing, we will try to
3032 rerecognize our original insn and, when it succeeds, we will
3033 delete the feeding insn, which is incorrect.
3034
3035 So force this insn not to match in this (rare) case. */
3036 if (! in_dest && code == REG && GET_CODE (from) == REG
3037 && REGNO (x) == REGNO (from))
38a448ca 3038 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
230d793d
RS
3039
3040 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3041 of which may contain things that can be combined. */
3042 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3043 return x;
3044
3045 /* It is possible to have a subexpression appear twice in the insn.
3046 Suppose that FROM is a register that appears within TO.
3047 Then, after that subexpression has been scanned once by `subst',
3048 the second time it is scanned, TO may be found. If we were
3049 to scan TO here, we would find FROM within it and create a
3050 self-referent rtl structure which is completely wrong. */
3051 if (COMBINE_RTX_EQUAL_P (x, to))
3052 return to;
3053
4f4b3679
RH
3054 /* Parallel asm_operands need special attention because all of the
3055 inputs are shared across the arms. Furthermore, unsharing the
3056 rtl results in recognition failures. Failure to handle this case
3057 specially can result in circular rtl.
3058
3059 Solve this by doing a normal pass across the first entry of the
3060 parallel, and only processing the SET_DESTs of the subsequent
3061 entries. Ug. */
3062
3063 if (code == PARALLEL
3064 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3065 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
230d793d 3066 {
4f4b3679
RH
3067 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3068
3069 /* If this substitution failed, this whole thing fails. */
3070 if (GET_CODE (new) == CLOBBER
3071 && XEXP (new, 0) == const0_rtx)
3072 return new;
3073
3074 SUBST (XVECEXP (x, 0, 0), new);
3075
3076 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
230d793d 3077 {
4f4b3679
RH
3078 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3079
3080 if (GET_CODE (dest) != REG
3081 && GET_CODE (dest) != CC0
3082 && GET_CODE (dest) != PC)
230d793d 3083 {
4f4b3679 3084 new = subst (dest, from, to, 0, unique_copy);
230d793d 3085
4f4b3679
RH
3086 /* If this substitution failed, this whole thing fails. */
3087 if (GET_CODE (new) == CLOBBER
3088 && XEXP (new, 0) == const0_rtx)
3089 return new;
230d793d 3090
4f4b3679 3091 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
230d793d
RS
3092 }
3093 }
4f4b3679
RH
3094 }
3095 else
3096 {
3097 len = GET_RTX_LENGTH (code);
3098 fmt = GET_RTX_FORMAT (code);
3099
3100 /* We don't need to process a SET_DEST that is a register, CC0,
3101 or PC, so set up to skip this common case. All other cases
3102 where we want to suppress replacing something inside a
3103 SET_SRC are handled via the IN_DEST operand. */
3104 if (code == SET
3105 && (GET_CODE (SET_DEST (x)) == REG
3106 || GET_CODE (SET_DEST (x)) == CC0
3107 || GET_CODE (SET_DEST (x)) == PC))
3108 fmt = "ie";
3109
3110 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3111 constant. */
3112 if (fmt[0] == 'e')
3113 op0_mode = GET_MODE (XEXP (x, 0));
3114
3115 for (i = 0; i < len; i++)
230d793d 3116 {
4f4b3679 3117 if (fmt[i] == 'E')
230d793d 3118 {
4f4b3679
RH
3119 register int j;
3120 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3121 {
3122 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3123 {
3124 new = (unique_copy && n_occurrences
3125 ? copy_rtx (to) : to);
3126 n_occurrences++;
3127 }
3128 else
3129 {
3130 new = subst (XVECEXP (x, i, j), from, to, 0,
3131 unique_copy);
3132
3133 /* If this substitution failed, this whole thing
3134 fails. */
3135 if (GET_CODE (new) == CLOBBER
3136 && XEXP (new, 0) == const0_rtx)
3137 return new;
3138 }
3139
3140 SUBST (XVECEXP (x, i, j), new);
3141 }
3142 }
3143 else if (fmt[i] == 'e')
3144 {
3145 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3146 {
3147 /* In general, don't install a subreg involving two
3148 modes not tieable. It can worsen register
3149 allocation, and can even make invalid reload
3150 insns, since the reg inside may need to be copied
3151 from in the outside mode, and that may be invalid
3152 if it is an fp reg copied in integer mode.
3153
3154 We allow two exceptions to this: It is valid if
3155 it is inside another SUBREG and the mode of that
3156 SUBREG and the mode of the inside of TO is
3157 tieable and it is valid if X is a SET that copies
3158 FROM to CC0. */
3159
3160 if (GET_CODE (to) == SUBREG
3161 && ! MODES_TIEABLE_P (GET_MODE (to),
3162 GET_MODE (SUBREG_REG (to)))
3163 && ! (code == SUBREG
3164 && MODES_TIEABLE_P (GET_MODE (x),
3165 GET_MODE (SUBREG_REG (to))))
42301240 3166#ifdef HAVE_cc0
4f4b3679 3167 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
42301240 3168#endif
4f4b3679
RH
3169 )
3170 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
42301240 3171
4f4b3679
RH
3172 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3173 n_occurrences++;
3174 }
3175 else
3176 /* If we are in a SET_DEST, suppress most cases unless we
3177 have gone inside a MEM, in which case we want to
3178 simplify the address. We assume here that things that
3179 are actually part of the destination have their inner
3180 parts in the first expression. This is true for SUBREG,
3181 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3182 things aside from REG and MEM that should appear in a
3183 SET_DEST. */
3184 new = subst (XEXP (x, i), from, to,
3185 (((in_dest
3186 && (code == SUBREG || code == STRICT_LOW_PART
3187 || code == ZERO_EXTRACT))
3188 || code == SET)
3189 && i == 0), unique_copy);
3190
3191 /* If we found that we will have to reject this combination,
3192 indicate that by returning the CLOBBER ourselves, rather than
3193 an expression containing it. This will speed things up as
3194 well as prevent accidents where two CLOBBERs are considered
3195 to be equal, thus producing an incorrect simplification. */
3196
3197 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3198 return new;
3199
3200 SUBST (XEXP (x, i), new);
230d793d 3201 }
230d793d
RS
3202 }
3203 }
3204
8079805d
RK
3205 /* Try to simplify X. If the simplification changed the code, it is likely
3206 that further simplification will help, so loop, but limit the number
3207 of repetitions that will be performed. */
3208
3209 for (i = 0; i < 4; i++)
3210 {
3211 /* If X is sufficiently simple, don't bother trying to do anything
3212 with it. */
3213 if (code != CONST_INT && code != REG && code != CLOBBER)
3214 x = simplify_rtx (x, op0_mode, i == 3, in_dest);
d0ab8cd3 3215
8079805d
RK
3216 if (GET_CODE (x) == code)
3217 break;
d0ab8cd3 3218
8079805d 3219 code = GET_CODE (x);
eeb43d32 3220
8079805d
RK
3221 /* We no longer know the original mode of operand 0 since we
3222 have changed the form of X) */
3223 op0_mode = VOIDmode;
3224 }
eeb43d32 3225
8079805d
RK
3226 return x;
3227}
3228\f
3229/* Simplify X, a piece of RTL. We just operate on the expression at the
3230 outer level; call `subst' to simplify recursively. Return the new
3231 expression.
3232
3233 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3234 will be the iteration even if an expression with a code different from
3235 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
eeb43d32 3236
8079805d
RK
3237static rtx
3238simplify_rtx (x, op0_mode, last, in_dest)
3239 rtx x;
3240 enum machine_mode op0_mode;
3241 int last;
3242 int in_dest;
3243{
3244 enum rtx_code code = GET_CODE (x);
3245 enum machine_mode mode = GET_MODE (x);
3246 rtx temp;
3247 int i;
d0ab8cd3 3248
230d793d
RS
3249 /* If this is a commutative operation, put a constant last and a complex
3250 expression first. We don't need to do this for comparisons here. */
3251 if (GET_RTX_CLASS (code) == 'c'
3252 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3253 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3254 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3255 || (GET_CODE (XEXP (x, 0)) == SUBREG
3256 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3257 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3258 {
3259 temp = XEXP (x, 0);
3260 SUBST (XEXP (x, 0), XEXP (x, 1));
3261 SUBST (XEXP (x, 1), temp);
3262 }
3263
22609cbf
RK
3264 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3265 sign extension of a PLUS with a constant, reverse the order of the sign
3266 extension and the addition. Note that this not the same as the original
3267 code, but overflow is undefined for signed values. Also note that the
3268 PLUS will have been partially moved "inside" the sign-extension, so that
3269 the first operand of X will really look like:
3270 (ashiftrt (plus (ashift A C4) C5) C4).
3271 We convert this to
3272 (plus (ashiftrt (ashift A C4) C2) C4)
3273 and replace the first operand of X with that expression. Later parts
3274 of this function may simplify the expression further.
3275
3276 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3277 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3278 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3279
3280 We do this to simplify address expressions. */
3281
3282 if ((code == PLUS || code == MINUS || code == MULT)
3283 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3284 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3285 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3286 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3287 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3288 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3289 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3290 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3291 XEXP (XEXP (XEXP (x, 0), 0), 1),
3292 XEXP (XEXP (x, 0), 1))) != 0)
3293 {
3294 rtx new
3295 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3296 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3297 INTVAL (XEXP (XEXP (x, 0), 1)));
3298
3299 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3300 INTVAL (XEXP (XEXP (x, 0), 1)));
3301
3302 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3303 }
3304
d0ab8cd3
RK
3305 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3306 applying it to the arms of the IF_THEN_ELSE. This often simplifies
abe6e52f
RK
3307 things. Check for cases where both arms are testing the same
3308 condition.
3309
3310 Don't do anything if all operands are very simple. */
3311
3312 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3313 || GET_RTX_CLASS (code) == '<')
3314 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3315 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3316 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3317 == 'o')))
3318 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3319 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3320 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3321 == 'o')))))
3322 || (GET_RTX_CLASS (code) == '1'
3323 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3324 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3325 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3326 == 'o'))))))
d0ab8cd3 3327 {
abe6e52f
RK
3328 rtx cond, true, false;
3329
3330 cond = if_then_else_cond (x, &true, &false);
0802d516
RK
3331 if (cond != 0
3332 /* If everything is a comparison, what we have is highly unlikely
3333 to be simpler, so don't use it. */
3334 && ! (GET_RTX_CLASS (code) == '<'
3335 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3336 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
abe6e52f
RK
3337 {
3338 rtx cop1 = const0_rtx;
3339 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3340
15448afc
RK
3341 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3342 return x;
3343
9210df58
RK
3344 /* Simplify the alternative arms; this may collapse the true and
3345 false arms to store-flag values. */
3346 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3347 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3348
3349 /* Restarting if we generate a store-flag expression will cause
3350 us to loop. Just drop through in this case. */
3351
abe6e52f
RK
3352 /* If the result values are STORE_FLAG_VALUE and zero, we can
3353 just make the comparison operation. */
3354 if (true == const_true_rtx && false == const0_rtx)
3355 x = gen_binary (cond_code, mode, cond, cop1);
3356 else if (true == const0_rtx && false == const_true_rtx)
3357 x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3358
3359 /* Likewise, we can make the negate of a comparison operation
3360 if the result values are - STORE_FLAG_VALUE and zero. */
3361 else if (GET_CODE (true) == CONST_INT
3362 && INTVAL (true) == - STORE_FLAG_VALUE
3363 && false == const0_rtx)
0c1c8ea6 3364 x = gen_unary (NEG, mode, mode,
abe6e52f
RK
3365 gen_binary (cond_code, mode, cond, cop1));
3366 else if (GET_CODE (false) == CONST_INT
3367 && INTVAL (false) == - STORE_FLAG_VALUE
3368 && true == const0_rtx)
0c1c8ea6 3369 x = gen_unary (NEG, mode, mode,
abe6e52f
RK
3370 gen_binary (reverse_condition (cond_code),
3371 mode, cond, cop1));
3372 else
38a448ca
RH
3373 return gen_rtx_IF_THEN_ELSE (mode,
3374 gen_binary (cond_code, VOIDmode,
3375 cond, cop1),
3376 true, false);
5109d49f 3377
9210df58
RK
3378 code = GET_CODE (x);
3379 op0_mode = VOIDmode;
abe6e52f 3380 }
d0ab8cd3
RK
3381 }
3382
230d793d
RS
3383 /* Try to fold this expression in case we have constants that weren't
3384 present before. */
3385 temp = 0;
3386 switch (GET_RTX_CLASS (code))
3387 {
3388 case '1':
3389 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3390 break;
3391 case '<':
3392 temp = simplify_relational_operation (code, op0_mode,
3393 XEXP (x, 0), XEXP (x, 1));
77fa0940
RK
3394#ifdef FLOAT_STORE_FLAG_VALUE
3395 if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3396 temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3397 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3398#endif
230d793d
RS
3399 break;
3400 case 'c':
3401 case '2':
3402 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3403 break;
3404 case 'b':
3405 case '3':
3406 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3407 XEXP (x, 1), XEXP (x, 2));
3408 break;
3409 }
3410
3411 if (temp)
d0ab8cd3 3412 x = temp, code = GET_CODE (temp);
230d793d 3413
230d793d 3414 /* First see if we can apply the inverse distributive law. */
224eeff2
RK
3415 if (code == PLUS || code == MINUS
3416 || code == AND || code == IOR || code == XOR)
230d793d
RS
3417 {
3418 x = apply_distributive_law (x);
3419 code = GET_CODE (x);
3420 }
3421
3422 /* If CODE is an associative operation not otherwise handled, see if we
3423 can associate some operands. This can win if they are constants or
3424 if they are logically related (i.e. (a & b) & a. */
3425 if ((code == PLUS || code == MINUS
3426 || code == MULT || code == AND || code == IOR || code == XOR
3427 || code == DIV || code == UDIV
3428 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3ad2180a 3429 && INTEGRAL_MODE_P (mode))
230d793d
RS
3430 {
3431 if (GET_CODE (XEXP (x, 0)) == code)
3432 {
3433 rtx other = XEXP (XEXP (x, 0), 0);
3434 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3435 rtx inner_op1 = XEXP (x, 1);
3436 rtx inner;
3437
3438 /* Make sure we pass the constant operand if any as the second
3439 one if this is a commutative operation. */
3440 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3441 {
3442 rtx tem = inner_op0;
3443 inner_op0 = inner_op1;
3444 inner_op1 = tem;
3445 }
3446 inner = simplify_binary_operation (code == MINUS ? PLUS
3447 : code == DIV ? MULT
3448 : code == UDIV ? MULT
3449 : code,
3450 mode, inner_op0, inner_op1);
3451
3452 /* For commutative operations, try the other pair if that one
3453 didn't simplify. */
3454 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3455 {
3456 other = XEXP (XEXP (x, 0), 1);
3457 inner = simplify_binary_operation (code, mode,
3458 XEXP (XEXP (x, 0), 0),
3459 XEXP (x, 1));
3460 }
3461
3462 if (inner)
8079805d 3463 return gen_binary (code, mode, other, inner);
230d793d
RS
3464 }
3465 }
3466
3467 /* A little bit of algebraic simplification here. */
3468 switch (code)
3469 {
3470 case MEM:
3471 /* Ensure that our address has any ASHIFTs converted to MULT in case
3472 address-recognizing predicates are called later. */
3473 temp = make_compound_operation (XEXP (x, 0), MEM);
3474 SUBST (XEXP (x, 0), temp);
3475 break;
3476
3477 case SUBREG:
3478 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3479 is paradoxical. If we can't do that safely, then it becomes
3480 something nonsensical so that this combination won't take place. */
3481
3482 if (GET_CODE (SUBREG_REG (x)) == MEM
3483 && (GET_MODE_SIZE (mode)
3484 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3485 {
3486 rtx inner = SUBREG_REG (x);
3487 int endian_offset = 0;
3488 /* Don't change the mode of the MEM
3489 if that would change the meaning of the address. */
3490 if (MEM_VOLATILE_P (SUBREG_REG (x))
3491 || mode_dependent_address_p (XEXP (inner, 0)))
38a448ca 3492 return gen_rtx_CLOBBER (mode, const0_rtx);
230d793d 3493
f76b9db2
ILT
3494 if (BYTES_BIG_ENDIAN)
3495 {
3496 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3497 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3498 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3499 endian_offset -= (UNITS_PER_WORD
3500 - GET_MODE_SIZE (GET_MODE (inner)));
3501 }
230d793d
RS
3502 /* Note if the plus_constant doesn't make a valid address
3503 then this combination won't be accepted. */
38a448ca
RH
3504 x = gen_rtx_MEM (mode,
3505 plus_constant (XEXP (inner, 0),
3506 (SUBREG_WORD (x) * UNITS_PER_WORD
3507 + endian_offset)));
230d793d 3508 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
c6df88cb 3509 MEM_COPY_ATTRIBUTES (x, inner);
230d793d
RS
3510 return x;
3511 }
3512
3513 /* If we are in a SET_DEST, these other cases can't apply. */
3514 if (in_dest)
3515 return x;
3516
3517 /* Changing mode twice with SUBREG => just change it once,
3518 or not at all if changing back to starting mode. */
3519 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3520 {
3521 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3522 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3523 return SUBREG_REG (SUBREG_REG (x));
3524
3525 SUBST_INT (SUBREG_WORD (x),
3526 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3527 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3528 }
3529
3530 /* SUBREG of a hard register => just change the register number
3531 and/or mode. If the hard register is not valid in that mode,
26ecfc76
RK
3532 suppress this combination. If the hard register is the stack,
3533 frame, or argument pointer, leave this as a SUBREG. */
230d793d
RS
3534
3535 if (GET_CODE (SUBREG_REG (x)) == REG
26ecfc76
RK
3536 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3537 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
6d7096b0
DE
3538#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3539 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3540#endif
26ecfc76
RK
3541#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3542 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3543#endif
3544 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
230d793d
RS
3545 {
3546 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3547 mode))
38a448ca
RH
3548 return gen_rtx_REG (mode,
3549 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
230d793d 3550 else
38a448ca 3551 return gen_rtx_CLOBBER (mode, const0_rtx);
230d793d
RS
3552 }
3553
3554 /* For a constant, try to pick up the part we want. Handle a full
a4bde0b1
RK
3555 word and low-order part. Only do this if we are narrowing
3556 the constant; if it is being widened, we have no idea what
3557 the extra bits will have been set to. */
230d793d
RS
3558
3559 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3560 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3c99d5ff 3561 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
230d793d
RS
3562 && GET_MODE_CLASS (mode) == MODE_INT)
3563 {
3564 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
5f4f0e22 3565 0, op0_mode);
230d793d
RS
3566 if (temp)
3567 return temp;
3568 }
3569
19808e22
RS
3570 /* If we want a subreg of a constant, at offset 0,
3571 take the low bits. On a little-endian machine, that's
3572 always valid. On a big-endian machine, it's valid
3c99d5ff 3573 only if the constant's mode fits in one word. Note that we
61b1bece 3574 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3c99d5ff
RK
3575 if (CONSTANT_P (SUBREG_REG (x))
3576 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3577 || ! WORDS_BIG_ENDIAN)
3578 ? SUBREG_WORD (x) == 0
3579 : (SUBREG_WORD (x)
3580 == ((GET_MODE_SIZE (op0_mode)
3581 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3582 / UNITS_PER_WORD)))
f82da7d2 3583 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
f76b9db2
ILT
3584 && (! WORDS_BIG_ENDIAN
3585 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
230d793d
RS
3586 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3587
b65c1b5b
RK
3588 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3589 since we are saying that the high bits don't matter. */
3590 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3591 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3592 return SUBREG_REG (x);
3593
87e3e0c1
RK
3594 /* Note that we cannot do any narrowing for non-constants since
3595 we might have been counting on using the fact that some bits were
3596 zero. We now do this in the SET. */
3597
230d793d
RS
3598 break;
3599
3600 case NOT:
3601 /* (not (plus X -1)) can become (neg X). */
3602 if (GET_CODE (XEXP (x, 0)) == PLUS
3603 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
8079805d 3604 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
230d793d
RS
3605
3606 /* Similarly, (not (neg X)) is (plus X -1). */
3607 if (GET_CODE (XEXP (x, 0)) == NEG)
8079805d
RK
3608 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3609 constm1_rtx);
230d793d 3610
d0ab8cd3
RK
3611 /* (not (xor X C)) for C constant is (xor X D) with D = ~ C. */
3612 if (GET_CODE (XEXP (x, 0)) == XOR
3613 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3614 && (temp = simplify_unary_operation (NOT, mode,
3615 XEXP (XEXP (x, 0), 1),
3616 mode)) != 0)
787745f5 3617 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
d0ab8cd3 3618
230d793d
RS
3619 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3620 other than 1, but that is not valid. We could do a similar
3621 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3622 but this doesn't seem common enough to bother with. */
3623 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3624 && XEXP (XEXP (x, 0), 0) == const1_rtx)
38a448ca
RH
3625 return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3626 XEXP (XEXP (x, 0), 1));
230d793d
RS
3627
3628 if (GET_CODE (XEXP (x, 0)) == SUBREG
3629 && subreg_lowpart_p (XEXP (x, 0))
3630 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3631 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3632 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3633 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3634 {
3635 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3636
38a448ca
RH
3637 x = gen_rtx_ROTATE (inner_mode,
3638 gen_unary (NOT, inner_mode, inner_mode,
3639 const1_rtx),
3640 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
8079805d 3641 return gen_lowpart_for_combine (mode, x);
230d793d
RS
3642 }
3643
0802d516
RK
3644 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3645 reversing the comparison code if valid. */
3646 if (STORE_FLAG_VALUE == -1
3647 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
230d793d
RS
3648 && reversible_comparison_p (XEXP (x, 0)))
3649 return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3650 mode, XEXP (XEXP (x, 0), 0),
3651 XEXP (XEXP (x, 0), 1));
500c518b
RK
3652
3653 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
0802d516
RK
3654 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3655 perform the above simplification. */
500c518b 3656
0802d516
RK
3657 if (STORE_FLAG_VALUE == -1
3658 && XEXP (x, 1) == const1_rtx
500c518b
RK
3659 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3660 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3661 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3662 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
230d793d
RS
3663
3664 /* Apply De Morgan's laws to reduce number of patterns for machines
3665 with negating logical insns (and-not, nand, etc.). If result has
3666 only one NOT, put it first, since that is how the patterns are
3667 coded. */
3668
3669 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3670 {
3671 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3672
3673 if (GET_CODE (in1) == NOT)
3674 in1 = XEXP (in1, 0);
3675 else
3676 in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3677
3678 if (GET_CODE (in2) == NOT)
3679 in2 = XEXP (in2, 0);
3680 else if (GET_CODE (in2) == CONST_INT
5f4f0e22
CH
3681 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3682 in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
230d793d
RS
3683 else
3684 in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3685
3686 if (GET_CODE (in2) == NOT)
3687 {
3688 rtx tem = in2;
3689 in2 = in1; in1 = tem;
3690 }
3691
8079805d
RK
3692 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3693 mode, in1, in2);
230d793d
RS
3694 }
3695 break;
3696
3697 case NEG:
3698 /* (neg (plus X 1)) can become (not X). */
3699 if (GET_CODE (XEXP (x, 0)) == PLUS
3700 && XEXP (XEXP (x, 0), 1) == const1_rtx)
8079805d 3701 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
230d793d
RS
3702
3703 /* Similarly, (neg (not X)) is (plus X 1). */
3704 if (GET_CODE (XEXP (x, 0)) == NOT)
8079805d 3705 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
230d793d 3706
230d793d
RS
3707 /* (neg (minus X Y)) can become (minus Y X). */
3708 if (GET_CODE (XEXP (x, 0)) == MINUS
3ad2180a 3709 && (! FLOAT_MODE_P (mode)
0f41302f 3710 /* x-y != -(y-x) with IEEE floating point. */
7e2a0d8e
RK
3711 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3712 || flag_fast_math))
8079805d
RK
3713 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3714 XEXP (XEXP (x, 0), 0));
230d793d 3715
0f41302f 3716 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
d0ab8cd3 3717 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
951553af 3718 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
8079805d 3719 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
d0ab8cd3 3720
230d793d
RS
3721 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3722 if we can then eliminate the NEG (e.g.,
3723 if the operand is a constant). */
3724
3725 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3726 {
3727 temp = simplify_unary_operation (NEG, mode,
3728 XEXP (XEXP (x, 0), 0), mode);
3729 if (temp)
3730 {
3731 SUBST (XEXP (XEXP (x, 0), 0), temp);
3732 return XEXP (x, 0);
3733 }
3734 }
3735
3736 temp = expand_compound_operation (XEXP (x, 0));
3737
3738 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3739 replaced by (lshiftrt X C). This will convert
3740 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3741
3742 if (GET_CODE (temp) == ASHIFTRT
3743 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3744 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
8079805d
RK
3745 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3746 INTVAL (XEXP (temp, 1)));
230d793d 3747
951553af 3748 /* If X has only a single bit that might be nonzero, say, bit I, convert
230d793d
RS
3749 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3750 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3751 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3752 or a SUBREG of one since we'd be making the expression more
3753 complex if it was just a register. */
3754
3755 if (GET_CODE (temp) != REG
3756 && ! (GET_CODE (temp) == SUBREG
3757 && GET_CODE (SUBREG_REG (temp)) == REG)
951553af 3758 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
230d793d
RS
3759 {
3760 rtx temp1 = simplify_shift_const
5f4f0e22
CH
3761 (NULL_RTX, ASHIFTRT, mode,
3762 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
230d793d
RS
3763 GET_MODE_BITSIZE (mode) - 1 - i),
3764 GET_MODE_BITSIZE (mode) - 1 - i);
3765
3766 /* If all we did was surround TEMP with the two shifts, we
3767 haven't improved anything, so don't use it. Otherwise,
3768 we are better off with TEMP1. */
3769 if (GET_CODE (temp1) != ASHIFTRT
3770 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3771 || XEXP (XEXP (temp1, 0), 0) != temp)
8079805d 3772 return temp1;
230d793d
RS
3773 }
3774 break;
3775
2ca9ae17 3776 case TRUNCATE:
e30fb98f
JL
3777 /* We can't handle truncation to a partial integer mode here
3778 because we don't know the real bitsize of the partial
3779 integer mode. */
3780 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3781 break;
3782
80608e27
JL
3783 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3784 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3785 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
2ca9ae17
JW
3786 SUBST (XEXP (x, 0),
3787 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3788 GET_MODE_MASK (mode), NULL_RTX, 0));
0f13a422
ILT
3789
3790 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3791 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3792 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3793 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3794 return XEXP (XEXP (x, 0), 0);
3795
3796 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3797 (OP:SI foo:SI) if OP is NEG or ABS. */
3798 if ((GET_CODE (XEXP (x, 0)) == ABS
3799 || GET_CODE (XEXP (x, 0)) == NEG)
3800 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3801 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3802 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3803 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3804 XEXP (XEXP (XEXP (x, 0), 0), 0));
3805
3806 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3807 (truncate:SI x). */
3808 if (GET_CODE (XEXP (x, 0)) == SUBREG
3809 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3810 && subreg_lowpart_p (XEXP (x, 0)))
3811 return SUBREG_REG (XEXP (x, 0));
3812
3813 /* If we know that the value is already truncated, we can
6a992214
JL
3814 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION is
3815 nonzero for the corresponding modes. */
3816 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3817 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
3818 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3819 >= GET_MODE_BITSIZE (mode) + 1)
0f13a422
ILT
3820 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3821
3822 /* A truncate of a comparison can be replaced with a subreg if
3823 STORE_FLAG_VALUE permits. This is like the previous test,
3824 but it works even if the comparison is done in a mode larger
3825 than HOST_BITS_PER_WIDE_INT. */
3826 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3827 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3828 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3829 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3830
3831 /* Similarly, a truncate of a register whose value is a
3832 comparison can be replaced with a subreg if STORE_FLAG_VALUE
3833 permits. */
3834 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3835 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3836 && (temp = get_last_value (XEXP (x, 0)))
3837 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3838 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3839
2ca9ae17
JW
3840 break;
3841
230d793d
RS
3842 case FLOAT_TRUNCATE:
3843 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
3844 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3845 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3846 return XEXP (XEXP (x, 0), 0);
4635f748
RK
3847
3848 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3849 (OP:SF foo:SF) if OP is NEG or ABS. */
3850 if ((GET_CODE (XEXP (x, 0)) == ABS
3851 || GET_CODE (XEXP (x, 0)) == NEG)
3852 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3853 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
0c1c8ea6
RK
3854 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3855 XEXP (XEXP (XEXP (x, 0), 0), 0));
1d12df72
RK
3856
3857 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3858 is (float_truncate:SF x). */
3859 if (GET_CODE (XEXP (x, 0)) == SUBREG
3860 && subreg_lowpart_p (XEXP (x, 0))
3861 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3862 return SUBREG_REG (XEXP (x, 0));
230d793d
RS
3863 break;
3864
3865#ifdef HAVE_cc0
3866 case COMPARE:
3867 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3868 using cc0, in which case we want to leave it as a COMPARE
3869 so we can distinguish it from a register-register-copy. */
3870 if (XEXP (x, 1) == const0_rtx)
3871 return XEXP (x, 0);
3872
3873 /* In IEEE floating point, x-0 is not the same as x. */
3874 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
7e2a0d8e
RK
3875 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3876 || flag_fast_math)
230d793d
RS
3877 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3878 return XEXP (x, 0);
3879 break;
3880#endif
3881
3882 case CONST:
3883 /* (const (const X)) can become (const X). Do it this way rather than
3884 returning the inner CONST since CONST can be shared with a
3885 REG_EQUAL note. */
3886 if (GET_CODE (XEXP (x, 0)) == CONST)
3887 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3888 break;
3889
3890#ifdef HAVE_lo_sum
3891 case LO_SUM:
3892 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
3893 can add in an offset. find_split_point will split this address up
3894 again if it doesn't match. */
3895 if (GET_CODE (XEXP (x, 0)) == HIGH
3896 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3897 return XEXP (x, 1);
3898 break;
3899#endif
3900
3901 case PLUS:
3902 /* If we have (plus (plus (A const) B)), associate it so that CONST is
3903 outermost. That's because that's the way indexed addresses are
3904 supposed to appear. This code used to check many more cases, but
3905 they are now checked elsewhere. */
3906 if (GET_CODE (XEXP (x, 0)) == PLUS
3907 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3908 return gen_binary (PLUS, mode,
3909 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3910 XEXP (x, 1)),
3911 XEXP (XEXP (x, 0), 1));
3912
3913 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3914 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3915 bit-field and can be replaced by either a sign_extend or a
e6380233
JL
3916 sign_extract. The `and' may be a zero_extend and the two
3917 <c>, -<c> constants may be reversed. */
230d793d
RS
3918 if (GET_CODE (XEXP (x, 0)) == XOR
3919 && GET_CODE (XEXP (x, 1)) == CONST_INT
3920 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3921 && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
e6380233
JL
3922 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3923 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5f4f0e22 3924 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
230d793d
RS
3925 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3926 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3927 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5f4f0e22 3928 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
230d793d
RS
3929 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3930 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3931 == i + 1))))
8079805d
RK
3932 return simplify_shift_const
3933 (NULL_RTX, ASHIFTRT, mode,
3934 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3935 XEXP (XEXP (XEXP (x, 0), 0), 0),
3936 GET_MODE_BITSIZE (mode) - (i + 1)),
3937 GET_MODE_BITSIZE (mode) - (i + 1));
230d793d 3938
bc0776c6
RK
3939 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3940 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3941 is 1. This produces better code than the alternative immediately
3942 below. */
3943 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3944 && reversible_comparison_p (XEXP (x, 0))
3945 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3946 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
8079805d 3947 return
0c1c8ea6 3948 gen_unary (NEG, mode, mode,
8079805d
RK
3949 gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3950 mode, XEXP (XEXP (x, 0), 0),
3951 XEXP (XEXP (x, 0), 1)));
bc0776c6
RK
3952
3953 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
230d793d
RS
3954 can become (ashiftrt (ashift (xor x 1) C) C) where C is
3955 the bitsize of the mode - 1. This allows simplification of
3956 "a = (b & 8) == 0;" */
3957 if (XEXP (x, 1) == constm1_rtx
3958 && GET_CODE (XEXP (x, 0)) != REG
3959 && ! (GET_CODE (XEXP (x,0)) == SUBREG
3960 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
951553af 3961 && nonzero_bits (XEXP (x, 0), mode) == 1)
8079805d
RK
3962 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3963 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3964 gen_rtx_combine (XOR, mode,
3965 XEXP (x, 0), const1_rtx),
3966 GET_MODE_BITSIZE (mode) - 1),
3967 GET_MODE_BITSIZE (mode) - 1);
02f4ada4
RK
3968
3969 /* If we are adding two things that have no bits in common, convert
3970 the addition into an IOR. This will often be further simplified,
3971 for example in cases like ((a & 1) + (a & 2)), which can
3972 become a & 3. */
3973
ac49a949 3974 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
951553af
RK
3975 && (nonzero_bits (XEXP (x, 0), mode)
3976 & nonzero_bits (XEXP (x, 1), mode)) == 0)
8079805d 3977 return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
230d793d
RS
3978 break;
3979
3980 case MINUS:
0802d516
RK
3981 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3982 by reversing the comparison code if valid. */
3983 if (STORE_FLAG_VALUE == 1
3984 && XEXP (x, 0) == const1_rtx
5109d49f
RK
3985 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
3986 && reversible_comparison_p (XEXP (x, 1)))
3987 return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
3988 mode, XEXP (XEXP (x, 1), 0),
3989 XEXP (XEXP (x, 1), 1));
5109d49f 3990
230d793d
RS
3991 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3992 (and <foo> (const_int pow2-1)) */
3993 if (GET_CODE (XEXP (x, 1)) == AND
3994 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3995 && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3996 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
8079805d
RK
3997 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3998 - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
7bef8680
RK
3999
4000 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4001 integers. */
4002 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
8079805d
RK
4003 return gen_binary (MINUS, mode,
4004 gen_binary (MINUS, mode, XEXP (x, 0),
4005 XEXP (XEXP (x, 1), 0)),
4006 XEXP (XEXP (x, 1), 1));
230d793d
RS
4007 break;
4008
4009 case MULT:
4010 /* If we have (mult (plus A B) C), apply the distributive law and then
4011 the inverse distributive law to see if things simplify. This
4012 occurs mostly in addresses, often when unrolling loops. */
4013
4014 if (GET_CODE (XEXP (x, 0)) == PLUS)
4015 {
4016 x = apply_distributive_law
4017 (gen_binary (PLUS, mode,
4018 gen_binary (MULT, mode,
4019 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4020 gen_binary (MULT, mode,
4021 XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
4022
4023 if (GET_CODE (x) != MULT)
8079805d 4024 return x;
230d793d 4025 }
230d793d
RS
4026 break;
4027
4028 case UDIV:
4029 /* If this is a divide by a power of two, treat it as a shift if
4030 its first operand is a shift. */
4031 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4032 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4033 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4034 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4035 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4036 || GET_CODE (XEXP (x, 0)) == ROTATE
4037 || GET_CODE (XEXP (x, 0)) == ROTATERT))
8079805d 4038 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
230d793d
RS
4039 break;
4040
4041 case EQ: case NE:
4042 case GT: case GTU: case GE: case GEU:
4043 case LT: case LTU: case LE: case LEU:
4044 /* If the first operand is a condition code, we can't do anything
4045 with it. */
4046 if (GET_CODE (XEXP (x, 0)) == COMPARE
4047 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4048#ifdef HAVE_cc0
4049 && XEXP (x, 0) != cc0_rtx
4050#endif
4051 ))
4052 {
4053 rtx op0 = XEXP (x, 0);
4054 rtx op1 = XEXP (x, 1);
4055 enum rtx_code new_code;
4056
4057 if (GET_CODE (op0) == COMPARE)
4058 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4059
4060 /* Simplify our comparison, if possible. */
4061 new_code = simplify_comparison (code, &op0, &op1);
4062
230d793d 4063 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
951553af 4064 if only the low-order bit is possibly nonzero in X (such as when
5109d49f
RK
4065 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4066 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4067 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4068 (plus X 1).
4069
4070 Remove any ZERO_EXTRACT we made when thinking this was a
4071 comparison. It may now be simpler to use, e.g., an AND. If a
4072 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4073 the call to make_compound_operation in the SET case. */
4074
0802d516
RK
4075 if (STORE_FLAG_VALUE == 1
4076 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4077 && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
818b11b9
RK
4078 return gen_lowpart_for_combine (mode,
4079 expand_compound_operation (op0));
5109d49f 4080
0802d516
RK
4081 else if (STORE_FLAG_VALUE == 1
4082 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4083 && op1 == const0_rtx
4084 && (num_sign_bit_copies (op0, mode)
4085 == GET_MODE_BITSIZE (mode)))
4086 {
4087 op0 = expand_compound_operation (op0);
0c1c8ea6 4088 return gen_unary (NEG, mode, mode,
8079805d 4089 gen_lowpart_for_combine (mode, op0));
5109d49f
RK
4090 }
4091
0802d516
RK
4092 else if (STORE_FLAG_VALUE == 1
4093 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
230d793d 4094 && op1 == const0_rtx
5109d49f 4095 && nonzero_bits (op0, mode) == 1)
818b11b9
RK
4096 {
4097 op0 = expand_compound_operation (op0);
8079805d
RK
4098 return gen_binary (XOR, mode,
4099 gen_lowpart_for_combine (mode, op0),
4100 const1_rtx);
5109d49f 4101 }
818b11b9 4102
0802d516
RK
4103 else if (STORE_FLAG_VALUE == 1
4104 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4105 && op1 == const0_rtx
4106 && (num_sign_bit_copies (op0, mode)
4107 == GET_MODE_BITSIZE (mode)))
4108 {
4109 op0 = expand_compound_operation (op0);
8079805d 4110 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
818b11b9 4111 }
230d793d 4112
5109d49f
RK
4113 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4114 those above. */
0802d516
RK
4115 if (STORE_FLAG_VALUE == -1
4116 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
230d793d 4117 && op1 == const0_rtx
5109d49f
RK
4118 && (num_sign_bit_copies (op0, mode)
4119 == GET_MODE_BITSIZE (mode)))
4120 return gen_lowpart_for_combine (mode,
4121 expand_compound_operation (op0));
4122
0802d516
RK
4123 else if (STORE_FLAG_VALUE == -1
4124 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4125 && op1 == const0_rtx
4126 && nonzero_bits (op0, mode) == 1)
4127 {
4128 op0 = expand_compound_operation (op0);
0c1c8ea6 4129 return gen_unary (NEG, mode, mode,
8079805d 4130 gen_lowpart_for_combine (mode, op0));
5109d49f
RK
4131 }
4132
0802d516
RK
4133 else if (STORE_FLAG_VALUE == -1
4134 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4135 && op1 == const0_rtx
4136 && (num_sign_bit_copies (op0, mode)
4137 == GET_MODE_BITSIZE (mode)))
230d793d 4138 {
818b11b9 4139 op0 = expand_compound_operation (op0);
0c1c8ea6 4140 return gen_unary (NOT, mode, mode,
8079805d 4141 gen_lowpart_for_combine (mode, op0));
5109d49f
RK
4142 }
4143
4144 /* If X is 0/1, (eq X 0) is X-1. */
0802d516
RK
4145 else if (STORE_FLAG_VALUE == -1
4146 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4147 && op1 == const0_rtx
4148 && nonzero_bits (op0, mode) == 1)
4149 {
4150 op0 = expand_compound_operation (op0);
8079805d 4151 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
230d793d 4152 }
230d793d
RS
4153
4154 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
951553af
RK
4155 one bit that might be nonzero, we can convert (ne x 0) to
4156 (ashift x c) where C puts the bit in the sign bit. Remove any
4157 AND with STORE_FLAG_VALUE when we are done, since we are only
4158 going to test the sign bit. */
3f508eca 4159 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5f4f0e22 4160 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
0802d516 4161 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
e51712db 4162 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE(mode)-1))
230d793d
RS
4163 && op1 == const0_rtx
4164 && mode == GET_MODE (op0)
5109d49f 4165 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
230d793d 4166 {
818b11b9
RK
4167 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4168 expand_compound_operation (op0),
230d793d
RS
4169 GET_MODE_BITSIZE (mode) - 1 - i);
4170 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4171 return XEXP (x, 0);
4172 else
4173 return x;
4174 }
4175
4176 /* If the code changed, return a whole new comparison. */
4177 if (new_code != code)
4178 return gen_rtx_combine (new_code, mode, op0, op1);
4179
4180 /* Otherwise, keep this operation, but maybe change its operands.
4181 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4182 SUBST (XEXP (x, 0), op0);
4183 SUBST (XEXP (x, 1), op1);
4184 }
4185 break;
4186
4187 case IF_THEN_ELSE:
8079805d 4188 return simplify_if_then_else (x);
9210df58 4189
8079805d
RK
4190 case ZERO_EXTRACT:
4191 case SIGN_EXTRACT:
4192 case ZERO_EXTEND:
4193 case SIGN_EXTEND:
0f41302f 4194 /* If we are processing SET_DEST, we are done. */
8079805d
RK
4195 if (in_dest)
4196 return x;
d0ab8cd3 4197
8079805d 4198 return expand_compound_operation (x);
d0ab8cd3 4199
8079805d
RK
4200 case SET:
4201 return simplify_set (x);
1a26b032 4202
8079805d
RK
4203 case AND:
4204 case IOR:
4205 case XOR:
4206 return simplify_logical (x, last);
d0ab8cd3 4207
b472527b 4208 case ABS:
8079805d
RK
4209 /* (abs (neg <foo>)) -> (abs <foo>) */
4210 if (GET_CODE (XEXP (x, 0)) == NEG)
4211 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
1a26b032 4212
b472527b
JL
4213 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4214 do nothing. */
4215 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4216 break;
f40421ce 4217
8079805d
RK
4218 /* If operand is something known to be positive, ignore the ABS. */
4219 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4220 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4221 <= HOST_BITS_PER_WIDE_INT)
4222 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4223 & ((HOST_WIDE_INT) 1
4224 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4225 == 0)))
4226 return XEXP (x, 0);
1a26b032 4227
1a26b032 4228
8079805d
RK
4229 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4230 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4231 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
1a26b032 4232
8079805d 4233 break;
1a26b032 4234
8079805d
RK
4235 case FFS:
4236 /* (ffs (*_extend <X>)) = (ffs <X>) */
4237 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4238 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4239 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4240 break;
1a26b032 4241
8079805d
RK
4242 case FLOAT:
4243 /* (float (sign_extend <X>)) = (float <X>). */
4244 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4245 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4246 break;
1a26b032 4247
8079805d
RK
4248 case ASHIFT:
4249 case LSHIFTRT:
4250 case ASHIFTRT:
4251 case ROTATE:
4252 case ROTATERT:
4253 /* If this is a shift by a constant amount, simplify it. */
4254 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4255 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4256 INTVAL (XEXP (x, 1)));
4257
4258#ifdef SHIFT_COUNT_TRUNCATED
4259 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4260 SUBST (XEXP (x, 1),
4261 force_to_mode (XEXP (x, 1), GET_MODE (x),
4262 ((HOST_WIDE_INT) 1
4263 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4264 - 1,
4265 NULL_RTX, 0));
4266#endif
4267
4268 break;
e9a25f70
JL
4269
4270 default:
4271 break;
8079805d
RK
4272 }
4273
4274 return x;
4275}
4276\f
4277/* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5109d49f 4278
8079805d
RK
4279static rtx
4280simplify_if_then_else (x)
4281 rtx x;
4282{
4283 enum machine_mode mode = GET_MODE (x);
4284 rtx cond = XEXP (x, 0);
4285 rtx true = XEXP (x, 1);
4286 rtx false = XEXP (x, 2);
4287 enum rtx_code true_code = GET_CODE (cond);
4288 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4289 rtx temp;
4290 int i;
4291
0f41302f 4292 /* Simplify storing of the truth value. */
8079805d
RK
4293 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4294 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4295
0f41302f 4296 /* Also when the truth value has to be reversed. */
8079805d
RK
4297 if (comparison_p && reversible_comparison_p (cond)
4298 && true == const0_rtx && false == const_true_rtx)
4299 return gen_binary (reverse_condition (true_code),
4300 mode, XEXP (cond, 0), XEXP (cond, 1));
4301
4302 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4303 in it is being compared against certain values. Get the true and false
4304 comparisons and see if that says anything about the value of each arm. */
4305
4306 if (comparison_p && reversible_comparison_p (cond)
4307 && GET_CODE (XEXP (cond, 0)) == REG)
4308 {
4309 HOST_WIDE_INT nzb;
4310 rtx from = XEXP (cond, 0);
4311 enum rtx_code false_code = reverse_condition (true_code);
4312 rtx true_val = XEXP (cond, 1);
4313 rtx false_val = true_val;
4314 int swapped = 0;
9210df58 4315
8079805d 4316 /* If FALSE_CODE is EQ, swap the codes and arms. */
5109d49f 4317
8079805d 4318 if (false_code == EQ)
1a26b032 4319 {
8079805d
RK
4320 swapped = 1, true_code = EQ, false_code = NE;
4321 temp = true, true = false, false = temp;
4322 }
5109d49f 4323
8079805d
RK
4324 /* If we are comparing against zero and the expression being tested has
4325 only a single bit that might be nonzero, that is its value when it is
4326 not equal to zero. Similarly if it is known to be -1 or 0. */
4327
4328 if (true_code == EQ && true_val == const0_rtx
4329 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4330 false_code = EQ, false_val = GEN_INT (nzb);
4331 else if (true_code == EQ && true_val == const0_rtx
4332 && (num_sign_bit_copies (from, GET_MODE (from))
4333 == GET_MODE_BITSIZE (GET_MODE (from))))
4334 false_code = EQ, false_val = constm1_rtx;
4335
4336 /* Now simplify an arm if we know the value of the register in the
4337 branch and it is used in the arm. Be careful due to the potential
4338 of locally-shared RTL. */
4339
4340 if (reg_mentioned_p (from, true))
4341 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4342 pc_rtx, pc_rtx, 0, 0);
4343 if (reg_mentioned_p (from, false))
4344 false = subst (known_cond (copy_rtx (false), false_code,
4345 from, false_val),
4346 pc_rtx, pc_rtx, 0, 0);
4347
4348 SUBST (XEXP (x, 1), swapped ? false : true);
4349 SUBST (XEXP (x, 2), swapped ? true : false);
4350
4351 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4352 }
5109d49f 4353
8079805d
RK
4354 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4355 reversed, do so to avoid needing two sets of patterns for
4356 subtract-and-branch insns. Similarly if we have a constant in the true
4357 arm, the false arm is the same as the first operand of the comparison, or
4358 the false arm is more complicated than the true arm. */
4359
4360 if (comparison_p && reversible_comparison_p (cond)
4361 && (true == pc_rtx
4362 || (CONSTANT_P (true)
4363 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4364 || true == const0_rtx
4365 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4366 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4367 || (GET_CODE (true) == SUBREG
4368 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4369 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4370 || reg_mentioned_p (true, false)
4371 || rtx_equal_p (false, XEXP (cond, 0))))
4372 {
4373 true_code = reverse_condition (true_code);
4374 SUBST (XEXP (x, 0),
4375 gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4376 XEXP (cond, 1)));
5109d49f 4377
8079805d
RK
4378 SUBST (XEXP (x, 1), false);
4379 SUBST (XEXP (x, 2), true);
1a26b032 4380
8079805d 4381 temp = true, true = false, false = temp, cond = XEXP (x, 0);
bb821298 4382
0f41302f 4383 /* It is possible that the conditional has been simplified out. */
bb821298
RK
4384 true_code = GET_CODE (cond);
4385 comparison_p = GET_RTX_CLASS (true_code) == '<';
8079805d 4386 }
abe6e52f 4387
8079805d 4388 /* If the two arms are identical, we don't need the comparison. */
1a26b032 4389
8079805d
RK
4390 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4391 return true;
1a26b032 4392
5be669c7
RK
4393 /* Convert a == b ? b : a to "a". */
4394 if (true_code == EQ && ! side_effects_p (cond)
4395 && rtx_equal_p (XEXP (cond, 0), false)
4396 && rtx_equal_p (XEXP (cond, 1), true))
4397 return false;
4398 else if (true_code == NE && ! side_effects_p (cond)
4399 && rtx_equal_p (XEXP (cond, 0), true)
4400 && rtx_equal_p (XEXP (cond, 1), false))
4401 return true;
4402
8079805d
RK
4403 /* Look for cases where we have (abs x) or (neg (abs X)). */
4404
4405 if (GET_MODE_CLASS (mode) == MODE_INT
4406 && GET_CODE (false) == NEG
4407 && rtx_equal_p (true, XEXP (false, 0))
4408 && comparison_p
4409 && rtx_equal_p (true, XEXP (cond, 0))
4410 && ! side_effects_p (true))
4411 switch (true_code)
4412 {
4413 case GT:
4414 case GE:
0c1c8ea6 4415 return gen_unary (ABS, mode, mode, true);
8079805d
RK
4416 case LT:
4417 case LE:
0c1c8ea6 4418 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
e9a25f70
JL
4419 default:
4420 break;
8079805d
RK
4421 }
4422
4423 /* Look for MIN or MAX. */
4424
34c8be72 4425 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
8079805d
RK
4426 && comparison_p
4427 && rtx_equal_p (XEXP (cond, 0), true)
4428 && rtx_equal_p (XEXP (cond, 1), false)
4429 && ! side_effects_p (cond))
4430 switch (true_code)
4431 {
4432 case GE:
4433 case GT:
4434 return gen_binary (SMAX, mode, true, false);
4435 case LE:
4436 case LT:
4437 return gen_binary (SMIN, mode, true, false);
4438 case GEU:
4439 case GTU:
4440 return gen_binary (UMAX, mode, true, false);
4441 case LEU:
4442 case LTU:
4443 return gen_binary (UMIN, mode, true, false);
e9a25f70
JL
4444 default:
4445 break;
8079805d
RK
4446 }
4447
8079805d
RK
4448 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4449 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4450 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4451 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4452 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
0802d516 4453 neither 1 or -1, but it isn't worth checking for. */
8079805d 4454
0802d516
RK
4455 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4456 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
8079805d
RK
4457 {
4458 rtx t = make_compound_operation (true, SET);
4459 rtx f = make_compound_operation (false, SET);
4460 rtx cond_op0 = XEXP (cond, 0);
4461 rtx cond_op1 = XEXP (cond, 1);
6a651371 4462 enum rtx_code op = NIL, extend_op = NIL;
8079805d 4463 enum machine_mode m = mode;
6a651371 4464 rtx z = 0, c1 = NULL_RTX;
8079805d 4465
8079805d
RK
4466 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4467 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4468 || GET_CODE (t) == ASHIFT
4469 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4470 && rtx_equal_p (XEXP (t, 0), f))
4471 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4472
4473 /* If an identity-zero op is commutative, check whether there
0f41302f 4474 would be a match if we swapped the operands. */
8079805d
RK
4475 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4476 || GET_CODE (t) == XOR)
4477 && rtx_equal_p (XEXP (t, 1), f))
4478 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4479 else if (GET_CODE (t) == SIGN_EXTEND
4480 && (GET_CODE (XEXP (t, 0)) == PLUS
4481 || GET_CODE (XEXP (t, 0)) == MINUS
4482 || GET_CODE (XEXP (t, 0)) == IOR
4483 || GET_CODE (XEXP (t, 0)) == XOR
4484 || GET_CODE (XEXP (t, 0)) == ASHIFT
4485 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4486 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4487 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4488 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4489 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4490 && (num_sign_bit_copies (f, GET_MODE (f))
4491 > (GET_MODE_BITSIZE (mode)
4492 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4493 {
4494 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4495 extend_op = SIGN_EXTEND;
4496 m = GET_MODE (XEXP (t, 0));
1a26b032 4497 }
8079805d
RK
4498 else if (GET_CODE (t) == SIGN_EXTEND
4499 && (GET_CODE (XEXP (t, 0)) == PLUS
4500 || GET_CODE (XEXP (t, 0)) == IOR
4501 || GET_CODE (XEXP (t, 0)) == XOR)
4502 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4503 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4504 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4505 && (num_sign_bit_copies (f, GET_MODE (f))
4506 > (GET_MODE_BITSIZE (mode)
4507 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4508 {
4509 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4510 extend_op = SIGN_EXTEND;
4511 m = GET_MODE (XEXP (t, 0));
4512 }
4513 else if (GET_CODE (t) == ZERO_EXTEND
4514 && (GET_CODE (XEXP (t, 0)) == PLUS
4515 || GET_CODE (XEXP (t, 0)) == MINUS
4516 || GET_CODE (XEXP (t, 0)) == IOR
4517 || GET_CODE (XEXP (t, 0)) == XOR
4518 || GET_CODE (XEXP (t, 0)) == ASHIFT
4519 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4520 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4521 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4522 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4523 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4524 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4525 && ((nonzero_bits (f, GET_MODE (f))
4526 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4527 == 0))
4528 {
4529 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4530 extend_op = ZERO_EXTEND;
4531 m = GET_MODE (XEXP (t, 0));
4532 }
4533 else if (GET_CODE (t) == ZERO_EXTEND
4534 && (GET_CODE (XEXP (t, 0)) == PLUS
4535 || GET_CODE (XEXP (t, 0)) == IOR
4536 || GET_CODE (XEXP (t, 0)) == XOR)
4537 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4538 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4539 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4540 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4541 && ((nonzero_bits (f, GET_MODE (f))
4542 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4543 == 0))
4544 {
4545 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4546 extend_op = ZERO_EXTEND;
4547 m = GET_MODE (XEXP (t, 0));
4548 }
4549
4550 if (z)
4551 {
4552 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4553 pc_rtx, pc_rtx, 0, 0);
4554 temp = gen_binary (MULT, m, temp,
4555 gen_binary (MULT, m, c1, const_true_rtx));
4556 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4557 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4558
4559 if (extend_op != NIL)
0c1c8ea6 4560 temp = gen_unary (extend_op, mode, m, temp);
8079805d
RK
4561
4562 return temp;
4563 }
4564 }
224eeff2 4565
8079805d
RK
4566 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4567 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4568 negation of a single bit, we can convert this operation to a shift. We
4569 can actually do this more generally, but it doesn't seem worth it. */
4570
4571 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4572 && false == const0_rtx && GET_CODE (true) == CONST_INT
4573 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4574 && (i = exact_log2 (INTVAL (true))) >= 0)
4575 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4576 == GET_MODE_BITSIZE (mode))
4577 && (i = exact_log2 (- INTVAL (true))) >= 0)))
4578 return
4579 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4580 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
230d793d 4581
8079805d
RK
4582 return x;
4583}
4584\f
4585/* Simplify X, a SET expression. Return the new expression. */
230d793d 4586
8079805d
RK
4587static rtx
4588simplify_set (x)
4589 rtx x;
4590{
4591 rtx src = SET_SRC (x);
4592 rtx dest = SET_DEST (x);
4593 enum machine_mode mode
4594 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4595 rtx other_insn;
4596 rtx *cc_use;
4597
4598 /* (set (pc) (return)) gets written as (return). */
4599 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4600 return src;
230d793d 4601
87e3e0c1
RK
4602 /* Now that we know for sure which bits of SRC we are using, see if we can
4603 simplify the expression for the object knowing that we only need the
4604 low-order bits. */
4605
4606 if (GET_MODE_CLASS (mode) == MODE_INT)
c5c76735
JL
4607 {
4608 src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4609 SUBST (SET_SRC (x), src);
4610 }
87e3e0c1 4611
8079805d
RK
4612 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4613 the comparison result and try to simplify it unless we already have used
4614 undobuf.other_insn. */
4615 if ((GET_CODE (src) == COMPARE
230d793d 4616#ifdef HAVE_cc0
8079805d 4617 || dest == cc0_rtx
230d793d 4618#endif
8079805d
RK
4619 )
4620 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4621 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4622 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
c0d3ac4d 4623 && rtx_equal_p (XEXP (*cc_use, 0), dest))
8079805d
RK
4624 {
4625 enum rtx_code old_code = GET_CODE (*cc_use);
4626 enum rtx_code new_code;
4627 rtx op0, op1;
4628 int other_changed = 0;
4629 enum machine_mode compare_mode = GET_MODE (dest);
4630
4631 if (GET_CODE (src) == COMPARE)
4632 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4633 else
4634 op0 = src, op1 = const0_rtx;
230d793d 4635
8079805d
RK
4636 /* Simplify our comparison, if possible. */
4637 new_code = simplify_comparison (old_code, &op0, &op1);
230d793d 4638
c141a106 4639#ifdef EXTRA_CC_MODES
8079805d
RK
4640 /* If this machine has CC modes other than CCmode, check to see if we
4641 need to use a different CC mode here. */
4642 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
c141a106 4643#endif /* EXTRA_CC_MODES */
230d793d 4644
c141a106 4645#if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
8079805d
RK
4646 /* If the mode changed, we have to change SET_DEST, the mode in the
4647 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4648 a hard register, just build new versions with the proper mode. If it
4649 is a pseudo, we lose unless it is only time we set the pseudo, in
4650 which case we can safely change its mode. */
4651 if (compare_mode != GET_MODE (dest))
4652 {
4653 int regno = REGNO (dest);
38a448ca 4654 rtx new_dest = gen_rtx_REG (compare_mode, regno);
8079805d
RK
4655
4656 if (regno < FIRST_PSEUDO_REGISTER
b1f21e0a 4657 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
230d793d 4658 {
8079805d
RK
4659 if (regno >= FIRST_PSEUDO_REGISTER)
4660 SUBST (regno_reg_rtx[regno], new_dest);
230d793d 4661
8079805d
RK
4662 SUBST (SET_DEST (x), new_dest);
4663 SUBST (XEXP (*cc_use, 0), new_dest);
4664 other_changed = 1;
230d793d 4665
8079805d 4666 dest = new_dest;
230d793d 4667 }
8079805d 4668 }
230d793d
RS
4669#endif
4670
8079805d
RK
4671 /* If the code changed, we have to build a new comparison in
4672 undobuf.other_insn. */
4673 if (new_code != old_code)
4674 {
4675 unsigned HOST_WIDE_INT mask;
4676
4677 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4678 dest, const0_rtx));
4679
4680 /* If the only change we made was to change an EQ into an NE or
4681 vice versa, OP0 has only one bit that might be nonzero, and OP1
4682 is zero, check if changing the user of the condition code will
4683 produce a valid insn. If it won't, we can keep the original code
4684 in that insn by surrounding our operation with an XOR. */
4685
4686 if (((old_code == NE && new_code == EQ)
4687 || (old_code == EQ && new_code == NE))
4688 && ! other_changed && op1 == const0_rtx
4689 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4690 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
230d793d 4691 {
8079805d 4692 rtx pat = PATTERN (other_insn), note = 0;
230d793d 4693
8e2f6e35 4694 if ((recog_for_combine (&pat, other_insn, &note) < 0
8079805d
RK
4695 && ! check_asm_operands (pat)))
4696 {
4697 PUT_CODE (*cc_use, old_code);
4698 other_insn = 0;
230d793d 4699
8079805d 4700 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
230d793d 4701 }
230d793d
RS
4702 }
4703
8079805d
RK
4704 other_changed = 1;
4705 }
4706
4707 if (other_changed)
4708 undobuf.other_insn = other_insn;
230d793d
RS
4709
4710#ifdef HAVE_cc0
8079805d
RK
4711 /* If we are now comparing against zero, change our source if
4712 needed. If we do not use cc0, we always have a COMPARE. */
4713 if (op1 == const0_rtx && dest == cc0_rtx)
4714 {
4715 SUBST (SET_SRC (x), op0);
4716 src = op0;
4717 }
4718 else
230d793d
RS
4719#endif
4720
8079805d
RK
4721 /* Otherwise, if we didn't previously have a COMPARE in the
4722 correct mode, we need one. */
4723 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4724 {
4725 SUBST (SET_SRC (x),
4726 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4727 src = SET_SRC (x);
230d793d
RS
4728 }
4729 else
4730 {
8079805d
RK
4731 /* Otherwise, update the COMPARE if needed. */
4732 SUBST (XEXP (src, 0), op0);
4733 SUBST (XEXP (src, 1), op1);
230d793d 4734 }
8079805d
RK
4735 }
4736 else
4737 {
4738 /* Get SET_SRC in a form where we have placed back any
4739 compound expressions. Then do the checks below. */
4740 src = make_compound_operation (src, SET);
4741 SUBST (SET_SRC (x), src);
4742 }
230d793d 4743
8079805d
RK
4744 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4745 and X being a REG or (subreg (reg)), we may be able to convert this to
4746 (set (subreg:m2 x) (op)).
df62f951 4747
8079805d
RK
4748 We can always do this if M1 is narrower than M2 because that means that
4749 we only care about the low bits of the result.
df62f951 4750
8079805d 4751 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
9ec36da5 4752 perform a narrower operation than requested since the high-order bits will
8079805d
RK
4753 be undefined. On machine where it is defined, this transformation is safe
4754 as long as M1 and M2 have the same number of words. */
df62f951 4755
8079805d
RK
4756 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4757 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4758 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4759 / UNITS_PER_WORD)
4760 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4761 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
8baf60bb 4762#ifndef WORD_REGISTER_OPERATIONS
8079805d
RK
4763 && (GET_MODE_SIZE (GET_MODE (src))
4764 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
df62f951 4765#endif
f507a070
RK
4766#ifdef CLASS_CANNOT_CHANGE_SIZE
4767 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4768 && (TEST_HARD_REG_BIT
4769 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4770 REGNO (dest)))
4771 && (GET_MODE_SIZE (GET_MODE (src))
4772 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4773#endif
8079805d
RK
4774 && (GET_CODE (dest) == REG
4775 || (GET_CODE (dest) == SUBREG
4776 && GET_CODE (SUBREG_REG (dest)) == REG)))
4777 {
4778 SUBST (SET_DEST (x),
4779 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4780 dest));
4781 SUBST (SET_SRC (x), SUBREG_REG (src));
4782
4783 src = SET_SRC (x), dest = SET_DEST (x);
4784 }
df62f951 4785
8baf60bb 4786#ifdef LOAD_EXTEND_OP
8079805d
RK
4787 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4788 would require a paradoxical subreg. Replace the subreg with a
0f41302f 4789 zero_extend to avoid the reload that would otherwise be required. */
8079805d
RK
4790
4791 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4792 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4793 && SUBREG_WORD (src) == 0
4794 && (GET_MODE_SIZE (GET_MODE (src))
4795 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4796 && GET_CODE (SUBREG_REG (src)) == MEM)
4797 {
4798 SUBST (SET_SRC (x),
4799 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4800 GET_MODE (src), XEXP (src, 0)));
4801
4802 src = SET_SRC (x);
4803 }
230d793d
RS
4804#endif
4805
8079805d
RK
4806 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4807 are comparing an item known to be 0 or -1 against 0, use a logical
4808 operation instead. Check for one of the arms being an IOR of the other
4809 arm with some value. We compute three terms to be IOR'ed together. In
4810 practice, at most two will be nonzero. Then we do the IOR's. */
4811
4812 if (GET_CODE (dest) != PC
4813 && GET_CODE (src) == IF_THEN_ELSE
36b8d792 4814 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
8079805d
RK
4815 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4816 && XEXP (XEXP (src, 0), 1) == const0_rtx
6dd49058 4817 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
ea414472
DE
4818#ifdef HAVE_conditional_move
4819 && ! can_conditionally_move_p (GET_MODE (src))
4820#endif
8079805d
RK
4821 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4822 GET_MODE (XEXP (XEXP (src, 0), 0)))
4823 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4824 && ! side_effects_p (src))
4825 {
4826 rtx true = (GET_CODE (XEXP (src, 0)) == NE
4827 ? XEXP (src, 1) : XEXP (src, 2));
4828 rtx false = (GET_CODE (XEXP (src, 0)) == NE
4829 ? XEXP (src, 2) : XEXP (src, 1));
4830 rtx term1 = const0_rtx, term2, term3;
4831
4832 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4833 term1 = false, true = XEXP (true, 1), false = const0_rtx;
4834 else if (GET_CODE (true) == IOR
4835 && rtx_equal_p (XEXP (true, 1), false))
4836 term1 = false, true = XEXP (true, 0), false = const0_rtx;
4837 else if (GET_CODE (false) == IOR
4838 && rtx_equal_p (XEXP (false, 0), true))
4839 term1 = true, false = XEXP (false, 1), true = const0_rtx;
4840 else if (GET_CODE (false) == IOR
4841 && rtx_equal_p (XEXP (false, 1), true))
4842 term1 = true, false = XEXP (false, 0), true = const0_rtx;
4843
4844 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4845 term3 = gen_binary (AND, GET_MODE (src),
0c1c8ea6 4846 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
8079805d
RK
4847 XEXP (XEXP (src, 0), 0)),
4848 false);
4849
4850 SUBST (SET_SRC (x),
4851 gen_binary (IOR, GET_MODE (src),
4852 gen_binary (IOR, GET_MODE (src), term1, term2),
4853 term3));
4854
4855 src = SET_SRC (x);
4856 }
230d793d 4857
c5c76735
JL
4858#ifdef HAVE_conditional_arithmetic
4859 /* If we have conditional arithmetic and the operand of a SET is
4860 a conditional expression, replace this with an IF_THEN_ELSE.
4861 We can either have a conditional expression or a MULT of that expression
4862 with a constant. */
4863 if ((GET_RTX_CLASS (GET_CODE (src)) == '1'
4864 || GET_RTX_CLASS (GET_CODE (src)) == '2'
4865 || GET_RTX_CLASS (GET_CODE (src)) == 'c')
4866 && (GET_RTX_CLASS (GET_CODE (XEXP (src, 0))) == '<'
4867 || (GET_CODE (XEXP (src, 0)) == MULT
4868 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (src, 0), 0))) == '<'
4869 && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT)))
4870 {
4871 rtx cond = XEXP (src, 0);
4872 rtx true_val = const1_rtx;
4873 rtx false_arm, true_arm;
4874
4875 if (GET_CODE (cond) == MULT)
4876 {
4877 true_val = XEXP (cond, 1);
4878 cond = XEXP (cond, 0);
4879 }
4880
4881 if (GET_RTX_CLASS (GET_CODE (src)) == '1')
4882 {
4883 true_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4884 GET_MODE (XEXP (src, 0)), true_val);
4885 false_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4886 GET_MODE (XEXP (src, 0)), const0_rtx);
4887 }
4888 else
4889 {
4890 true_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4891 true_val, XEXP (src, 1));
4892 false_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4893 const0_rtx, XEXP (src, 1));
4894 }
4895
4896 /* Canonicalize if true_arm is the simpler one. */
4897 if (GET_RTX_CLASS (GET_CODE (true_arm)) == 'o'
4898 && GET_RTX_CLASS (GET_CODE (false_arm)) != 'o'
4899 && reversible_comparison_p (cond))
4900 {
4901 rtx temp = true_arm;
4902
4903 true_arm = false_arm;
4904 false_arm = temp;
4905
4906 cond = gen_rtx_combine (reverse_condition (GET_CODE (cond)),
4907 GET_MODE (cond), XEXP (cond, 0),
4908 XEXP (cond, 1));
4909 }
4910
4911 src = gen_rtx_combine (IF_THEN_ELSE, GET_MODE (src),
4912 gen_rtx_combine (GET_CODE (cond), VOIDmode,
4913 XEXP (cond, 0),
4914 XEXP (cond, 1)),
4915 true_arm, false_arm);
4916 SUBST (SET_SRC (x), src);
4917 }
4918#endif
4919
246e00f2
RK
4920 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4921 whole thing fail. */
4922 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4923 return src;
4924 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4925 return dest;
4926 else
4927 /* Convert this into a field assignment operation, if possible. */
4928 return make_field_assignment (x);
8079805d
RK
4929}
4930\f
4931/* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4932 result. LAST is nonzero if this is the last retry. */
4933
4934static rtx
4935simplify_logical (x, last)
4936 rtx x;
4937 int last;
4938{
4939 enum machine_mode mode = GET_MODE (x);
4940 rtx op0 = XEXP (x, 0);
4941 rtx op1 = XEXP (x, 1);
4942
4943 switch (GET_CODE (x))
4944 {
230d793d 4945 case AND:
8079805d
RK
4946 /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4947 insn (and may simplify more). */
4948 if (GET_CODE (op0) == XOR
4949 && rtx_equal_p (XEXP (op0, 0), op1)
4950 && ! side_effects_p (op1))
0c1c8ea6
RK
4951 x = gen_binary (AND, mode,
4952 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
8079805d
RK
4953
4954 if (GET_CODE (op0) == XOR
4955 && rtx_equal_p (XEXP (op0, 1), op1)
4956 && ! side_effects_p (op1))
0c1c8ea6
RK
4957 x = gen_binary (AND, mode,
4958 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
8079805d
RK
4959
4960 /* Similarly for (~ (A ^ B)) & A. */
4961 if (GET_CODE (op0) == NOT
4962 && GET_CODE (XEXP (op0, 0)) == XOR
4963 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4964 && ! side_effects_p (op1))
4965 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4966
4967 if (GET_CODE (op0) == NOT
4968 && GET_CODE (XEXP (op0, 0)) == XOR
4969 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4970 && ! side_effects_p (op1))
4971 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4972
2e8f9abf
DM
4973 /* We can call simplify_and_const_int only if we don't lose
4974 any (sign) bits when converting INTVAL (op1) to
4975 "unsigned HOST_WIDE_INT". */
4976 if (GET_CODE (op1) == CONST_INT
4977 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4978 || INTVAL (op1) > 0))
230d793d 4979 {
8079805d 4980 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
230d793d
RS
4981
4982 /* If we have (ior (and (X C1) C2)) and the next restart would be
4983 the last, simplify this by making C1 as small as possible
0f41302f 4984 and then exit. */
8079805d
RK
4985 if (last
4986 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
4987 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4988 && GET_CODE (op1) == CONST_INT)
4989 return gen_binary (IOR, mode,
4990 gen_binary (AND, mode, XEXP (op0, 0),
4991 GEN_INT (INTVAL (XEXP (op0, 1))
4992 & ~ INTVAL (op1))), op1);
230d793d
RS
4993
4994 if (GET_CODE (x) != AND)
8079805d 4995 return x;
0e32506c
RK
4996
4997 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
4998 || GET_RTX_CLASS (GET_CODE (x)) == '2')
4999 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
230d793d
RS
5000 }
5001
5002 /* Convert (A | B) & A to A. */
8079805d
RK
5003 if (GET_CODE (op0) == IOR
5004 && (rtx_equal_p (XEXP (op0, 0), op1)
5005 || rtx_equal_p (XEXP (op0, 1), op1))
5006 && ! side_effects_p (XEXP (op0, 0))
5007 && ! side_effects_p (XEXP (op0, 1)))
5008 return op1;
230d793d 5009
d0ab8cd3 5010 /* In the following group of tests (and those in case IOR below),
230d793d
RS
5011 we start with some combination of logical operations and apply
5012 the distributive law followed by the inverse distributive law.
5013 Most of the time, this results in no change. However, if some of
5014 the operands are the same or inverses of each other, simplifications
5015 will result.
5016
5017 For example, (and (ior A B) (not B)) can occur as the result of
5018 expanding a bit field assignment. When we apply the distributive
5019 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8079805d 5020 which then simplifies to (and (A (not B))).
230d793d 5021
8079805d 5022 If we have (and (ior A B) C), apply the distributive law and then
230d793d
RS
5023 the inverse distributive law to see if things simplify. */
5024
8079805d 5025 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
230d793d
RS
5026 {
5027 x = apply_distributive_law
8079805d
RK
5028 (gen_binary (GET_CODE (op0), mode,
5029 gen_binary (AND, mode, XEXP (op0, 0), op1),
5030 gen_binary (AND, mode, XEXP (op0, 1), op1)));
230d793d 5031 if (GET_CODE (x) != AND)
8079805d 5032 return x;
230d793d
RS
5033 }
5034
8079805d
RK
5035 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5036 return apply_distributive_law
5037 (gen_binary (GET_CODE (op1), mode,
5038 gen_binary (AND, mode, XEXP (op1, 0), op0),
5039 gen_binary (AND, mode, XEXP (op1, 1), op0)));
230d793d
RS
5040
5041 /* Similarly, taking advantage of the fact that
5042 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5043
8079805d
RK
5044 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5045 return apply_distributive_law
5046 (gen_binary (XOR, mode,
5047 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5048 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
230d793d 5049
8079805d
RK
5050 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5051 return apply_distributive_law
5052 (gen_binary (XOR, mode,
5053 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5054 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
230d793d
RS
5055 break;
5056
5057 case IOR:
951553af 5058 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
8079805d 5059 if (GET_CODE (op1) == CONST_INT
ac49a949 5060 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8079805d
RK
5061 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
5062 return op1;
d0ab8cd3 5063
230d793d 5064 /* Convert (A & B) | A to A. */
8079805d
RK
5065 if (GET_CODE (op0) == AND
5066 && (rtx_equal_p (XEXP (op0, 0), op1)
5067 || rtx_equal_p (XEXP (op0, 1), op1))
5068 && ! side_effects_p (XEXP (op0, 0))
5069 && ! side_effects_p (XEXP (op0, 1)))
5070 return op1;
230d793d
RS
5071
5072 /* If we have (ior (and A B) C), apply the distributive law and then
5073 the inverse distributive law to see if things simplify. */
5074
8079805d 5075 if (GET_CODE (op0) == AND)
230d793d
RS
5076 {
5077 x = apply_distributive_law
5078 (gen_binary (AND, mode,
8079805d
RK
5079 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5080 gen_binary (IOR, mode, XEXP (op0, 1), op1)));
230d793d
RS
5081
5082 if (GET_CODE (x) != IOR)
8079805d 5083 return x;
230d793d
RS
5084 }
5085
8079805d 5086 if (GET_CODE (op1) == AND)
230d793d
RS
5087 {
5088 x = apply_distributive_law
5089 (gen_binary (AND, mode,
8079805d
RK
5090 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5091 gen_binary (IOR, mode, XEXP (op1, 1), op0)));
230d793d
RS
5092
5093 if (GET_CODE (x) != IOR)
8079805d 5094 return x;
230d793d
RS
5095 }
5096
5097 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5098 mode size to (rotate A CX). */
5099
8079805d
RK
5100 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5101 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5102 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5103 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5104 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5105 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
230d793d 5106 == GET_MODE_BITSIZE (mode)))
38a448ca
RH
5107 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5108 (GET_CODE (op0) == ASHIFT
5109 ? XEXP (op0, 1) : XEXP (op1, 1)));
230d793d 5110
71923da7
RK
5111 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5112 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5113 does not affect any of the bits in OP1, it can really be done
5114 as a PLUS and we can associate. We do this by seeing if OP1
5115 can be safely shifted left C bits. */
5116 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5117 && GET_CODE (XEXP (op0, 0)) == PLUS
5118 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5119 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5120 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5121 {
5122 int count = INTVAL (XEXP (op0, 1));
5123 HOST_WIDE_INT mask = INTVAL (op1) << count;
5124
5125 if (mask >> count == INTVAL (op1)
5126 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5127 {
5128 SUBST (XEXP (XEXP (op0, 0), 1),
5129 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5130 return op0;
5131 }
5132 }
230d793d
RS
5133 break;
5134
5135 case XOR:
5136 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5137 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5138 (NOT y). */
5139 {
5140 int num_negated = 0;
230d793d 5141
8079805d
RK
5142 if (GET_CODE (op0) == NOT)
5143 num_negated++, op0 = XEXP (op0, 0);
5144 if (GET_CODE (op1) == NOT)
5145 num_negated++, op1 = XEXP (op1, 0);
230d793d
RS
5146
5147 if (num_negated == 2)
5148 {
8079805d
RK
5149 SUBST (XEXP (x, 0), op0);
5150 SUBST (XEXP (x, 1), op1);
230d793d
RS
5151 }
5152 else if (num_negated == 1)
0c1c8ea6 5153 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
230d793d
RS
5154 }
5155
5156 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5157 correspond to a machine insn or result in further simplifications
5158 if B is a constant. */
5159
8079805d
RK
5160 if (GET_CODE (op0) == AND
5161 && rtx_equal_p (XEXP (op0, 1), op1)
5162 && ! side_effects_p (op1))
0c1c8ea6
RK
5163 return gen_binary (AND, mode,
5164 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
8079805d 5165 op1);
230d793d 5166
8079805d
RK
5167 else if (GET_CODE (op0) == AND
5168 && rtx_equal_p (XEXP (op0, 0), op1)
5169 && ! side_effects_p (op1))
0c1c8ea6
RK
5170 return gen_binary (AND, mode,
5171 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
8079805d 5172 op1);
230d793d 5173
230d793d 5174 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
0802d516
RK
5175 comparison if STORE_FLAG_VALUE is 1. */
5176 if (STORE_FLAG_VALUE == 1
5177 && op1 == const1_rtx
8079805d
RK
5178 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5179 && reversible_comparison_p (op0))
5180 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5181 mode, XEXP (op0, 0), XEXP (op0, 1));
500c518b
RK
5182
5183 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5184 is (lt foo (const_int 0)), so we can perform the above
0802d516 5185 simplification if STORE_FLAG_VALUE is 1. */
500c518b 5186
0802d516
RK
5187 if (STORE_FLAG_VALUE == 1
5188 && op1 == const1_rtx
8079805d
RK
5189 && GET_CODE (op0) == LSHIFTRT
5190 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5191 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5192 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
230d793d
RS
5193
5194 /* (xor (comparison foo bar) (const_int sign-bit))
5195 when STORE_FLAG_VALUE is the sign bit. */
5f4f0e22 5196 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
0802d516 5197 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
e51712db 5198 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
8079805d
RK
5199 && op1 == const_true_rtx
5200 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5201 && reversible_comparison_p (op0))
5202 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5203 mode, XEXP (op0, 0), XEXP (op0, 1));
230d793d 5204 break;
e9a25f70
JL
5205
5206 default:
5207 abort ();
230d793d
RS
5208 }
5209
5210 return x;
5211}
5212\f
5213/* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5214 operations" because they can be replaced with two more basic operations.
5215 ZERO_EXTEND is also considered "compound" because it can be replaced with
5216 an AND operation, which is simpler, though only one operation.
5217
5218 The function expand_compound_operation is called with an rtx expression
5219 and will convert it to the appropriate shifts and AND operations,
5220 simplifying at each stage.
5221
5222 The function make_compound_operation is called to convert an expression
5223 consisting of shifts and ANDs into the equivalent compound expression.
5224 It is the inverse of this function, loosely speaking. */
5225
5226static rtx
5227expand_compound_operation (x)
5228 rtx x;
5229{
5230 int pos = 0, len;
5231 int unsignedp = 0;
5232 int modewidth;
5233 rtx tem;
5234
5235 switch (GET_CODE (x))
5236 {
5237 case ZERO_EXTEND:
5238 unsignedp = 1;
5239 case SIGN_EXTEND:
75473182
RS
5240 /* We can't necessarily use a const_int for a multiword mode;
5241 it depends on implicitly extending the value.
5242 Since we don't know the right way to extend it,
5243 we can't tell whether the implicit way is right.
5244
5245 Even for a mode that is no wider than a const_int,
5246 we can't win, because we need to sign extend one of its bits through
5247 the rest of it, and we don't know which bit. */
230d793d 5248 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
75473182 5249 return x;
230d793d 5250
8079805d
RK
5251 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5252 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5253 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5254 reloaded. If not for that, MEM's would very rarely be safe.
5255
5256 Reject MODEs bigger than a word, because we might not be able
5257 to reference a two-register group starting with an arbitrary register
5258 (and currently gen_lowpart might crash for a SUBREG). */
5259
5260 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
230d793d
RS
5261 return x;
5262
5263 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5264 /* If the inner object has VOIDmode (the only way this can happen
5265 is if it is a ASM_OPERANDS), we can't do anything since we don't
5266 know how much masking to do. */
5267 if (len == 0)
5268 return x;
5269
5270 break;
5271
5272 case ZERO_EXTRACT:
5273 unsignedp = 1;
5274 case SIGN_EXTRACT:
5275 /* If the operand is a CLOBBER, just return it. */
5276 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5277 return XEXP (x, 0);
5278
5279 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5280 || GET_CODE (XEXP (x, 2)) != CONST_INT
5281 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5282 return x;
5283
5284 len = INTVAL (XEXP (x, 1));
5285 pos = INTVAL (XEXP (x, 2));
5286
5287 /* If this goes outside the object being extracted, replace the object
5288 with a (use (mem ...)) construct that only combine understands
5289 and is used only for this purpose. */
5290 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
38a448ca 5291 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
230d793d 5292
f76b9db2
ILT
5293 if (BITS_BIG_ENDIAN)
5294 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5295
230d793d
RS
5296 break;
5297
5298 default:
5299 return x;
5300 }
5301
0f13a422
ILT
5302 /* We can optimize some special cases of ZERO_EXTEND. */
5303 if (GET_CODE (x) == ZERO_EXTEND)
5304 {
5305 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5306 know that the last value didn't have any inappropriate bits
5307 set. */
5308 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5309 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5310 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5311 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5312 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5313 return XEXP (XEXP (x, 0), 0);
5314
5315 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5316 if (GET_CODE (XEXP (x, 0)) == SUBREG
5317 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5318 && subreg_lowpart_p (XEXP (x, 0))
5319 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5320 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
fcc60894 5321 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
0f13a422
ILT
5322 return SUBREG_REG (XEXP (x, 0));
5323
5324 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5325 is a comparison and STORE_FLAG_VALUE permits. This is like
5326 the first case, but it works even when GET_MODE (x) is larger
5327 than HOST_WIDE_INT. */
5328 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5329 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5330 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5331 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5332 <= HOST_BITS_PER_WIDE_INT)
5333 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5334 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5335 return XEXP (XEXP (x, 0), 0);
5336
5337 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5338 if (GET_CODE (XEXP (x, 0)) == SUBREG
5339 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5340 && subreg_lowpart_p (XEXP (x, 0))
5341 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5342 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5343 <= HOST_BITS_PER_WIDE_INT)
5344 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5345 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5346 return SUBREG_REG (XEXP (x, 0));
5347
5348 /* If sign extension is cheaper than zero extension, then use it
5349 if we know that no extraneous bits are set, and that the high
5350 bit is not set. */
5351 if (flag_expensive_optimizations
5352 && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5353 && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5354 & ~ (((unsigned HOST_WIDE_INT)
5355 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5356 >> 1))
5357 == 0))
5358 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5359 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5360 <= HOST_BITS_PER_WIDE_INT)
5361 && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5362 & ~ (((unsigned HOST_WIDE_INT)
5363 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5364 >> 1))
5365 == 0))))
5366 {
38a448ca 5367 rtx temp = gen_rtx_SIGN_EXTEND (GET_MODE (x), XEXP (x, 0));
0f13a422
ILT
5368
5369 if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5370 return expand_compound_operation (temp);
5371 }
5372 }
5373
230d793d
RS
5374 /* If we reach here, we want to return a pair of shifts. The inner
5375 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5376 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5377 logical depending on the value of UNSIGNEDP.
5378
5379 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5380 converted into an AND of a shift.
5381
5382 We must check for the case where the left shift would have a negative
5383 count. This can happen in a case like (x >> 31) & 255 on machines
5384 that can't shift by a constant. On those machines, we would first
5385 combine the shift with the AND to produce a variable-position
5386 extraction. Then the constant of 31 would be substituted in to produce
5387 a such a position. */
5388
5389 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5390 if (modewidth >= pos - len)
5f4f0e22 5391 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
230d793d 5392 GET_MODE (x),
5f4f0e22
CH
5393 simplify_shift_const (NULL_RTX, ASHIFT,
5394 GET_MODE (x),
230d793d
RS
5395 XEXP (x, 0),
5396 modewidth - pos - len),
5397 modewidth - len);
5398
5f4f0e22
CH
5399 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5400 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5401 simplify_shift_const (NULL_RTX, LSHIFTRT,
230d793d
RS
5402 GET_MODE (x),
5403 XEXP (x, 0), pos),
5f4f0e22 5404 ((HOST_WIDE_INT) 1 << len) - 1);
230d793d
RS
5405 else
5406 /* Any other cases we can't handle. */
5407 return x;
5408
5409
5410 /* If we couldn't do this for some reason, return the original
5411 expression. */
5412 if (GET_CODE (tem) == CLOBBER)
5413 return x;
5414
5415 return tem;
5416}
5417\f
5418/* X is a SET which contains an assignment of one object into
5419 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5420 or certain SUBREGS). If possible, convert it into a series of
5421 logical operations.
5422
5423 We half-heartedly support variable positions, but do not at all
5424 support variable lengths. */
5425
5426static rtx
5427expand_field_assignment (x)
5428 rtx x;
5429{
5430 rtx inner;
0f41302f 5431 rtx pos; /* Always counts from low bit. */
230d793d
RS
5432 int len;
5433 rtx mask;
5434 enum machine_mode compute_mode;
5435
5436 /* Loop until we find something we can't simplify. */
5437 while (1)
5438 {
5439 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5440 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5441 {
5442 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5443 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
4d9cfc7b 5444 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
230d793d
RS
5445 }
5446 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5447 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5448 {
5449 inner = XEXP (SET_DEST (x), 0);
5450 len = INTVAL (XEXP (SET_DEST (x), 1));
5451 pos = XEXP (SET_DEST (x), 2);
5452
5453 /* If the position is constant and spans the width of INNER,
5454 surround INNER with a USE to indicate this. */
5455 if (GET_CODE (pos) == CONST_INT
5456 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
38a448ca 5457 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
230d793d 5458
f76b9db2
ILT
5459 if (BITS_BIG_ENDIAN)
5460 {
5461 if (GET_CODE (pos) == CONST_INT)
5462 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5463 - INTVAL (pos));
5464 else if (GET_CODE (pos) == MINUS
5465 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5466 && (INTVAL (XEXP (pos, 1))
5467 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5468 /* If position is ADJUST - X, new position is X. */
5469 pos = XEXP (pos, 0);
5470 else
5471 pos = gen_binary (MINUS, GET_MODE (pos),
5472 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5473 - len),
5474 pos);
5475 }
230d793d
RS
5476 }
5477
5478 /* A SUBREG between two modes that occupy the same numbers of words
5479 can be done by moving the SUBREG to the source. */
5480 else if (GET_CODE (SET_DEST (x)) == SUBREG
5481 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5482 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5483 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5484 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5485 {
38a448ca 5486 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
c5c76735
JL
5487 gen_lowpart_for_combine
5488 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5489 SET_SRC (x)));
230d793d
RS
5490 continue;
5491 }
5492 else
5493 break;
5494
5495 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5496 inner = SUBREG_REG (inner);
5497
5498 compute_mode = GET_MODE (inner);
5499
861556b4
RH
5500 /* Don't attempt bitwise arithmetic on non-integral modes. */
5501 if (! INTEGRAL_MODE_P (compute_mode))
5502 {
5503 enum machine_mode imode;
5504
5505 /* Something is probably seriously wrong if this matches. */
5506 if (! FLOAT_MODE_P (compute_mode))
5507 break;
5508
5509 /* Try to find an integral mode to pun with. */
5510 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5511 if (imode == BLKmode)
5512 break;
5513
5514 compute_mode = imode;
5515 inner = gen_lowpart_for_combine (imode, inner);
5516 }
5517
230d793d 5518 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5f4f0e22
CH
5519 if (len < HOST_BITS_PER_WIDE_INT)
5520 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
230d793d
RS
5521 else
5522 break;
5523
5524 /* Now compute the equivalent expression. Make a copy of INNER
5525 for the SET_DEST in case it is a MEM into which we will substitute;
5526 we don't want shared RTL in that case. */
c5c76735
JL
5527 x = gen_rtx_SET
5528 (VOIDmode, copy_rtx (inner),
5529 gen_binary (IOR, compute_mode,
5530 gen_binary (AND, compute_mode,
5531 gen_unary (NOT, compute_mode,
5532 compute_mode,
5533 gen_binary (ASHIFT,
5534 compute_mode,
5535 mask, pos)),
5536 inner),
5537 gen_binary (ASHIFT, compute_mode,
5538 gen_binary (AND, compute_mode,
5539 gen_lowpart_for_combine
5540 (compute_mode, SET_SRC (x)),
5541 mask),
5542 pos)));
230d793d
RS
5543 }
5544
5545 return x;
5546}
5547\f
8999a12e
RK
5548/* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5549 it is an RTX that represents a variable starting position; otherwise,
5550 POS is the (constant) starting bit position (counted from the LSB).
230d793d
RS
5551
5552 INNER may be a USE. This will occur when we started with a bitfield
5553 that went outside the boundary of the object in memory, which is
5554 allowed on most machines. To isolate this case, we produce a USE
5555 whose mode is wide enough and surround the MEM with it. The only
5556 code that understands the USE is this routine. If it is not removed,
5557 it will cause the resulting insn not to match.
5558
5559 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5560 signed reference.
5561
5562 IN_DEST is non-zero if this is a reference in the destination of a
5563 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5564 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5565 be used.
5566
5567 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5568 ZERO_EXTRACT should be built even for bits starting at bit 0.
5569
76184def
DE
5570 MODE is the desired mode of the result (if IN_DEST == 0).
5571
5572 The result is an RTX for the extraction or NULL_RTX if the target
5573 can't handle it. */
230d793d
RS
5574
5575static rtx
5576make_extraction (mode, inner, pos, pos_rtx, len,
5577 unsignedp, in_dest, in_compare)
5578 enum machine_mode mode;
5579 rtx inner;
5580 int pos;
5581 rtx pos_rtx;
5582 int len;
5583 int unsignedp;
5584 int in_dest, in_compare;
5585{
94b4b17a
RS
5586 /* This mode describes the size of the storage area
5587 to fetch the overall value from. Within that, we
5588 ignore the POS lowest bits, etc. */
230d793d
RS
5589 enum machine_mode is_mode = GET_MODE (inner);
5590 enum machine_mode inner_mode;
d7cd794f
RK
5591 enum machine_mode wanted_inner_mode = byte_mode;
5592 enum machine_mode wanted_inner_reg_mode = word_mode;
230d793d
RS
5593 enum machine_mode pos_mode = word_mode;
5594 enum machine_mode extraction_mode = word_mode;
5595 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5596 int spans_byte = 0;
5597 rtx new = 0;
8999a12e 5598 rtx orig_pos_rtx = pos_rtx;
6139ff20 5599 int orig_pos;
230d793d
RS
5600
5601 /* Get some information about INNER and get the innermost object. */
5602 if (GET_CODE (inner) == USE)
94b4b17a 5603 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
230d793d
RS
5604 /* We don't need to adjust the position because we set up the USE
5605 to pretend that it was a full-word object. */
5606 spans_byte = 1, inner = XEXP (inner, 0);
5607 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
94b4b17a
RS
5608 {
5609 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5610 consider just the QI as the memory to extract from.
5611 The subreg adds or removes high bits; its mode is
5612 irrelevant to the meaning of this extraction,
5613 since POS and LEN count from the lsb. */
5614 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5615 is_mode = GET_MODE (SUBREG_REG (inner));
5616 inner = SUBREG_REG (inner);
5617 }
230d793d
RS
5618
5619 inner_mode = GET_MODE (inner);
5620
5621 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
8999a12e 5622 pos = INTVAL (pos_rtx), pos_rtx = 0;
230d793d
RS
5623
5624 /* See if this can be done without an extraction. We never can if the
5625 width of the field is not the same as that of some integer mode. For
5626 registers, we can only avoid the extraction if the position is at the
5627 low-order bit and this is either not in the destination or we have the
5628 appropriate STRICT_LOW_PART operation available.
5629
5630 For MEM, we can avoid an extract if the field starts on an appropriate
5631 boundary and we can change the mode of the memory reference. However,
5632 we cannot directly access the MEM if we have a USE and the underlying
5633 MEM is not TMODE. This combination means that MEM was being used in a
5634 context where bits outside its mode were being referenced; that is only
5635 valid in bit-field insns. */
5636
5637 if (tmode != BLKmode
5638 && ! (spans_byte && inner_mode != tmode)
4d9cfc7b
RK
5639 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5640 && GET_CODE (inner) != MEM
230d793d 5641 && (! in_dest
df62f951
RK
5642 || (GET_CODE (inner) == REG
5643 && (movstrict_optab->handlers[(int) tmode].insn_code
5644 != CODE_FOR_nothing))))
8999a12e 5645 || (GET_CODE (inner) == MEM && pos_rtx == 0
dfbe1b2f
RK
5646 && (pos
5647 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5648 : BITS_PER_UNIT)) == 0
230d793d
RS
5649 /* We can't do this if we are widening INNER_MODE (it
5650 may not be aligned, for one thing). */
5651 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5652 && (inner_mode == tmode
5653 || (! mode_dependent_address_p (XEXP (inner, 0))
5654 && ! MEM_VOLATILE_P (inner))))))
5655 {
230d793d
RS
5656 /* If INNER is a MEM, make a new MEM that encompasses just the desired
5657 field. If the original and current mode are the same, we need not
5658 adjust the offset. Otherwise, we do if bytes big endian.
5659
4d9cfc7b
RK
5660 If INNER is not a MEM, get a piece consisting of just the field
5661 of interest (in this case POS % BITS_PER_WORD must be 0). */
230d793d
RS
5662
5663 if (GET_CODE (inner) == MEM)
5664 {
94b4b17a
RS
5665 int offset;
5666 /* POS counts from lsb, but make OFFSET count in memory order. */
5667 if (BYTES_BIG_ENDIAN)
5668 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5669 else
5670 offset = pos / BITS_PER_UNIT;
230d793d 5671
38a448ca 5672 new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
230d793d 5673 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
c6df88cb 5674 MEM_COPY_ATTRIBUTES (new, inner);
230d793d 5675 }
df62f951 5676 else if (GET_CODE (inner) == REG)
c0d3ac4d
RK
5677 {
5678 /* We can't call gen_lowpart_for_combine here since we always want
5679 a SUBREG and it would sometimes return a new hard register. */
5680 if (tmode != inner_mode)
38a448ca
RH
5681 new = gen_rtx_SUBREG (tmode, inner,
5682 (WORDS_BIG_ENDIAN
c5c76735
JL
5683 && (GET_MODE_SIZE (inner_mode)
5684 > UNITS_PER_WORD)
38a448ca
RH
5685 ? (((GET_MODE_SIZE (inner_mode)
5686 - GET_MODE_SIZE (tmode))
5687 / UNITS_PER_WORD)
5688 - pos / BITS_PER_WORD)
5689 : pos / BITS_PER_WORD));
c0d3ac4d
RK
5690 else
5691 new = inner;
5692 }
230d793d 5693 else
6139ff20
RK
5694 new = force_to_mode (inner, tmode,
5695 len >= HOST_BITS_PER_WIDE_INT
5696 ? GET_MODE_MASK (tmode)
5697 : ((HOST_WIDE_INT) 1 << len) - 1,
e3d616e3 5698 NULL_RTX, 0);
230d793d
RS
5699
5700 /* If this extraction is going into the destination of a SET,
5701 make a STRICT_LOW_PART unless we made a MEM. */
5702
5703 if (in_dest)
5704 return (GET_CODE (new) == MEM ? new
77fa0940 5705 : (GET_CODE (new) != SUBREG
38a448ca 5706 ? gen_rtx_CLOBBER (tmode, const0_rtx)
77fa0940 5707 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
230d793d
RS
5708
5709 /* Otherwise, sign- or zero-extend unless we already are in the
5710 proper mode. */
5711
5712 return (mode == tmode ? new
5713 : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5714 mode, new));
5715 }
5716
cc471082
RS
5717 /* Unless this is a COMPARE or we have a funny memory reference,
5718 don't do anything with zero-extending field extracts starting at
5719 the low-order bit since they are simple AND operations. */
8999a12e
RK
5720 if (pos_rtx == 0 && pos == 0 && ! in_dest
5721 && ! in_compare && ! spans_byte && unsignedp)
230d793d
RS
5722 return 0;
5723
c5c76735
JL
5724 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
5725 we would be spanning bytes or if the position is not a constant and the
5726 length is not 1. In all other cases, we would only be going outside
5727 our object in cases when an original shift would have been
e7373556 5728 undefined. */
c5c76735 5729 if (! spans_byte && GET_CODE (inner) == MEM
e7373556
RK
5730 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5731 || (pos_rtx != 0 && len != 1)))
5732 return 0;
5733
d7cd794f 5734 /* Get the mode to use should INNER not be a MEM, the mode for the position,
230d793d
RS
5735 and the mode for the result. */
5736#ifdef HAVE_insv
5737 if (in_dest)
5738 {
0d8e55d8
JL
5739 wanted_inner_reg_mode
5740 = (insn_operand_mode[(int) CODE_FOR_insv][0] == VOIDmode
5741 ? word_mode
5742 : insn_operand_mode[(int) CODE_FOR_insv][0]);
5743 pos_mode = (insn_operand_mode[(int) CODE_FOR_insv][2] == VOIDmode
5744 ? word_mode : insn_operand_mode[(int) CODE_FOR_insv][2]);
5745 extraction_mode = (insn_operand_mode[(int) CODE_FOR_insv][3] == VOIDmode
5746 ? word_mode
5747 : insn_operand_mode[(int) CODE_FOR_insv][3]);
230d793d
RS
5748 }
5749#endif
5750
5751#ifdef HAVE_extzv
5752 if (! in_dest && unsignedp)
5753 {
0d8e55d8
JL
5754 wanted_inner_reg_mode
5755 = (insn_operand_mode[(int) CODE_FOR_extzv][1] == VOIDmode
5756 ? word_mode
5757 : insn_operand_mode[(int) CODE_FOR_extzv][1]);
5758 pos_mode = (insn_operand_mode[(int) CODE_FOR_extzv][3] == VOIDmode
5759 ? word_mode : insn_operand_mode[(int) CODE_FOR_extzv][3]);
5760 extraction_mode = (insn_operand_mode[(int) CODE_FOR_extzv][0] == VOIDmode
5761 ? word_mode
5762 : insn_operand_mode[(int) CODE_FOR_extzv][0]);
230d793d
RS
5763 }
5764#endif
5765
5766#ifdef HAVE_extv
5767 if (! in_dest && ! unsignedp)
5768 {
0d8e55d8
JL
5769 wanted_inner_reg_mode
5770 = (insn_operand_mode[(int) CODE_FOR_extv][1] == VOIDmode
5771 ? word_mode
5772 : insn_operand_mode[(int) CODE_FOR_extv][1]);
5773 pos_mode = (insn_operand_mode[(int) CODE_FOR_extv][3] == VOIDmode
5774 ? word_mode : insn_operand_mode[(int) CODE_FOR_extv][3]);
5775 extraction_mode = (insn_operand_mode[(int) CODE_FOR_extv][0] == VOIDmode
5776 ? word_mode
5777 : insn_operand_mode[(int) CODE_FOR_extv][0]);
230d793d
RS
5778 }
5779#endif
5780
5781 /* Never narrow an object, since that might not be safe. */
5782
5783 if (mode != VOIDmode
5784 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5785 extraction_mode = mode;
5786
5787 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5788 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5789 pos_mode = GET_MODE (pos_rtx);
5790
d7cd794f
RK
5791 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5792 if we have to change the mode of memory and cannot, the desired mode is
5793 EXTRACTION_MODE. */
5794 if (GET_CODE (inner) != MEM)
5795 wanted_inner_mode = wanted_inner_reg_mode;
5796 else if (inner_mode != wanted_inner_mode
5797 && (mode_dependent_address_p (XEXP (inner, 0))
5798 || MEM_VOLATILE_P (inner)))
5799 wanted_inner_mode = extraction_mode;
230d793d 5800
6139ff20
RK
5801 orig_pos = pos;
5802
f76b9db2
ILT
5803 if (BITS_BIG_ENDIAN)
5804 {
cf54c2cd
DE
5805 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5806 BITS_BIG_ENDIAN style. If position is constant, compute new
5807 position. Otherwise, build subtraction.
5808 Note that POS is relative to the mode of the original argument.
5809 If it's a MEM we need to recompute POS relative to that.
5810 However, if we're extracting from (or inserting into) a register,
5811 we want to recompute POS relative to wanted_inner_mode. */
5812 int width = (GET_CODE (inner) == MEM
5813 ? GET_MODE_BITSIZE (is_mode)
5814 : GET_MODE_BITSIZE (wanted_inner_mode));
5815
f76b9db2 5816 if (pos_rtx == 0)
cf54c2cd 5817 pos = width - len - pos;
f76b9db2
ILT
5818 else
5819 pos_rtx
5820 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
cf54c2cd
DE
5821 GEN_INT (width - len), pos_rtx);
5822 /* POS may be less than 0 now, but we check for that below.
5823 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
f76b9db2 5824 }
230d793d
RS
5825
5826 /* If INNER has a wider mode, make it smaller. If this is a constant
5827 extract, try to adjust the byte to point to the byte containing
5828 the value. */
d7cd794f
RK
5829 if (wanted_inner_mode != VOIDmode
5830 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
230d793d 5831 && ((GET_CODE (inner) == MEM
d7cd794f 5832 && (inner_mode == wanted_inner_mode
230d793d
RS
5833 || (! mode_dependent_address_p (XEXP (inner, 0))
5834 && ! MEM_VOLATILE_P (inner))))))
5835 {
5836 int offset = 0;
5837
5838 /* The computations below will be correct if the machine is big
5839 endian in both bits and bytes or little endian in bits and bytes.
5840 If it is mixed, we must adjust. */
5841
230d793d 5842 /* If bytes are big endian and we had a paradoxical SUBREG, we must
0f41302f 5843 adjust OFFSET to compensate. */
f76b9db2
ILT
5844 if (BYTES_BIG_ENDIAN
5845 && ! spans_byte
230d793d
RS
5846 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5847 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
230d793d
RS
5848
5849 /* If this is a constant position, we can move to the desired byte. */
8999a12e 5850 if (pos_rtx == 0)
230d793d
RS
5851 {
5852 offset += pos / BITS_PER_UNIT;
d7cd794f 5853 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
230d793d
RS
5854 }
5855
f76b9db2
ILT
5856 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5857 && ! spans_byte
d7cd794f 5858 && is_mode != wanted_inner_mode)
c6b3f1f2 5859 offset = (GET_MODE_SIZE (is_mode)
d7cd794f 5860 - GET_MODE_SIZE (wanted_inner_mode) - offset);
c6b3f1f2 5861
d7cd794f 5862 if (offset != 0 || inner_mode != wanted_inner_mode)
230d793d 5863 {
38a448ca
RH
5864 rtx newmem = gen_rtx_MEM (wanted_inner_mode,
5865 plus_constant (XEXP (inner, 0), offset));
230d793d 5866 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
c6df88cb 5867 MEM_COPY_ATTRIBUTES (newmem, inner);
230d793d
RS
5868 inner = newmem;
5869 }
5870 }
5871
9e74dc41
RK
5872 /* If INNER is not memory, we can always get it into the proper mode. If we
5873 are changing its mode, POS must be a constant and smaller than the size
5874 of the new mode. */
230d793d 5875 else if (GET_CODE (inner) != MEM)
9e74dc41
RK
5876 {
5877 if (GET_MODE (inner) != wanted_inner_mode
5878 && (pos_rtx != 0
5879 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5880 return 0;
5881
5882 inner = force_to_mode (inner, wanted_inner_mode,
5883 pos_rtx
5884 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5885 ? GET_MODE_MASK (wanted_inner_mode)
5886 : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5887 NULL_RTX, 0);
5888 }
230d793d
RS
5889
5890 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
5891 have to zero extend. Otherwise, we can just use a SUBREG. */
8999a12e 5892 if (pos_rtx != 0
230d793d
RS
5893 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5894 pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
8999a12e 5895 else if (pos_rtx != 0
230d793d
RS
5896 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5897 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5898
8999a12e
RK
5899 /* Make POS_RTX unless we already have it and it is correct. If we don't
5900 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
0f41302f 5901 be a CONST_INT. */
8999a12e
RK
5902 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5903 pos_rtx = orig_pos_rtx;
5904
5905 else if (pos_rtx == 0)
5f4f0e22 5906 pos_rtx = GEN_INT (pos);
230d793d
RS
5907
5908 /* Make the required operation. See if we can use existing rtx. */
5909 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5f4f0e22 5910 extraction_mode, inner, GEN_INT (len), pos_rtx);
230d793d
RS
5911 if (! in_dest)
5912 new = gen_lowpart_for_combine (mode, new);
5913
5914 return new;
5915}
5916\f
71923da7
RK
5917/* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5918 with any other operations in X. Return X without that shift if so. */
5919
5920static rtx
5921extract_left_shift (x, count)
5922 rtx x;
5923 int count;
5924{
5925 enum rtx_code code = GET_CODE (x);
5926 enum machine_mode mode = GET_MODE (x);
5927 rtx tem;
5928
5929 switch (code)
5930 {
5931 case ASHIFT:
5932 /* This is the shift itself. If it is wide enough, we will return
5933 either the value being shifted if the shift count is equal to
5934 COUNT or a shift for the difference. */
5935 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5936 && INTVAL (XEXP (x, 1)) >= count)
5937 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5938 INTVAL (XEXP (x, 1)) - count);
5939 break;
5940
5941 case NEG: case NOT:
5942 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
0c1c8ea6 5943 return gen_unary (code, mode, mode, tem);
71923da7
RK
5944
5945 break;
5946
5947 case PLUS: case IOR: case XOR: case AND:
5948 /* If we can safely shift this constant and we find the inner shift,
5949 make a new operation. */
5950 if (GET_CODE (XEXP (x,1)) == CONST_INT
b729186a 5951 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
71923da7
RK
5952 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5953 return gen_binary (code, mode, tem,
5954 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5955
5956 break;
e9a25f70
JL
5957
5958 default:
5959 break;
71923da7
RK
5960 }
5961
5962 return 0;
5963}
5964\f
230d793d
RS
5965/* Look at the expression rooted at X. Look for expressions
5966 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5967 Form these expressions.
5968
5969 Return the new rtx, usually just X.
5970
5971 Also, for machines like the Vax that don't have logical shift insns,
5972 try to convert logical to arithmetic shift operations in cases where
5973 they are equivalent. This undoes the canonicalizations to logical
5974 shifts done elsewhere.
5975
5976 We try, as much as possible, to re-use rtl expressions to save memory.
5977
5978 IN_CODE says what kind of expression we are processing. Normally, it is
42495ca0
RK
5979 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
5980 being kludges), it is MEM. When processing the arguments of a comparison
230d793d
RS
5981 or a COMPARE against zero, it is COMPARE. */
5982
5983static rtx
5984make_compound_operation (x, in_code)
5985 rtx x;
5986 enum rtx_code in_code;
5987{
5988 enum rtx_code code = GET_CODE (x);
5989 enum machine_mode mode = GET_MODE (x);
5990 int mode_width = GET_MODE_BITSIZE (mode);
71923da7 5991 rtx rhs, lhs;
230d793d 5992 enum rtx_code next_code;
f24ad0e4 5993 int i;
230d793d 5994 rtx new = 0;
280f58ba 5995 rtx tem;
6f7d635c 5996 const char *fmt;
230d793d
RS
5997
5998 /* Select the code to be used in recursive calls. Once we are inside an
5999 address, we stay there. If we have a comparison, set to COMPARE,
6000 but once inside, go back to our default of SET. */
6001
42495ca0 6002 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
230d793d
RS
6003 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6004 && XEXP (x, 1) == const0_rtx) ? COMPARE
6005 : in_code == COMPARE ? SET : in_code);
6006
6007 /* Process depending on the code of this operation. If NEW is set
6008 non-zero, it will be returned. */
6009
6010 switch (code)
6011 {
6012 case ASHIFT:
230d793d
RS
6013 /* Convert shifts by constants into multiplications if inside
6014 an address. */
6015 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5f4f0e22 6016 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
230d793d 6017 && INTVAL (XEXP (x, 1)) >= 0)
280f58ba
RK
6018 {
6019 new = make_compound_operation (XEXP (x, 0), next_code);
6020 new = gen_rtx_combine (MULT, mode, new,
6021 GEN_INT ((HOST_WIDE_INT) 1
6022 << INTVAL (XEXP (x, 1))));
6023 }
230d793d
RS
6024 break;
6025
6026 case AND:
6027 /* If the second operand is not a constant, we can't do anything
6028 with it. */
6029 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6030 break;
6031
6032 /* If the constant is a power of two minus one and the first operand
6033 is a logical right shift, make an extraction. */
6034 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6035 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
280f58ba
RK
6036 {
6037 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6038 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6039 0, in_code == COMPARE);
6040 }
dfbe1b2f 6041
230d793d
RS
6042 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6043 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6044 && subreg_lowpart_p (XEXP (x, 0))
6045 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6046 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
280f58ba
RK
6047 {
6048 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6049 next_code);
2f99f437 6050 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
280f58ba
RK
6051 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6052 0, in_code == COMPARE);
6053 }
45620ed4 6054 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
c2f9f64e
JW
6055 else if ((GET_CODE (XEXP (x, 0)) == XOR
6056 || GET_CODE (XEXP (x, 0)) == IOR)
6057 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6058 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6059 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6060 {
6061 /* Apply the distributive law, and then try to make extractions. */
6062 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
38a448ca
RH
6063 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6064 XEXP (x, 1)),
6065 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6066 XEXP (x, 1)));
c2f9f64e
JW
6067 new = make_compound_operation (new, in_code);
6068 }
a7c99304
RK
6069
6070 /* If we are have (and (rotate X C) M) and C is larger than the number
6071 of bits in M, this is an extraction. */
6072
6073 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6074 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6075 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6076 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
280f58ba
RK
6077 {
6078 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6079 new = make_extraction (mode, new,
6080 (GET_MODE_BITSIZE (mode)
6081 - INTVAL (XEXP (XEXP (x, 0), 1))),
6082 NULL_RTX, i, 1, 0, in_code == COMPARE);
6083 }
a7c99304
RK
6084
6085 /* On machines without logical shifts, if the operand of the AND is
230d793d
RS
6086 a logical shift and our mask turns off all the propagated sign
6087 bits, we can replace the logical shift with an arithmetic shift. */
d0ab8cd3
RK
6088 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6089 && (lshr_optab->handlers[(int) mode].insn_code
6090 == CODE_FOR_nothing)
230d793d
RS
6091 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6092 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6093 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5f4f0e22
CH
6094 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6095 && mode_width <= HOST_BITS_PER_WIDE_INT)
230d793d 6096 {
5f4f0e22 6097 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
230d793d
RS
6098
6099 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6100 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6101 SUBST (XEXP (x, 0),
280f58ba
RK
6102 gen_rtx_combine (ASHIFTRT, mode,
6103 make_compound_operation (XEXP (XEXP (x, 0), 0),
6104 next_code),
230d793d
RS
6105 XEXP (XEXP (x, 0), 1)));
6106 }
6107
6108 /* If the constant is one less than a power of two, this might be
6109 representable by an extraction even if no shift is present.
6110 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6111 we are in a COMPARE. */
6112 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
280f58ba
RK
6113 new = make_extraction (mode,
6114 make_compound_operation (XEXP (x, 0),
6115 next_code),
6116 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
230d793d
RS
6117
6118 /* If we are in a comparison and this is an AND with a power of two,
6119 convert this into the appropriate bit extract. */
6120 else if (in_code == COMPARE
6121 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
280f58ba
RK
6122 new = make_extraction (mode,
6123 make_compound_operation (XEXP (x, 0),
6124 next_code),
6125 i, NULL_RTX, 1, 1, 0, 1);
230d793d
RS
6126
6127 break;
6128
6129 case LSHIFTRT:
6130 /* If the sign bit is known to be zero, replace this with an
6131 arithmetic shift. */
d0ab8cd3
RK
6132 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
6133 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5f4f0e22 6134 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 6135 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
230d793d 6136 {
280f58ba
RK
6137 new = gen_rtx_combine (ASHIFTRT, mode,
6138 make_compound_operation (XEXP (x, 0),
6139 next_code),
6140 XEXP (x, 1));
230d793d
RS
6141 break;
6142 }
6143
0f41302f 6144 /* ... fall through ... */
230d793d
RS
6145
6146 case ASHIFTRT:
71923da7
RK
6147 lhs = XEXP (x, 0);
6148 rhs = XEXP (x, 1);
6149
230d793d
RS
6150 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6151 this is a SIGN_EXTRACT. */
71923da7
RK
6152 if (GET_CODE (rhs) == CONST_INT
6153 && GET_CODE (lhs) == ASHIFT
6154 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6155 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
280f58ba 6156 {
71923da7 6157 new = make_compound_operation (XEXP (lhs, 0), next_code);
280f58ba 6158 new = make_extraction (mode, new,
71923da7
RK
6159 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6160 NULL_RTX, mode_width - INTVAL (rhs),
d0ab8cd3
RK
6161 code == LSHIFTRT, 0, in_code == COMPARE);
6162 }
6163
71923da7
RK
6164 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6165 If so, try to merge the shifts into a SIGN_EXTEND. We could
6166 also do this for some cases of SIGN_EXTRACT, but it doesn't
6167 seem worth the effort; the case checked for occurs on Alpha. */
6168
6169 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6170 && ! (GET_CODE (lhs) == SUBREG
6171 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6172 && GET_CODE (rhs) == CONST_INT
6173 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6174 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6175 new = make_extraction (mode, make_compound_operation (new, next_code),
6176 0, NULL_RTX, mode_width - INTVAL (rhs),
6177 code == LSHIFTRT, 0, in_code == COMPARE);
6178
230d793d 6179 break;
280f58ba
RK
6180
6181 case SUBREG:
6182 /* Call ourselves recursively on the inner expression. If we are
6183 narrowing the object and it has a different RTL code from
6184 what it originally did, do this SUBREG as a force_to_mode. */
6185
0a5cbff6 6186 tem = make_compound_operation (SUBREG_REG (x), in_code);
280f58ba
RK
6187 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6188 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6189 && subreg_lowpart_p (x))
0a5cbff6
RK
6190 {
6191 rtx newer = force_to_mode (tem, mode,
e3d616e3 6192 GET_MODE_MASK (mode), NULL_RTX, 0);
0a5cbff6
RK
6193
6194 /* If we have something other than a SUBREG, we might have
6195 done an expansion, so rerun outselves. */
6196 if (GET_CODE (newer) != SUBREG)
6197 newer = make_compound_operation (newer, in_code);
6198
6199 return newer;
6200 }
6f28d3e9
RH
6201
6202 /* If this is a paradoxical subreg, and the new code is a sign or
6203 zero extension, omit the subreg and widen the extension. If it
6204 is a regular subreg, we can still get rid of the subreg by not
6205 widening so much, or in fact removing the extension entirely. */
6206 if ((GET_CODE (tem) == SIGN_EXTEND
6207 || GET_CODE (tem) == ZERO_EXTEND)
6208 && subreg_lowpart_p (x))
6209 {
6210 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6211 || (GET_MODE_SIZE (mode) >
6212 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6213 tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6214 else
6215 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6216 return tem;
6217 }
e9a25f70
JL
6218 break;
6219
6220 default:
6221 break;
230d793d
RS
6222 }
6223
6224 if (new)
6225 {
df62f951 6226 x = gen_lowpart_for_combine (mode, new);
230d793d
RS
6227 code = GET_CODE (x);
6228 }
6229
6230 /* Now recursively process each operand of this operation. */
6231 fmt = GET_RTX_FORMAT (code);
6232 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6233 if (fmt[i] == 'e')
6234 {
6235 new = make_compound_operation (XEXP (x, i), next_code);
6236 SUBST (XEXP (x, i), new);
6237 }
6238
6239 return x;
6240}
6241\f
6242/* Given M see if it is a value that would select a field of bits
6243 within an item, but not the entire word. Return -1 if not.
6244 Otherwise, return the starting position of the field, where 0 is the
6245 low-order bit.
6246
6247 *PLEN is set to the length of the field. */
6248
6249static int
6250get_pos_from_mask (m, plen)
5f4f0e22 6251 unsigned HOST_WIDE_INT m;
230d793d
RS
6252 int *plen;
6253{
6254 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6255 int pos = exact_log2 (m & - m);
6256
6257 if (pos < 0)
6258 return -1;
6259
6260 /* Now shift off the low-order zero bits and see if we have a power of
6261 two minus 1. */
6262 *plen = exact_log2 ((m >> pos) + 1);
6263
6264 if (*plen <= 0)
6265 return -1;
6266
6267 return pos;
6268}
6269\f
6139ff20
RK
6270/* See if X can be simplified knowing that we will only refer to it in
6271 MODE and will only refer to those bits that are nonzero in MASK.
6272 If other bits are being computed or if masking operations are done
6273 that select a superset of the bits in MASK, they can sometimes be
6274 ignored.
6275
6276 Return a possibly simplified expression, but always convert X to
6277 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
dfbe1b2f
RK
6278
6279 Also, if REG is non-zero and X is a register equal in value to REG,
e3d616e3
RK
6280 replace X with REG.
6281
6282 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6283 are all off in X. This is used when X will be complemented, by either
180b8e4b 6284 NOT, NEG, or XOR. */
dfbe1b2f
RK
6285
6286static rtx
e3d616e3 6287force_to_mode (x, mode, mask, reg, just_select)
dfbe1b2f
RK
6288 rtx x;
6289 enum machine_mode mode;
6139ff20 6290 unsigned HOST_WIDE_INT mask;
dfbe1b2f 6291 rtx reg;
e3d616e3 6292 int just_select;
dfbe1b2f
RK
6293{
6294 enum rtx_code code = GET_CODE (x);
180b8e4b 6295 int next_select = just_select || code == XOR || code == NOT || code == NEG;
ef026f91
RS
6296 enum machine_mode op_mode;
6297 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6139ff20
RK
6298 rtx op0, op1, temp;
6299
132d2040
RK
6300 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6301 code below will do the wrong thing since the mode of such an
be3d27d6
CI
6302 expression is VOIDmode.
6303
6304 Also do nothing if X is a CLOBBER; this can happen if X was
6305 the return value from a call to gen_lowpart_for_combine. */
6306 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
246e00f2
RK
6307 return x;
6308
6139ff20
RK
6309 /* We want to perform the operation is its present mode unless we know
6310 that the operation is valid in MODE, in which case we do the operation
6311 in MODE. */
1c75dfa4
RK
6312 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6313 && code_to_optab[(int) code] != 0
ef026f91
RS
6314 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6315 != CODE_FOR_nothing))
6316 ? mode : GET_MODE (x));
e3d616e3 6317
aa988991
RS
6318 /* It is not valid to do a right-shift in a narrower mode
6319 than the one it came in with. */
6320 if ((code == LSHIFTRT || code == ASHIFTRT)
6321 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6322 op_mode = GET_MODE (x);
ef026f91
RS
6323
6324 /* Truncate MASK to fit OP_MODE. */
6325 if (op_mode)
6326 mask &= GET_MODE_MASK (op_mode);
6139ff20
RK
6327
6328 /* When we have an arithmetic operation, or a shift whose count we
6329 do not know, we need to assume that all bit the up to the highest-order
6330 bit in MASK will be needed. This is how we form such a mask. */
ef026f91
RS
6331 if (op_mode)
6332 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6333 ? GET_MODE_MASK (op_mode)
6334 : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6335 else
6336 fuller_mask = ~ (HOST_WIDE_INT) 0;
6337
6338 /* Determine what bits of X are guaranteed to be (non)zero. */
6339 nonzero = nonzero_bits (x, mode);
6139ff20
RK
6340
6341 /* If none of the bits in X are needed, return a zero. */
e3d616e3 6342 if (! just_select && (nonzero & mask) == 0)
6139ff20 6343 return const0_rtx;
dfbe1b2f 6344
6139ff20
RK
6345 /* If X is a CONST_INT, return a new one. Do this here since the
6346 test below will fail. */
6347 if (GET_CODE (x) == CONST_INT)
ceb7983c
RK
6348 {
6349 HOST_WIDE_INT cval = INTVAL (x) & mask;
6350 int width = GET_MODE_BITSIZE (mode);
6351
6352 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6353 number, sign extend it. */
6354 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6355 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6356 cval |= (HOST_WIDE_INT) -1 << width;
6357
6358 return GEN_INT (cval);
6359 }
dfbe1b2f 6360
180b8e4b
RK
6361 /* If X is narrower than MODE and we want all the bits in X's mode, just
6362 get X in the proper mode. */
6363 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6364 && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
dfbe1b2f
RK
6365 return gen_lowpart_for_combine (mode, x);
6366
71923da7
RK
6367 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6368 MASK are already known to be zero in X, we need not do anything. */
6369 if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6139ff20
RK
6370 return x;
6371
dfbe1b2f
RK
6372 switch (code)
6373 {
6139ff20
RK
6374 case CLOBBER:
6375 /* If X is a (clobber (const_int)), return it since we know we are
0f41302f 6376 generating something that won't match. */
6139ff20
RK
6377 return x;
6378
6139ff20
RK
6379 case USE:
6380 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6381 spanned the boundary of the MEM. If we are now masking so it is
6382 within that boundary, we don't need the USE any more. */
f76b9db2
ILT
6383 if (! BITS_BIG_ENDIAN
6384 && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
e3d616e3 6385 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
f76b9db2 6386 break;
6139ff20 6387
dfbe1b2f
RK
6388 case SIGN_EXTEND:
6389 case ZERO_EXTEND:
6390 case ZERO_EXTRACT:
6391 case SIGN_EXTRACT:
6392 x = expand_compound_operation (x);
6393 if (GET_CODE (x) != code)
e3d616e3 6394 return force_to_mode (x, mode, mask, reg, next_select);
dfbe1b2f
RK
6395 break;
6396
6397 case REG:
6398 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6399 || rtx_equal_p (reg, get_last_value (x))))
6400 x = reg;
6401 break;
6402
dfbe1b2f 6403 case SUBREG:
6139ff20 6404 if (subreg_lowpart_p (x)
180b8e4b
RK
6405 /* We can ignore the effect of this SUBREG if it narrows the mode or
6406 if the constant masks to zero all the bits the mode doesn't
6407 have. */
6139ff20
RK
6408 && ((GET_MODE_SIZE (GET_MODE (x))
6409 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6139ff20
RK
6410 || (0 == (mask
6411 & GET_MODE_MASK (GET_MODE (x))
180b8e4b 6412 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
e3d616e3 6413 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
dfbe1b2f
RK
6414 break;
6415
6416 case AND:
6139ff20
RK
6417 /* If this is an AND with a constant, convert it into an AND
6418 whose constant is the AND of that constant with MASK. If it
6419 remains an AND of MASK, delete it since it is redundant. */
dfbe1b2f 6420
2ca9ae17 6421 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
dfbe1b2f 6422 {
6139ff20
RK
6423 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6424 mask & INTVAL (XEXP (x, 1)));
dfbe1b2f
RK
6425
6426 /* If X is still an AND, see if it is an AND with a mask that
71923da7
RK
6427 is just some low-order bits. If so, and it is MASK, we don't
6428 need it. */
dfbe1b2f
RK
6429
6430 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
e51712db 6431 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == mask)
dfbe1b2f 6432 x = XEXP (x, 0);
d0ab8cd3 6433
71923da7
RK
6434 /* If it remains an AND, try making another AND with the bits
6435 in the mode mask that aren't in MASK turned on. If the
6436 constant in the AND is wide enough, this might make a
6437 cheaper constant. */
6438
6439 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
2ca9ae17
JW
6440 && GET_MODE_MASK (GET_MODE (x)) != mask
6441 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
71923da7
RK
6442 {
6443 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6444 | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6445 int width = GET_MODE_BITSIZE (GET_MODE (x));
6446 rtx y;
6447
6448 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6449 number, sign extend it. */
6450 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6451 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6452 cval |= (HOST_WIDE_INT) -1 << width;
6453
6454 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6455 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6456 x = y;
6457 }
6458
d0ab8cd3 6459 break;
dfbe1b2f
RK
6460 }
6461
6139ff20 6462 goto binop;
dfbe1b2f
RK
6463
6464 case PLUS:
6139ff20
RK
6465 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6466 low-order bits (as in an alignment operation) and FOO is already
6467 aligned to that boundary, mask C1 to that boundary as well.
6468 This may eliminate that PLUS and, later, the AND. */
9fa6d012
TG
6469
6470 {
6471 int width = GET_MODE_BITSIZE (mode);
6472 unsigned HOST_WIDE_INT smask = mask;
6473
6474 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6475 number, sign extend it. */
6476
6477 if (width < HOST_BITS_PER_WIDE_INT
6478 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6479 smask |= (HOST_WIDE_INT) -1 << width;
6480
6481 if (GET_CODE (XEXP (x, 1)) == CONST_INT
0e9ff885
DM
6482 && exact_log2 (- smask) >= 0)
6483 {
6484#ifdef STACK_BIAS
6485 if (STACK_BIAS
6486 && (XEXP (x, 0) == stack_pointer_rtx
6487 || XEXP (x, 0) == frame_pointer_rtx))
6488 {
6489 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6490 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6491
6492 sp_mask &= ~ (sp_alignment - 1);
835c8e04
DT
6493 if ((sp_mask & ~ smask) == 0
6494 && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~ smask) != 0)
0e9ff885
DM
6495 return force_to_mode (plus_constant (XEXP (x, 0),
6496 ((INTVAL (XEXP (x, 1)) -
835c8e04 6497 STACK_BIAS) & smask)
0e9ff885 6498 + STACK_BIAS),
835c8e04 6499 mode, smask, reg, next_select);
0e9ff885
DM
6500 }
6501#endif
835c8e04
DT
6502 if ((nonzero_bits (XEXP (x, 0), mode) & ~ smask) == 0
6503 && (INTVAL (XEXP (x, 1)) & ~ smask) != 0)
0e9ff885 6504 return force_to_mode (plus_constant (XEXP (x, 0),
835c8e04
DT
6505 (INTVAL (XEXP (x, 1))
6506 & smask)),
6507 mode, smask, reg, next_select);
0e9ff885 6508 }
9fa6d012 6509 }
6139ff20 6510
0f41302f 6511 /* ... fall through ... */
6139ff20 6512
dfbe1b2f
RK
6513 case MINUS:
6514 case MULT:
6139ff20
RK
6515 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6516 most significant bit in MASK since carries from those bits will
6517 affect the bits we are interested in. */
6518 mask = fuller_mask;
6519 goto binop;
6520
dfbe1b2f
RK
6521 case IOR:
6522 case XOR:
6139ff20
RK
6523 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6524 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6525 operation which may be a bitfield extraction. Ensure that the
6526 constant we form is not wider than the mode of X. */
6527
6528 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6529 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6530 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6531 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6532 && GET_CODE (XEXP (x, 1)) == CONST_INT
6533 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6534 + floor_log2 (INTVAL (XEXP (x, 1))))
6535 < GET_MODE_BITSIZE (GET_MODE (x)))
6536 && (INTVAL (XEXP (x, 1))
01c82bbb 6537 & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6139ff20
RK
6538 {
6539 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6540 << INTVAL (XEXP (XEXP (x, 0), 1)));
6541 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6542 XEXP (XEXP (x, 0), 0), temp);
d4d2b13f
RK
6543 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6544 XEXP (XEXP (x, 0), 1));
e3d616e3 6545 return force_to_mode (x, mode, mask, reg, next_select);
6139ff20
RK
6546 }
6547
6548 binop:
dfbe1b2f 6549 /* For most binary operations, just propagate into the operation and
6139ff20
RK
6550 change the mode if we have an operation of that mode. */
6551
e3d616e3
RK
6552 op0 = gen_lowpart_for_combine (op_mode,
6553 force_to_mode (XEXP (x, 0), mode, mask,
6554 reg, next_select));
6555 op1 = gen_lowpart_for_combine (op_mode,
6556 force_to_mode (XEXP (x, 1), mode, mask,
6557 reg, next_select));
6139ff20 6558
2dd484ed
RK
6559 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6560 MASK since OP1 might have been sign-extended but we never want
6561 to turn on extra bits, since combine might have previously relied
6562 on them being off. */
6563 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6564 && (INTVAL (op1) & mask) != 0)
6565 op1 = GEN_INT (INTVAL (op1) & mask);
6566
6139ff20
RK
6567 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6568 x = gen_binary (code, op_mode, op0, op1);
d0ab8cd3 6569 break;
dfbe1b2f
RK
6570
6571 case ASHIFT:
dfbe1b2f 6572 /* For left shifts, do the same, but just for the first operand.
f6785026
RK
6573 However, we cannot do anything with shifts where we cannot
6574 guarantee that the counts are smaller than the size of the mode
6575 because such a count will have a different meaning in a
6139ff20 6576 wider mode. */
f6785026
RK
6577
6578 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6139ff20 6579 && INTVAL (XEXP (x, 1)) >= 0
f6785026
RK
6580 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6581 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6582 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
adb7a1cb 6583 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
f6785026
RK
6584 break;
6585
6139ff20
RK
6586 /* If the shift count is a constant and we can do arithmetic in
6587 the mode of the shift, refine which bits we need. Otherwise, use the
6588 conservative form of the mask. */
6589 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6590 && INTVAL (XEXP (x, 1)) >= 0
6591 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6592 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6593 mask >>= INTVAL (XEXP (x, 1));
6594 else
6595 mask = fuller_mask;
6596
6597 op0 = gen_lowpart_for_combine (op_mode,
6598 force_to_mode (XEXP (x, 0), op_mode,
e3d616e3 6599 mask, reg, next_select));
6139ff20
RK
6600
6601 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6602 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
d0ab8cd3 6603 break;
dfbe1b2f
RK
6604
6605 case LSHIFTRT:
1347292b
JW
6606 /* Here we can only do something if the shift count is a constant,
6607 this shift constant is valid for the host, and we can do arithmetic
6608 in OP_MODE. */
dfbe1b2f
RK
6609
6610 if (GET_CODE (XEXP (x, 1)) == CONST_INT
1347292b 6611 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6139ff20 6612 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
d0ab8cd3 6613 {
6139ff20
RK
6614 rtx inner = XEXP (x, 0);
6615
6616 /* Select the mask of the bits we need for the shift operand. */
6617 mask <<= INTVAL (XEXP (x, 1));
d0ab8cd3 6618
6139ff20
RK
6619 /* We can only change the mode of the shift if we can do arithmetic
6620 in the mode of the shift and MASK is no wider than the width of
6621 OP_MODE. */
6622 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6623 || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
d0ab8cd3
RK
6624 op_mode = GET_MODE (x);
6625
e3d616e3 6626 inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6139ff20
RK
6627
6628 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6629 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
d0ab8cd3 6630 }
6139ff20
RK
6631
6632 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6633 shift and AND produces only copies of the sign bit (C2 is one less
6634 than a power of two), we can do this with just a shift. */
6635
6636 if (GET_CODE (x) == LSHIFTRT
6637 && GET_CODE (XEXP (x, 1)) == CONST_INT
6638 && ((INTVAL (XEXP (x, 1))
6639 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6640 >= GET_MODE_BITSIZE (GET_MODE (x)))
6641 && exact_log2 (mask + 1) >= 0
6642 && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6643 >= exact_log2 (mask + 1)))
6644 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6645 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6646 - exact_log2 (mask + 1)));
fae2db47
JW
6647
6648 goto shiftrt;
d0ab8cd3
RK
6649
6650 case ASHIFTRT:
6139ff20
RK
6651 /* If we are just looking for the sign bit, we don't need this shift at
6652 all, even if it has a variable count. */
9bf22b75 6653 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
e51712db 6654 && (mask == ((unsigned HOST_WIDE_INT) 1
9bf22b75 6655 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
e3d616e3 6656 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6139ff20
RK
6657
6658 /* If this is a shift by a constant, get a mask that contains those bits
6659 that are not copies of the sign bit. We then have two cases: If
6660 MASK only includes those bits, this can be a logical shift, which may
6661 allow simplifications. If MASK is a single-bit field not within
6662 those bits, we are requesting a copy of the sign bit and hence can
6663 shift the sign bit to the appropriate location. */
6664
6665 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6666 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6667 {
6668 int i = -1;
6669
b69960ac
RK
6670 /* If the considered data is wider then HOST_WIDE_INT, we can't
6671 represent a mask for all its bits in a single scalar.
6672 But we only care about the lower bits, so calculate these. */
6673
6a11342f 6674 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
b69960ac 6675 {
0f41302f 6676 nonzero = ~ (HOST_WIDE_INT) 0;
b69960ac
RK
6677
6678 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6679 is the number of bits a full-width mask would have set.
6680 We need only shift if these are fewer than nonzero can
6681 hold. If not, we must keep all bits set in nonzero. */
6682
6683 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6684 < HOST_BITS_PER_WIDE_INT)
6685 nonzero >>= INTVAL (XEXP (x, 1))
6686 + HOST_BITS_PER_WIDE_INT
6687 - GET_MODE_BITSIZE (GET_MODE (x)) ;
6688 }
6689 else
6690 {
6691 nonzero = GET_MODE_MASK (GET_MODE (x));
6692 nonzero >>= INTVAL (XEXP (x, 1));
6693 }
6139ff20
RK
6694
6695 if ((mask & ~ nonzero) == 0
6696 || (i = exact_log2 (mask)) >= 0)
6697 {
6698 x = simplify_shift_const
6699 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6700 i < 0 ? INTVAL (XEXP (x, 1))
6701 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6702
6703 if (GET_CODE (x) != ASHIFTRT)
e3d616e3 6704 return force_to_mode (x, mode, mask, reg, next_select);
6139ff20
RK
6705 }
6706 }
6707
6708 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
6709 even if the shift count isn't a constant. */
6710 if (mask == 1)
6711 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6712
fae2db47
JW
6713 shiftrt:
6714
6715 /* If this is a zero- or sign-extension operation that just affects bits
4c002f29
RK
6716 we don't care about, remove it. Be sure the call above returned
6717 something that is still a shift. */
d0ab8cd3 6718
4c002f29
RK
6719 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6720 && GET_CODE (XEXP (x, 1)) == CONST_INT
d0ab8cd3 6721 && INTVAL (XEXP (x, 1)) >= 0
6139ff20
RK
6722 && (INTVAL (XEXP (x, 1))
6723 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
d0ab8cd3
RK
6724 && GET_CODE (XEXP (x, 0)) == ASHIFT
6725 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6726 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
e3d616e3
RK
6727 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6728 reg, next_select);
6139ff20 6729
dfbe1b2f
RK
6730 break;
6731
6139ff20
RK
6732 case ROTATE:
6733 case ROTATERT:
6734 /* If the shift count is constant and we can do computations
6735 in the mode of X, compute where the bits we care about are.
6736 Otherwise, we can't do anything. Don't change the mode of
6737 the shift or propagate MODE into the shift, though. */
6738 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6739 && INTVAL (XEXP (x, 1)) >= 0)
6740 {
6741 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6742 GET_MODE (x), GEN_INT (mask),
6743 XEXP (x, 1));
7d171a1e 6744 if (temp && GET_CODE(temp) == CONST_INT)
6139ff20
RK
6745 SUBST (XEXP (x, 0),
6746 force_to_mode (XEXP (x, 0), GET_MODE (x),
e3d616e3 6747 INTVAL (temp), reg, next_select));
6139ff20
RK
6748 }
6749 break;
6750
dfbe1b2f 6751 case NEG:
180b8e4b
RK
6752 /* If we just want the low-order bit, the NEG isn't needed since it
6753 won't change the low-order bit. */
6754 if (mask == 1)
6755 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6756
6139ff20
RK
6757 /* We need any bits less significant than the most significant bit in
6758 MASK since carries from those bits will affect the bits we are
6759 interested in. */
6760 mask = fuller_mask;
6761 goto unop;
6762
dfbe1b2f 6763 case NOT:
6139ff20
RK
6764 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6765 same as the XOR case above. Ensure that the constant we form is not
6766 wider than the mode of X. */
6767
6768 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6769 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6770 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6771 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6772 < GET_MODE_BITSIZE (GET_MODE (x)))
6773 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6774 {
6775 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6776 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6777 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6778
e3d616e3 6779 return force_to_mode (x, mode, mask, reg, next_select);
6139ff20
RK
6780 }
6781
f82da7d2
JW
6782 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6783 use the full mask inside the NOT. */
6784 mask = fuller_mask;
6785
6139ff20 6786 unop:
e3d616e3
RK
6787 op0 = gen_lowpart_for_combine (op_mode,
6788 force_to_mode (XEXP (x, 0), mode, mask,
6789 reg, next_select));
6139ff20 6790 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
0c1c8ea6 6791 x = gen_unary (code, op_mode, op_mode, op0);
6139ff20
RK
6792 break;
6793
6794 case NE:
6795 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
3aceff0d 6796 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
1a6ec070 6797 which is equal to STORE_FLAG_VALUE. */
3aceff0d
RK
6798 if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6799 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
1a6ec070 6800 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
e3d616e3 6801 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6139ff20 6802
d0ab8cd3
RK
6803 break;
6804
6805 case IF_THEN_ELSE:
6806 /* We have no way of knowing if the IF_THEN_ELSE can itself be
6807 written in a narrower mode. We play it safe and do not do so. */
6808
6809 SUBST (XEXP (x, 1),
6810 gen_lowpart_for_combine (GET_MODE (x),
6811 force_to_mode (XEXP (x, 1), mode,
e3d616e3 6812 mask, reg, next_select)));
d0ab8cd3
RK
6813 SUBST (XEXP (x, 2),
6814 gen_lowpart_for_combine (GET_MODE (x),
6815 force_to_mode (XEXP (x, 2), mode,
e3d616e3 6816 mask, reg,next_select)));
d0ab8cd3 6817 break;
e9a25f70
JL
6818
6819 default:
6820 break;
dfbe1b2f
RK
6821 }
6822
d0ab8cd3 6823 /* Ensure we return a value of the proper mode. */
dfbe1b2f
RK
6824 return gen_lowpart_for_combine (mode, x);
6825}
6826\f
abe6e52f
RK
6827/* Return nonzero if X is an expression that has one of two values depending on
6828 whether some other value is zero or nonzero. In that case, we return the
6829 value that is being tested, *PTRUE is set to the value if the rtx being
6830 returned has a nonzero value, and *PFALSE is set to the other alternative.
6831
6832 If we return zero, we set *PTRUE and *PFALSE to X. */
6833
6834static rtx
6835if_then_else_cond (x, ptrue, pfalse)
6836 rtx x;
6837 rtx *ptrue, *pfalse;
6838{
6839 enum machine_mode mode = GET_MODE (x);
6840 enum rtx_code code = GET_CODE (x);
6841 int size = GET_MODE_BITSIZE (mode);
6842 rtx cond0, cond1, true0, true1, false0, false1;
6843 unsigned HOST_WIDE_INT nz;
6844
6845 /* If this is a unary operation whose operand has one of two values, apply
6846 our opcode to compute those values. */
6847 if (GET_RTX_CLASS (code) == '1'
6848 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6849 {
0c1c8ea6
RK
6850 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6851 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
abe6e52f
RK
6852 return cond0;
6853 }
6854
3a19aabc 6855 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
ddd5a7c1 6856 make can't possibly match and would suppress other optimizations. */
3a19aabc
RK
6857 else if (code == COMPARE)
6858 ;
6859
abe6e52f
RK
6860 /* If this is a binary operation, see if either side has only one of two
6861 values. If either one does or if both do and they are conditional on
6862 the same value, compute the new true and false values. */
6863 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6864 || GET_RTX_CLASS (code) == '<')
6865 {
6866 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6867 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6868
6869 if ((cond0 != 0 || cond1 != 0)
6870 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6871 {
987e845a
JW
6872 /* If if_then_else_cond returned zero, then true/false are the
6873 same rtl. We must copy one of them to prevent invalid rtl
6874 sharing. */
6875 if (cond0 == 0)
6876 true0 = copy_rtx (true0);
6877 else if (cond1 == 0)
6878 true1 = copy_rtx (true1);
6879
abe6e52f
RK
6880 *ptrue = gen_binary (code, mode, true0, true1);
6881 *pfalse = gen_binary (code, mode, false0, false1);
6882 return cond0 ? cond0 : cond1;
6883 }
9210df58 6884
9210df58 6885 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
0802d516
RK
6886 operands is zero when the other is non-zero, and vice-versa,
6887 and STORE_FLAG_VALUE is 1 or -1. */
9210df58 6888
0802d516
RK
6889 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6890 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9210df58
RK
6891 || code == UMAX)
6892 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6893 {
6894 rtx op0 = XEXP (XEXP (x, 0), 1);
6895 rtx op1 = XEXP (XEXP (x, 1), 1);
6896
6897 cond0 = XEXP (XEXP (x, 0), 0);
6898 cond1 = XEXP (XEXP (x, 1), 0);
6899
6900 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6901 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6902 && reversible_comparison_p (cond1)
6903 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6904 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6905 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6906 || ((swap_condition (GET_CODE (cond0))
6907 == reverse_condition (GET_CODE (cond1)))
6908 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6909 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6910 && ! side_effects_p (x))
6911 {
6912 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6913 *pfalse = gen_binary (MULT, mode,
6914 (code == MINUS
0c1c8ea6 6915 ? gen_unary (NEG, mode, mode, op1) : op1),
9210df58
RK
6916 const_true_rtx);
6917 return cond0;
6918 }
6919 }
6920
6921 /* Similarly for MULT, AND and UMIN, execpt that for these the result
6922 is always zero. */
0802d516
RK
6923 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6924 && (code == MULT || code == AND || code == UMIN)
9210df58
RK
6925 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6926 {
6927 cond0 = XEXP (XEXP (x, 0), 0);
6928 cond1 = XEXP (XEXP (x, 1), 0);
6929
6930 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6931 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6932 && reversible_comparison_p (cond1)
6933 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6934 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6935 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6936 || ((swap_condition (GET_CODE (cond0))
6937 == reverse_condition (GET_CODE (cond1)))
6938 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6939 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6940 && ! side_effects_p (x))
6941 {
6942 *ptrue = *pfalse = const0_rtx;
6943 return cond0;
6944 }
6945 }
abe6e52f
RK
6946 }
6947
6948 else if (code == IF_THEN_ELSE)
6949 {
6950 /* If we have IF_THEN_ELSE already, extract the condition and
6951 canonicalize it if it is NE or EQ. */
6952 cond0 = XEXP (x, 0);
6953 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6954 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6955 return XEXP (cond0, 0);
6956 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6957 {
6958 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6959 return XEXP (cond0, 0);
6960 }
6961 else
6962 return cond0;
6963 }
6964
6965 /* If X is a normal SUBREG with both inner and outer modes integral,
6966 we can narrow both the true and false values of the inner expression,
6967 if there is a condition. */
6968 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6969 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6970 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6971 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6972 &true0, &false0)))
6973 {
00244e6b
RK
6974 *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6975 *pfalse
6976 = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
abe6e52f 6977
abe6e52f
RK
6978 return cond0;
6979 }
6980
6981 /* If X is a constant, this isn't special and will cause confusions
6982 if we treat it as such. Likewise if it is equivalent to a constant. */
6983 else if (CONSTANT_P (x)
6984 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
6985 ;
6986
6987 /* If X is known to be either 0 or -1, those are the true and
6988 false values when testing X. */
6989 else if (num_sign_bit_copies (x, mode) == size)
6990 {
6991 *ptrue = constm1_rtx, *pfalse = const0_rtx;
6992 return x;
6993 }
6994
6995 /* Likewise for 0 or a single bit. */
6996 else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
6997 {
6998 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
6999 return x;
7000 }
7001
7002 /* Otherwise fail; show no condition with true and false values the same. */
7003 *ptrue = *pfalse = x;
7004 return 0;
7005}
7006\f
1a26b032
RK
7007/* Return the value of expression X given the fact that condition COND
7008 is known to be true when applied to REG as its first operand and VAL
7009 as its second. X is known to not be shared and so can be modified in
7010 place.
7011
7012 We only handle the simplest cases, and specifically those cases that
7013 arise with IF_THEN_ELSE expressions. */
7014
7015static rtx
7016known_cond (x, cond, reg, val)
7017 rtx x;
7018 enum rtx_code cond;
7019 rtx reg, val;
7020{
7021 enum rtx_code code = GET_CODE (x);
f24ad0e4 7022 rtx temp;
6f7d635c 7023 const char *fmt;
1a26b032
RK
7024 int i, j;
7025
7026 if (side_effects_p (x))
7027 return x;
7028
7029 if (cond == EQ && rtx_equal_p (x, reg))
7030 return val;
7031
7032 /* If X is (abs REG) and we know something about REG's relationship
7033 with zero, we may be able to simplify this. */
7034
7035 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7036 switch (cond)
7037 {
7038 case GE: case GT: case EQ:
7039 return XEXP (x, 0);
7040 case LT: case LE:
0c1c8ea6
RK
7041 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
7042 XEXP (x, 0));
e9a25f70
JL
7043 default:
7044 break;
1a26b032
RK
7045 }
7046
7047 /* The only other cases we handle are MIN, MAX, and comparisons if the
7048 operands are the same as REG and VAL. */
7049
7050 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7051 {
7052 if (rtx_equal_p (XEXP (x, 0), val))
7053 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7054
7055 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7056 {
7057 if (GET_RTX_CLASS (code) == '<')
7058 return (comparison_dominates_p (cond, code) ? const_true_rtx
7059 : (comparison_dominates_p (cond,
7060 reverse_condition (code))
7061 ? const0_rtx : x));
7062
7063 else if (code == SMAX || code == SMIN
7064 || code == UMIN || code == UMAX)
7065 {
7066 int unsignedp = (code == UMIN || code == UMAX);
7067
7068 if (code == SMAX || code == UMAX)
7069 cond = reverse_condition (cond);
7070
7071 switch (cond)
7072 {
7073 case GE: case GT:
7074 return unsignedp ? x : XEXP (x, 1);
7075 case LE: case LT:
7076 return unsignedp ? x : XEXP (x, 0);
7077 case GEU: case GTU:
7078 return unsignedp ? XEXP (x, 1) : x;
7079 case LEU: case LTU:
7080 return unsignedp ? XEXP (x, 0) : x;
e9a25f70
JL
7081 default:
7082 break;
1a26b032
RK
7083 }
7084 }
7085 }
7086 }
7087
7088 fmt = GET_RTX_FORMAT (code);
7089 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7090 {
7091 if (fmt[i] == 'e')
7092 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7093 else if (fmt[i] == 'E')
7094 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7095 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7096 cond, reg, val));
7097 }
7098
7099 return x;
7100}
7101\f
e11fa86f
RK
7102/* See if X and Y are equal for the purposes of seeing if we can rewrite an
7103 assignment as a field assignment. */
7104
7105static int
7106rtx_equal_for_field_assignment_p (x, y)
7107 rtx x;
7108 rtx y;
7109{
e11fa86f
RK
7110 if (x == y || rtx_equal_p (x, y))
7111 return 1;
7112
7113 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7114 return 0;
7115
7116 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7117 Note that all SUBREGs of MEM are paradoxical; otherwise they
7118 would have been rewritten. */
7119 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7120 && GET_CODE (SUBREG_REG (y)) == MEM
7121 && rtx_equal_p (SUBREG_REG (y),
7122 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7123 return 1;
7124
7125 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7126 && GET_CODE (SUBREG_REG (x)) == MEM
7127 && rtx_equal_p (SUBREG_REG (x),
7128 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7129 return 1;
7130
9ec36da5
JL
7131 /* We used to see if get_last_value of X and Y were the same but that's
7132 not correct. In one direction, we'll cause the assignment to have
7133 the wrong destination and in the case, we'll import a register into this
7134 insn that might have already have been dead. So fail if none of the
7135 above cases are true. */
7136 return 0;
e11fa86f
RK
7137}
7138\f
230d793d
RS
7139/* See if X, a SET operation, can be rewritten as a bit-field assignment.
7140 Return that assignment if so.
7141
7142 We only handle the most common cases. */
7143
7144static rtx
7145make_field_assignment (x)
7146 rtx x;
7147{
7148 rtx dest = SET_DEST (x);
7149 rtx src = SET_SRC (x);
dfbe1b2f 7150 rtx assign;
e11fa86f 7151 rtx rhs, lhs;
5f4f0e22
CH
7152 HOST_WIDE_INT c1;
7153 int pos, len;
dfbe1b2f
RK
7154 rtx other;
7155 enum machine_mode mode;
230d793d
RS
7156
7157 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7158 a clear of a one-bit field. We will have changed it to
7159 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7160 for a SUBREG. */
7161
7162 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7163 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7164 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
e11fa86f 7165 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
230d793d 7166 {
8999a12e 7167 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
230d793d 7168 1, 1, 1, 0);
76184def 7169 if (assign != 0)
38a448ca 7170 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
76184def 7171 return x;
230d793d
RS
7172 }
7173
7174 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7175 && subreg_lowpart_p (XEXP (src, 0))
7176 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7177 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7178 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7179 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
e11fa86f 7180 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
230d793d 7181 {
8999a12e 7182 assign = make_extraction (VOIDmode, dest, 0,
230d793d
RS
7183 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7184 1, 1, 1, 0);
76184def 7185 if (assign != 0)
38a448ca 7186 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
76184def 7187 return x;
230d793d
RS
7188 }
7189
9dd11dcb 7190 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
230d793d
RS
7191 one-bit field. */
7192 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7193 && XEXP (XEXP (src, 0), 0) == const1_rtx
e11fa86f 7194 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
230d793d 7195 {
8999a12e 7196 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
230d793d 7197 1, 1, 1, 0);
76184def 7198 if (assign != 0)
38a448ca 7199 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
76184def 7200 return x;
230d793d
RS
7201 }
7202
dfbe1b2f 7203 /* The other case we handle is assignments into a constant-position
9dd11dcb 7204 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
dfbe1b2f
RK
7205 a mask that has all one bits except for a group of zero bits and
7206 OTHER is known to have zeros where C1 has ones, this is such an
7207 assignment. Compute the position and length from C1. Shift OTHER
7208 to the appropriate position, force it to the required mode, and
7209 make the extraction. Check for the AND in both operands. */
7210
9dd11dcb 7211 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
e11fa86f
RK
7212 return x;
7213
7214 rhs = expand_compound_operation (XEXP (src, 0));
7215 lhs = expand_compound_operation (XEXP (src, 1));
7216
7217 if (GET_CODE (rhs) == AND
7218 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7219 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7220 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7221 else if (GET_CODE (lhs) == AND
7222 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7223 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7224 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
dfbe1b2f
RK
7225 else
7226 return x;
230d793d 7227
e11fa86f 7228 pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
dfbe1b2f 7229 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
e5e809f4
JL
7230 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7231 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
dfbe1b2f 7232 return x;
230d793d 7233
5f4f0e22 7234 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
76184def
DE
7235 if (assign == 0)
7236 return x;
230d793d 7237
dfbe1b2f
RK
7238 /* The mode to use for the source is the mode of the assignment, or of
7239 what is inside a possible STRICT_LOW_PART. */
7240 mode = (GET_CODE (assign) == STRICT_LOW_PART
7241 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
230d793d 7242
dfbe1b2f
RK
7243 /* Shift OTHER right POS places and make it the source, restricting it
7244 to the proper length and mode. */
230d793d 7245
5f4f0e22
CH
7246 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7247 GET_MODE (src), other, pos),
6139ff20
RK
7248 mode,
7249 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7250 ? GET_MODE_MASK (mode)
7251 : ((HOST_WIDE_INT) 1 << len) - 1,
e3d616e3 7252 dest, 0);
230d793d 7253
dfbe1b2f 7254 return gen_rtx_combine (SET, VOIDmode, assign, src);
230d793d
RS
7255}
7256\f
7257/* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7258 if so. */
7259
7260static rtx
7261apply_distributive_law (x)
7262 rtx x;
7263{
7264 enum rtx_code code = GET_CODE (x);
7265 rtx lhs, rhs, other;
7266 rtx tem;
7267 enum rtx_code inner_code;
7268
d8a8a4da
RS
7269 /* Distributivity is not true for floating point.
7270 It can change the value. So don't do it.
7271 -- rms and moshier@world.std.com. */
3ad2180a 7272 if (FLOAT_MODE_P (GET_MODE (x)))
d8a8a4da
RS
7273 return x;
7274
230d793d
RS
7275 /* The outer operation can only be one of the following: */
7276 if (code != IOR && code != AND && code != XOR
7277 && code != PLUS && code != MINUS)
7278 return x;
7279
7280 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7281
0f41302f
MS
7282 /* If either operand is a primitive we can't do anything, so get out
7283 fast. */
230d793d 7284 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
dfbe1b2f 7285 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
230d793d
RS
7286 return x;
7287
7288 lhs = expand_compound_operation (lhs);
7289 rhs = expand_compound_operation (rhs);
7290 inner_code = GET_CODE (lhs);
7291 if (inner_code != GET_CODE (rhs))
7292 return x;
7293
7294 /* See if the inner and outer operations distribute. */
7295 switch (inner_code)
7296 {
7297 case LSHIFTRT:
7298 case ASHIFTRT:
7299 case AND:
7300 case IOR:
7301 /* These all distribute except over PLUS. */
7302 if (code == PLUS || code == MINUS)
7303 return x;
7304 break;
7305
7306 case MULT:
7307 if (code != PLUS && code != MINUS)
7308 return x;
7309 break;
7310
7311 case ASHIFT:
45620ed4 7312 /* This is also a multiply, so it distributes over everything. */
230d793d
RS
7313 break;
7314
7315 case SUBREG:
dfbe1b2f
RK
7316 /* Non-paradoxical SUBREGs distributes over all operations, provided
7317 the inner modes and word numbers are the same, this is an extraction
2b4bd1bc
JW
7318 of a low-order part, we don't convert an fp operation to int or
7319 vice versa, and we would not be converting a single-word
dfbe1b2f 7320 operation into a multi-word operation. The latter test is not
2b4bd1bc 7321 required, but it prevents generating unneeded multi-word operations.
dfbe1b2f
RK
7322 Some of the previous tests are redundant given the latter test, but
7323 are retained because they are required for correctness.
7324
7325 We produce the result slightly differently in this case. */
7326
7327 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7328 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7329 || ! subreg_lowpart_p (lhs)
2b4bd1bc
JW
7330 || (GET_MODE_CLASS (GET_MODE (lhs))
7331 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
dfbe1b2f 7332 || (GET_MODE_SIZE (GET_MODE (lhs))
8af24e26 7333 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
dfbe1b2f 7334 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
230d793d
RS
7335 return x;
7336
7337 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7338 SUBREG_REG (lhs), SUBREG_REG (rhs));
7339 return gen_lowpart_for_combine (GET_MODE (x), tem);
7340
7341 default:
7342 return x;
7343 }
7344
7345 /* Set LHS and RHS to the inner operands (A and B in the example
7346 above) and set OTHER to the common operand (C in the example).
7347 These is only one way to do this unless the inner operation is
7348 commutative. */
7349 if (GET_RTX_CLASS (inner_code) == 'c'
7350 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7351 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7352 else if (GET_RTX_CLASS (inner_code) == 'c'
7353 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7354 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7355 else if (GET_RTX_CLASS (inner_code) == 'c'
7356 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7357 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7358 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7359 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7360 else
7361 return x;
7362
7363 /* Form the new inner operation, seeing if it simplifies first. */
7364 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7365
7366 /* There is one exception to the general way of distributing:
7367 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7368 if (code == XOR && inner_code == IOR)
7369 {
7370 inner_code = AND;
0c1c8ea6 7371 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
230d793d
RS
7372 }
7373
7374 /* We may be able to continuing distributing the result, so call
7375 ourselves recursively on the inner operation before forming the
7376 outer operation, which we return. */
7377 return gen_binary (inner_code, GET_MODE (x),
7378 apply_distributive_law (tem), other);
7379}
7380\f
7381/* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7382 in MODE.
7383
7384 Return an equivalent form, if different from X. Otherwise, return X. If
7385 X is zero, we are to always construct the equivalent form. */
7386
7387static rtx
7388simplify_and_const_int (x, mode, varop, constop)
7389 rtx x;
7390 enum machine_mode mode;
7391 rtx varop;
5f4f0e22 7392 unsigned HOST_WIDE_INT constop;
230d793d 7393{
951553af 7394 unsigned HOST_WIDE_INT nonzero;
42301240 7395 int i;
230d793d 7396
6139ff20
RK
7397 /* Simplify VAROP knowing that we will be only looking at some of the
7398 bits in it. */
e3d616e3 7399 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
230d793d 7400
6139ff20
RK
7401 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7402 CONST_INT, we are done. */
7403 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7404 return varop;
230d793d 7405
fc06d7aa
RK
7406 /* See what bits may be nonzero in VAROP. Unlike the general case of
7407 a call to nonzero_bits, here we don't care about bits outside
7408 MODE. */
7409
7410 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7e4ce834 7411 nonzero = trunc_int_for_mode (nonzero, mode);
9fa6d012 7412
230d793d 7413 /* Turn off all bits in the constant that are known to already be zero.
951553af 7414 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
230d793d
RS
7415 which is tested below. */
7416
951553af 7417 constop &= nonzero;
230d793d
RS
7418
7419 /* If we don't have any bits left, return zero. */
7420 if (constop == 0)
7421 return const0_rtx;
7422
42301240
RK
7423 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7424 a power of two, we can replace this with a ASHIFT. */
7425 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7426 && (i = exact_log2 (constop)) >= 0)
7427 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7428
6139ff20
RK
7429 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7430 or XOR, then try to apply the distributive law. This may eliminate
7431 operations if either branch can be simplified because of the AND.
7432 It may also make some cases more complex, but those cases probably
7433 won't match a pattern either with or without this. */
7434
7435 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7436 return
7437 gen_lowpart_for_combine
7438 (mode,
7439 apply_distributive_law
7440 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7441 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7442 XEXP (varop, 0), constop),
7443 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7444 XEXP (varop, 1), constop))));
7445
230d793d
RS
7446 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7447 if we already had one (just check for the simplest cases). */
7448 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7449 && GET_MODE (XEXP (x, 0)) == mode
7450 && SUBREG_REG (XEXP (x, 0)) == varop)
7451 varop = XEXP (x, 0);
7452 else
7453 varop = gen_lowpart_for_combine (mode, varop);
7454
0f41302f 7455 /* If we can't make the SUBREG, try to return what we were given. */
230d793d
RS
7456 if (GET_CODE (varop) == CLOBBER)
7457 return x ? x : varop;
7458
7459 /* If we are only masking insignificant bits, return VAROP. */
951553af 7460 if (constop == nonzero)
230d793d
RS
7461 x = varop;
7462
7463 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7464 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
6139ff20 7465 x = gen_binary (AND, mode, varop, GEN_INT (constop));
230d793d
RS
7466
7467 else
7468 {
7469 if (GET_CODE (XEXP (x, 1)) != CONST_INT
e51712db 7470 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
5f4f0e22 7471 SUBST (XEXP (x, 1), GEN_INT (constop));
230d793d
RS
7472
7473 SUBST (XEXP (x, 0), varop);
7474 }
7475
7476 return x;
7477}
7478\f
b3728b0e
JW
7479/* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7480 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7481 is less useful. We can't allow both, because that results in exponential
956d6950 7482 run time recursion. There is a nullstone testcase that triggered
b3728b0e
JW
7483 this. This macro avoids accidental uses of num_sign_bit_copies. */
7484#define num_sign_bit_copies()
7485
230d793d
RS
7486/* Given an expression, X, compute which bits in X can be non-zero.
7487 We don't care about bits outside of those defined in MODE.
7488
7489 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7490 a shift, AND, or zero_extract, we can do better. */
7491
5f4f0e22 7492static unsigned HOST_WIDE_INT
951553af 7493nonzero_bits (x, mode)
230d793d
RS
7494 rtx x;
7495 enum machine_mode mode;
7496{
951553af
RK
7497 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7498 unsigned HOST_WIDE_INT inner_nz;
230d793d
RS
7499 enum rtx_code code;
7500 int mode_width = GET_MODE_BITSIZE (mode);
7501 rtx tem;
7502
1c75dfa4
RK
7503 /* For floating-point values, assume all bits are needed. */
7504 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7505 return nonzero;
7506
230d793d
RS
7507 /* If X is wider than MODE, use its mode instead. */
7508 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7509 {
7510 mode = GET_MODE (x);
951553af 7511 nonzero = GET_MODE_MASK (mode);
230d793d
RS
7512 mode_width = GET_MODE_BITSIZE (mode);
7513 }
7514
5f4f0e22 7515 if (mode_width > HOST_BITS_PER_WIDE_INT)
230d793d
RS
7516 /* Our only callers in this case look for single bit values. So
7517 just return the mode mask. Those tests will then be false. */
951553af 7518 return nonzero;
230d793d 7519
8baf60bb 7520#ifndef WORD_REGISTER_OPERATIONS
c6965c0f 7521 /* If MODE is wider than X, but both are a single word for both the host
0840fd91
RK
7522 and target machines, we can compute this from which bits of the
7523 object might be nonzero in its own mode, taking into account the fact
7524 that on many CISC machines, accessing an object in a wider mode
7525 causes the high-order bits to become undefined. So they are
7526 not known to be zero. */
7527
7528 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7529 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7530 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
c6965c0f 7531 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
0840fd91
RK
7532 {
7533 nonzero &= nonzero_bits (x, GET_MODE (x));
7534 nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7535 return nonzero;
7536 }
7537#endif
7538
230d793d
RS
7539 code = GET_CODE (x);
7540 switch (code)
7541 {
7542 case REG:
320dd7a7
RK
7543#ifdef POINTERS_EXTEND_UNSIGNED
7544 /* If pointers extend unsigned and this is a pointer in Pmode, say that
7545 all the bits above ptr_mode are known to be zero. */
7546 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7547 && REGNO_POINTER_FLAG (REGNO (x)))
7548 nonzero &= GET_MODE_MASK (ptr_mode);
7549#endif
7550
b0d71df9
RK
7551#ifdef STACK_BOUNDARY
7552 /* If this is the stack pointer, we may know something about its
7553 alignment. If PUSH_ROUNDING is defined, it is possible for the
230d793d
RS
7554 stack to be momentarily aligned only to that amount, so we pick
7555 the least alignment. */
7556
ee49a9c7
JW
7557 /* We can't check for arg_pointer_rtx here, because it is not
7558 guaranteed to have as much alignment as the stack pointer.
7559 In particular, in the Irix6 n64 ABI, the stack has 128 bit
7560 alignment but the argument pointer has only 64 bit alignment. */
7561
0e9ff885
DM
7562 if ((x == frame_pointer_rtx
7563 || x == stack_pointer_rtx
7564 || x == hard_frame_pointer_rtx
7565 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7566 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7567#ifdef STACK_BIAS
7568 && !STACK_BIAS
7569#endif
7570 )
230d793d 7571 {
b0d71df9 7572 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
230d793d
RS
7573
7574#ifdef PUSH_ROUNDING
91102d5a 7575 if (REGNO (x) == STACK_POINTER_REGNUM)
b0d71df9 7576 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
230d793d
RS
7577#endif
7578
320dd7a7
RK
7579 /* We must return here, otherwise we may get a worse result from
7580 one of the choices below. There is nothing useful below as
7581 far as the stack pointer is concerned. */
b0d71df9 7582 return nonzero &= ~ (sp_alignment - 1);
230d793d 7583 }
b0d71df9 7584#endif
230d793d 7585
55310dad
RK
7586 /* If X is a register whose nonzero bits value is current, use it.
7587 Otherwise, if X is a register whose value we can find, use that
7588 value. Otherwise, use the previously-computed global nonzero bits
7589 for this register. */
7590
7591 if (reg_last_set_value[REGNO (x)] != 0
7592 && reg_last_set_mode[REGNO (x)] == mode
57cf50a4
GRK
7593 && (reg_last_set_label[REGNO (x)] == label_tick
7594 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
7595 && REG_N_SETS (REGNO (x)) == 1
7596 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
7597 REGNO (x))))
55310dad
RK
7598 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7599 return reg_last_set_nonzero_bits[REGNO (x)];
230d793d
RS
7600
7601 tem = get_last_value (x);
9afa3d54 7602
230d793d 7603 if (tem)
9afa3d54
RK
7604 {
7605#ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7606 /* If X is narrower than MODE and TEM is a non-negative
7607 constant that would appear negative in the mode of X,
7608 sign-extend it for use in reg_nonzero_bits because some
7609 machines (maybe most) will actually do the sign-extension
7610 and this is the conservative approach.
7611
7612 ??? For 2.5, try to tighten up the MD files in this regard
7613 instead of this kludge. */
7614
7615 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7616 && GET_CODE (tem) == CONST_INT
7617 && INTVAL (tem) > 0
7618 && 0 != (INTVAL (tem)
7619 & ((HOST_WIDE_INT) 1
9e69be8c 7620 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9afa3d54
RK
7621 tem = GEN_INT (INTVAL (tem)
7622 | ((HOST_WIDE_INT) (-1)
7623 << GET_MODE_BITSIZE (GET_MODE (x))));
7624#endif
7625 return nonzero_bits (tem, mode);
7626 }
951553af
RK
7627 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7628 return reg_nonzero_bits[REGNO (x)] & nonzero;
230d793d 7629 else
951553af 7630 return nonzero;
230d793d
RS
7631
7632 case CONST_INT:
9afa3d54
RK
7633#ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7634 /* If X is negative in MODE, sign-extend the value. */
9e69be8c
RK
7635 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7636 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7637 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
9afa3d54
RK
7638#endif
7639
230d793d
RS
7640 return INTVAL (x);
7641
230d793d 7642 case MEM:
8baf60bb 7643#ifdef LOAD_EXTEND_OP
230d793d
RS
7644 /* In many, if not most, RISC machines, reading a byte from memory
7645 zeros the rest of the register. Noticing that fact saves a lot
7646 of extra zero-extends. */
8baf60bb
RK
7647 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7648 nonzero &= GET_MODE_MASK (GET_MODE (x));
230d793d 7649#endif
8baf60bb 7650 break;
230d793d 7651
230d793d
RS
7652 case EQ: case NE:
7653 case GT: case GTU:
7654 case LT: case LTU:
7655 case GE: case GEU:
7656 case LE: case LEU:
3f508eca 7657
c6965c0f
RK
7658 /* If this produces an integer result, we know which bits are set.
7659 Code here used to clear bits outside the mode of X, but that is
7660 now done above. */
230d793d 7661
c6965c0f
RK
7662 if (GET_MODE_CLASS (mode) == MODE_INT
7663 && mode_width <= HOST_BITS_PER_WIDE_INT)
7664 nonzero = STORE_FLAG_VALUE;
230d793d 7665 break;
230d793d 7666
230d793d 7667 case NEG:
b3728b0e
JW
7668#if 0
7669 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7670 and num_sign_bit_copies. */
d0ab8cd3
RK
7671 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7672 == GET_MODE_BITSIZE (GET_MODE (x)))
951553af 7673 nonzero = 1;
b3728b0e 7674#endif
230d793d
RS
7675
7676 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
951553af 7677 nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
230d793d 7678 break;
d0ab8cd3
RK
7679
7680 case ABS:
b3728b0e
JW
7681#if 0
7682 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7683 and num_sign_bit_copies. */
d0ab8cd3
RK
7684 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7685 == GET_MODE_BITSIZE (GET_MODE (x)))
951553af 7686 nonzero = 1;
b3728b0e 7687#endif
d0ab8cd3 7688 break;
230d793d
RS
7689
7690 case TRUNCATE:
951553af 7691 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
230d793d
RS
7692 break;
7693
7694 case ZERO_EXTEND:
951553af 7695 nonzero &= nonzero_bits (XEXP (x, 0), mode);
230d793d 7696 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
951553af 7697 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
230d793d
RS
7698 break;
7699
7700 case SIGN_EXTEND:
7701 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7702 Otherwise, show all the bits in the outer mode but not the inner
7703 may be non-zero. */
951553af 7704 inner_nz = nonzero_bits (XEXP (x, 0), mode);
230d793d
RS
7705 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7706 {
951553af 7707 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
e3da301d
MS
7708 if (inner_nz
7709 & (((HOST_WIDE_INT) 1
7710 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
951553af 7711 inner_nz |= (GET_MODE_MASK (mode)
230d793d
RS
7712 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7713 }
7714
951553af 7715 nonzero &= inner_nz;
230d793d
RS
7716 break;
7717
7718 case AND:
951553af
RK
7719 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7720 & nonzero_bits (XEXP (x, 1), mode));
230d793d
RS
7721 break;
7722
d0ab8cd3
RK
7723 case XOR: case IOR:
7724 case UMIN: case UMAX: case SMIN: case SMAX:
951553af
RK
7725 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7726 | nonzero_bits (XEXP (x, 1), mode));
230d793d
RS
7727 break;
7728
7729 case PLUS: case MINUS:
7730 case MULT:
7731 case DIV: case UDIV:
7732 case MOD: case UMOD:
7733 /* We can apply the rules of arithmetic to compute the number of
7734 high- and low-order zero bits of these operations. We start by
7735 computing the width (position of the highest-order non-zero bit)
7736 and the number of low-order zero bits for each value. */
7737 {
951553af
RK
7738 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7739 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7740 int width0 = floor_log2 (nz0) + 1;
7741 int width1 = floor_log2 (nz1) + 1;
7742 int low0 = floor_log2 (nz0 & -nz0);
7743 int low1 = floor_log2 (nz1 & -nz1);
318b149c
RK
7744 HOST_WIDE_INT op0_maybe_minusp
7745 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7746 HOST_WIDE_INT op1_maybe_minusp
7747 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
230d793d
RS
7748 int result_width = mode_width;
7749 int result_low = 0;
7750
7751 switch (code)
7752 {
7753 case PLUS:
0e9ff885
DM
7754#ifdef STACK_BIAS
7755 if (STACK_BIAS
7756 && (XEXP (x, 0) == stack_pointer_rtx
7757 || XEXP (x, 0) == frame_pointer_rtx)
7758 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7759 {
7760 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7761
7762 nz0 = (GET_MODE_MASK (mode) & ~ (sp_alignment - 1));
7763 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
7764 width0 = floor_log2 (nz0) + 1;
7765 width1 = floor_log2 (nz1) + 1;
7766 low0 = floor_log2 (nz0 & -nz0);
7767 low1 = floor_log2 (nz1 & -nz1);
7768 }
7769#endif
230d793d
RS
7770 result_width = MAX (width0, width1) + 1;
7771 result_low = MIN (low0, low1);
7772 break;
7773 case MINUS:
7774 result_low = MIN (low0, low1);
7775 break;
7776 case MULT:
7777 result_width = width0 + width1;
7778 result_low = low0 + low1;
7779 break;
7780 case DIV:
7781 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7782 result_width = width0;
7783 break;
7784 case UDIV:
7785 result_width = width0;
7786 break;
7787 case MOD:
7788 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7789 result_width = MIN (width0, width1);
7790 result_low = MIN (low0, low1);
7791 break;
7792 case UMOD:
7793 result_width = MIN (width0, width1);
7794 result_low = MIN (low0, low1);
7795 break;
e9a25f70
JL
7796 default:
7797 abort ();
230d793d
RS
7798 }
7799
7800 if (result_width < mode_width)
951553af 7801 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
230d793d
RS
7802
7803 if (result_low > 0)
951553af 7804 nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
230d793d
RS
7805 }
7806 break;
7807
7808 case ZERO_EXTRACT:
7809 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5f4f0e22 7810 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
951553af 7811 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
230d793d
RS
7812 break;
7813
7814 case SUBREG:
c3c2cb37
RK
7815 /* If this is a SUBREG formed for a promoted variable that has
7816 been zero-extended, we know that at least the high-order bits
7817 are zero, though others might be too. */
7818
7819 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
951553af
RK
7820 nonzero = (GET_MODE_MASK (GET_MODE (x))
7821 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
c3c2cb37 7822
230d793d
RS
7823 /* If the inner mode is a single word for both the host and target
7824 machines, we can compute this from which bits of the inner
951553af 7825 object might be nonzero. */
230d793d 7826 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
5f4f0e22
CH
7827 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7828 <= HOST_BITS_PER_WIDE_INT))
230d793d 7829 {
951553af 7830 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
8baf60bb 7831
b52ce03d
R
7832#if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
7833 /* If this is a typical RISC machine, we only have to worry
7834 about the way loads are extended. */
7835 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
7836 ? (nonzero
7837 & (1L << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1)))
7838 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
230d793d 7839#endif
b52ce03d
R
7840 {
7841 /* On many CISC machines, accessing an object in a wider mode
7842 causes the high-order bits to become undefined. So they are
7843 not known to be zero. */
7844 if (GET_MODE_SIZE (GET_MODE (x))
7845 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7846 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7847 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7848 }
230d793d
RS
7849 }
7850 break;
7851
7852 case ASHIFTRT:
7853 case LSHIFTRT:
7854 case ASHIFT:
230d793d 7855 case ROTATE:
951553af 7856 /* The nonzero bits are in two classes: any bits within MODE
230d793d 7857 that aren't in GET_MODE (x) are always significant. The rest of the
951553af 7858 nonzero bits are those that are significant in the operand of
230d793d
RS
7859 the shift when shifted the appropriate number of bits. This
7860 shows that high-order bits are cleared by the right shift and
7861 low-order bits by left shifts. */
7862 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7863 && INTVAL (XEXP (x, 1)) >= 0
5f4f0e22 7864 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
230d793d
RS
7865 {
7866 enum machine_mode inner_mode = GET_MODE (x);
7867 int width = GET_MODE_BITSIZE (inner_mode);
7868 int count = INTVAL (XEXP (x, 1));
5f4f0e22 7869 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
951553af
RK
7870 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7871 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
5f4f0e22 7872 unsigned HOST_WIDE_INT outer = 0;
230d793d
RS
7873
7874 if (mode_width > width)
951553af 7875 outer = (op_nonzero & nonzero & ~ mode_mask);
230d793d
RS
7876
7877 if (code == LSHIFTRT)
7878 inner >>= count;
7879 else if (code == ASHIFTRT)
7880 {
7881 inner >>= count;
7882
951553af 7883 /* If the sign bit may have been nonzero before the shift, we
230d793d 7884 need to mark all the places it could have been copied to
951553af 7885 by the shift as possibly nonzero. */
5f4f0e22
CH
7886 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7887 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
230d793d 7888 }
45620ed4 7889 else if (code == ASHIFT)
230d793d
RS
7890 inner <<= count;
7891 else
7892 inner = ((inner << (count % width)
7893 | (inner >> (width - (count % width)))) & mode_mask);
7894
951553af 7895 nonzero &= (outer | inner);
230d793d
RS
7896 }
7897 break;
7898
7899 case FFS:
7900 /* This is at most the number of bits in the mode. */
951553af 7901 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
230d793d 7902 break;
d0ab8cd3
RK
7903
7904 case IF_THEN_ELSE:
951553af
RK
7905 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7906 | nonzero_bits (XEXP (x, 2), mode));
d0ab8cd3 7907 break;
e9a25f70
JL
7908
7909 default:
7910 break;
230d793d
RS
7911 }
7912
951553af 7913 return nonzero;
230d793d 7914}
b3728b0e
JW
7915
7916/* See the macro definition above. */
7917#undef num_sign_bit_copies
230d793d 7918\f
d0ab8cd3 7919/* Return the number of bits at the high-order end of X that are known to
5109d49f
RK
7920 be equal to the sign bit. X will be used in mode MODE; if MODE is
7921 VOIDmode, X will be used in its own mode. The returned value will always
7922 be between 1 and the number of bits in MODE. */
d0ab8cd3
RK
7923
7924static int
7925num_sign_bit_copies (x, mode)
7926 rtx x;
7927 enum machine_mode mode;
7928{
7929 enum rtx_code code = GET_CODE (x);
7930 int bitwidth;
7931 int num0, num1, result;
951553af 7932 unsigned HOST_WIDE_INT nonzero;
d0ab8cd3
RK
7933 rtx tem;
7934
7935 /* If we weren't given a mode, use the mode of X. If the mode is still
1c75dfa4
RK
7936 VOIDmode, we don't know anything. Likewise if one of the modes is
7937 floating-point. */
d0ab8cd3
RK
7938
7939 if (mode == VOIDmode)
7940 mode = GET_MODE (x);
7941
1c75dfa4 7942 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
6752e8d2 7943 return 1;
d0ab8cd3
RK
7944
7945 bitwidth = GET_MODE_BITSIZE (mode);
7946
0f41302f 7947 /* For a smaller object, just ignore the high bits. */
312def2e
RK
7948 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7949 return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7950 - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7951
e9a25f70
JL
7952 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7953 {
0c314d1a
RK
7954#ifndef WORD_REGISTER_OPERATIONS
7955 /* If this machine does not do all register operations on the entire
7956 register and MODE is wider than the mode of X, we can say nothing
7957 at all about the high-order bits. */
e9a25f70
JL
7958 return 1;
7959#else
7960 /* Likewise on machines that do, if the mode of the object is smaller
7961 than a word and loads of that size don't sign extend, we can say
7962 nothing about the high order bits. */
7963 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
7964#ifdef LOAD_EXTEND_OP
7965 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
7966#endif
7967 )
7968 return 1;
0c314d1a 7969#endif
e9a25f70 7970 }
0c314d1a 7971
d0ab8cd3
RK
7972 switch (code)
7973 {
7974 case REG:
55310dad 7975
ff0dbdd1
RK
7976#ifdef POINTERS_EXTEND_UNSIGNED
7977 /* If pointers extend signed and this is a pointer in Pmode, say that
7978 all the bits above ptr_mode are known to be sign bit copies. */
7979 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7980 && REGNO_POINTER_FLAG (REGNO (x)))
7981 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7982#endif
7983
55310dad
RK
7984 if (reg_last_set_value[REGNO (x)] != 0
7985 && reg_last_set_mode[REGNO (x)] == mode
57cf50a4
GRK
7986 && (reg_last_set_label[REGNO (x)] == label_tick
7987 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
7988 && REG_N_SETS (REGNO (x)) == 1
7989 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
7990 REGNO (x))))
55310dad
RK
7991 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7992 return reg_last_set_sign_bit_copies[REGNO (x)];
d0ab8cd3
RK
7993
7994 tem = get_last_value (x);
7995 if (tem != 0)
7996 return num_sign_bit_copies (tem, mode);
55310dad
RK
7997
7998 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
7999 return reg_sign_bit_copies[REGNO (x)];
d0ab8cd3
RK
8000 break;
8001
457816e2 8002 case MEM:
8baf60bb 8003#ifdef LOAD_EXTEND_OP
457816e2 8004 /* Some RISC machines sign-extend all loads of smaller than a word. */
8baf60bb
RK
8005 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8006 return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
457816e2 8007#endif
8baf60bb 8008 break;
457816e2 8009
d0ab8cd3
RK
8010 case CONST_INT:
8011 /* If the constant is negative, take its 1's complement and remask.
8012 Then see how many zero bits we have. */
951553af 8013 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
ac49a949 8014 if (bitwidth <= HOST_BITS_PER_WIDE_INT
951553af
RK
8015 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8016 nonzero = (~ nonzero) & GET_MODE_MASK (mode);
d0ab8cd3 8017
951553af 8018 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
d0ab8cd3
RK
8019
8020 case SUBREG:
c3c2cb37
RK
8021 /* If this is a SUBREG for a promoted object that is sign-extended
8022 and we are looking at it in a wider mode, we know that at least the
8023 high-order bits are known to be sign bit copies. */
8024
8025 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
dc3e17ad
RK
8026 return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8027 num_sign_bit_copies (SUBREG_REG (x), mode));
c3c2cb37 8028
0f41302f 8029 /* For a smaller object, just ignore the high bits. */
d0ab8cd3
RK
8030 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8031 {
8032 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
8033 return MAX (1, (num0
8034 - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8035 - bitwidth)));
8036 }
457816e2 8037
8baf60bb 8038#ifdef WORD_REGISTER_OPERATIONS
2aec5b7a 8039#ifdef LOAD_EXTEND_OP
8baf60bb
RK
8040 /* For paradoxical SUBREGs on machines where all register operations
8041 affect the entire register, just look inside. Note that we are
8042 passing MODE to the recursive call, so the number of sign bit copies
8043 will remain relative to that mode, not the inner mode. */
457816e2 8044
2aec5b7a
JW
8045 /* This works only if loads sign extend. Otherwise, if we get a
8046 reload for the inner part, it may be loaded from the stack, and
8047 then we lose all sign bit copies that existed before the store
8048 to the stack. */
8049
8050 if ((GET_MODE_SIZE (GET_MODE (x))
8051 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8052 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
457816e2 8053 return num_sign_bit_copies (SUBREG_REG (x), mode);
2aec5b7a 8054#endif
457816e2 8055#endif
d0ab8cd3
RK
8056 break;
8057
8058 case SIGN_EXTRACT:
8059 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8060 return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
8061 break;
8062
8063 case SIGN_EXTEND:
8064 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8065 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
8066
8067 case TRUNCATE:
0f41302f 8068 /* For a smaller object, just ignore the high bits. */
d0ab8cd3
RK
8069 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
8070 return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8071 - bitwidth)));
8072
8073 case NOT:
8074 return num_sign_bit_copies (XEXP (x, 0), mode);
8075
8076 case ROTATE: case ROTATERT:
8077 /* If we are rotating left by a number of bits less than the number
8078 of sign bit copies, we can just subtract that amount from the
8079 number. */
8080 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8081 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
8082 {
8083 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8084 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8085 : bitwidth - INTVAL (XEXP (x, 1))));
8086 }
8087 break;
8088
8089 case NEG:
8090 /* In general, this subtracts one sign bit copy. But if the value
8091 is known to be positive, the number of sign bit copies is the
951553af
RK
8092 same as that of the input. Finally, if the input has just one bit
8093 that might be nonzero, all the bits are copies of the sign bit. */
70186b34
BS
8094 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8095 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8096 return num0 > 1 ? num0 - 1 : 1;
8097
951553af
RK
8098 nonzero = nonzero_bits (XEXP (x, 0), mode);
8099 if (nonzero == 1)
d0ab8cd3
RK
8100 return bitwidth;
8101
d0ab8cd3 8102 if (num0 > 1
951553af 8103 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
d0ab8cd3
RK
8104 num0--;
8105
8106 return num0;
8107
8108 case IOR: case AND: case XOR:
8109 case SMIN: case SMAX: case UMIN: case UMAX:
8110 /* Logical operations will preserve the number of sign-bit copies.
8111 MIN and MAX operations always return one of the operands. */
8112 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8113 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8114 return MIN (num0, num1);
8115
8116 case PLUS: case MINUS:
8117 /* For addition and subtraction, we can have a 1-bit carry. However,
8118 if we are subtracting 1 from a positive number, there will not
8119 be such a carry. Furthermore, if the positive number is known to
8120 be 0 or 1, we know the result is either -1 or 0. */
8121
3e3ea975 8122 if (code == PLUS && XEXP (x, 1) == constm1_rtx
9295e6af 8123 && bitwidth <= HOST_BITS_PER_WIDE_INT)
d0ab8cd3 8124 {
951553af
RK
8125 nonzero = nonzero_bits (XEXP (x, 0), mode);
8126 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8127 return (nonzero == 1 || nonzero == 0 ? bitwidth
8128 : bitwidth - floor_log2 (nonzero) - 1);
d0ab8cd3
RK
8129 }
8130
8131 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8132 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8133 return MAX (1, MIN (num0, num1) - 1);
8134
8135 case MULT:
8136 /* The number of bits of the product is the sum of the number of
8137 bits of both terms. However, unless one of the terms if known
8138 to be positive, we must allow for an additional bit since negating
8139 a negative number can remove one sign bit copy. */
8140
8141 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8142 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8143
8144 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8145 if (result > 0
70186b34
BS
8146 && (bitwidth > HOST_BITS_PER_WIDE_INT
8147 || (((nonzero_bits (XEXP (x, 0), mode)
8148 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8149 && ((nonzero_bits (XEXP (x, 1), mode)
8150 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
d0ab8cd3
RK
8151 result--;
8152
8153 return MAX (1, result);
8154
8155 case UDIV:
70186b34
BS
8156 /* The result must be <= the first operand. If the first operand
8157 has the high bit set, we know nothing about the number of sign
8158 bit copies. */
8159 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8160 return 1;
8161 else if ((nonzero_bits (XEXP (x, 0), mode)
8162 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8163 return 1;
8164 else
8165 return num_sign_bit_copies (XEXP (x, 0), mode);
8166
d0ab8cd3
RK
8167 case UMOD:
8168 /* The result must be <= the scond operand. */
8169 return num_sign_bit_copies (XEXP (x, 1), mode);
8170
8171 case DIV:
8172 /* Similar to unsigned division, except that we have to worry about
8173 the case where the divisor is negative, in which case we have
8174 to add 1. */
8175 result = num_sign_bit_copies (XEXP (x, 0), mode);
8176 if (result > 1
70186b34
BS
8177 && (bitwidth > HOST_BITS_PER_WIDE_INT
8178 || (nonzero_bits (XEXP (x, 1), mode)
8179 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8180 result--;
d0ab8cd3
RK
8181
8182 return result;
8183
8184 case MOD:
8185 result = num_sign_bit_copies (XEXP (x, 1), mode);
8186 if (result > 1
70186b34
BS
8187 && (bitwidth > HOST_BITS_PER_WIDE_INT
8188 || (nonzero_bits (XEXP (x, 1), mode)
8189 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8190 result--;
d0ab8cd3
RK
8191
8192 return result;
8193
8194 case ASHIFTRT:
8195 /* Shifts by a constant add to the number of bits equal to the
8196 sign bit. */
8197 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8198 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8199 && INTVAL (XEXP (x, 1)) > 0)
8200 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8201
8202 return num0;
8203
8204 case ASHIFT:
d0ab8cd3
RK
8205 /* Left shifts destroy copies. */
8206 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8207 || INTVAL (XEXP (x, 1)) < 0
8208 || INTVAL (XEXP (x, 1)) >= bitwidth)
8209 return 1;
8210
8211 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8212 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8213
8214 case IF_THEN_ELSE:
8215 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8216 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8217 return MIN (num0, num1);
8218
d0ab8cd3
RK
8219 case EQ: case NE: case GE: case GT: case LE: case LT:
8220 case GEU: case GTU: case LEU: case LTU:
0802d516
RK
8221 if (STORE_FLAG_VALUE == -1)
8222 return bitwidth;
e9a25f70
JL
8223 break;
8224
8225 default:
8226 break;
d0ab8cd3
RK
8227 }
8228
8229 /* If we haven't been able to figure it out by one of the above rules,
8230 see if some of the high-order bits are known to be zero. If so,
ac49a949
RS
8231 count those bits and return one less than that amount. If we can't
8232 safely compute the mask for this mode, always return BITWIDTH. */
8233
8234 if (bitwidth > HOST_BITS_PER_WIDE_INT)
6752e8d2 8235 return 1;
d0ab8cd3 8236
951553af 8237 nonzero = nonzero_bits (x, mode);
df6f4086 8238 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
951553af 8239 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
d0ab8cd3
RK
8240}
8241\f
1a26b032
RK
8242/* Return the number of "extended" bits there are in X, when interpreted
8243 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8244 unsigned quantities, this is the number of high-order zero bits.
8245 For signed quantities, this is the number of copies of the sign bit
8246 minus 1. In both case, this function returns the number of "spare"
8247 bits. For example, if two quantities for which this function returns
8248 at least 1 are added, the addition is known not to overflow.
8249
8250 This function will always return 0 unless called during combine, which
8251 implies that it must be called from a define_split. */
8252
8253int
8254extended_count (x, mode, unsignedp)
8255 rtx x;
8256 enum machine_mode mode;
8257 int unsignedp;
8258{
951553af 8259 if (nonzero_sign_valid == 0)
1a26b032
RK
8260 return 0;
8261
8262 return (unsignedp
ac49a949
RS
8263 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8264 && (GET_MODE_BITSIZE (mode) - 1
951553af 8265 - floor_log2 (nonzero_bits (x, mode))))
1a26b032
RK
8266 : num_sign_bit_copies (x, mode) - 1);
8267}
8268\f
230d793d
RS
8269/* This function is called from `simplify_shift_const' to merge two
8270 outer operations. Specifically, we have already found that we need
8271 to perform operation *POP0 with constant *PCONST0 at the outermost
8272 position. We would now like to also perform OP1 with constant CONST1
8273 (with *POP0 being done last).
8274
8275 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8276 the resulting operation. *PCOMP_P is set to 1 if we would need to
8277 complement the innermost operand, otherwise it is unchanged.
8278
8279 MODE is the mode in which the operation will be done. No bits outside
8280 the width of this mode matter. It is assumed that the width of this mode
5f4f0e22 8281 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
230d793d
RS
8282
8283 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8284 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8285 result is simply *PCONST0.
8286
8287 If the resulting operation cannot be expressed as one operation, we
8288 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8289
8290static int
8291merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8292 enum rtx_code *pop0;
5f4f0e22 8293 HOST_WIDE_INT *pconst0;
230d793d 8294 enum rtx_code op1;
5f4f0e22 8295 HOST_WIDE_INT const1;
230d793d
RS
8296 enum machine_mode mode;
8297 int *pcomp_p;
8298{
8299 enum rtx_code op0 = *pop0;
5f4f0e22 8300 HOST_WIDE_INT const0 = *pconst0;
230d793d
RS
8301
8302 const0 &= GET_MODE_MASK (mode);
8303 const1 &= GET_MODE_MASK (mode);
8304
8305 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8306 if (op0 == AND)
8307 const1 &= const0;
8308
8309 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8310 if OP0 is SET. */
8311
8312 if (op1 == NIL || op0 == SET)
8313 return 1;
8314
8315 else if (op0 == NIL)
8316 op0 = op1, const0 = const1;
8317
8318 else if (op0 == op1)
8319 {
8320 switch (op0)
8321 {
8322 case AND:
8323 const0 &= const1;
8324 break;
8325 case IOR:
8326 const0 |= const1;
8327 break;
8328 case XOR:
8329 const0 ^= const1;
8330 break;
8331 case PLUS:
8332 const0 += const1;
8333 break;
8334 case NEG:
8335 op0 = NIL;
8336 break;
e9a25f70
JL
8337 default:
8338 break;
230d793d
RS
8339 }
8340 }
8341
8342 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8343 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8344 return 0;
8345
8346 /* If the two constants aren't the same, we can't do anything. The
8347 remaining six cases can all be done. */
8348 else if (const0 != const1)
8349 return 0;
8350
8351 else
8352 switch (op0)
8353 {
8354 case IOR:
8355 if (op1 == AND)
8356 /* (a & b) | b == b */
8357 op0 = SET;
8358 else /* op1 == XOR */
8359 /* (a ^ b) | b == a | b */
b729186a 8360 {;}
230d793d
RS
8361 break;
8362
8363 case XOR:
8364 if (op1 == AND)
8365 /* (a & b) ^ b == (~a) & b */
8366 op0 = AND, *pcomp_p = 1;
8367 else /* op1 == IOR */
8368 /* (a | b) ^ b == a & ~b */
8369 op0 = AND, *pconst0 = ~ const0;
8370 break;
8371
8372 case AND:
8373 if (op1 == IOR)
8374 /* (a | b) & b == b */
8375 op0 = SET;
8376 else /* op1 == XOR */
8377 /* (a ^ b) & b) == (~a) & b */
8378 *pcomp_p = 1;
8379 break;
e9a25f70
JL
8380 default:
8381 break;
230d793d
RS
8382 }
8383
8384 /* Check for NO-OP cases. */
8385 const0 &= GET_MODE_MASK (mode);
8386 if (const0 == 0
8387 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8388 op0 = NIL;
8389 else if (const0 == 0 && op0 == AND)
8390 op0 = SET;
e51712db
KG
8391 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8392 && op0 == AND)
230d793d
RS
8393 op0 = NIL;
8394
7e4ce834
RH
8395 /* ??? Slightly redundant with the above mask, but not entirely.
8396 Moving this above means we'd have to sign-extend the mode mask
8397 for the final test. */
8398 const0 = trunc_int_for_mode (const0, mode);
9fa6d012 8399
230d793d
RS
8400 *pop0 = op0;
8401 *pconst0 = const0;
8402
8403 return 1;
8404}
8405\f
8406/* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8407 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8408 that we started with.
8409
8410 The shift is normally computed in the widest mode we find in VAROP, as
8411 long as it isn't a different number of words than RESULT_MODE. Exceptions
8412 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8413
8414static rtx
8415simplify_shift_const (x, code, result_mode, varop, count)
8416 rtx x;
8417 enum rtx_code code;
8418 enum machine_mode result_mode;
8419 rtx varop;
8420 int count;
8421{
8422 enum rtx_code orig_code = code;
8423 int orig_count = count;
8424 enum machine_mode mode = result_mode;
8425 enum machine_mode shift_mode, tmode;
8426 int mode_words
8427 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8428 /* We form (outer_op (code varop count) (outer_const)). */
8429 enum rtx_code outer_op = NIL;
c4e861e8 8430 HOST_WIDE_INT outer_const = 0;
230d793d
RS
8431 rtx const_rtx;
8432 int complement_p = 0;
8433 rtx new;
8434
8435 /* If we were given an invalid count, don't do anything except exactly
8436 what was requested. */
8437
8438 if (count < 0 || count > GET_MODE_BITSIZE (mode))
8439 {
8440 if (x)
8441 return x;
8442
38a448ca 8443 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (count));
230d793d
RS
8444 }
8445
8446 /* Unless one of the branches of the `if' in this loop does a `continue',
8447 we will `break' the loop after the `if'. */
8448
8449 while (count != 0)
8450 {
8451 /* If we have an operand of (clobber (const_int 0)), just return that
8452 value. */
8453 if (GET_CODE (varop) == CLOBBER)
8454 return varop;
8455
8456 /* If we discovered we had to complement VAROP, leave. Making a NOT
8457 here would cause an infinite loop. */
8458 if (complement_p)
8459 break;
8460
abc95ed3 8461 /* Convert ROTATERT to ROTATE. */
230d793d
RS
8462 if (code == ROTATERT)
8463 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8464
230d793d 8465 /* We need to determine what mode we will do the shift in. If the
f6789c77
RK
8466 shift is a right shift or a ROTATE, we must always do it in the mode
8467 it was originally done in. Otherwise, we can do it in MODE, the
0f41302f 8468 widest mode encountered. */
f6789c77
RK
8469 shift_mode
8470 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8471 ? result_mode : mode);
230d793d
RS
8472
8473 /* Handle cases where the count is greater than the size of the mode
8474 minus 1. For ASHIFT, use the size minus one as the count (this can
8475 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8476 take the count modulo the size. For other shifts, the result is
8477 zero.
8478
8479 Since these shifts are being produced by the compiler by combining
8480 multiple operations, each of which are defined, we know what the
8481 result is supposed to be. */
8482
8483 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8484 {
8485 if (code == ASHIFTRT)
8486 count = GET_MODE_BITSIZE (shift_mode) - 1;
8487 else if (code == ROTATE || code == ROTATERT)
8488 count %= GET_MODE_BITSIZE (shift_mode);
8489 else
8490 {
8491 /* We can't simply return zero because there may be an
8492 outer op. */
8493 varop = const0_rtx;
8494 count = 0;
8495 break;
8496 }
8497 }
8498
8499 /* Negative counts are invalid and should not have been made (a
8500 programmer-specified negative count should have been handled
0f41302f 8501 above). */
230d793d
RS
8502 else if (count < 0)
8503 abort ();
8504
312def2e
RK
8505 /* An arithmetic right shift of a quantity known to be -1 or 0
8506 is a no-op. */
8507 if (code == ASHIFTRT
8508 && (num_sign_bit_copies (varop, shift_mode)
8509 == GET_MODE_BITSIZE (shift_mode)))
d0ab8cd3 8510 {
312def2e
RK
8511 count = 0;
8512 break;
8513 }
d0ab8cd3 8514
312def2e
RK
8515 /* If we are doing an arithmetic right shift and discarding all but
8516 the sign bit copies, this is equivalent to doing a shift by the
8517 bitsize minus one. Convert it into that shift because it will often
8518 allow other simplifications. */
500c518b 8519
312def2e
RK
8520 if (code == ASHIFTRT
8521 && (count + num_sign_bit_copies (varop, shift_mode)
8522 >= GET_MODE_BITSIZE (shift_mode)))
8523 count = GET_MODE_BITSIZE (shift_mode) - 1;
500c518b 8524
230d793d
RS
8525 /* We simplify the tests below and elsewhere by converting
8526 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8527 `make_compound_operation' will convert it to a ASHIFTRT for
8528 those machines (such as Vax) that don't have a LSHIFTRT. */
5f4f0e22 8529 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
230d793d 8530 && code == ASHIFTRT
951553af 8531 && ((nonzero_bits (varop, shift_mode)
5f4f0e22
CH
8532 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8533 == 0))
230d793d
RS
8534 code = LSHIFTRT;
8535
8536 switch (GET_CODE (varop))
8537 {
8538 case SIGN_EXTEND:
8539 case ZERO_EXTEND:
8540 case SIGN_EXTRACT:
8541 case ZERO_EXTRACT:
8542 new = expand_compound_operation (varop);
8543 if (new != varop)
8544 {
8545 varop = new;
8546 continue;
8547 }
8548 break;
8549
8550 case MEM:
8551 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8552 minus the width of a smaller mode, we can do this with a
8553 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8554 if ((code == ASHIFTRT || code == LSHIFTRT)
8555 && ! mode_dependent_address_p (XEXP (varop, 0))
8556 && ! MEM_VOLATILE_P (varop)
8557 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8558 MODE_INT, 1)) != BLKmode)
8559 {
f76b9db2 8560 if (BYTES_BIG_ENDIAN)
38a448ca 8561 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
f76b9db2 8562 else
38a448ca
RH
8563 new = gen_rtx_MEM (tmode,
8564 plus_constant (XEXP (varop, 0),
8565 count / BITS_PER_UNIT));
e24b00c8 8566 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
c6df88cb 8567 MEM_COPY_ATTRIBUTES (new, varop);
230d793d
RS
8568 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8569 : ZERO_EXTEND, mode, new);
8570 count = 0;
8571 continue;
8572 }
8573 break;
8574
8575 case USE:
8576 /* Similar to the case above, except that we can only do this if
8577 the resulting mode is the same as that of the underlying
8578 MEM and adjust the address depending on the *bits* endianness
8579 because of the way that bit-field extract insns are defined. */
8580 if ((code == ASHIFTRT || code == LSHIFTRT)
8581 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8582 MODE_INT, 1)) != BLKmode
8583 && tmode == GET_MODE (XEXP (varop, 0)))
8584 {
f76b9db2
ILT
8585 if (BITS_BIG_ENDIAN)
8586 new = XEXP (varop, 0);
8587 else
8588 {
8589 new = copy_rtx (XEXP (varop, 0));
8590 SUBST (XEXP (new, 0),
8591 plus_constant (XEXP (new, 0),
8592 count / BITS_PER_UNIT));
8593 }
230d793d
RS
8594
8595 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8596 : ZERO_EXTEND, mode, new);
8597 count = 0;
8598 continue;
8599 }
8600 break;
8601
8602 case SUBREG:
8603 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8604 the same number of words as what we've seen so far. Then store
8605 the widest mode in MODE. */
f9e67232
RS
8606 if (subreg_lowpart_p (varop)
8607 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8608 > GET_MODE_SIZE (GET_MODE (varop)))
230d793d
RS
8609 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8610 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8611 == mode_words))
8612 {
8613 varop = SUBREG_REG (varop);
8614 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8615 mode = GET_MODE (varop);
8616 continue;
8617 }
8618 break;
8619
8620 case MULT:
8621 /* Some machines use MULT instead of ASHIFT because MULT
8622 is cheaper. But it is still better on those machines to
8623 merge two shifts into one. */
8624 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8625 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8626 {
8627 varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
6d649d26 8628 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
230d793d
RS
8629 continue;
8630 }
8631 break;
8632
8633 case UDIV:
8634 /* Similar, for when divides are cheaper. */
8635 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8636 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8637 {
8638 varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
5f4f0e22 8639 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
230d793d
RS
8640 continue;
8641 }
8642 break;
8643
8644 case ASHIFTRT:
8645 /* If we are extracting just the sign bit of an arithmetic right
8646 shift, that shift is not needed. */
8647 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8648 {
8649 varop = XEXP (varop, 0);
8650 continue;
8651 }
8652
0f41302f 8653 /* ... fall through ... */
230d793d
RS
8654
8655 case LSHIFTRT:
8656 case ASHIFT:
230d793d
RS
8657 case ROTATE:
8658 /* Here we have two nested shifts. The result is usually the
8659 AND of a new shift with a mask. We compute the result below. */
8660 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8661 && INTVAL (XEXP (varop, 1)) >= 0
8662 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
5f4f0e22
CH
8663 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8664 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
230d793d
RS
8665 {
8666 enum rtx_code first_code = GET_CODE (varop);
8667 int first_count = INTVAL (XEXP (varop, 1));
5f4f0e22 8668 unsigned HOST_WIDE_INT mask;
230d793d 8669 rtx mask_rtx;
230d793d 8670
230d793d
RS
8671 /* We have one common special case. We can't do any merging if
8672 the inner code is an ASHIFTRT of a smaller mode. However, if
8673 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8674 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8675 we can convert it to
8676 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8677 This simplifies certain SIGN_EXTEND operations. */
8678 if (code == ASHIFT && first_code == ASHIFTRT
8679 && (GET_MODE_BITSIZE (result_mode)
8680 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8681 {
8682 /* C3 has the low-order C1 bits zero. */
8683
5f4f0e22
CH
8684 mask = (GET_MODE_MASK (mode)
8685 & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
230d793d 8686
5f4f0e22 8687 varop = simplify_and_const_int (NULL_RTX, result_mode,
230d793d 8688 XEXP (varop, 0), mask);
5f4f0e22 8689 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
230d793d
RS
8690 varop, count);
8691 count = first_count;
8692 code = ASHIFTRT;
8693 continue;
8694 }
8695
d0ab8cd3
RK
8696 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8697 than C1 high-order bits equal to the sign bit, we can convert
8698 this to either an ASHIFT or a ASHIFTRT depending on the
8699 two counts.
230d793d
RS
8700
8701 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8702
8703 if (code == ASHIFTRT && first_code == ASHIFT
8704 && GET_MODE (varop) == shift_mode
d0ab8cd3
RK
8705 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8706 > first_count))
230d793d 8707 {
d0ab8cd3
RK
8708 count -= first_count;
8709 if (count < 0)
8710 count = - count, code = ASHIFT;
8711 varop = XEXP (varop, 0);
8712 continue;
230d793d
RS
8713 }
8714
8715 /* There are some cases we can't do. If CODE is ASHIFTRT,
8716 we can only do this if FIRST_CODE is also ASHIFTRT.
8717
8718 We can't do the case when CODE is ROTATE and FIRST_CODE is
8719 ASHIFTRT.
8720
8721 If the mode of this shift is not the mode of the outer shift,
bdaae9a0 8722 we can't do this if either shift is a right shift or ROTATE.
230d793d
RS
8723
8724 Finally, we can't do any of these if the mode is too wide
8725 unless the codes are the same.
8726
8727 Handle the case where the shift codes are the same
8728 first. */
8729
8730 if (code == first_code)
8731 {
8732 if (GET_MODE (varop) != result_mode
bdaae9a0
RK
8733 && (code == ASHIFTRT || code == LSHIFTRT
8734 || code == ROTATE))
230d793d
RS
8735 break;
8736
8737 count += first_count;
8738 varop = XEXP (varop, 0);
8739 continue;
8740 }
8741
8742 if (code == ASHIFTRT
8743 || (code == ROTATE && first_code == ASHIFTRT)
5f4f0e22 8744 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
230d793d 8745 || (GET_MODE (varop) != result_mode
bdaae9a0
RK
8746 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8747 || first_code == ROTATE
230d793d
RS
8748 || code == ROTATE)))
8749 break;
8750
8751 /* To compute the mask to apply after the shift, shift the
951553af 8752 nonzero bits of the inner shift the same way the
230d793d
RS
8753 outer shift will. */
8754
951553af 8755 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
230d793d
RS
8756
8757 mask_rtx
8758 = simplify_binary_operation (code, result_mode, mask_rtx,
5f4f0e22 8759 GEN_INT (count));
230d793d
RS
8760
8761 /* Give up if we can't compute an outer operation to use. */
8762 if (mask_rtx == 0
8763 || GET_CODE (mask_rtx) != CONST_INT
8764 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8765 INTVAL (mask_rtx),
8766 result_mode, &complement_p))
8767 break;
8768
8769 /* If the shifts are in the same direction, we add the
8770 counts. Otherwise, we subtract them. */
8771 if ((code == ASHIFTRT || code == LSHIFTRT)
8772 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8773 count += first_count;
8774 else
8775 count -= first_count;
8776
8777 /* If COUNT is positive, the new shift is usually CODE,
8778 except for the two exceptions below, in which case it is
8779 FIRST_CODE. If the count is negative, FIRST_CODE should
8780 always be used */
8781 if (count > 0
8782 && ((first_code == ROTATE && code == ASHIFT)
8783 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8784 code = first_code;
8785 else if (count < 0)
8786 code = first_code, count = - count;
8787
8788 varop = XEXP (varop, 0);
8789 continue;
8790 }
8791
8792 /* If we have (A << B << C) for any shift, we can convert this to
8793 (A << C << B). This wins if A is a constant. Only try this if
8794 B is not a constant. */
8795
8796 else if (GET_CODE (varop) == code
8797 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8798 && 0 != (new
8799 = simplify_binary_operation (code, mode,
8800 XEXP (varop, 0),
5f4f0e22 8801 GEN_INT (count))))
230d793d
RS
8802 {
8803 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8804 count = 0;
8805 continue;
8806 }
8807 break;
8808
8809 case NOT:
8810 /* Make this fit the case below. */
8811 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
5f4f0e22 8812 GEN_INT (GET_MODE_MASK (mode)));
230d793d
RS
8813 continue;
8814
8815 case IOR:
8816 case AND:
8817 case XOR:
8818 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8819 with C the size of VAROP - 1 and the shift is logical if
8820 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8821 we have an (le X 0) operation. If we have an arithmetic shift
8822 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8823 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8824
8825 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8826 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8827 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8828 && (code == LSHIFTRT || code == ASHIFTRT)
8829 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8830 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8831 {
8832 count = 0;
8833 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8834 const0_rtx);
8835
8836 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8837 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8838
8839 continue;
8840 }
8841
8842 /* If we have (shift (logical)), move the logical to the outside
8843 to allow it to possibly combine with another logical and the
8844 shift to combine with another shift. This also canonicalizes to
8845 what a ZERO_EXTRACT looks like. Also, some machines have
8846 (and (shift)) insns. */
8847
8848 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8849 && (new = simplify_binary_operation (code, result_mode,
8850 XEXP (varop, 1),
5f4f0e22 8851 GEN_INT (count))) != 0
7d171a1e 8852 && GET_CODE(new) == CONST_INT
230d793d
RS
8853 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8854 INTVAL (new), result_mode, &complement_p))
8855 {
8856 varop = XEXP (varop, 0);
8857 continue;
8858 }
8859
8860 /* If we can't do that, try to simplify the shift in each arm of the
8861 logical expression, make a new logical expression, and apply
8862 the inverse distributive law. */
8863 {
00d4ca1c 8864 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
230d793d 8865 XEXP (varop, 0), count);
00d4ca1c 8866 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
230d793d
RS
8867 XEXP (varop, 1), count);
8868
21a64bf1 8869 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
230d793d
RS
8870 varop = apply_distributive_law (varop);
8871
8872 count = 0;
8873 }
8874 break;
8875
8876 case EQ:
45620ed4 8877 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
230d793d 8878 says that the sign bit can be tested, FOO has mode MODE, C is
45620ed4
RK
8879 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8880 that may be nonzero. */
8881 if (code == LSHIFTRT
230d793d
RS
8882 && XEXP (varop, 1) == const0_rtx
8883 && GET_MODE (XEXP (varop, 0)) == result_mode
8884 && count == GET_MODE_BITSIZE (result_mode) - 1
5f4f0e22 8885 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
230d793d 8886 && ((STORE_FLAG_VALUE
5f4f0e22 8887 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
951553af 8888 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
5f4f0e22
CH
8889 && merge_outer_ops (&outer_op, &outer_const, XOR,
8890 (HOST_WIDE_INT) 1, result_mode,
8891 &complement_p))
230d793d
RS
8892 {
8893 varop = XEXP (varop, 0);
8894 count = 0;
8895 continue;
8896 }
8897 break;
8898
8899 case NEG:
d0ab8cd3
RK
8900 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8901 than the number of bits in the mode is equivalent to A. */
8902 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
951553af 8903 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
230d793d 8904 {
d0ab8cd3 8905 varop = XEXP (varop, 0);
230d793d
RS
8906 count = 0;
8907 continue;
8908 }
8909
8910 /* NEG commutes with ASHIFT since it is multiplication. Move the
8911 NEG outside to allow shifts to combine. */
8912 if (code == ASHIFT
5f4f0e22
CH
8913 && merge_outer_ops (&outer_op, &outer_const, NEG,
8914 (HOST_WIDE_INT) 0, result_mode,
8915 &complement_p))
230d793d
RS
8916 {
8917 varop = XEXP (varop, 0);
8918 continue;
8919 }
8920 break;
8921
8922 case PLUS:
d0ab8cd3
RK
8923 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8924 is one less than the number of bits in the mode is
8925 equivalent to (xor A 1). */
230d793d
RS
8926 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8927 && XEXP (varop, 1) == constm1_rtx
951553af 8928 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
5f4f0e22
CH
8929 && merge_outer_ops (&outer_op, &outer_const, XOR,
8930 (HOST_WIDE_INT) 1, result_mode,
8931 &complement_p))
230d793d
RS
8932 {
8933 count = 0;
8934 varop = XEXP (varop, 0);
8935 continue;
8936 }
8937
3f508eca 8938 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
951553af 8939 that might be nonzero in BAR are those being shifted out and those
3f508eca
RK
8940 bits are known zero in FOO, we can replace the PLUS with FOO.
8941 Similarly in the other operand order. This code occurs when
8942 we are computing the size of a variable-size array. */
8943
8944 if ((code == ASHIFTRT || code == LSHIFTRT)
5f4f0e22 8945 && count < HOST_BITS_PER_WIDE_INT
951553af
RK
8946 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8947 && (nonzero_bits (XEXP (varop, 1), result_mode)
8948 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
3f508eca
RK
8949 {
8950 varop = XEXP (varop, 0);
8951 continue;
8952 }
8953 else if ((code == ASHIFTRT || code == LSHIFTRT)
5f4f0e22 8954 && count < HOST_BITS_PER_WIDE_INT
ac49a949 8955 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
951553af 8956 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
3f508eca 8957 >> count)
951553af
RK
8958 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8959 & nonzero_bits (XEXP (varop, 1),
3f508eca
RK
8960 result_mode)))
8961 {
8962 varop = XEXP (varop, 1);
8963 continue;
8964 }
8965
230d793d
RS
8966 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
8967 if (code == ASHIFT
8968 && GET_CODE (XEXP (varop, 1)) == CONST_INT
8969 && (new = simplify_binary_operation (ASHIFT, result_mode,
8970 XEXP (varop, 1),
5f4f0e22 8971 GEN_INT (count))) != 0
7d171a1e 8972 && GET_CODE(new) == CONST_INT
230d793d
RS
8973 && merge_outer_ops (&outer_op, &outer_const, PLUS,
8974 INTVAL (new), result_mode, &complement_p))
8975 {
8976 varop = XEXP (varop, 0);
8977 continue;
8978 }
8979 break;
8980
8981 case MINUS:
8982 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8983 with C the size of VAROP - 1 and the shift is logical if
8984 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8985 we have a (gt X 0) operation. If the shift is arithmetic with
8986 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8987 we have a (neg (gt X 0)) operation. */
8988
0802d516
RK
8989 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8990 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
230d793d 8991 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
230d793d
RS
8992 && (code == LSHIFTRT || code == ASHIFTRT)
8993 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8994 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
8995 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8996 {
8997 count = 0;
8998 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
8999 const0_rtx);
9000
9001 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9002 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
9003
9004 continue;
9005 }
9006 break;
6e0ef100
JC
9007
9008 case TRUNCATE:
9009 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9010 if the truncate does not affect the value. */
9011 if (code == LSHIFTRT
9012 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9013 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9014 && (INTVAL (XEXP (XEXP (varop, 0), 1))
b577a8ff
JL
9015 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9016 - GET_MODE_BITSIZE (GET_MODE (varop)))))
6e0ef100
JC
9017 {
9018 rtx varop_inner = XEXP (varop, 0);
9019
9020 varop_inner = gen_rtx_combine (LSHIFTRT,
9021 GET_MODE (varop_inner),
9022 XEXP (varop_inner, 0),
9023 GEN_INT (count + INTVAL (XEXP (varop_inner, 1))));
9024 varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
9025 varop_inner);
9026 count = 0;
9027 continue;
9028 }
9029 break;
e9a25f70
JL
9030
9031 default:
9032 break;
230d793d
RS
9033 }
9034
9035 break;
9036 }
9037
9038 /* We need to determine what mode to do the shift in. If the shift is
f6789c77
RK
9039 a right shift or ROTATE, we must always do it in the mode it was
9040 originally done in. Otherwise, we can do it in MODE, the widest mode
9041 encountered. The code we care about is that of the shift that will
9042 actually be done, not the shift that was originally requested. */
9043 shift_mode
9044 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9045 ? result_mode : mode);
230d793d
RS
9046
9047 /* We have now finished analyzing the shift. The result should be
9048 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9049 OUTER_OP is non-NIL, it is an operation that needs to be applied
9050 to the result of the shift. OUTER_CONST is the relevant constant,
9051 but we must turn off all bits turned off in the shift.
9052
9053 If we were passed a value for X, see if we can use any pieces of
9054 it. If not, make new rtx. */
9055
9056 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9057 && GET_CODE (XEXP (x, 1)) == CONST_INT
9058 && INTVAL (XEXP (x, 1)) == count)
9059 const_rtx = XEXP (x, 1);
9060 else
5f4f0e22 9061 const_rtx = GEN_INT (count);
230d793d
RS
9062
9063 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9064 && GET_MODE (XEXP (x, 0)) == shift_mode
9065 && SUBREG_REG (XEXP (x, 0)) == varop)
9066 varop = XEXP (x, 0);
9067 else if (GET_MODE (varop) != shift_mode)
9068 varop = gen_lowpart_for_combine (shift_mode, varop);
9069
0f41302f 9070 /* If we can't make the SUBREG, try to return what we were given. */
230d793d
RS
9071 if (GET_CODE (varop) == CLOBBER)
9072 return x ? x : varop;
9073
9074 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9075 if (new != 0)
9076 x = new;
9077 else
9078 {
9079 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
9080 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
9081
9082 SUBST (XEXP (x, 0), varop);
9083 SUBST (XEXP (x, 1), const_rtx);
9084 }
9085
224eeff2
RK
9086 /* If we have an outer operation and we just made a shift, it is
9087 possible that we could have simplified the shift were it not
9088 for the outer operation. So try to do the simplification
9089 recursively. */
9090
9091 if (outer_op != NIL && GET_CODE (x) == code
9092 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9093 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9094 INTVAL (XEXP (x, 1)));
9095
230d793d
RS
9096 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
9097 turn off all the bits that the shift would have turned off. */
9098 if (orig_code == LSHIFTRT && result_mode != shift_mode)
5f4f0e22 9099 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
230d793d
RS
9100 GET_MODE_MASK (result_mode) >> orig_count);
9101
9102 /* Do the remainder of the processing in RESULT_MODE. */
9103 x = gen_lowpart_for_combine (result_mode, x);
9104
9105 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9106 operation. */
9107 if (complement_p)
0c1c8ea6 9108 x = gen_unary (NOT, result_mode, result_mode, x);
230d793d
RS
9109
9110 if (outer_op != NIL)
9111 {
5f4f0e22 9112 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
7e4ce834 9113 outer_const = trunc_int_for_mode (outer_const, result_mode);
230d793d
RS
9114
9115 if (outer_op == AND)
5f4f0e22 9116 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
230d793d
RS
9117 else if (outer_op == SET)
9118 /* This means that we have determined that the result is
9119 equivalent to a constant. This should be rare. */
5f4f0e22 9120 x = GEN_INT (outer_const);
230d793d 9121 else if (GET_RTX_CLASS (outer_op) == '1')
0c1c8ea6 9122 x = gen_unary (outer_op, result_mode, result_mode, x);
230d793d 9123 else
5f4f0e22 9124 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
230d793d
RS
9125 }
9126
9127 return x;
9128}
9129\f
9130/* Like recog, but we receive the address of a pointer to a new pattern.
9131 We try to match the rtx that the pointer points to.
9132 If that fails, we may try to modify or replace the pattern,
9133 storing the replacement into the same pointer object.
9134
9135 Modifications include deletion or addition of CLOBBERs.
9136
9137 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9138 the CLOBBERs are placed.
9139
9140 The value is the final insn code from the pattern ultimately matched,
9141 or -1. */
9142
9143static int
8e2f6e35 9144recog_for_combine (pnewpat, insn, pnotes)
230d793d
RS
9145 rtx *pnewpat;
9146 rtx insn;
9147 rtx *pnotes;
9148{
9149 register rtx pat = *pnewpat;
9150 int insn_code_number;
9151 int num_clobbers_to_add = 0;
9152 int i;
9153 rtx notes = 0;
9154
974f4146
RK
9155 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9156 we use to indicate that something didn't match. If we find such a
9157 thing, force rejection. */
d96023cf 9158 if (GET_CODE (pat) == PARALLEL)
974f4146 9159 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
d96023cf
RK
9160 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9161 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
974f4146
RK
9162 return -1;
9163
230d793d
RS
9164 /* Is the result of combination a valid instruction? */
9165 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9166
9167 /* If it isn't, there is the possibility that we previously had an insn
9168 that clobbered some register as a side effect, but the combined
9169 insn doesn't need to do that. So try once more without the clobbers
9170 unless this represents an ASM insn. */
9171
9172 if (insn_code_number < 0 && ! check_asm_operands (pat)
9173 && GET_CODE (pat) == PARALLEL)
9174 {
9175 int pos;
9176
9177 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9178 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9179 {
9180 if (i != pos)
9181 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9182 pos++;
9183 }
9184
9185 SUBST_INT (XVECLEN (pat, 0), pos);
9186
9187 if (pos == 1)
9188 pat = XVECEXP (pat, 0, 0);
9189
9190 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9191 }
9192
9193 /* If we had any clobbers to add, make a new pattern than contains
9194 them. Then check to make sure that all of them are dead. */
9195 if (num_clobbers_to_add)
9196 {
38a448ca
RH
9197 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9198 gen_rtvec (GET_CODE (pat) == PARALLEL
c5c76735
JL
9199 ? (XVECLEN (pat, 0)
9200 + num_clobbers_to_add)
38a448ca 9201 : num_clobbers_to_add + 1));
230d793d
RS
9202
9203 if (GET_CODE (pat) == PARALLEL)
9204 for (i = 0; i < XVECLEN (pat, 0); i++)
9205 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9206 else
9207 XVECEXP (newpat, 0, 0) = pat;
9208
9209 add_clobbers (newpat, insn_code_number);
9210
9211 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9212 i < XVECLEN (newpat, 0); i++)
9213 {
9214 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9215 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9216 return -1;
38a448ca
RH
9217 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9218 XEXP (XVECEXP (newpat, 0, i), 0), notes);
230d793d
RS
9219 }
9220 pat = newpat;
9221 }
9222
9223 *pnewpat = pat;
9224 *pnotes = notes;
9225
9226 return insn_code_number;
9227}
9228\f
9229/* Like gen_lowpart but for use by combine. In combine it is not possible
9230 to create any new pseudoregs. However, it is safe to create
9231 invalid memory addresses, because combine will try to recognize
9232 them and all they will do is make the combine attempt fail.
9233
9234 If for some reason this cannot do its job, an rtx
9235 (clobber (const_int 0)) is returned.
9236 An insn containing that will not be recognized. */
9237
9238#undef gen_lowpart
9239
9240static rtx
9241gen_lowpart_for_combine (mode, x)
9242 enum machine_mode mode;
9243 register rtx x;
9244{
9245 rtx result;
9246
9247 if (GET_MODE (x) == mode)
9248 return x;
9249
eae957a8
RK
9250 /* We can only support MODE being wider than a word if X is a
9251 constant integer or has a mode the same size. */
9252
9253 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9254 && ! ((GET_MODE (x) == VOIDmode
9255 && (GET_CODE (x) == CONST_INT
9256 || GET_CODE (x) == CONST_DOUBLE))
9257 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
38a448ca 9258 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
230d793d
RS
9259
9260 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9261 won't know what to do. So we will strip off the SUBREG here and
9262 process normally. */
9263 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9264 {
9265 x = SUBREG_REG (x);
9266 if (GET_MODE (x) == mode)
9267 return x;
9268 }
9269
9270 result = gen_lowpart_common (mode, x);
64bf47a2
RK
9271 if (result != 0
9272 && GET_CODE (result) == SUBREG
9273 && GET_CODE (SUBREG_REG (result)) == REG
9274 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9275 && (GET_MODE_SIZE (GET_MODE (result))
9276 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
b1f21e0a 9277 REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
64bf47a2 9278
230d793d
RS
9279 if (result)
9280 return result;
9281
9282 if (GET_CODE (x) == MEM)
9283 {
9284 register int offset = 0;
9285 rtx new;
9286
9287 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9288 address. */
9289 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
38a448ca 9290 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
230d793d
RS
9291
9292 /* If we want to refer to something bigger than the original memref,
9293 generate a perverse subreg instead. That will force a reload
9294 of the original memref X. */
9295 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
38a448ca 9296 return gen_rtx_SUBREG (mode, x, 0);
230d793d 9297
f76b9db2
ILT
9298 if (WORDS_BIG_ENDIAN)
9299 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9300 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
c5c76735 9301
f76b9db2
ILT
9302 if (BYTES_BIG_ENDIAN)
9303 {
9304 /* Adjust the address so that the address-after-the-data is
9305 unchanged. */
9306 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9307 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9308 }
38a448ca 9309 new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
230d793d 9310 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
c6df88cb 9311 MEM_COPY_ATTRIBUTES (new, x);
230d793d
RS
9312 return new;
9313 }
9314
9315 /* If X is a comparison operator, rewrite it in a new mode. This
9316 probably won't match, but may allow further simplifications. */
9317 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9318 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9319
9320 /* If we couldn't simplify X any other way, just enclose it in a
9321 SUBREG. Normally, this SUBREG won't match, but some patterns may
a7c99304 9322 include an explicit SUBREG or we may simplify it further in combine. */
230d793d 9323 else
dfbe1b2f
RK
9324 {
9325 int word = 0;
9326
9327 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9328 word = ((GET_MODE_SIZE (GET_MODE (x))
9329 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9330 / UNITS_PER_WORD);
38a448ca 9331 return gen_rtx_SUBREG (mode, x, word);
dfbe1b2f 9332 }
230d793d
RS
9333}
9334\f
9335/* Make an rtx expression. This is a subset of gen_rtx and only supports
9336 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9337
9338 If the identical expression was previously in the insn (in the undobuf),
9339 it will be returned. Only if it is not found will a new expression
9340 be made. */
9341
9342/*VARARGS2*/
9343static rtx
4f90e4a0 9344gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
230d793d 9345{
5148a72b 9346#ifndef ANSI_PROTOTYPES
230d793d
RS
9347 enum rtx_code code;
9348 enum machine_mode mode;
4f90e4a0
RK
9349#endif
9350 va_list p;
230d793d
RS
9351 int n_args;
9352 rtx args[3];
b729186a 9353 int j;
6f7d635c 9354 const char *fmt;
230d793d 9355 rtx rt;
241cea85 9356 struct undo *undo;
230d793d 9357
4f90e4a0
RK
9358 VA_START (p, mode);
9359
5148a72b 9360#ifndef ANSI_PROTOTYPES
230d793d
RS
9361 code = va_arg (p, enum rtx_code);
9362 mode = va_arg (p, enum machine_mode);
4f90e4a0
RK
9363#endif
9364
230d793d
RS
9365 n_args = GET_RTX_LENGTH (code);
9366 fmt = GET_RTX_FORMAT (code);
9367
9368 if (n_args == 0 || n_args > 3)
9369 abort ();
9370
9371 /* Get each arg and verify that it is supposed to be an expression. */
9372 for (j = 0; j < n_args; j++)
9373 {
9374 if (*fmt++ != 'e')
9375 abort ();
9376
9377 args[j] = va_arg (p, rtx);
9378 }
9379
9380 /* See if this is in undobuf. Be sure we don't use objects that came
9381 from another insn; this could produce circular rtl structures. */
9382
241cea85
RK
9383 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9384 if (!undo->is_int
9385 && GET_CODE (undo->old_contents.r) == code
9386 && GET_MODE (undo->old_contents.r) == mode)
230d793d
RS
9387 {
9388 for (j = 0; j < n_args; j++)
241cea85 9389 if (XEXP (undo->old_contents.r, j) != args[j])
230d793d
RS
9390 break;
9391
9392 if (j == n_args)
241cea85 9393 return undo->old_contents.r;
230d793d
RS
9394 }
9395
9396 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9397 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9398 rt = rtx_alloc (code);
9399 PUT_MODE (rt, mode);
9400 XEXP (rt, 0) = args[0];
9401 if (n_args > 1)
9402 {
9403 XEXP (rt, 1) = args[1];
9404 if (n_args > 2)
9405 XEXP (rt, 2) = args[2];
9406 }
9407 return rt;
9408}
9409
9410/* These routines make binary and unary operations by first seeing if they
9411 fold; if not, a new expression is allocated. */
9412
9413static rtx
9414gen_binary (code, mode, op0, op1)
9415 enum rtx_code code;
9416 enum machine_mode mode;
9417 rtx op0, op1;
9418{
9419 rtx result;
1a26b032
RK
9420 rtx tem;
9421
9422 if (GET_RTX_CLASS (code) == 'c'
9423 && (GET_CODE (op0) == CONST_INT
9424 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9425 tem = op0, op0 = op1, op1 = tem;
230d793d
RS
9426
9427 if (GET_RTX_CLASS (code) == '<')
9428 {
9429 enum machine_mode op_mode = GET_MODE (op0);
9210df58
RK
9430
9431 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
0f41302f 9432 just (REL_OP X Y). */
9210df58
RK
9433 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9434 {
9435 op1 = XEXP (op0, 1);
9436 op0 = XEXP (op0, 0);
9437 op_mode = GET_MODE (op0);
9438 }
9439
230d793d
RS
9440 if (op_mode == VOIDmode)
9441 op_mode = GET_MODE (op1);
9442 result = simplify_relational_operation (code, op_mode, op0, op1);
9443 }
9444 else
9445 result = simplify_binary_operation (code, mode, op0, op1);
9446
9447 if (result)
9448 return result;
9449
9450 /* Put complex operands first and constants second. */
9451 if (GET_RTX_CLASS (code) == 'c'
9452 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9453 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9454 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9455 || (GET_CODE (op0) == SUBREG
9456 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9457 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9458 return gen_rtx_combine (code, mode, op1, op0);
9459
e5e809f4
JL
9460 /* If we are turning off bits already known off in OP0, we need not do
9461 an AND. */
9462 else if (code == AND && GET_CODE (op1) == CONST_INT
9463 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9464 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
9465 return op0;
9466
230d793d
RS
9467 return gen_rtx_combine (code, mode, op0, op1);
9468}
9469
9470static rtx
0c1c8ea6 9471gen_unary (code, mode, op0_mode, op0)
230d793d 9472 enum rtx_code code;
0c1c8ea6 9473 enum machine_mode mode, op0_mode;
230d793d
RS
9474 rtx op0;
9475{
0c1c8ea6 9476 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
230d793d
RS
9477
9478 if (result)
9479 return result;
9480
9481 return gen_rtx_combine (code, mode, op0);
9482}
9483\f
9484/* Simplify a comparison between *POP0 and *POP1 where CODE is the
9485 comparison code that will be tested.
9486
9487 The result is a possibly different comparison code to use. *POP0 and
9488 *POP1 may be updated.
9489
9490 It is possible that we might detect that a comparison is either always
9491 true or always false. However, we do not perform general constant
5089e22e 9492 folding in combine, so this knowledge isn't useful. Such tautologies
230d793d
RS
9493 should have been detected earlier. Hence we ignore all such cases. */
9494
9495static enum rtx_code
9496simplify_comparison (code, pop0, pop1)
9497 enum rtx_code code;
9498 rtx *pop0;
9499 rtx *pop1;
9500{
9501 rtx op0 = *pop0;
9502 rtx op1 = *pop1;
9503 rtx tem, tem1;
9504 int i;
9505 enum machine_mode mode, tmode;
9506
9507 /* Try a few ways of applying the same transformation to both operands. */
9508 while (1)
9509 {
3a19aabc
RK
9510#ifndef WORD_REGISTER_OPERATIONS
9511 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9512 so check specially. */
9513 if (code != GTU && code != GEU && code != LTU && code != LEU
9514 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9515 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9516 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9517 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9518 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9519 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
ad25ba17 9520 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
3a19aabc
RK
9521 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9522 && GET_CODE (XEXP (op1, 1)) == CONST_INT
9523 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9524 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9525 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9526 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9527 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9528 && (INTVAL (XEXP (op0, 1))
9529 == (GET_MODE_BITSIZE (GET_MODE (op0))
9530 - (GET_MODE_BITSIZE
9531 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9532 {
9533 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9534 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9535 }
9536#endif
9537
230d793d
RS
9538 /* If both operands are the same constant shift, see if we can ignore the
9539 shift. We can if the shift is a rotate or if the bits shifted out of
951553af 9540 this shift are known to be zero for both inputs and if the type of
230d793d 9541 comparison is compatible with the shift. */
67232b23
RK
9542 if (GET_CODE (op0) == GET_CODE (op1)
9543 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9544 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
45620ed4 9545 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
67232b23
RK
9546 && (code != GT && code != LT && code != GE && code != LE))
9547 || (GET_CODE (op0) == ASHIFTRT
9548 && (code != GTU && code != LTU
9549 && code != GEU && code != GEU)))
9550 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9551 && INTVAL (XEXP (op0, 1)) >= 0
9552 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9553 && XEXP (op0, 1) == XEXP (op1, 1))
230d793d
RS
9554 {
9555 enum machine_mode mode = GET_MODE (op0);
5f4f0e22 9556 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
230d793d
RS
9557 int shift_count = INTVAL (XEXP (op0, 1));
9558
9559 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9560 mask &= (mask >> shift_count) << shift_count;
45620ed4 9561 else if (GET_CODE (op0) == ASHIFT)
230d793d
RS
9562 mask = (mask & (mask << shift_count)) >> shift_count;
9563
951553af
RK
9564 if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9565 && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
230d793d
RS
9566 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9567 else
9568 break;
9569 }
9570
9571 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9572 SUBREGs are of the same mode, and, in both cases, the AND would
9573 be redundant if the comparison was done in the narrower mode,
9574 do the comparison in the narrower mode (e.g., we are AND'ing with 1
951553af
RK
9575 and the operand's possibly nonzero bits are 0xffffff01; in that case
9576 if we only care about QImode, we don't need the AND). This case
9577 occurs if the output mode of an scc insn is not SImode and
7e4dc511
RK
9578 STORE_FLAG_VALUE == 1 (e.g., the 386).
9579
9580 Similarly, check for a case where the AND's are ZERO_EXTEND
9581 operations from some narrower mode even though a SUBREG is not
9582 present. */
230d793d
RS
9583
9584 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9585 && GET_CODE (XEXP (op0, 1)) == CONST_INT
7e4dc511 9586 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
230d793d 9587 {
7e4dc511
RK
9588 rtx inner_op0 = XEXP (op0, 0);
9589 rtx inner_op1 = XEXP (op1, 0);
9590 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9591 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9592 int changed = 0;
9593
9594 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9595 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9596 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9597 && (GET_MODE (SUBREG_REG (inner_op0))
9598 == GET_MODE (SUBREG_REG (inner_op1)))
729a2bc6 9599 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
7e4dc511 9600 <= HOST_BITS_PER_WIDE_INT)
01c82bbb 9601 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
729a2bc6 9602 GET_MODE (SUBREG_REG (inner_op0)))))
01c82bbb
RK
9603 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9604 GET_MODE (SUBREG_REG (inner_op1))))))
7e4dc511
RK
9605 {
9606 op0 = SUBREG_REG (inner_op0);
9607 op1 = SUBREG_REG (inner_op1);
9608
9609 /* The resulting comparison is always unsigned since we masked
0f41302f 9610 off the original sign bit. */
7e4dc511
RK
9611 code = unsigned_condition (code);
9612
9613 changed = 1;
9614 }
230d793d 9615
7e4dc511
RK
9616 else if (c0 == c1)
9617 for (tmode = GET_CLASS_NARROWEST_MODE
9618 (GET_MODE_CLASS (GET_MODE (op0)));
9619 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
e51712db 9620 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
7e4dc511
RK
9621 {
9622 op0 = gen_lowpart_for_combine (tmode, inner_op0);
9623 op1 = gen_lowpart_for_combine (tmode, inner_op1);
66415c8b 9624 code = unsigned_condition (code);
7e4dc511
RK
9625 changed = 1;
9626 break;
9627 }
9628
9629 if (! changed)
9630 break;
230d793d 9631 }
3a19aabc 9632
ad25ba17
RK
9633 /* If both operands are NOT, we can strip off the outer operation
9634 and adjust the comparison code for swapped operands; similarly for
9635 NEG, except that this must be an equality comparison. */
9636 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9637 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9638 && (code == EQ || code == NE)))
9639 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
3a19aabc 9640
230d793d
RS
9641 else
9642 break;
9643 }
9644
9645 /* If the first operand is a constant, swap the operands and adjust the
3aceff0d
RK
9646 comparison code appropriately, but don't do this if the second operand
9647 is already a constant integer. */
9648 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
230d793d
RS
9649 {
9650 tem = op0, op0 = op1, op1 = tem;
9651 code = swap_condition (code);
9652 }
9653
9654 /* We now enter a loop during which we will try to simplify the comparison.
9655 For the most part, we only are concerned with comparisons with zero,
9656 but some things may really be comparisons with zero but not start
9657 out looking that way. */
9658
9659 while (GET_CODE (op1) == CONST_INT)
9660 {
9661 enum machine_mode mode = GET_MODE (op0);
9662 int mode_width = GET_MODE_BITSIZE (mode);
5f4f0e22 9663 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
230d793d
RS
9664 int equality_comparison_p;
9665 int sign_bit_comparison_p;
9666 int unsigned_comparison_p;
5f4f0e22 9667 HOST_WIDE_INT const_op;
230d793d
RS
9668
9669 /* We only want to handle integral modes. This catches VOIDmode,
9670 CCmode, and the floating-point modes. An exception is that we
9671 can handle VOIDmode if OP0 is a COMPARE or a comparison
9672 operation. */
9673
9674 if (GET_MODE_CLASS (mode) != MODE_INT
9675 && ! (mode == VOIDmode
9676 && (GET_CODE (op0) == COMPARE
9677 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9678 break;
9679
9680 /* Get the constant we are comparing against and turn off all bits
9681 not on in our mode. */
9682 const_op = INTVAL (op1);
5f4f0e22 9683 if (mode_width <= HOST_BITS_PER_WIDE_INT)
4803a34a 9684 const_op &= mask;
230d793d
RS
9685
9686 /* If we are comparing against a constant power of two and the value
951553af 9687 being compared can only have that single bit nonzero (e.g., it was
230d793d
RS
9688 `and'ed with that bit), we can replace this with a comparison
9689 with zero. */
9690 if (const_op
9691 && (code == EQ || code == NE || code == GE || code == GEU
9692 || code == LT || code == LTU)
5f4f0e22 9693 && mode_width <= HOST_BITS_PER_WIDE_INT
230d793d 9694 && exact_log2 (const_op) >= 0
e51712db 9695 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
230d793d
RS
9696 {
9697 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9698 op1 = const0_rtx, const_op = 0;
9699 }
9700
d0ab8cd3
RK
9701 /* Similarly, if we are comparing a value known to be either -1 or
9702 0 with -1, change it to the opposite comparison against zero. */
9703
9704 if (const_op == -1
9705 && (code == EQ || code == NE || code == GT || code == LE
9706 || code == GEU || code == LTU)
9707 && num_sign_bit_copies (op0, mode) == mode_width)
9708 {
9709 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9710 op1 = const0_rtx, const_op = 0;
9711 }
9712
230d793d 9713 /* Do some canonicalizations based on the comparison code. We prefer
4803a34a
RK
9714 comparisons against zero and then prefer equality comparisons.
9715 If we can reduce the size of a constant, we will do that too. */
230d793d
RS
9716
9717 switch (code)
9718 {
9719 case LT:
4803a34a
RK
9720 /* < C is equivalent to <= (C - 1) */
9721 if (const_op > 0)
230d793d 9722 {
4803a34a 9723 const_op -= 1;
5f4f0e22 9724 op1 = GEN_INT (const_op);
230d793d
RS
9725 code = LE;
9726 /* ... fall through to LE case below. */
9727 }
9728 else
9729 break;
9730
9731 case LE:
4803a34a
RK
9732 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9733 if (const_op < 0)
9734 {
9735 const_op += 1;
5f4f0e22 9736 op1 = GEN_INT (const_op);
4803a34a
RK
9737 code = LT;
9738 }
230d793d
RS
9739
9740 /* If we are doing a <= 0 comparison on a value known to have
9741 a zero sign bit, we can replace this with == 0. */
9742 else if (const_op == 0
5f4f0e22 9743 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 9744 && (nonzero_bits (op0, mode)
5f4f0e22 9745 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
230d793d
RS
9746 code = EQ;
9747 break;
9748
9749 case GE:
0f41302f 9750 /* >= C is equivalent to > (C - 1). */
4803a34a 9751 if (const_op > 0)
230d793d 9752 {
4803a34a 9753 const_op -= 1;
5f4f0e22 9754 op1 = GEN_INT (const_op);
230d793d
RS
9755 code = GT;
9756 /* ... fall through to GT below. */
9757 }
9758 else
9759 break;
9760
9761 case GT:
4803a34a
RK
9762 /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9763 if (const_op < 0)
9764 {
9765 const_op += 1;
5f4f0e22 9766 op1 = GEN_INT (const_op);
4803a34a
RK
9767 code = GE;
9768 }
230d793d
RS
9769
9770 /* If we are doing a > 0 comparison on a value known to have
9771 a zero sign bit, we can replace this with != 0. */
9772 else if (const_op == 0
5f4f0e22 9773 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 9774 && (nonzero_bits (op0, mode)
5f4f0e22 9775 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
230d793d
RS
9776 code = NE;
9777 break;
9778
230d793d 9779 case LTU:
4803a34a
RK
9780 /* < C is equivalent to <= (C - 1). */
9781 if (const_op > 0)
9782 {
9783 const_op -= 1;
5f4f0e22 9784 op1 = GEN_INT (const_op);
4803a34a 9785 code = LEU;
0f41302f 9786 /* ... fall through ... */
4803a34a 9787 }
d0ab8cd3
RK
9788
9789 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
f77aada2
JW
9790 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9791 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
d0ab8cd3
RK
9792 {
9793 const_op = 0, op1 = const0_rtx;
9794 code = GE;
9795 break;
9796 }
4803a34a
RK
9797 else
9798 break;
230d793d
RS
9799
9800 case LEU:
9801 /* unsigned <= 0 is equivalent to == 0 */
9802 if (const_op == 0)
9803 code = EQ;
d0ab8cd3 9804
0f41302f 9805 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
f77aada2
JW
9806 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9807 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
d0ab8cd3
RK
9808 {
9809 const_op = 0, op1 = const0_rtx;
9810 code = GE;
9811 }
230d793d
RS
9812 break;
9813
4803a34a
RK
9814 case GEU:
9815 /* >= C is equivalent to < (C - 1). */
9816 if (const_op > 1)
9817 {
9818 const_op -= 1;
5f4f0e22 9819 op1 = GEN_INT (const_op);
4803a34a 9820 code = GTU;
0f41302f 9821 /* ... fall through ... */
4803a34a 9822 }
d0ab8cd3
RK
9823
9824 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
f77aada2
JW
9825 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9826 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
d0ab8cd3
RK
9827 {
9828 const_op = 0, op1 = const0_rtx;
9829 code = LT;
8b2e69e1 9830 break;
d0ab8cd3 9831 }
4803a34a
RK
9832 else
9833 break;
9834
230d793d
RS
9835 case GTU:
9836 /* unsigned > 0 is equivalent to != 0 */
9837 if (const_op == 0)
9838 code = NE;
d0ab8cd3
RK
9839
9840 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
f77aada2
JW
9841 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9842 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
d0ab8cd3
RK
9843 {
9844 const_op = 0, op1 = const0_rtx;
9845 code = LT;
9846 }
230d793d 9847 break;
e9a25f70
JL
9848
9849 default:
9850 break;
230d793d
RS
9851 }
9852
9853 /* Compute some predicates to simplify code below. */
9854
9855 equality_comparison_p = (code == EQ || code == NE);
9856 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9857 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9858 || code == LEU);
9859
6139ff20
RK
9860 /* If this is a sign bit comparison and we can do arithmetic in
9861 MODE, say that we will only be needing the sign bit of OP0. */
9862 if (sign_bit_comparison_p
9863 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9864 op0 = force_to_mode (op0, mode,
9865 ((HOST_WIDE_INT) 1
9866 << (GET_MODE_BITSIZE (mode) - 1)),
e3d616e3 9867 NULL_RTX, 0);
6139ff20 9868
230d793d
RS
9869 /* Now try cases based on the opcode of OP0. If none of the cases
9870 does a "continue", we exit this loop immediately after the
9871 switch. */
9872
9873 switch (GET_CODE (op0))
9874 {
9875 case ZERO_EXTRACT:
9876 /* If we are extracting a single bit from a variable position in
9877 a constant that has only a single bit set and are comparing it
9878 with zero, we can convert this into an equality comparison
d7cd794f 9879 between the position and the location of the single bit. */
230d793d 9880
230d793d
RS
9881 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9882 && XEXP (op0, 1) == const1_rtx
9883 && equality_comparison_p && const_op == 0
d7cd794f 9884 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
230d793d 9885 {
f76b9db2 9886 if (BITS_BIG_ENDIAN)
0d8e55d8 9887 {
d7cd794f 9888#ifdef HAVE_extzv
0d8e55d8
JL
9889 mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
9890 if (mode == VOIDmode)
9891 mode = word_mode;
9892 i = (GET_MODE_BITSIZE (mode) - 1 - i);
d7cd794f 9893#else
0d8e55d8 9894 i = BITS_PER_WORD - 1 - i;
230d793d 9895#endif
0d8e55d8 9896 }
230d793d
RS
9897
9898 op0 = XEXP (op0, 2);
5f4f0e22 9899 op1 = GEN_INT (i);
230d793d
RS
9900 const_op = i;
9901
9902 /* Result is nonzero iff shift count is equal to I. */
9903 code = reverse_condition (code);
9904 continue;
9905 }
230d793d 9906
0f41302f 9907 /* ... fall through ... */
230d793d
RS
9908
9909 case SIGN_EXTRACT:
9910 tem = expand_compound_operation (op0);
9911 if (tem != op0)
9912 {
9913 op0 = tem;
9914 continue;
9915 }
9916 break;
9917
9918 case NOT:
9919 /* If testing for equality, we can take the NOT of the constant. */
9920 if (equality_comparison_p
9921 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9922 {
9923 op0 = XEXP (op0, 0);
9924 op1 = tem;
9925 continue;
9926 }
9927
9928 /* If just looking at the sign bit, reverse the sense of the
9929 comparison. */
9930 if (sign_bit_comparison_p)
9931 {
9932 op0 = XEXP (op0, 0);
9933 code = (code == GE ? LT : GE);
9934 continue;
9935 }
9936 break;
9937
9938 case NEG:
9939 /* If testing for equality, we can take the NEG of the constant. */
9940 if (equality_comparison_p
9941 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9942 {
9943 op0 = XEXP (op0, 0);
9944 op1 = tem;
9945 continue;
9946 }
9947
9948 /* The remaining cases only apply to comparisons with zero. */
9949 if (const_op != 0)
9950 break;
9951
9952 /* When X is ABS or is known positive,
9953 (neg X) is < 0 if and only if X != 0. */
9954
9955 if (sign_bit_comparison_p
9956 && (GET_CODE (XEXP (op0, 0)) == ABS
5f4f0e22 9957 || (mode_width <= HOST_BITS_PER_WIDE_INT
951553af 9958 && (nonzero_bits (XEXP (op0, 0), mode)
5f4f0e22 9959 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
230d793d
RS
9960 {
9961 op0 = XEXP (op0, 0);
9962 code = (code == LT ? NE : EQ);
9963 continue;
9964 }
9965
3bed8141 9966 /* If we have NEG of something whose two high-order bits are the
0f41302f 9967 same, we know that "(-a) < 0" is equivalent to "a > 0". */
3bed8141 9968 if (num_sign_bit_copies (op0, mode) >= 2)
230d793d
RS
9969 {
9970 op0 = XEXP (op0, 0);
9971 code = swap_condition (code);
9972 continue;
9973 }
9974 break;
9975
9976 case ROTATE:
9977 /* If we are testing equality and our count is a constant, we
9978 can perform the inverse operation on our RHS. */
9979 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9980 && (tem = simplify_binary_operation (ROTATERT, mode,
9981 op1, XEXP (op0, 1))) != 0)
9982 {
9983 op0 = XEXP (op0, 0);
9984 op1 = tem;
9985 continue;
9986 }
9987
9988 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9989 a particular bit. Convert it to an AND of a constant of that
9990 bit. This will be converted into a ZERO_EXTRACT. */
9991 if (const_op == 0 && sign_bit_comparison_p
9992 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5f4f0e22 9993 && mode_width <= HOST_BITS_PER_WIDE_INT)
230d793d 9994 {
5f4f0e22
CH
9995 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9996 ((HOST_WIDE_INT) 1
9997 << (mode_width - 1
9998 - INTVAL (XEXP (op0, 1)))));
230d793d
RS
9999 code = (code == LT ? NE : EQ);
10000 continue;
10001 }
10002
0f41302f 10003 /* ... fall through ... */
230d793d
RS
10004
10005 case ABS:
10006 /* ABS is ignorable inside an equality comparison with zero. */
10007 if (const_op == 0 && equality_comparison_p)
10008 {
10009 op0 = XEXP (op0, 0);
10010 continue;
10011 }
10012 break;
10013
10014
10015 case SIGN_EXTEND:
10016 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10017 to (compare FOO CONST) if CONST fits in FOO's mode and we
10018 are either testing inequality or have an unsigned comparison
10019 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10020 if (! unsigned_comparison_p
10021 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
5f4f0e22
CH
10022 <= HOST_BITS_PER_WIDE_INT)
10023 && ((unsigned HOST_WIDE_INT) const_op
e51712db 10024 < (((unsigned HOST_WIDE_INT) 1
5f4f0e22 10025 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
230d793d
RS
10026 {
10027 op0 = XEXP (op0, 0);
10028 continue;
10029 }
10030 break;
10031
10032 case SUBREG:
a687e897 10033 /* Check for the case where we are comparing A - C1 with C2,
abc95ed3 10034 both constants are smaller than 1/2 the maximum positive
a687e897
RK
10035 value in MODE, and the comparison is equality or unsigned.
10036 In that case, if A is either zero-extended to MODE or has
10037 sufficient sign bits so that the high-order bit in MODE
10038 is a copy of the sign in the inner mode, we can prove that it is
10039 safe to do the operation in the wider mode. This simplifies
10040 many range checks. */
10041
10042 if (mode_width <= HOST_BITS_PER_WIDE_INT
10043 && subreg_lowpart_p (op0)
10044 && GET_CODE (SUBREG_REG (op0)) == PLUS
10045 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10046 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10047 && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
e51712db 10048 < (HOST_WIDE_INT)(GET_MODE_MASK (mode) / 2))
adb7a1cb 10049 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
951553af
RK
10050 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10051 GET_MODE (SUBREG_REG (op0)))
a687e897
RK
10052 & ~ GET_MODE_MASK (mode))
10053 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10054 GET_MODE (SUBREG_REG (op0)))
10055 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10056 - GET_MODE_BITSIZE (mode)))))
10057 {
10058 op0 = SUBREG_REG (op0);
10059 continue;
10060 }
10061
fe0cf571
RK
10062 /* If the inner mode is narrower and we are extracting the low part,
10063 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10064 if (subreg_lowpart_p (op0)
89f1c7f2
RS
10065 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10066 /* Fall through */ ;
10067 else
230d793d
RS
10068 break;
10069
0f41302f 10070 /* ... fall through ... */
230d793d
RS
10071
10072 case ZERO_EXTEND:
10073 if ((unsigned_comparison_p || equality_comparison_p)
10074 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
5f4f0e22
CH
10075 <= HOST_BITS_PER_WIDE_INT)
10076 && ((unsigned HOST_WIDE_INT) const_op
230d793d
RS
10077 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10078 {
10079 op0 = XEXP (op0, 0);
10080 continue;
10081 }
10082 break;
10083
10084 case PLUS:
20fdd649 10085 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
5089e22e 10086 this for equality comparisons due to pathological cases involving
230d793d 10087 overflows. */
20fdd649
RK
10088 if (equality_comparison_p
10089 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10090 op1, XEXP (op0, 1))))
230d793d
RS
10091 {
10092 op0 = XEXP (op0, 0);
10093 op1 = tem;
10094 continue;
10095 }
10096
10097 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10098 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10099 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10100 {
10101 op0 = XEXP (XEXP (op0, 0), 0);
10102 code = (code == LT ? EQ : NE);
10103 continue;
10104 }
10105 break;
10106
10107 case MINUS:
20fdd649
RK
10108 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10109 (eq B (minus A C)), whichever simplifies. We can only do
10110 this for equality comparisons due to pathological cases involving
10111 overflows. */
10112 if (equality_comparison_p
10113 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10114 XEXP (op0, 1), op1)))
10115 {
10116 op0 = XEXP (op0, 0);
10117 op1 = tem;
10118 continue;
10119 }
10120
10121 if (equality_comparison_p
10122 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10123 XEXP (op0, 0), op1)))
10124 {
10125 op0 = XEXP (op0, 1);
10126 op1 = tem;
10127 continue;
10128 }
10129
230d793d
RS
10130 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10131 of bits in X minus 1, is one iff X > 0. */
10132 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10133 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10134 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
10135 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10136 {
10137 op0 = XEXP (op0, 1);
10138 code = (code == GE ? LE : GT);
10139 continue;
10140 }
10141 break;
10142
10143 case XOR:
10144 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10145 if C is zero or B is a constant. */
10146 if (equality_comparison_p
10147 && 0 != (tem = simplify_binary_operation (XOR, mode,
10148 XEXP (op0, 1), op1)))
10149 {
10150 op0 = XEXP (op0, 0);
10151 op1 = tem;
10152 continue;
10153 }
10154 break;
10155
10156 case EQ: case NE:
10157 case LT: case LTU: case LE: case LEU:
10158 case GT: case GTU: case GE: case GEU:
10159 /* We can't do anything if OP0 is a condition code value, rather
10160 than an actual data value. */
10161 if (const_op != 0
10162#ifdef HAVE_cc0
10163 || XEXP (op0, 0) == cc0_rtx
10164#endif
10165 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10166 break;
10167
10168 /* Get the two operands being compared. */
10169 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10170 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10171 else
10172 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10173
10174 /* Check for the cases where we simply want the result of the
10175 earlier test or the opposite of that result. */
10176 if (code == NE
10177 || (code == EQ && reversible_comparison_p (op0))
5f4f0e22 10178 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
3f508eca 10179 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
230d793d 10180 && (STORE_FLAG_VALUE
5f4f0e22
CH
10181 & (((HOST_WIDE_INT) 1
10182 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
230d793d
RS
10183 && (code == LT
10184 || (code == GE && reversible_comparison_p (op0)))))
10185 {
10186 code = (code == LT || code == NE
10187 ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
10188 op0 = tem, op1 = tem1;
10189 continue;
10190 }
10191 break;
10192
10193 case IOR:
10194 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10195 iff X <= 0. */
10196 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10197 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10198 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10199 {
10200 op0 = XEXP (op0, 1);
10201 code = (code == GE ? GT : LE);
10202 continue;
10203 }
10204 break;
10205
10206 case AND:
10207 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10208 will be converted to a ZERO_EXTRACT later. */
10209 if (const_op == 0 && equality_comparison_p
45620ed4 10210 && GET_CODE (XEXP (op0, 0)) == ASHIFT
230d793d
RS
10211 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10212 {
10213 op0 = simplify_and_const_int
10214 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10215 XEXP (op0, 1),
10216 XEXP (XEXP (op0, 0), 1)),
5f4f0e22 10217 (HOST_WIDE_INT) 1);
230d793d
RS
10218 continue;
10219 }
10220
10221 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10222 zero and X is a comparison and C1 and C2 describe only bits set
10223 in STORE_FLAG_VALUE, we can compare with X. */
10224 if (const_op == 0 && equality_comparison_p
5f4f0e22 10225 && mode_width <= HOST_BITS_PER_WIDE_INT
230d793d
RS
10226 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10227 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10228 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10229 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
5f4f0e22 10230 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
230d793d
RS
10231 {
10232 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10233 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10234 if ((~ STORE_FLAG_VALUE & mask) == 0
10235 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10236 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10237 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10238 {
10239 op0 = XEXP (XEXP (op0, 0), 0);
10240 continue;
10241 }
10242 }
10243
10244 /* If we are doing an equality comparison of an AND of a bit equal
10245 to the sign bit, replace this with a LT or GE comparison of
10246 the underlying value. */
10247 if (equality_comparison_p
10248 && const_op == 0
10249 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5f4f0e22 10250 && mode_width <= HOST_BITS_PER_WIDE_INT
230d793d 10251 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
e51712db 10252 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
230d793d
RS
10253 {
10254 op0 = XEXP (op0, 0);
10255 code = (code == EQ ? GE : LT);
10256 continue;
10257 }
10258
10259 /* If this AND operation is really a ZERO_EXTEND from a narrower
10260 mode, the constant fits within that mode, and this is either an
10261 equality or unsigned comparison, try to do this comparison in
10262 the narrower mode. */
10263 if ((equality_comparison_p || unsigned_comparison_p)
10264 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10265 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10266 & GET_MODE_MASK (mode))
10267 + 1)) >= 0
10268 && const_op >> i == 0
10269 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10270 {
10271 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10272 continue;
10273 }
e5e809f4
JL
10274
10275 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10276 in both M1 and M2 and the SUBREG is either paradoxical or
10277 represents the low part, permute the SUBREG and the AND and
10278 try again. */
10279 if (GET_CODE (XEXP (op0, 0)) == SUBREG
c5c76735 10280 && (0
9ec36da5 10281#ifdef WORD_REGISTER_OPERATIONS
c5c76735
JL
10282 || ((mode_width
10283 > (GET_MODE_BITSIZE
10284 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10285 && mode_width <= BITS_PER_WORD)
9ec36da5 10286#endif
c5c76735
JL
10287 || ((mode_width
10288 <= (GET_MODE_BITSIZE
10289 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10290 && subreg_lowpart_p (XEXP (op0, 0))))
adc05e6c
JL
10291#ifndef WORD_REGISTER_OPERATIONS
10292 /* It is unsafe to commute the AND into the SUBREG if the SUBREG
10293 is paradoxical and WORD_REGISTER_OPERATIONS is not defined.
10294 As originally written the upper bits have a defined value
10295 due to the AND operation. However, if we commute the AND
10296 inside the SUBREG then they no longer have defined values
10297 and the meaning of the code has been changed. */
10298 && (GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)))
10299 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10300#endif
e5e809f4
JL
10301 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10302 && mode_width <= HOST_BITS_PER_WIDE_INT
10303 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10304 <= HOST_BITS_PER_WIDE_INT)
10305 && (INTVAL (XEXP (op0, 1)) & ~ mask) == 0
10306 && 0 == (~ GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
9ec36da5 10307 & INTVAL (XEXP (op0, 1)))
e51712db
KG
10308 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1)) != mask
10309 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
9ec36da5 10310 != GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
e5e809f4
JL
10311
10312 {
10313 op0
10314 = gen_lowpart_for_combine
10315 (mode,
10316 gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10317 SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10318 continue;
10319 }
10320
230d793d
RS
10321 break;
10322
10323 case ASHIFT:
45620ed4 10324 /* If we have (compare (ashift FOO N) (const_int C)) and
230d793d 10325 the high order N bits of FOO (N+1 if an inequality comparison)
951553af 10326 are known to be zero, we can do this by comparing FOO with C
230d793d
RS
10327 shifted right N bits so long as the low-order N bits of C are
10328 zero. */
10329 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10330 && INTVAL (XEXP (op0, 1)) >= 0
10331 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
5f4f0e22
CH
10332 < HOST_BITS_PER_WIDE_INT)
10333 && ((const_op
34785d05 10334 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
5f4f0e22 10335 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 10336 && (nonzero_bits (XEXP (op0, 0), mode)
230d793d
RS
10337 & ~ (mask >> (INTVAL (XEXP (op0, 1))
10338 + ! equality_comparison_p))) == 0)
10339 {
10340 const_op >>= INTVAL (XEXP (op0, 1));
5f4f0e22 10341 op1 = GEN_INT (const_op);
230d793d
RS
10342 op0 = XEXP (op0, 0);
10343 continue;
10344 }
10345
dfbe1b2f 10346 /* If we are doing a sign bit comparison, it means we are testing
230d793d 10347 a particular bit. Convert it to the appropriate AND. */
dfbe1b2f 10348 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
5f4f0e22 10349 && mode_width <= HOST_BITS_PER_WIDE_INT)
230d793d 10350 {
5f4f0e22
CH
10351 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10352 ((HOST_WIDE_INT) 1
10353 << (mode_width - 1
10354 - INTVAL (XEXP (op0, 1)))));
230d793d
RS
10355 code = (code == LT ? NE : EQ);
10356 continue;
10357 }
dfbe1b2f
RK
10358
10359 /* If this an equality comparison with zero and we are shifting
10360 the low bit to the sign bit, we can convert this to an AND of the
10361 low-order bit. */
10362 if (const_op == 0 && equality_comparison_p
10363 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10364 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10365 {
5f4f0e22
CH
10366 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10367 (HOST_WIDE_INT) 1);
dfbe1b2f
RK
10368 continue;
10369 }
230d793d
RS
10370 break;
10371
10372 case ASHIFTRT:
d0ab8cd3
RK
10373 /* If this is an equality comparison with zero, we can do this
10374 as a logical shift, which might be much simpler. */
10375 if (equality_comparison_p && const_op == 0
10376 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10377 {
10378 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10379 XEXP (op0, 0),
10380 INTVAL (XEXP (op0, 1)));
10381 continue;
10382 }
10383
230d793d
RS
10384 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10385 do the comparison in a narrower mode. */
10386 if (! unsigned_comparison_p
10387 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10388 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10389 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10390 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
22331794 10391 MODE_INT, 1)) != BLKmode
5f4f0e22
CH
10392 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10393 || ((unsigned HOST_WIDE_INT) - const_op
10394 <= GET_MODE_MASK (tmode))))
230d793d
RS
10395 {
10396 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10397 continue;
10398 }
10399
0f41302f 10400 /* ... fall through ... */
230d793d
RS
10401 case LSHIFTRT:
10402 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
951553af 10403 the low order N bits of FOO are known to be zero, we can do this
230d793d
RS
10404 by comparing FOO with C shifted left N bits so long as no
10405 overflow occurs. */
10406 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10407 && INTVAL (XEXP (op0, 1)) >= 0
5f4f0e22
CH
10408 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10409 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 10410 && (nonzero_bits (XEXP (op0, 0), mode)
5f4f0e22 10411 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
230d793d
RS
10412 && (const_op == 0
10413 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10414 < mode_width)))
10415 {
10416 const_op <<= INTVAL (XEXP (op0, 1));
5f4f0e22 10417 op1 = GEN_INT (const_op);
230d793d
RS
10418 op0 = XEXP (op0, 0);
10419 continue;
10420 }
10421
10422 /* If we are using this shift to extract just the sign bit, we
10423 can replace this with an LT or GE comparison. */
10424 if (const_op == 0
10425 && (equality_comparison_p || sign_bit_comparison_p)
10426 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10427 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10428 {
10429 op0 = XEXP (op0, 0);
10430 code = (code == NE || code == GT ? LT : GE);
10431 continue;
10432 }
10433 break;
e9a25f70
JL
10434
10435 default:
10436 break;
230d793d
RS
10437 }
10438
10439 break;
10440 }
10441
10442 /* Now make any compound operations involved in this comparison. Then,
76d31c63 10443 check for an outmost SUBREG on OP0 that is not doing anything or is
230d793d
RS
10444 paradoxical. The latter case can only occur when it is known that the
10445 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
10446 We can never remove a SUBREG for a non-equality comparison because the
10447 sign bit is in a different place in the underlying object. */
10448
10449 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10450 op1 = make_compound_operation (op1, SET);
10451
10452 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10453 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10454 && (code == NE || code == EQ)
10455 && ((GET_MODE_SIZE (GET_MODE (op0))
10456 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10457 {
10458 op0 = SUBREG_REG (op0);
10459 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10460 }
10461
10462 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10463 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10464 && (code == NE || code == EQ)
ac49a949
RS
10465 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10466 <= HOST_BITS_PER_WIDE_INT)
951553af 10467 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
230d793d
RS
10468 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10469 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10470 op1),
951553af 10471 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
230d793d
RS
10472 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10473 op0 = SUBREG_REG (op0), op1 = tem;
10474
10475 /* We now do the opposite procedure: Some machines don't have compare
10476 insns in all modes. If OP0's mode is an integer mode smaller than a
10477 word and we can't do a compare in that mode, see if there is a larger
a687e897
RK
10478 mode for which we can do the compare. There are a number of cases in
10479 which we can use the wider mode. */
230d793d
RS
10480
10481 mode = GET_MODE (op0);
10482 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10483 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10484 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10485 for (tmode = GET_MODE_WIDER_MODE (mode);
5f4f0e22
CH
10486 (tmode != VOIDmode
10487 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
230d793d 10488 tmode = GET_MODE_WIDER_MODE (tmode))
a687e897 10489 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
230d793d 10490 {
951553af 10491 /* If the only nonzero bits in OP0 and OP1 are those in the
a687e897
RK
10492 narrower mode and this is an equality or unsigned comparison,
10493 we can use the wider mode. Similarly for sign-extended
7e4dc511 10494 values, in which case it is true for all comparisons. */
a687e897
RK
10495 if (((code == EQ || code == NE
10496 || code == GEU || code == GTU || code == LEU || code == LTU)
951553af
RK
10497 && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10498 && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
7e4dc511
RK
10499 || ((num_sign_bit_copies (op0, tmode)
10500 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
a687e897 10501 && (num_sign_bit_copies (op1, tmode)
58744483 10502 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
a687e897
RK
10503 {
10504 op0 = gen_lowpart_for_combine (tmode, op0);
10505 op1 = gen_lowpart_for_combine (tmode, op1);
10506 break;
10507 }
230d793d 10508
a687e897
RK
10509 /* If this is a test for negative, we can make an explicit
10510 test of the sign bit. */
10511
10512 if (op1 == const0_rtx && (code == LT || code == GE)
10513 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
230d793d 10514 {
a687e897
RK
10515 op0 = gen_binary (AND, tmode,
10516 gen_lowpart_for_combine (tmode, op0),
5f4f0e22
CH
10517 GEN_INT ((HOST_WIDE_INT) 1
10518 << (GET_MODE_BITSIZE (mode) - 1)));
230d793d 10519 code = (code == LT) ? NE : EQ;
a687e897 10520 break;
230d793d 10521 }
230d793d
RS
10522 }
10523
b7a775b2
RK
10524#ifdef CANONICALIZE_COMPARISON
10525 /* If this machine only supports a subset of valid comparisons, see if we
10526 can convert an unsupported one into a supported one. */
10527 CANONICALIZE_COMPARISON (code, op0, op1);
10528#endif
10529
230d793d
RS
10530 *pop0 = op0;
10531 *pop1 = op1;
10532
10533 return code;
10534}
10535\f
10536/* Return 1 if we know that X, a comparison operation, is not operating
10537 on a floating-point value or is EQ or NE, meaning that we can safely
10538 reverse it. */
10539
10540static int
10541reversible_comparison_p (x)
10542 rtx x;
10543{
10544 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
7e2a0d8e 10545 || flag_fast_math
230d793d
RS
10546 || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10547 return 1;
10548
10549 switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10550 {
10551 case MODE_INT:
3ad2180a
RK
10552 case MODE_PARTIAL_INT:
10553 case MODE_COMPLEX_INT:
230d793d
RS
10554 return 1;
10555
10556 case MODE_CC:
9210df58
RK
10557 /* If the mode of the condition codes tells us that this is safe,
10558 we need look no further. */
10559 if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10560 return 1;
10561
10562 /* Otherwise try and find where the condition codes were last set and
10563 use that. */
230d793d
RS
10564 x = get_last_value (XEXP (x, 0));
10565 return (x && GET_CODE (x) == COMPARE
3ad2180a 10566 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
e9a25f70
JL
10567
10568 default:
10569 return 0;
230d793d 10570 }
230d793d
RS
10571}
10572\f
10573/* Utility function for following routine. Called when X is part of a value
10574 being stored into reg_last_set_value. Sets reg_last_set_table_tick
10575 for each register mentioned. Similar to mention_regs in cse.c */
10576
10577static void
10578update_table_tick (x)
10579 rtx x;
10580{
10581 register enum rtx_code code = GET_CODE (x);
6f7d635c 10582 register const char *fmt = GET_RTX_FORMAT (code);
230d793d
RS
10583 register int i;
10584
10585 if (code == REG)
10586 {
10587 int regno = REGNO (x);
10588 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10589 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10590
10591 for (i = regno; i < endregno; i++)
10592 reg_last_set_table_tick[i] = label_tick;
10593
10594 return;
10595 }
10596
10597 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10598 /* Note that we can't have an "E" in values stored; see
10599 get_last_value_validate. */
10600 if (fmt[i] == 'e')
10601 update_table_tick (XEXP (x, i));
10602}
10603
10604/* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10605 are saying that the register is clobbered and we no longer know its
7988fd36
RK
10606 value. If INSN is zero, don't update reg_last_set; this is only permitted
10607 with VALUE also zero and is used to invalidate the register. */
230d793d
RS
10608
10609static void
10610record_value_for_reg (reg, insn, value)
10611 rtx reg;
10612 rtx insn;
10613 rtx value;
10614{
10615 int regno = REGNO (reg);
10616 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10617 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10618 int i;
10619
10620 /* If VALUE contains REG and we have a previous value for REG, substitute
10621 the previous value. */
10622 if (value && insn && reg_overlap_mentioned_p (reg, value))
10623 {
10624 rtx tem;
10625
10626 /* Set things up so get_last_value is allowed to see anything set up to
10627 our insn. */
10628 subst_low_cuid = INSN_CUID (insn);
10629 tem = get_last_value (reg);
10630
10631 if (tem)
10632 value = replace_rtx (copy_rtx (value), reg, tem);
10633 }
10634
10635 /* For each register modified, show we don't know its value, that
ef026f91
RS
10636 we don't know about its bitwise content, that its value has been
10637 updated, and that we don't know the location of the death of the
10638 register. */
230d793d
RS
10639 for (i = regno; i < endregno; i ++)
10640 {
10641 if (insn)
10642 reg_last_set[i] = insn;
10643 reg_last_set_value[i] = 0;
ef026f91
RS
10644 reg_last_set_mode[i] = 0;
10645 reg_last_set_nonzero_bits[i] = 0;
10646 reg_last_set_sign_bit_copies[i] = 0;
230d793d
RS
10647 reg_last_death[i] = 0;
10648 }
10649
10650 /* Mark registers that are being referenced in this value. */
10651 if (value)
10652 update_table_tick (value);
10653
10654 /* Now update the status of each register being set.
10655 If someone is using this register in this block, set this register
10656 to invalid since we will get confused between the two lives in this
10657 basic block. This makes using this register always invalid. In cse, we
10658 scan the table to invalidate all entries using this register, but this
10659 is too much work for us. */
10660
10661 for (i = regno; i < endregno; i++)
10662 {
10663 reg_last_set_label[i] = label_tick;
10664 if (value && reg_last_set_table_tick[i] == label_tick)
10665 reg_last_set_invalid[i] = 1;
10666 else
10667 reg_last_set_invalid[i] = 0;
10668 }
10669
10670 /* The value being assigned might refer to X (like in "x++;"). In that
10671 case, we must replace it with (clobber (const_int 0)) to prevent
10672 infinite loops. */
9a893315 10673 if (value && ! get_last_value_validate (&value, insn,
230d793d
RS
10674 reg_last_set_label[regno], 0))
10675 {
10676 value = copy_rtx (value);
9a893315
JW
10677 if (! get_last_value_validate (&value, insn,
10678 reg_last_set_label[regno], 1))
230d793d
RS
10679 value = 0;
10680 }
10681
55310dad
RK
10682 /* For the main register being modified, update the value, the mode, the
10683 nonzero bits, and the number of sign bit copies. */
10684
230d793d
RS
10685 reg_last_set_value[regno] = value;
10686
55310dad
RK
10687 if (value)
10688 {
2afabb48 10689 subst_low_cuid = INSN_CUID (insn);
55310dad
RK
10690 reg_last_set_mode[regno] = GET_MODE (reg);
10691 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10692 reg_last_set_sign_bit_copies[regno]
10693 = num_sign_bit_copies (value, GET_MODE (reg));
10694 }
230d793d
RS
10695}
10696
10697/* Used for communication between the following two routines. */
10698static rtx record_dead_insn;
10699
10700/* Called via note_stores from record_dead_and_set_regs to handle one
10701 SET or CLOBBER in an insn. */
10702
10703static void
10704record_dead_and_set_regs_1 (dest, setter)
10705 rtx dest, setter;
10706{
ca89d290
RK
10707 if (GET_CODE (dest) == SUBREG)
10708 dest = SUBREG_REG (dest);
10709
230d793d
RS
10710 if (GET_CODE (dest) == REG)
10711 {
10712 /* If we are setting the whole register, we know its value. Otherwise
10713 show that we don't know the value. We can handle SUBREG in
10714 some cases. */
10715 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10716 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10717 else if (GET_CODE (setter) == SET
10718 && GET_CODE (SET_DEST (setter)) == SUBREG
10719 && SUBREG_REG (SET_DEST (setter)) == dest
90bf8081 10720 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
230d793d 10721 && subreg_lowpart_p (SET_DEST (setter)))
d0ab8cd3
RK
10722 record_value_for_reg (dest, record_dead_insn,
10723 gen_lowpart_for_combine (GET_MODE (dest),
10724 SET_SRC (setter)));
230d793d 10725 else
5f4f0e22 10726 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
230d793d
RS
10727 }
10728 else if (GET_CODE (dest) == MEM
10729 /* Ignore pushes, they clobber nothing. */
10730 && ! push_operand (dest, GET_MODE (dest)))
10731 mem_last_set = INSN_CUID (record_dead_insn);
10732}
10733
10734/* Update the records of when each REG was most recently set or killed
10735 for the things done by INSN. This is the last thing done in processing
10736 INSN in the combiner loop.
10737
ef026f91
RS
10738 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10739 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10740 and also the similar information mem_last_set (which insn most recently
10741 modified memory) and last_call_cuid (which insn was the most recent
10742 subroutine call). */
230d793d
RS
10743
10744static void
10745record_dead_and_set_regs (insn)
10746 rtx insn;
10747{
10748 register rtx link;
55310dad
RK
10749 int i;
10750
230d793d
RS
10751 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10752 {
dbc131f3
RK
10753 if (REG_NOTE_KIND (link) == REG_DEAD
10754 && GET_CODE (XEXP (link, 0)) == REG)
10755 {
10756 int regno = REGNO (XEXP (link, 0));
10757 int endregno
10758 = regno + (regno < FIRST_PSEUDO_REGISTER
10759 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10760 : 1);
dbc131f3
RK
10761
10762 for (i = regno; i < endregno; i++)
10763 reg_last_death[i] = insn;
10764 }
230d793d 10765 else if (REG_NOTE_KIND (link) == REG_INC)
5f4f0e22 10766 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
230d793d
RS
10767 }
10768
10769 if (GET_CODE (insn) == CALL_INSN)
55310dad
RK
10770 {
10771 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10772 if (call_used_regs[i])
10773 {
10774 reg_last_set_value[i] = 0;
ef026f91
RS
10775 reg_last_set_mode[i] = 0;
10776 reg_last_set_nonzero_bits[i] = 0;
10777 reg_last_set_sign_bit_copies[i] = 0;
55310dad
RK
10778 reg_last_death[i] = 0;
10779 }
10780
10781 last_call_cuid = mem_last_set = INSN_CUID (insn);
10782 }
230d793d
RS
10783
10784 record_dead_insn = insn;
10785 note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10786}
10787\f
10788/* Utility routine for the following function. Verify that all the registers
10789 mentioned in *LOC are valid when *LOC was part of a value set when
10790 label_tick == TICK. Return 0 if some are not.
10791
10792 If REPLACE is non-zero, replace the invalid reference with
10793 (clobber (const_int 0)) and return 1. This replacement is useful because
10794 we often can get useful information about the form of a value (e.g., if
10795 it was produced by a shift that always produces -1 or 0) even though
10796 we don't know exactly what registers it was produced from. */
10797
10798static int
9a893315 10799get_last_value_validate (loc, insn, tick, replace)
230d793d 10800 rtx *loc;
9a893315 10801 rtx insn;
230d793d
RS
10802 int tick;
10803 int replace;
10804{
10805 rtx x = *loc;
6f7d635c 10806 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
230d793d
RS
10807 int len = GET_RTX_LENGTH (GET_CODE (x));
10808 int i;
10809
10810 if (GET_CODE (x) == REG)
10811 {
10812 int regno = REGNO (x);
10813 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10814 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10815 int j;
10816
10817 for (j = regno; j < endregno; j++)
10818 if (reg_last_set_invalid[j]
57cf50a4
GRK
10819 /* If this is a pseudo-register that was only set once and not
10820 live at the beginning of the function, it is always valid. */
10821 || (! (regno >= FIRST_PSEUDO_REGISTER
10822 && REG_N_SETS (regno) == 1
10823 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
230d793d
RS
10824 && reg_last_set_label[j] > tick))
10825 {
10826 if (replace)
38a448ca 10827 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
230d793d
RS
10828 return replace;
10829 }
10830
10831 return 1;
10832 }
9a893315
JW
10833 /* If this is a memory reference, make sure that there were
10834 no stores after it that might have clobbered the value. We don't
10835 have alias info, so we assume any store invalidates it. */
10836 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10837 && INSN_CUID (insn) <= mem_last_set)
10838 {
10839 if (replace)
38a448ca 10840 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9a893315
JW
10841 return replace;
10842 }
230d793d
RS
10843
10844 for (i = 0; i < len; i++)
10845 if ((fmt[i] == 'e'
9a893315 10846 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
230d793d
RS
10847 /* Don't bother with these. They shouldn't occur anyway. */
10848 || fmt[i] == 'E')
10849 return 0;
10850
10851 /* If we haven't found a reason for it to be invalid, it is valid. */
10852 return 1;
10853}
10854
10855/* Get the last value assigned to X, if known. Some registers
10856 in the value may be replaced with (clobber (const_int 0)) if their value
10857 is known longer known reliably. */
10858
10859static rtx
10860get_last_value (x)
10861 rtx x;
10862{
10863 int regno;
10864 rtx value;
10865
10866 /* If this is a non-paradoxical SUBREG, get the value of its operand and
10867 then convert it to the desired mode. If this is a paradoxical SUBREG,
0f41302f 10868 we cannot predict what values the "extra" bits might have. */
230d793d
RS
10869 if (GET_CODE (x) == SUBREG
10870 && subreg_lowpart_p (x)
10871 && (GET_MODE_SIZE (GET_MODE (x))
10872 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10873 && (value = get_last_value (SUBREG_REG (x))) != 0)
10874 return gen_lowpart_for_combine (GET_MODE (x), value);
10875
10876 if (GET_CODE (x) != REG)
10877 return 0;
10878
10879 regno = REGNO (x);
10880 value = reg_last_set_value[regno];
10881
57cf50a4
GRK
10882 /* If we don't have a value, or if it isn't for this basic block and
10883 it's either a hard register, set more than once, or it's a live
10884 at the beginning of the function, return 0.
10885
10886 Because if it's not live at the beginnning of the function then the reg
10887 is always set before being used (is never used without being set).
10888 And, if it's set only once, and it's always set before use, then all
10889 uses must have the same last value, even if it's not from this basic
10890 block. */
230d793d
RS
10891
10892 if (value == 0
57cf50a4
GRK
10893 || (reg_last_set_label[regno] != label_tick
10894 && (regno < FIRST_PSEUDO_REGISTER
10895 || REG_N_SETS (regno) != 1
10896 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))))
230d793d
RS
10897 return 0;
10898
4255220d 10899 /* If the value was set in a later insn than the ones we are processing,
4090a6b3
RK
10900 we can't use it even if the register was only set once, but make a quick
10901 check to see if the previous insn set it to something. This is commonly
0d9641d1
JW
10902 the case when the same pseudo is used by repeated insns.
10903
10904 This does not work if there exists an instruction which is temporarily
10905 not on the insn chain. */
d0ab8cd3 10906
bcd49eb7 10907 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
d0ab8cd3
RK
10908 {
10909 rtx insn, set;
10910
6c1b3bf2
BS
10911 /* We can't do anything if the value is set in between the insns we are
10912 processing. */
10913 if (INSN_CUID (reg_last_set[regno]) <= INSN_CUID (subst_insn))
10914 return 0;
10915
bcd49eb7
JW
10916 /* We can not do anything useful in this case, because there is
10917 an instruction which is not on the insn chain. */
10918 if (subst_prev_insn)
10919 return 0;
10920
4255220d
JW
10921 /* Skip over USE insns. They are not useful here, and they may have
10922 been made by combine, in which case they do not have a INSN_CUID
d6c80562 10923 value. We can't use prev_real_insn, because that would incorrectly
e340018d
JW
10924 take us backwards across labels. Skip over BARRIERs also, since
10925 they could have been made by combine. If we see one, we must be
10926 optimizing dead code, so it doesn't matter what we do. */
d6c80562
JW
10927 for (insn = prev_nonnote_insn (subst_insn);
10928 insn && ((GET_CODE (insn) == INSN
10929 && GET_CODE (PATTERN (insn)) == USE)
e340018d 10930 || GET_CODE (insn) == BARRIER
4255220d 10931 || INSN_CUID (insn) >= subst_low_cuid);
d6c80562 10932 insn = prev_nonnote_insn (insn))
3adde2a5 10933 ;
d0ab8cd3
RK
10934
10935 if (insn
10936 && (set = single_set (insn)) != 0
10937 && rtx_equal_p (SET_DEST (set), x))
10938 {
10939 value = SET_SRC (set);
10940
10941 /* Make sure that VALUE doesn't reference X. Replace any
ddd5a7c1 10942 explicit references with a CLOBBER. If there are any remaining
d0ab8cd3
RK
10943 references (rare), don't use the value. */
10944
10945 if (reg_mentioned_p (x, value))
10946 value = replace_rtx (copy_rtx (value), x,
38a448ca 10947 gen_rtx_CLOBBER (GET_MODE (x), const0_rtx));
d0ab8cd3
RK
10948
10949 if (reg_overlap_mentioned_p (x, value))
10950 return 0;
10951 }
10952 else
10953 return 0;
10954 }
10955
10956 /* If the value has all its registers valid, return it. */
9a893315
JW
10957 if (get_last_value_validate (&value, reg_last_set[regno],
10958 reg_last_set_label[regno], 0))
230d793d
RS
10959 return value;
10960
10961 /* Otherwise, make a copy and replace any invalid register with
10962 (clobber (const_int 0)). If that fails for some reason, return 0. */
10963
10964 value = copy_rtx (value);
9a893315
JW
10965 if (get_last_value_validate (&value, reg_last_set[regno],
10966 reg_last_set_label[regno], 1))
230d793d
RS
10967 return value;
10968
10969 return 0;
10970}
10971\f
10972/* Return nonzero if expression X refers to a REG or to memory
10973 that is set in an instruction more recent than FROM_CUID. */
10974
10975static int
10976use_crosses_set_p (x, from_cuid)
10977 register rtx x;
10978 int from_cuid;
10979{
6f7d635c 10980 register const char *fmt;
230d793d
RS
10981 register int i;
10982 register enum rtx_code code = GET_CODE (x);
10983
10984 if (code == REG)
10985 {
10986 register int regno = REGNO (x);
e28f5732
RK
10987 int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10988 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10989
230d793d
RS
10990#ifdef PUSH_ROUNDING
10991 /* Don't allow uses of the stack pointer to be moved,
10992 because we don't know whether the move crosses a push insn. */
10993 if (regno == STACK_POINTER_REGNUM)
10994 return 1;
10995#endif
e28f5732
RK
10996 for (;regno < endreg; regno++)
10997 if (reg_last_set[regno]
10998 && INSN_CUID (reg_last_set[regno]) > from_cuid)
10999 return 1;
11000 return 0;
230d793d
RS
11001 }
11002
11003 if (code == MEM && mem_last_set > from_cuid)
11004 return 1;
11005
11006 fmt = GET_RTX_FORMAT (code);
11007
11008 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11009 {
11010 if (fmt[i] == 'E')
11011 {
11012 register int j;
11013 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11014 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11015 return 1;
11016 }
11017 else if (fmt[i] == 'e'
11018 && use_crosses_set_p (XEXP (x, i), from_cuid))
11019 return 1;
11020 }
11021 return 0;
11022}
11023\f
11024/* Define three variables used for communication between the following
11025 routines. */
11026
11027static int reg_dead_regno, reg_dead_endregno;
11028static int reg_dead_flag;
11029
11030/* Function called via note_stores from reg_dead_at_p.
11031
ddd5a7c1 11032 If DEST is within [reg_dead_regno, reg_dead_endregno), set
230d793d
RS
11033 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11034
11035static void
11036reg_dead_at_p_1 (dest, x)
11037 rtx dest;
11038 rtx x;
11039{
11040 int regno, endregno;
11041
11042 if (GET_CODE (dest) != REG)
11043 return;
11044
11045 regno = REGNO (dest);
11046 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11047 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11048
11049 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11050 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11051}
11052
11053/* Return non-zero if REG is known to be dead at INSN.
11054
11055 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11056 referencing REG, it is dead. If we hit a SET referencing REG, it is
11057 live. Otherwise, see if it is live or dead at the start of the basic
6e25d159
RK
11058 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11059 must be assumed to be always live. */
230d793d
RS
11060
11061static int
11062reg_dead_at_p (reg, insn)
11063 rtx reg;
11064 rtx insn;
11065{
11066 int block, i;
11067
11068 /* Set variables for reg_dead_at_p_1. */
11069 reg_dead_regno = REGNO (reg);
11070 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11071 ? HARD_REGNO_NREGS (reg_dead_regno,
11072 GET_MODE (reg))
11073 : 1);
11074
11075 reg_dead_flag = 0;
11076
6e25d159
RK
11077 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11078 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11079 {
11080 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11081 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11082 return 0;
11083 }
11084
230d793d
RS
11085 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11086 beginning of function. */
60715d0b 11087 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
230d793d
RS
11088 insn = prev_nonnote_insn (insn))
11089 {
11090 note_stores (PATTERN (insn), reg_dead_at_p_1);
11091 if (reg_dead_flag)
11092 return reg_dead_flag == 1 ? 1 : 0;
11093
11094 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11095 return 1;
11096 }
11097
11098 /* Get the basic block number that we were in. */
11099 if (insn == 0)
11100 block = 0;
11101 else
11102 {
11103 for (block = 0; block < n_basic_blocks; block++)
3b413743 11104 if (insn == BLOCK_HEAD (block))
230d793d
RS
11105 break;
11106
11107 if (block == n_basic_blocks)
11108 return 0;
11109 }
11110
11111 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
e881bb1b 11112 if (REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start, i))
230d793d
RS
11113 return 0;
11114
11115 return 1;
11116}
6e25d159
RK
11117\f
11118/* Note hard registers in X that are used. This code is similar to
11119 that in flow.c, but much simpler since we don't care about pseudos. */
11120
11121static void
11122mark_used_regs_combine (x)
11123 rtx x;
11124{
11125 register RTX_CODE code = GET_CODE (x);
11126 register int regno;
11127 int i;
11128
11129 switch (code)
11130 {
11131 case LABEL_REF:
11132 case SYMBOL_REF:
11133 case CONST_INT:
11134 case CONST:
11135 case CONST_DOUBLE:
11136 case PC:
11137 case ADDR_VEC:
11138 case ADDR_DIFF_VEC:
11139 case ASM_INPUT:
11140#ifdef HAVE_cc0
11141 /* CC0 must die in the insn after it is set, so we don't need to take
11142 special note of it here. */
11143 case CC0:
11144#endif
11145 return;
11146
11147 case CLOBBER:
11148 /* If we are clobbering a MEM, mark any hard registers inside the
11149 address as used. */
11150 if (GET_CODE (XEXP (x, 0)) == MEM)
11151 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11152 return;
11153
11154 case REG:
11155 regno = REGNO (x);
11156 /* A hard reg in a wide mode may really be multiple registers.
11157 If so, mark all of them just like the first. */
11158 if (regno < FIRST_PSEUDO_REGISTER)
11159 {
11160 /* None of this applies to the stack, frame or arg pointers */
11161 if (regno == STACK_POINTER_REGNUM
11162#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11163 || regno == HARD_FRAME_POINTER_REGNUM
11164#endif
11165#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11166 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11167#endif
11168 || regno == FRAME_POINTER_REGNUM)
11169 return;
11170
11171 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
11172 while (i-- > 0)
11173 SET_HARD_REG_BIT (newpat_used_regs, regno + i);
11174 }
11175 return;
11176
11177 case SET:
11178 {
11179 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11180 the address. */
11181 register rtx testreg = SET_DEST (x);
11182
e048778f
RK
11183 while (GET_CODE (testreg) == SUBREG
11184 || GET_CODE (testreg) == ZERO_EXTRACT
11185 || GET_CODE (testreg) == SIGN_EXTRACT
11186 || GET_CODE (testreg) == STRICT_LOW_PART)
6e25d159
RK
11187 testreg = XEXP (testreg, 0);
11188
11189 if (GET_CODE (testreg) == MEM)
11190 mark_used_regs_combine (XEXP (testreg, 0));
11191
11192 mark_used_regs_combine (SET_SRC (x));
6e25d159 11193 }
e9a25f70
JL
11194 return;
11195
11196 default:
11197 break;
6e25d159
RK
11198 }
11199
11200 /* Recursively scan the operands of this expression. */
11201
11202 {
6f7d635c 11203 register const char *fmt = GET_RTX_FORMAT (code);
6e25d159
RK
11204
11205 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11206 {
11207 if (fmt[i] == 'e')
11208 mark_used_regs_combine (XEXP (x, i));
11209 else if (fmt[i] == 'E')
11210 {
11211 register int j;
11212
11213 for (j = 0; j < XVECLEN (x, i); j++)
11214 mark_used_regs_combine (XVECEXP (x, i, j));
11215 }
11216 }
11217 }
11218}
11219
230d793d
RS
11220\f
11221/* Remove register number REGNO from the dead registers list of INSN.
11222
11223 Return the note used to record the death, if there was one. */
11224
11225rtx
11226remove_death (regno, insn)
11227 int regno;
11228 rtx insn;
11229{
11230 register rtx note = find_regno_note (insn, REG_DEAD, regno);
11231
11232 if (note)
1a26b032 11233 {
b1f21e0a 11234 REG_N_DEATHS (regno)--;
1a26b032
RK
11235 remove_note (insn, note);
11236 }
230d793d
RS
11237
11238 return note;
11239}
11240
11241/* For each register (hardware or pseudo) used within expression X, if its
11242 death is in an instruction with cuid between FROM_CUID (inclusive) and
11243 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11244 list headed by PNOTES.
11245
6eb12cef
RK
11246 That said, don't move registers killed by maybe_kill_insn.
11247
230d793d
RS
11248 This is done when X is being merged by combination into TO_INSN. These
11249 notes will then be distributed as needed. */
11250
11251static void
6eb12cef 11252move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
230d793d 11253 rtx x;
6eb12cef 11254 rtx maybe_kill_insn;
230d793d
RS
11255 int from_cuid;
11256 rtx to_insn;
11257 rtx *pnotes;
11258{
6f7d635c 11259 register const char *fmt;
230d793d
RS
11260 register int len, i;
11261 register enum rtx_code code = GET_CODE (x);
11262
11263 if (code == REG)
11264 {
11265 register int regno = REGNO (x);
11266 register rtx where_dead = reg_last_death[regno];
e340018d
JW
11267 register rtx before_dead, after_dead;
11268
6eb12cef
RK
11269 /* Don't move the register if it gets killed in between from and to */
11270 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11271 && !reg_referenced_p (x, maybe_kill_insn))
11272 return;
11273
e340018d
JW
11274 /* WHERE_DEAD could be a USE insn made by combine, so first we
11275 make sure that we have insns with valid INSN_CUID values. */
11276 before_dead = where_dead;
11277 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11278 before_dead = PREV_INSN (before_dead);
11279 after_dead = where_dead;
11280 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11281 after_dead = NEXT_INSN (after_dead);
11282
11283 if (before_dead && after_dead
11284 && INSN_CUID (before_dead) >= from_cuid
11285 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11286 || (where_dead != after_dead
11287 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
230d793d 11288 {
dbc131f3 11289 rtx note = remove_death (regno, where_dead);
230d793d
RS
11290
11291 /* It is possible for the call above to return 0. This can occur
11292 when reg_last_death points to I2 or I1 that we combined with.
dbc131f3
RK
11293 In that case make a new note.
11294
11295 We must also check for the case where X is a hard register
11296 and NOTE is a death note for a range of hard registers
11297 including X. In that case, we must put REG_DEAD notes for
11298 the remaining registers in place of NOTE. */
11299
11300 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11301 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
24e46fc4 11302 > GET_MODE_SIZE (GET_MODE (x))))
dbc131f3
RK
11303 {
11304 int deadregno = REGNO (XEXP (note, 0));
11305 int deadend
11306 = (deadregno + HARD_REGNO_NREGS (deadregno,
11307 GET_MODE (XEXP (note, 0))));
11308 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11309 int i;
11310
11311 for (i = deadregno; i < deadend; i++)
11312 if (i < regno || i >= ourend)
11313 REG_NOTES (where_dead)
38a448ca
RH
11314 = gen_rtx_EXPR_LIST (REG_DEAD,
11315 gen_rtx_REG (reg_raw_mode[i], i),
11316 REG_NOTES (where_dead));
dbc131f3 11317 }
24e46fc4
JW
11318 /* If we didn't find any note, or if we found a REG_DEAD note that
11319 covers only part of the given reg, and we have a multi-reg hard
fabd69e8
RK
11320 register, then to be safe we must check for REG_DEAD notes
11321 for each register other than the first. They could have
11322 their own REG_DEAD notes lying around. */
24e46fc4
JW
11323 else if ((note == 0
11324 || (note != 0
11325 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11326 < GET_MODE_SIZE (GET_MODE (x)))))
11327 && regno < FIRST_PSEUDO_REGISTER
fabd69e8
RK
11328 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11329 {
11330 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
24e46fc4 11331 int i, offset;
fabd69e8
RK
11332 rtx oldnotes = 0;
11333
24e46fc4
JW
11334 if (note)
11335 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11336 else
11337 offset = 1;
11338
11339 for (i = regno + offset; i < ourend; i++)
38a448ca 11340 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
6eb12cef 11341 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
fabd69e8 11342 }
230d793d 11343
dbc131f3 11344 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
230d793d
RS
11345 {
11346 XEXP (note, 1) = *pnotes;
11347 *pnotes = note;
11348 }
11349 else
38a448ca 11350 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
1a26b032 11351
b1f21e0a 11352 REG_N_DEATHS (regno)++;
230d793d
RS
11353 }
11354
11355 return;
11356 }
11357
11358 else if (GET_CODE (x) == SET)
11359 {
11360 rtx dest = SET_DEST (x);
11361
6eb12cef 11362 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
230d793d 11363
a7c99304
RK
11364 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11365 that accesses one word of a multi-word item, some
11366 piece of everything register in the expression is used by
11367 this insn, so remove any old death. */
11368
11369 if (GET_CODE (dest) == ZERO_EXTRACT
11370 || GET_CODE (dest) == STRICT_LOW_PART
11371 || (GET_CODE (dest) == SUBREG
11372 && (((GET_MODE_SIZE (GET_MODE (dest))
11373 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11374 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11375 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
230d793d 11376 {
6eb12cef 11377 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
a7c99304 11378 return;
230d793d
RS
11379 }
11380
a7c99304
RK
11381 /* If this is some other SUBREG, we know it replaces the entire
11382 value, so use that as the destination. */
11383 if (GET_CODE (dest) == SUBREG)
11384 dest = SUBREG_REG (dest);
11385
11386 /* If this is a MEM, adjust deaths of anything used in the address.
11387 For a REG (the only other possibility), the entire value is
11388 being replaced so the old value is not used in this insn. */
230d793d
RS
11389
11390 if (GET_CODE (dest) == MEM)
6eb12cef
RK
11391 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11392 to_insn, pnotes);
230d793d
RS
11393 return;
11394 }
11395
11396 else if (GET_CODE (x) == CLOBBER)
11397 return;
11398
11399 len = GET_RTX_LENGTH (code);
11400 fmt = GET_RTX_FORMAT (code);
11401
11402 for (i = 0; i < len; i++)
11403 {
11404 if (fmt[i] == 'E')
11405 {
11406 register int j;
11407 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6eb12cef
RK
11408 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11409 to_insn, pnotes);
230d793d
RS
11410 }
11411 else if (fmt[i] == 'e')
6eb12cef 11412 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
230d793d
RS
11413 }
11414}
11415\f
a7c99304
RK
11416/* Return 1 if X is the target of a bit-field assignment in BODY, the
11417 pattern of an insn. X must be a REG. */
230d793d
RS
11418
11419static int
a7c99304
RK
11420reg_bitfield_target_p (x, body)
11421 rtx x;
230d793d
RS
11422 rtx body;
11423{
11424 int i;
11425
11426 if (GET_CODE (body) == SET)
a7c99304
RK
11427 {
11428 rtx dest = SET_DEST (body);
11429 rtx target;
11430 int regno, tregno, endregno, endtregno;
11431
11432 if (GET_CODE (dest) == ZERO_EXTRACT)
11433 target = XEXP (dest, 0);
11434 else if (GET_CODE (dest) == STRICT_LOW_PART)
11435 target = SUBREG_REG (XEXP (dest, 0));
11436 else
11437 return 0;
11438
11439 if (GET_CODE (target) == SUBREG)
11440 target = SUBREG_REG (target);
11441
11442 if (GET_CODE (target) != REG)
11443 return 0;
11444
11445 tregno = REGNO (target), regno = REGNO (x);
11446 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11447 return target == x;
11448
11449 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11450 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11451
11452 return endregno > tregno && regno < endtregno;
11453 }
230d793d
RS
11454
11455 else if (GET_CODE (body) == PARALLEL)
11456 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
a7c99304 11457 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
230d793d
RS
11458 return 1;
11459
11460 return 0;
11461}
11462\f
11463/* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11464 as appropriate. I3 and I2 are the insns resulting from the combination
11465 insns including FROM (I2 may be zero).
11466
11467 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11468 not need REG_DEAD notes because they are being substituted for. This
11469 saves searching in the most common cases.
11470
11471 Each note in the list is either ignored or placed on some insns, depending
11472 on the type of note. */
11473
11474static void
11475distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11476 rtx notes;
11477 rtx from_insn;
11478 rtx i3, i2;
11479 rtx elim_i2, elim_i1;
11480{
11481 rtx note, next_note;
11482 rtx tem;
11483
11484 for (note = notes; note; note = next_note)
11485 {
11486 rtx place = 0, place2 = 0;
11487
11488 /* If this NOTE references a pseudo register, ensure it references
11489 the latest copy of that register. */
11490 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11491 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11492 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11493
11494 next_note = XEXP (note, 1);
11495 switch (REG_NOTE_KIND (note))
11496 {
c9903b44
DE
11497 case REG_BR_PROB:
11498 case REG_EXEC_COUNT:
11499 /* Doesn't matter much where we put this, as long as it's somewhere.
11500 It is preferable to keep these notes on branches, which is most
11501 likely to be i3. */
11502 place = i3;
11503 break;
11504
4b7c585f 11505 case REG_EH_REGION:
0e403ec3
AS
11506 case REG_EH_RETHROW:
11507 /* These notes must remain with the call. It should not be
11508 possible for both I2 and I3 to be a call. */
4b7c585f
JL
11509 if (GET_CODE (i3) == CALL_INSN)
11510 place = i3;
11511 else if (i2 && GET_CODE (i2) == CALL_INSN)
11512 place = i2;
11513 else
11514 abort ();
11515 break;
11516
230d793d 11517 case REG_UNUSED:
07d0cbdd 11518 /* Any clobbers for i3 may still exist, and so we must process
176c9e6b
JW
11519 REG_UNUSED notes from that insn.
11520
11521 Any clobbers from i2 or i1 can only exist if they were added by
11522 recog_for_combine. In that case, recog_for_combine created the
11523 necessary REG_UNUSED notes. Trying to keep any original
11524 REG_UNUSED notes from these insns can cause incorrect output
11525 if it is for the same register as the original i3 dest.
11526 In that case, we will notice that the register is set in i3,
11527 and then add a REG_UNUSED note for the destination of i3, which
07d0cbdd
JW
11528 is wrong. However, it is possible to have REG_UNUSED notes from
11529 i2 or i1 for register which were both used and clobbered, so
11530 we keep notes from i2 or i1 if they will turn into REG_DEAD
11531 notes. */
176c9e6b 11532
230d793d
RS
11533 /* If this register is set or clobbered in I3, put the note there
11534 unless there is one already. */
07d0cbdd 11535 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
230d793d 11536 {
07d0cbdd
JW
11537 if (from_insn != i3)
11538 break;
11539
230d793d
RS
11540 if (! (GET_CODE (XEXP (note, 0)) == REG
11541 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11542 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11543 place = i3;
11544 }
11545 /* Otherwise, if this register is used by I3, then this register
11546 now dies here, so we must put a REG_DEAD note here unless there
11547 is one already. */
11548 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11549 && ! (GET_CODE (XEXP (note, 0)) == REG
11550 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11551 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11552 {
11553 PUT_REG_NOTE_KIND (note, REG_DEAD);
11554 place = i3;
11555 }
11556 break;
11557
11558 case REG_EQUAL:
11559 case REG_EQUIV:
11560 case REG_NONNEG:
9ae8ffe7 11561 case REG_NOALIAS:
230d793d
RS
11562 /* These notes say something about results of an insn. We can
11563 only support them if they used to be on I3 in which case they
a687e897
RK
11564 remain on I3. Otherwise they are ignored.
11565
11566 If the note refers to an expression that is not a constant, we
11567 must also ignore the note since we cannot tell whether the
11568 equivalence is still true. It might be possible to do
11569 slightly better than this (we only have a problem if I2DEST
11570 or I1DEST is present in the expression), but it doesn't
11571 seem worth the trouble. */
11572
11573 if (from_insn == i3
11574 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
230d793d
RS
11575 place = i3;
11576 break;
11577
11578 case REG_INC:
11579 case REG_NO_CONFLICT:
230d793d
RS
11580 /* These notes say something about how a register is used. They must
11581 be present on any use of the register in I2 or I3. */
11582 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11583 place = i3;
11584
11585 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11586 {
11587 if (place)
11588 place2 = i2;
11589 else
11590 place = i2;
11591 }
11592 break;
11593
e55b4486
RH
11594 case REG_LABEL:
11595 /* This can show up in several ways -- either directly in the
11596 pattern, or hidden off in the constant pool with (or without?)
11597 a REG_EQUAL note. */
11598 /* ??? Ignore the without-reg_equal-note problem for now. */
11599 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11600 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11601 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11602 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11603 place = i3;
11604
11605 if (i2
11606 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11607 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11608 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11609 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11610 {
11611 if (place)
11612 place2 = i2;
11613 else
11614 place = i2;
11615 }
11616 break;
11617
230d793d
RS
11618 case REG_WAS_0:
11619 /* It is too much trouble to try to see if this note is still
11620 correct in all situations. It is better to simply delete it. */
11621 break;
11622
11623 case REG_RETVAL:
11624 /* If the insn previously containing this note still exists,
11625 put it back where it was. Otherwise move it to the previous
11626 insn. Adjust the corresponding REG_LIBCALL note. */
11627 if (GET_CODE (from_insn) != NOTE)
11628 place = from_insn;
11629 else
11630 {
5f4f0e22 11631 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
230d793d
RS
11632 place = prev_real_insn (from_insn);
11633 if (tem && place)
11634 XEXP (tem, 0) = place;
11635 }
11636 break;
11637
11638 case REG_LIBCALL:
11639 /* This is handled similarly to REG_RETVAL. */
11640 if (GET_CODE (from_insn) != NOTE)
11641 place = from_insn;
11642 else
11643 {
5f4f0e22 11644 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
230d793d
RS
11645 place = next_real_insn (from_insn);
11646 if (tem && place)
11647 XEXP (tem, 0) = place;
11648 }
11649 break;
11650
11651 case REG_DEAD:
11652 /* If the register is used as an input in I3, it dies there.
11653 Similarly for I2, if it is non-zero and adjacent to I3.
11654
11655 If the register is not used as an input in either I3 or I2
11656 and it is not one of the registers we were supposed to eliminate,
11657 there are two possibilities. We might have a non-adjacent I2
11658 or we might have somehow eliminated an additional register
11659 from a computation. For example, we might have had A & B where
11660 we discover that B will always be zero. In this case we will
11661 eliminate the reference to A.
11662
11663 In both cases, we must search to see if we can find a previous
11664 use of A and put the death note there. */
11665
6e2d1486
RK
11666 if (from_insn
11667 && GET_CODE (from_insn) == CALL_INSN
11668 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11669 place = from_insn;
11670 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
230d793d
RS
11671 place = i3;
11672 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11673 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11674 place = i2;
11675
11676 if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11677 break;
11678
510dd77e
RK
11679 /* If the register is used in both I2 and I3 and it dies in I3,
11680 we might have added another reference to it. If reg_n_refs
11681 was 2, bump it to 3. This has to be correct since the
11682 register must have been set somewhere. The reason this is
11683 done is because local-alloc.c treats 2 references as a
11684 special case. */
11685
11686 if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
b1f21e0a 11687 && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
510dd77e 11688 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
b1f21e0a 11689 REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
510dd77e 11690
230d793d 11691 if (place == 0)
38d8473f
RK
11692 {
11693 for (tem = prev_nonnote_insn (i3);
11694 place == 0 && tem
11695 && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11696 tem = prev_nonnote_insn (tem))
11697 {
11698 /* If the register is being set at TEM, see if that is all
11699 TEM is doing. If so, delete TEM. Otherwise, make this
11700 into a REG_UNUSED note instead. */
11701 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11702 {
11703 rtx set = single_set (tem);
e5e809f4 11704 rtx inner_dest = 0;
e51712db 11705#ifdef HAVE_cc0
f5c97640 11706 rtx cc0_setter = NULL_RTX;
e51712db 11707#endif
e5e809f4
JL
11708
11709 if (set != 0)
11710 for (inner_dest = SET_DEST (set);
11711 GET_CODE (inner_dest) == STRICT_LOW_PART
11712 || GET_CODE (inner_dest) == SUBREG
11713 || GET_CODE (inner_dest) == ZERO_EXTRACT;
11714 inner_dest = XEXP (inner_dest, 0))
11715 ;
38d8473f
RK
11716
11717 /* Verify that it was the set, and not a clobber that
f5c97640
RH
11718 modified the register.
11719
11720 CC0 targets must be careful to maintain setter/user
11721 pairs. If we cannot delete the setter due to side
11722 effects, mark the user with an UNUSED note instead
11723 of deleting it. */
38d8473f
RK
11724
11725 if (set != 0 && ! side_effects_p (SET_SRC (set))
f5c97640
RH
11726 && rtx_equal_p (XEXP (note, 0), inner_dest)
11727#ifdef HAVE_cc0
11728 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
11729 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
11730 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
11731#endif
11732 )
38d8473f
RK
11733 {
11734 /* Move the notes and links of TEM elsewhere.
11735 This might delete other dead insns recursively.
11736 First set the pattern to something that won't use
11737 any register. */
11738
11739 PATTERN (tem) = pc_rtx;
11740
11741 distribute_notes (REG_NOTES (tem), tem, tem,
11742 NULL_RTX, NULL_RTX, NULL_RTX);
11743 distribute_links (LOG_LINKS (tem));
11744
11745 PUT_CODE (tem, NOTE);
11746 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11747 NOTE_SOURCE_FILE (tem) = 0;
f5c97640
RH
11748
11749#ifdef HAVE_cc0
11750 /* Delete the setter too. */
11751 if (cc0_setter)
11752 {
11753 PATTERN (cc0_setter) = pc_rtx;
11754
11755 distribute_notes (REG_NOTES (cc0_setter),
11756 cc0_setter, cc0_setter,
11757 NULL_RTX, NULL_RTX, NULL_RTX);
11758 distribute_links (LOG_LINKS (cc0_setter));
11759
11760 PUT_CODE (cc0_setter, NOTE);
11761 NOTE_LINE_NUMBER (cc0_setter) = NOTE_INSN_DELETED;
11762 NOTE_SOURCE_FILE (cc0_setter) = 0;
11763 }
11764#endif
38d8473f 11765 }
e5e809f4
JL
11766 /* If the register is both set and used here, put the
11767 REG_DEAD note here, but place a REG_UNUSED note
11768 here too unless there already is one. */
11769 else if (reg_referenced_p (XEXP (note, 0),
11770 PATTERN (tem)))
11771 {
11772 place = tem;
11773
11774 if (! find_regno_note (tem, REG_UNUSED,
11775 REGNO (XEXP (note, 0))))
11776 REG_NOTES (tem)
c5c76735 11777 = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
9e6a5703 11778 REG_NOTES (tem));
e5e809f4 11779 }
38d8473f
RK
11780 else
11781 {
11782 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11783
11784 /* If there isn't already a REG_UNUSED note, put one
11785 here. */
11786 if (! find_regno_note (tem, REG_UNUSED,
11787 REGNO (XEXP (note, 0))))
11788 place = tem;
11789 break;
230d793d
RS
11790 }
11791 }
13018fad
RE
11792 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11793 || (GET_CODE (tem) == CALL_INSN
11794 && find_reg_fusage (tem, USE, XEXP (note, 0))))
230d793d
RS
11795 {
11796 place = tem;
932d1119
RK
11797
11798 /* If we are doing a 3->2 combination, and we have a
11799 register which formerly died in i3 and was not used
11800 by i2, which now no longer dies in i3 and is used in
11801 i2 but does not die in i2, and place is between i2
11802 and i3, then we may need to move a link from place to
11803 i2. */
a8908849
RK
11804 if (i2 && INSN_UID (place) <= max_uid_cuid
11805 && INSN_CUID (place) > INSN_CUID (i2)
932d1119
RK
11806 && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11807 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11808 {
11809 rtx links = LOG_LINKS (place);
11810 LOG_LINKS (place) = 0;
11811 distribute_links (links);
11812 }
230d793d
RS
11813 break;
11814 }
38d8473f
RK
11815 }
11816
11817 /* If we haven't found an insn for the death note and it
11818 is still a REG_DEAD note, but we have hit a CODE_LABEL,
11819 insert a USE insn for the register at that label and
11820 put the death node there. This prevents problems with
11821 call-state tracking in caller-save.c. */
11822 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
e2cce0cf
RK
11823 {
11824 place
38a448ca 11825 = emit_insn_after (gen_rtx_USE (VOIDmode, XEXP (note, 0)),
e2cce0cf
RK
11826 tem);
11827
11828 /* If this insn was emitted between blocks, then update
3b413743
RH
11829 BLOCK_HEAD of the current block to include it. */
11830 if (BLOCK_END (this_basic_block - 1) == tem)
11831 BLOCK_HEAD (this_basic_block) = place;
e2cce0cf 11832 }
38d8473f 11833 }
230d793d
RS
11834
11835 /* If the register is set or already dead at PLACE, we needn't do
e5e809f4
JL
11836 anything with this note if it is still a REG_DEAD note.
11837 We can here if it is set at all, not if is it totally replace,
11838 which is what `dead_or_set_p' checks, so also check for it being
11839 set partially. */
11840
230d793d 11841
230d793d
RS
11842 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11843 {
11844 int regno = REGNO (XEXP (note, 0));
11845
11846 if (dead_or_set_p (place, XEXP (note, 0))
11847 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11848 {
11849 /* Unless the register previously died in PLACE, clear
11850 reg_last_death. [I no longer understand why this is
11851 being done.] */
11852 if (reg_last_death[regno] != place)
11853 reg_last_death[regno] = 0;
11854 place = 0;
11855 }
11856 else
11857 reg_last_death[regno] = place;
11858
11859 /* If this is a death note for a hard reg that is occupying
11860 multiple registers, ensure that we are still using all
11861 parts of the object. If we find a piece of the object
11862 that is unused, we must add a USE for that piece before
11863 PLACE and put the appropriate REG_DEAD note on it.
11864
11865 An alternative would be to put a REG_UNUSED for the pieces
11866 on the insn that set the register, but that can't be done if
11867 it is not in the same block. It is simpler, though less
11868 efficient, to add the USE insns. */
11869
11870 if (place && regno < FIRST_PSEUDO_REGISTER
11871 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11872 {
11873 int endregno
11874 = regno + HARD_REGNO_NREGS (regno,
11875 GET_MODE (XEXP (note, 0)));
11876 int all_used = 1;
11877 int i;
11878
11879 for (i = regno; i < endregno; i++)
9fd5bb62
JW
11880 if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11881 && ! find_regno_fusage (place, USE, i))
230d793d 11882 {
38a448ca 11883 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
28f6d3af
RK
11884 rtx p;
11885
11886 /* See if we already placed a USE note for this
11887 register in front of PLACE. */
11888 for (p = place;
11889 GET_CODE (PREV_INSN (p)) == INSN
11890 && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11891 p = PREV_INSN (p))
11892 if (rtx_equal_p (piece,
11893 XEXP (PATTERN (PREV_INSN (p)), 0)))
11894 {
11895 p = 0;
11896 break;
11897 }
11898
11899 if (p)
11900 {
11901 rtx use_insn
38a448ca
RH
11902 = emit_insn_before (gen_rtx_USE (VOIDmode,
11903 piece),
28f6d3af
RK
11904 p);
11905 REG_NOTES (use_insn)
38a448ca
RH
11906 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11907 REG_NOTES (use_insn));
28f6d3af 11908 }
230d793d 11909
5089e22e 11910 all_used = 0;
230d793d
RS
11911 }
11912
a394b17b
JW
11913 /* Check for the case where the register dying partially
11914 overlaps the register set by this insn. */
11915 if (all_used)
11916 for (i = regno; i < endregno; i++)
11917 if (dead_or_set_regno_p (place, i))
11918 {
11919 all_used = 0;
11920 break;
11921 }
11922
230d793d
RS
11923 if (! all_used)
11924 {
11925 /* Put only REG_DEAD notes for pieces that are
11926 still used and that are not already dead or set. */
11927
11928 for (i = regno; i < endregno; i++)
11929 {
38a448ca 11930 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
230d793d 11931
17cbf358
JW
11932 if ((reg_referenced_p (piece, PATTERN (place))
11933 || (GET_CODE (place) == CALL_INSN
11934 && find_reg_fusage (place, USE, piece)))
230d793d
RS
11935 && ! dead_or_set_p (place, piece)
11936 && ! reg_bitfield_target_p (piece,
11937 PATTERN (place)))
38a448ca 11938 REG_NOTES (place)
c5c76735
JL
11939 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11940 REG_NOTES (place));
230d793d
RS
11941 }
11942
11943 place = 0;
11944 }
11945 }
11946 }
11947 break;
11948
11949 default:
11950 /* Any other notes should not be present at this point in the
11951 compilation. */
11952 abort ();
11953 }
11954
11955 if (place)
11956 {
11957 XEXP (note, 1) = REG_NOTES (place);
11958 REG_NOTES (place) = note;
11959 }
1a26b032
RK
11960 else if ((REG_NOTE_KIND (note) == REG_DEAD
11961 || REG_NOTE_KIND (note) == REG_UNUSED)
11962 && GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 11963 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
230d793d
RS
11964
11965 if (place2)
1a26b032
RK
11966 {
11967 if ((REG_NOTE_KIND (note) == REG_DEAD
11968 || REG_NOTE_KIND (note) == REG_UNUSED)
11969 && GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 11970 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
1a26b032 11971
38a448ca
RH
11972 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
11973 REG_NOTE_KIND (note),
11974 XEXP (note, 0),
11975 REG_NOTES (place2));
1a26b032 11976 }
230d793d
RS
11977 }
11978}
11979\f
11980/* Similarly to above, distribute the LOG_LINKS that used to be present on
5089e22e
RS
11981 I3, I2, and I1 to new locations. This is also called in one case to
11982 add a link pointing at I3 when I3's destination is changed. */
230d793d
RS
11983
11984static void
11985distribute_links (links)
11986 rtx links;
11987{
11988 rtx link, next_link;
11989
11990 for (link = links; link; link = next_link)
11991 {
11992 rtx place = 0;
11993 rtx insn;
11994 rtx set, reg;
11995
11996 next_link = XEXP (link, 1);
11997
11998 /* If the insn that this link points to is a NOTE or isn't a single
11999 set, ignore it. In the latter case, it isn't clear what we
12000 can do other than ignore the link, since we can't tell which
12001 register it was for. Such links wouldn't be used by combine
12002 anyway.
12003
12004 It is not possible for the destination of the target of the link to
12005 have been changed by combine. The only potential of this is if we
12006 replace I3, I2, and I1 by I3 and I2. But in that case the
12007 destination of I2 also remains unchanged. */
12008
12009 if (GET_CODE (XEXP (link, 0)) == NOTE
12010 || (set = single_set (XEXP (link, 0))) == 0)
12011 continue;
12012
12013 reg = SET_DEST (set);
12014 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12015 || GET_CODE (reg) == SIGN_EXTRACT
12016 || GET_CODE (reg) == STRICT_LOW_PART)
12017 reg = XEXP (reg, 0);
12018
12019 /* A LOG_LINK is defined as being placed on the first insn that uses
12020 a register and points to the insn that sets the register. Start
12021 searching at the next insn after the target of the link and stop
12022 when we reach a set of the register or the end of the basic block.
12023
12024 Note that this correctly handles the link that used to point from
5089e22e 12025 I3 to I2. Also note that not much searching is typically done here
230d793d
RS
12026 since most links don't point very far away. */
12027
12028 for (insn = NEXT_INSN (XEXP (link, 0));
0d4d42c3 12029 (insn && (this_basic_block == n_basic_blocks - 1
3b413743 12030 || BLOCK_HEAD (this_basic_block + 1) != insn));
230d793d
RS
12031 insn = NEXT_INSN (insn))
12032 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
12033 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12034 {
12035 if (reg_referenced_p (reg, PATTERN (insn)))
12036 place = insn;
12037 break;
12038 }
6e2d1486
RK
12039 else if (GET_CODE (insn) == CALL_INSN
12040 && find_reg_fusage (insn, USE, reg))
12041 {
12042 place = insn;
12043 break;
12044 }
230d793d
RS
12045
12046 /* If we found a place to put the link, place it there unless there
12047 is already a link to the same insn as LINK at that point. */
12048
12049 if (place)
12050 {
12051 rtx link2;
12052
12053 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12054 if (XEXP (link2, 0) == XEXP (link, 0))
12055 break;
12056
12057 if (link2 == 0)
12058 {
12059 XEXP (link, 1) = LOG_LINKS (place);
12060 LOG_LINKS (place) = link;
abe6e52f
RK
12061
12062 /* Set added_links_insn to the earliest insn we added a
12063 link to. */
12064 if (added_links_insn == 0
12065 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12066 added_links_insn = place;
230d793d
RS
12067 }
12068 }
12069 }
12070}
12071\f
1427d6d2
RK
12072/* Compute INSN_CUID for INSN, which is an insn made by combine. */
12073
12074static int
12075insn_cuid (insn)
12076 rtx insn;
12077{
12078 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12079 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
12080 insn = NEXT_INSN (insn);
12081
12082 if (INSN_UID (insn) > max_uid_cuid)
12083 abort ();
12084
12085 return INSN_CUID (insn);
12086}
12087\f
230d793d
RS
12088void
12089dump_combine_stats (file)
12090 FILE *file;
12091{
ab87f8c8 12092 fnotice
230d793d
RS
12093 (file,
12094 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12095 combine_attempts, combine_merges, combine_extras, combine_successes);
12096}
12097
12098void
12099dump_combine_total_stats (file)
12100 FILE *file;
12101{
ab87f8c8 12102 fnotice
230d793d
RS
12103 (file,
12104 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12105 total_attempts, total_merges, total_extras, total_successes);
12106}