Work towards NEXT_INSN/PREV_INSN requiring insns as their params
[gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* This module is essentially the "combiner" phase of the U. of Arizona
21 Portable Optimizer, but redone to work on our list-structured
22 representation for RTL instead of their string representation.
23
24 The LOG_LINKS of each insn identify the most recent assignment
25 to each REG used in the insn. It is a list of previous insns,
26 each of which contains a SET for a REG that is used in this insn
27 and not used or set in between. LOG_LINKs never cross basic blocks.
28 They were set up by the preceding pass (lifetime analysis).
29
30 We try to combine each pair of insns joined by a logical link.
31 We also try to combine triplets of insns A, B and C when C has
32 a link back to B and B has a link back to A. Likewise for a
33 small number of quadruplets of insns A, B, C and D for which
34 there's high likelihood of of success.
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 isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
55
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
64
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
68
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
77
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "stor-layout.h"
85 #include "tm_p.h"
86 #include "flags.h"
87 #include "regs.h"
88 #include "hard-reg-set.h"
89 #include "basic-block.h"
90 #include "insn-config.h"
91 #include "function.h"
92 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
93 #include "expr.h"
94 #include "insn-attr.h"
95 #include "recog.h"
96 #include "diagnostic-core.h"
97 #include "target.h"
98 #include "optabs.h"
99 #include "insn-codes.h"
100 #include "rtlhooks-def.h"
101 #include "params.h"
102 #include "tree-pass.h"
103 #include "df.h"
104 #include "valtrack.h"
105 #include "cgraph.h"
106 #include "obstack.h"
107 #include "statistics.h"
108 #include "params.h"
109 #include "rtl-iter.h"
110
111 /* Number of attempts to combine instructions in this function. */
112
113 static int combine_attempts;
114
115 /* Number of attempts that got as far as substitution in this function. */
116
117 static int combine_merges;
118
119 /* Number of instructions combined with added SETs in this function. */
120
121 static int combine_extras;
122
123 /* Number of instructions combined in this function. */
124
125 static int combine_successes;
126
127 /* Totals over entire compilation. */
128
129 static int total_attempts, total_merges, total_extras, total_successes;
130
131 /* combine_instructions may try to replace the right hand side of the
132 second instruction with the value of an associated REG_EQUAL note
133 before throwing it at try_combine. That is problematic when there
134 is a REG_DEAD note for a register used in the old right hand side
135 and can cause distribute_notes to do wrong things. This is the
136 second instruction if it has been so modified, null otherwise. */
137
138 static rtx_insn *i2mod;
139
140 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
141
142 static rtx i2mod_old_rhs;
143
144 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
145
146 static rtx i2mod_new_rhs;
147 \f
148 typedef struct reg_stat_struct {
149 /* Record last point of death of (hard or pseudo) register n. */
150 rtx_insn *last_death;
151
152 /* Record last point of modification of (hard or pseudo) register n. */
153 rtx_insn *last_set;
154
155 /* The next group of fields allows the recording of the last value assigned
156 to (hard or pseudo) register n. We use this information to see if an
157 operation being processed is redundant given a prior operation performed
158 on the register. For example, an `and' with a constant is redundant if
159 all the zero bits are already known to be turned off.
160
161 We use an approach similar to that used by cse, but change it in the
162 following ways:
163
164 (1) We do not want to reinitialize at each label.
165 (2) It is useful, but not critical, to know the actual value assigned
166 to a register. Often just its form is helpful.
167
168 Therefore, we maintain the following fields:
169
170 last_set_value the last value assigned
171 last_set_label records the value of label_tick when the
172 register was assigned
173 last_set_table_tick records the value of label_tick when a
174 value using the register is assigned
175 last_set_invalid set to nonzero when it is not valid
176 to use the value of this register in some
177 register's value
178
179 To understand the usage of these tables, it is important to understand
180 the distinction between the value in last_set_value being valid and
181 the register being validly contained in some other expression in the
182 table.
183
184 (The next two parameters are out of date).
185
186 reg_stat[i].last_set_value is valid if it is nonzero, and either
187 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
188
189 Register I may validly appear in any expression returned for the value
190 of another register if reg_n_sets[i] is 1. It may also appear in the
191 value for register J if reg_stat[j].last_set_invalid is zero, or
192 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
193
194 If an expression is found in the table containing a register which may
195 not validly appear in an expression, the register is replaced by
196 something that won't match, (clobber (const_int 0)). */
197
198 /* Record last value assigned to (hard or pseudo) register n. */
199
200 rtx last_set_value;
201
202 /* Record the value of label_tick when an expression involving register n
203 is placed in last_set_value. */
204
205 int last_set_table_tick;
206
207 /* Record the value of label_tick when the value for register n is placed in
208 last_set_value. */
209
210 int last_set_label;
211
212 /* These fields are maintained in parallel with last_set_value and are
213 used to store the mode in which the register was last set, the bits
214 that were known to be zero when it was last set, and the number of
215 sign bits copies it was known to have when it was last set. */
216
217 unsigned HOST_WIDE_INT last_set_nonzero_bits;
218 char last_set_sign_bit_copies;
219 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
220
221 /* Set nonzero if references to register n in expressions should not be
222 used. last_set_invalid is set nonzero when this register is being
223 assigned to and last_set_table_tick == label_tick. */
224
225 char last_set_invalid;
226
227 /* Some registers that are set more than once and used in more than one
228 basic block are nevertheless always set in similar ways. For example,
229 a QImode register may be loaded from memory in two places on a machine
230 where byte loads zero extend.
231
232 We record in the following fields if a register has some leading bits
233 that are always equal to the sign bit, and what we know about the
234 nonzero bits of a register, specifically which bits are known to be
235 zero.
236
237 If an entry is zero, it means that we don't know anything special. */
238
239 unsigned char sign_bit_copies;
240
241 unsigned HOST_WIDE_INT nonzero_bits;
242
243 /* Record the value of the label_tick when the last truncation
244 happened. The field truncated_to_mode is only valid if
245 truncation_label == label_tick. */
246
247 int truncation_label;
248
249 /* Record the last truncation seen for this register. If truncation
250 is not a nop to this mode we might be able to save an explicit
251 truncation if we know that value already contains a truncated
252 value. */
253
254 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
255 } reg_stat_type;
256
257
258 static vec<reg_stat_type> reg_stat;
259
260 /* Record the luid of the last insn that invalidated memory
261 (anything that writes memory, and subroutine calls, but not pushes). */
262
263 static int mem_last_set;
264
265 /* Record the luid of the last CALL_INSN
266 so we can tell whether a potential combination crosses any calls. */
267
268 static int last_call_luid;
269
270 /* When `subst' is called, this is the insn that is being modified
271 (by combining in a previous insn). The PATTERN of this insn
272 is still the old pattern partially modified and it should not be
273 looked at, but this may be used to examine the successors of the insn
274 to judge whether a simplification is valid. */
275
276 static rtx_insn *subst_insn;
277
278 /* This is the lowest LUID that `subst' is currently dealing with.
279 get_last_value will not return a value if the register was set at or
280 after this LUID. If not for this mechanism, we could get confused if
281 I2 or I1 in try_combine were an insn that used the old value of a register
282 to obtain a new value. In that case, we might erroneously get the
283 new value of the register when we wanted the old one. */
284
285 static int subst_low_luid;
286
287 /* This contains any hard registers that are used in newpat; reg_dead_at_p
288 must consider all these registers to be always live. */
289
290 static HARD_REG_SET newpat_used_regs;
291
292 /* This is an insn to which a LOG_LINKS entry has been added. If this
293 insn is the earlier than I2 or I3, combine should rescan starting at
294 that location. */
295
296 static rtx_insn *added_links_insn;
297
298 /* Basic block in which we are performing combines. */
299 static basic_block this_basic_block;
300 static bool optimize_this_for_speed_p;
301
302 \f
303 /* Length of the currently allocated uid_insn_cost array. */
304
305 static int max_uid_known;
306
307 /* The following array records the insn_rtx_cost for every insn
308 in the instruction stream. */
309
310 static int *uid_insn_cost;
311
312 /* The following array records the LOG_LINKS for every insn in the
313 instruction stream as struct insn_link pointers. */
314
315 struct insn_link {
316 rtx_insn *insn;
317 struct insn_link *next;
318 };
319
320 static struct insn_link **uid_log_links;
321
322 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
323 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
324
325 #define FOR_EACH_LOG_LINK(L, INSN) \
326 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
327
328 /* Links for LOG_LINKS are allocated from this obstack. */
329
330 static struct obstack insn_link_obstack;
331
332 /* Allocate a link. */
333
334 static inline struct insn_link *
335 alloc_insn_link (rtx_insn *insn, struct insn_link *next)
336 {
337 struct insn_link *l
338 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
339 sizeof (struct insn_link));
340 l->insn = insn;
341 l->next = next;
342 return l;
343 }
344
345 /* Incremented for each basic block. */
346
347 static int label_tick;
348
349 /* Reset to label_tick for each extended basic block in scanning order. */
350
351 static int label_tick_ebb_start;
352
353 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
354 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
355
356 static enum machine_mode nonzero_bits_mode;
357
358 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
359 be safely used. It is zero while computing them and after combine has
360 completed. This former test prevents propagating values based on
361 previously set values, which can be incorrect if a variable is modified
362 in a loop. */
363
364 static int nonzero_sign_valid;
365
366 \f
367 /* Record one modification to rtl structure
368 to be undone by storing old_contents into *where. */
369
370 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
371
372 struct undo
373 {
374 struct undo *next;
375 enum undo_kind kind;
376 union { rtx r; int i; enum machine_mode m; struct insn_link *l; } old_contents;
377 union { rtx *r; int *i; struct insn_link **l; } where;
378 };
379
380 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
381 num_undo says how many are currently recorded.
382
383 other_insn is nonzero if we have modified some other insn in the process
384 of working on subst_insn. It must be verified too. */
385
386 struct undobuf
387 {
388 struct undo *undos;
389 struct undo *frees;
390 rtx_insn *other_insn;
391 };
392
393 static struct undobuf undobuf;
394
395 /* Number of times the pseudo being substituted for
396 was found and replaced. */
397
398 static int n_occurrences;
399
400 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
401 enum machine_mode,
402 unsigned HOST_WIDE_INT,
403 unsigned HOST_WIDE_INT *);
404 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
405 enum machine_mode,
406 unsigned int, unsigned int *);
407 static void do_SUBST (rtx *, rtx);
408 static void do_SUBST_INT (int *, int);
409 static void init_reg_last (void);
410 static void setup_incoming_promotions (rtx_insn *);
411 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
412 static int cant_combine_insn_p (rtx_insn *);
413 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
414 rtx_insn *, rtx_insn *, rtx *, rtx *);
415 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
416 static int contains_muldiv (rtx);
417 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
418 int *, rtx_insn *);
419 static void undo_all (void);
420 static void undo_commit (void);
421 static rtx *find_split_point (rtx *, rtx_insn *, bool);
422 static rtx subst (rtx, rtx, rtx, int, int, int);
423 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
424 static rtx simplify_if_then_else (rtx);
425 static rtx simplify_set (rtx);
426 static rtx simplify_logical (rtx);
427 static rtx expand_compound_operation (rtx);
428 static const_rtx expand_field_assignment (const_rtx);
429 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
430 rtx, unsigned HOST_WIDE_INT, int, int, int);
431 static rtx extract_left_shift (rtx, int);
432 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
433 unsigned HOST_WIDE_INT *);
434 static rtx canon_reg_for_combine (rtx, rtx);
435 static rtx force_to_mode (rtx, enum machine_mode,
436 unsigned HOST_WIDE_INT, int);
437 static rtx if_then_else_cond (rtx, rtx *, rtx *);
438 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
439 static int rtx_equal_for_field_assignment_p (rtx, rtx);
440 static rtx make_field_assignment (rtx);
441 static rtx apply_distributive_law (rtx);
442 static rtx distribute_and_simplify_rtx (rtx, int);
443 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
444 unsigned HOST_WIDE_INT);
445 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
446 unsigned HOST_WIDE_INT);
447 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
448 HOST_WIDE_INT, enum machine_mode, int *);
449 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
450 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
451 int);
452 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
453 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
454 static enum rtx_code simplify_compare_const (enum rtx_code, enum machine_mode,
455 rtx, rtx *);
456 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
457 static void update_table_tick (rtx);
458 static void record_value_for_reg (rtx, rtx_insn *, rtx);
459 static void check_promoted_subreg (rtx_insn *, rtx);
460 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
461 static void record_dead_and_set_regs (rtx_insn *);
462 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
463 static rtx get_last_value (const_rtx);
464 static int use_crosses_set_p (const_rtx, int);
465 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
466 static int reg_dead_at_p (rtx, rtx_insn *);
467 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
468 static int reg_bitfield_target_p (rtx, rtx);
469 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
470 static void distribute_links (struct insn_link *);
471 static void mark_used_regs_combine (rtx);
472 static void record_promoted_value (rtx_insn *, rtx);
473 static bool unmentioned_reg_p (rtx, rtx);
474 static void record_truncated_values (rtx *, void *);
475 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
476 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
477 \f
478
479 /* It is not safe to use ordinary gen_lowpart in combine.
480 See comments in gen_lowpart_for_combine. */
481 #undef RTL_HOOKS_GEN_LOWPART
482 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
483
484 /* Our implementation of gen_lowpart never emits a new pseudo. */
485 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
486 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
487
488 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
489 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
490
491 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
492 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
493
494 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
495 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
496
497 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
498
499 \f
500 /* Convenience wrapper for the canonicalize_comparison target hook.
501 Target hooks cannot use enum rtx_code. */
502 static inline void
503 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
504 bool op0_preserve_value)
505 {
506 int code_int = (int)*code;
507 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
508 *code = (enum rtx_code)code_int;
509 }
510
511 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
512 PATTERN can not be split. Otherwise, it returns an insn sequence.
513 This is a wrapper around split_insns which ensures that the
514 reg_stat vector is made larger if the splitter creates a new
515 register. */
516
517 static rtx_insn *
518 combine_split_insns (rtx pattern, rtx insn)
519 {
520 rtx_insn *ret;
521 unsigned int nregs;
522
523 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
524 nregs = max_reg_num ();
525 if (nregs > reg_stat.length ())
526 reg_stat.safe_grow_cleared (nregs);
527 return ret;
528 }
529
530 /* This is used by find_single_use to locate an rtx in LOC that
531 contains exactly one use of DEST, which is typically either a REG
532 or CC0. It returns a pointer to the innermost rtx expression
533 containing DEST. Appearances of DEST that are being used to
534 totally replace it are not counted. */
535
536 static rtx *
537 find_single_use_1 (rtx dest, rtx *loc)
538 {
539 rtx x = *loc;
540 enum rtx_code code = GET_CODE (x);
541 rtx *result = NULL;
542 rtx *this_result;
543 int i;
544 const char *fmt;
545
546 switch (code)
547 {
548 case CONST:
549 case LABEL_REF:
550 case SYMBOL_REF:
551 CASE_CONST_ANY:
552 case CLOBBER:
553 return 0;
554
555 case SET:
556 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
557 of a REG that occupies all of the REG, the insn uses DEST if
558 it is mentioned in the destination or the source. Otherwise, we
559 need just check the source. */
560 if (GET_CODE (SET_DEST (x)) != CC0
561 && GET_CODE (SET_DEST (x)) != PC
562 && !REG_P (SET_DEST (x))
563 && ! (GET_CODE (SET_DEST (x)) == SUBREG
564 && REG_P (SUBREG_REG (SET_DEST (x)))
565 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
566 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
567 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
568 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
569 break;
570
571 return find_single_use_1 (dest, &SET_SRC (x));
572
573 case MEM:
574 case SUBREG:
575 return find_single_use_1 (dest, &XEXP (x, 0));
576
577 default:
578 break;
579 }
580
581 /* If it wasn't one of the common cases above, check each expression and
582 vector of this code. Look for a unique usage of DEST. */
583
584 fmt = GET_RTX_FORMAT (code);
585 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
586 {
587 if (fmt[i] == 'e')
588 {
589 if (dest == XEXP (x, i)
590 || (REG_P (dest) && REG_P (XEXP (x, i))
591 && REGNO (dest) == REGNO (XEXP (x, i))))
592 this_result = loc;
593 else
594 this_result = find_single_use_1 (dest, &XEXP (x, i));
595
596 if (result == NULL)
597 result = this_result;
598 else if (this_result)
599 /* Duplicate usage. */
600 return NULL;
601 }
602 else if (fmt[i] == 'E')
603 {
604 int j;
605
606 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
607 {
608 if (XVECEXP (x, i, j) == dest
609 || (REG_P (dest)
610 && REG_P (XVECEXP (x, i, j))
611 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
612 this_result = loc;
613 else
614 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
615
616 if (result == NULL)
617 result = this_result;
618 else if (this_result)
619 return NULL;
620 }
621 }
622 }
623
624 return result;
625 }
626
627
628 /* See if DEST, produced in INSN, is used only a single time in the
629 sequel. If so, return a pointer to the innermost rtx expression in which
630 it is used.
631
632 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
633
634 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
635 care about REG_DEAD notes or LOG_LINKS.
636
637 Otherwise, we find the single use by finding an insn that has a
638 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
639 only referenced once in that insn, we know that it must be the first
640 and last insn referencing DEST. */
641
642 static rtx *
643 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
644 {
645 basic_block bb;
646 rtx_insn *next;
647 rtx *result;
648 struct insn_link *link;
649
650 #ifdef HAVE_cc0
651 if (dest == cc0_rtx)
652 {
653 next = NEXT_INSN (insn);
654 if (next == 0
655 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
656 return 0;
657
658 result = find_single_use_1 (dest, &PATTERN (next));
659 if (result && ploc)
660 *ploc = next;
661 return result;
662 }
663 #endif
664
665 if (!REG_P (dest))
666 return 0;
667
668 bb = BLOCK_FOR_INSN (insn);
669 for (next = NEXT_INSN (insn);
670 next && BLOCK_FOR_INSN (next) == bb;
671 next = NEXT_INSN (next))
672 if (INSN_P (next) && dead_or_set_p (next, dest))
673 {
674 FOR_EACH_LOG_LINK (link, next)
675 if (link->insn == insn)
676 break;
677
678 if (link)
679 {
680 result = find_single_use_1 (dest, &PATTERN (next));
681 if (ploc)
682 *ploc = next;
683 return result;
684 }
685 }
686
687 return 0;
688 }
689 \f
690 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
691 insn. The substitution can be undone by undo_all. If INTO is already
692 set to NEWVAL, do not record this change. Because computing NEWVAL might
693 also call SUBST, we have to compute it before we put anything into
694 the undo table. */
695
696 static void
697 do_SUBST (rtx *into, rtx newval)
698 {
699 struct undo *buf;
700 rtx oldval = *into;
701
702 if (oldval == newval)
703 return;
704
705 /* We'd like to catch as many invalid transformations here as
706 possible. Unfortunately, there are way too many mode changes
707 that are perfectly valid, so we'd waste too much effort for
708 little gain doing the checks here. Focus on catching invalid
709 transformations involving integer constants. */
710 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
711 && CONST_INT_P (newval))
712 {
713 /* Sanity check that we're replacing oldval with a CONST_INT
714 that is a valid sign-extension for the original mode. */
715 gcc_assert (INTVAL (newval)
716 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
717
718 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
719 CONST_INT is not valid, because after the replacement, the
720 original mode would be gone. Unfortunately, we can't tell
721 when do_SUBST is called to replace the operand thereof, so we
722 perform this test on oldval instead, checking whether an
723 invalid replacement took place before we got here. */
724 gcc_assert (!(GET_CODE (oldval) == SUBREG
725 && CONST_INT_P (SUBREG_REG (oldval))));
726 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
727 && CONST_INT_P (XEXP (oldval, 0))));
728 }
729
730 if (undobuf.frees)
731 buf = undobuf.frees, undobuf.frees = buf->next;
732 else
733 buf = XNEW (struct undo);
734
735 buf->kind = UNDO_RTX;
736 buf->where.r = into;
737 buf->old_contents.r = oldval;
738 *into = newval;
739
740 buf->next = undobuf.undos, undobuf.undos = buf;
741 }
742
743 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
744
745 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
746 for the value of a HOST_WIDE_INT value (including CONST_INT) is
747 not safe. */
748
749 static void
750 do_SUBST_INT (int *into, int newval)
751 {
752 struct undo *buf;
753 int oldval = *into;
754
755 if (oldval == newval)
756 return;
757
758 if (undobuf.frees)
759 buf = undobuf.frees, undobuf.frees = buf->next;
760 else
761 buf = XNEW (struct undo);
762
763 buf->kind = UNDO_INT;
764 buf->where.i = into;
765 buf->old_contents.i = oldval;
766 *into = newval;
767
768 buf->next = undobuf.undos, undobuf.undos = buf;
769 }
770
771 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
772
773 /* Similar to SUBST, but just substitute the mode. This is used when
774 changing the mode of a pseudo-register, so that any other
775 references to the entry in the regno_reg_rtx array will change as
776 well. */
777
778 static void
779 do_SUBST_MODE (rtx *into, enum machine_mode newval)
780 {
781 struct undo *buf;
782 enum machine_mode oldval = GET_MODE (*into);
783
784 if (oldval == newval)
785 return;
786
787 if (undobuf.frees)
788 buf = undobuf.frees, undobuf.frees = buf->next;
789 else
790 buf = XNEW (struct undo);
791
792 buf->kind = UNDO_MODE;
793 buf->where.r = into;
794 buf->old_contents.m = oldval;
795 adjust_reg_mode (*into, newval);
796
797 buf->next = undobuf.undos, undobuf.undos = buf;
798 }
799
800 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
801
802 #ifndef HAVE_cc0
803 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
804
805 static void
806 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
807 {
808 struct undo *buf;
809 struct insn_link * oldval = *into;
810
811 if (oldval == newval)
812 return;
813
814 if (undobuf.frees)
815 buf = undobuf.frees, undobuf.frees = buf->next;
816 else
817 buf = XNEW (struct undo);
818
819 buf->kind = UNDO_LINKS;
820 buf->where.l = into;
821 buf->old_contents.l = oldval;
822 *into = newval;
823
824 buf->next = undobuf.undos, undobuf.undos = buf;
825 }
826
827 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
828 #endif
829 \f
830 /* Subroutine of try_combine. Determine whether the replacement patterns
831 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
832 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
833 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
834 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
835 of all the instructions can be estimated and the replacements are more
836 expensive than the original sequence. */
837
838 static bool
839 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
840 rtx newpat, rtx newi2pat, rtx newotherpat)
841 {
842 int i0_cost, i1_cost, i2_cost, i3_cost;
843 int new_i2_cost, new_i3_cost;
844 int old_cost, new_cost;
845
846 /* Lookup the original insn_rtx_costs. */
847 i2_cost = INSN_COST (i2);
848 i3_cost = INSN_COST (i3);
849
850 if (i1)
851 {
852 i1_cost = INSN_COST (i1);
853 if (i0)
854 {
855 i0_cost = INSN_COST (i0);
856 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
857 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
858 }
859 else
860 {
861 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
862 ? i1_cost + i2_cost + i3_cost : 0);
863 i0_cost = 0;
864 }
865 }
866 else
867 {
868 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
869 i1_cost = i0_cost = 0;
870 }
871
872 /* Calculate the replacement insn_rtx_costs. */
873 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
874 if (newi2pat)
875 {
876 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
877 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
878 ? new_i2_cost + new_i3_cost : 0;
879 }
880 else
881 {
882 new_cost = new_i3_cost;
883 new_i2_cost = 0;
884 }
885
886 if (undobuf.other_insn)
887 {
888 int old_other_cost, new_other_cost;
889
890 old_other_cost = INSN_COST (undobuf.other_insn);
891 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
892 if (old_other_cost > 0 && new_other_cost > 0)
893 {
894 old_cost += old_other_cost;
895 new_cost += new_other_cost;
896 }
897 else
898 old_cost = 0;
899 }
900
901 /* Disallow this combination if both new_cost and old_cost are greater than
902 zero, and new_cost is greater than old cost. */
903 if (old_cost > 0 && new_cost > old_cost)
904 {
905 if (dump_file)
906 {
907 if (i0)
908 {
909 fprintf (dump_file,
910 "rejecting combination of insns %d, %d, %d and %d\n",
911 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
912 INSN_UID (i3));
913 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
914 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
915 }
916 else if (i1)
917 {
918 fprintf (dump_file,
919 "rejecting combination of insns %d, %d and %d\n",
920 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
921 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
922 i1_cost, i2_cost, i3_cost, old_cost);
923 }
924 else
925 {
926 fprintf (dump_file,
927 "rejecting combination of insns %d and %d\n",
928 INSN_UID (i2), INSN_UID (i3));
929 fprintf (dump_file, "original costs %d + %d = %d\n",
930 i2_cost, i3_cost, old_cost);
931 }
932
933 if (newi2pat)
934 {
935 fprintf (dump_file, "replacement costs %d + %d = %d\n",
936 new_i2_cost, new_i3_cost, new_cost);
937 }
938 else
939 fprintf (dump_file, "replacement cost %d\n", new_cost);
940 }
941
942 return false;
943 }
944
945 /* Update the uid_insn_cost array with the replacement costs. */
946 INSN_COST (i2) = new_i2_cost;
947 INSN_COST (i3) = new_i3_cost;
948 if (i1)
949 {
950 INSN_COST (i1) = 0;
951 if (i0)
952 INSN_COST (i0) = 0;
953 }
954
955 return true;
956 }
957
958
959 /* Delete any insns that copy a register to itself. */
960
961 static void
962 delete_noop_moves (void)
963 {
964 rtx_insn *insn, *next;
965 basic_block bb;
966
967 FOR_EACH_BB_FN (bb, cfun)
968 {
969 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
970 {
971 next = NEXT_INSN (insn);
972 if (INSN_P (insn) && noop_move_p (insn))
973 {
974 if (dump_file)
975 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
976
977 delete_insn_and_edges (insn);
978 }
979 }
980 }
981 }
982
983 \f
984 /* Fill in log links field for all insns. */
985
986 static void
987 create_log_links (void)
988 {
989 basic_block bb;
990 rtx_insn **next_use;
991 rtx_insn *insn;
992 df_ref def, use;
993
994 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
995
996 /* Pass through each block from the end, recording the uses of each
997 register and establishing log links when def is encountered.
998 Note that we do not clear next_use array in order to save time,
999 so we have to test whether the use is in the same basic block as def.
1000
1001 There are a few cases below when we do not consider the definition or
1002 usage -- these are taken from original flow.c did. Don't ask me why it is
1003 done this way; I don't know and if it works, I don't want to know. */
1004
1005 FOR_EACH_BB_FN (bb, cfun)
1006 {
1007 FOR_BB_INSNS_REVERSE (bb, insn)
1008 {
1009 if (!NONDEBUG_INSN_P (insn))
1010 continue;
1011
1012 /* Log links are created only once. */
1013 gcc_assert (!LOG_LINKS (insn));
1014
1015 FOR_EACH_INSN_DEF (def, insn)
1016 {
1017 int regno = DF_REF_REGNO (def);
1018 rtx_insn *use_insn;
1019
1020 if (!next_use[regno])
1021 continue;
1022
1023 /* Do not consider if it is pre/post modification in MEM. */
1024 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1025 continue;
1026
1027 /* Do not make the log link for frame pointer. */
1028 if ((regno == FRAME_POINTER_REGNUM
1029 && (! reload_completed || frame_pointer_needed))
1030 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1031 || (regno == HARD_FRAME_POINTER_REGNUM
1032 && (! reload_completed || frame_pointer_needed))
1033 #endif
1034 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1035 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1036 #endif
1037 )
1038 continue;
1039
1040 use_insn = next_use[regno];
1041 if (BLOCK_FOR_INSN (use_insn) == bb)
1042 {
1043 /* flow.c claimed:
1044
1045 We don't build a LOG_LINK for hard registers contained
1046 in ASM_OPERANDs. If these registers get replaced,
1047 we might wind up changing the semantics of the insn,
1048 even if reload can make what appear to be valid
1049 assignments later. */
1050 if (regno >= FIRST_PSEUDO_REGISTER
1051 || asm_noperands (PATTERN (use_insn)) < 0)
1052 {
1053 /* Don't add duplicate links between instructions. */
1054 struct insn_link *links;
1055 FOR_EACH_LOG_LINK (links, use_insn)
1056 if (insn == links->insn)
1057 break;
1058
1059 if (!links)
1060 LOG_LINKS (use_insn)
1061 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1062 }
1063 }
1064 next_use[regno] = NULL;
1065 }
1066
1067 FOR_EACH_INSN_USE (use, insn)
1068 {
1069 int regno = DF_REF_REGNO (use);
1070
1071 /* Do not consider the usage of the stack pointer
1072 by function call. */
1073 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1074 continue;
1075
1076 next_use[regno] = insn;
1077 }
1078 }
1079 }
1080
1081 free (next_use);
1082 }
1083
1084 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1085 true if we found a LOG_LINK that proves that A feeds B. This only works
1086 if there are no instructions between A and B which could have a link
1087 depending on A, since in that case we would not record a link for B.
1088 We also check the implicit dependency created by a cc0 setter/user
1089 pair. */
1090
1091 static bool
1092 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1093 {
1094 struct insn_link *links;
1095 FOR_EACH_LOG_LINK (links, b)
1096 if (links->insn == a)
1097 return true;
1098 #ifdef HAVE_cc0
1099 if (sets_cc0_p (a))
1100 return true;
1101 #endif
1102 return false;
1103 }
1104 \f
1105 /* Main entry point for combiner. F is the first insn of the function.
1106 NREGS is the first unused pseudo-reg number.
1107
1108 Return nonzero if the combiner has turned an indirect jump
1109 instruction into a direct jump. */
1110 static int
1111 combine_instructions (rtx_insn *f, unsigned int nregs)
1112 {
1113 rtx_insn *insn, *next;
1114 #ifdef HAVE_cc0
1115 rtx_insn *prev;
1116 #endif
1117 struct insn_link *links, *nextlinks;
1118 rtx_insn *first;
1119 basic_block last_bb;
1120
1121 int new_direct_jump_p = 0;
1122
1123 for (first = f; first && !INSN_P (first); )
1124 first = NEXT_INSN (first);
1125 if (!first)
1126 return 0;
1127
1128 combine_attempts = 0;
1129 combine_merges = 0;
1130 combine_extras = 0;
1131 combine_successes = 0;
1132
1133 rtl_hooks = combine_rtl_hooks;
1134
1135 reg_stat.safe_grow_cleared (nregs);
1136
1137 init_recog_no_volatile ();
1138
1139 /* Allocate array for insn info. */
1140 max_uid_known = get_max_uid ();
1141 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1142 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1143 gcc_obstack_init (&insn_link_obstack);
1144
1145 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1146
1147 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1148 problems when, for example, we have j <<= 1 in a loop. */
1149
1150 nonzero_sign_valid = 0;
1151 label_tick = label_tick_ebb_start = 1;
1152
1153 /* Scan all SETs and see if we can deduce anything about what
1154 bits are known to be zero for some registers and how many copies
1155 of the sign bit are known to exist for those registers.
1156
1157 Also set any known values so that we can use it while searching
1158 for what bits are known to be set. */
1159
1160 setup_incoming_promotions (first);
1161 /* Allow the entry block and the first block to fall into the same EBB.
1162 Conceptually the incoming promotions are assigned to the entry block. */
1163 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1164
1165 create_log_links ();
1166 FOR_EACH_BB_FN (this_basic_block, cfun)
1167 {
1168 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1169 last_call_luid = 0;
1170 mem_last_set = -1;
1171
1172 label_tick++;
1173 if (!single_pred_p (this_basic_block)
1174 || single_pred (this_basic_block) != last_bb)
1175 label_tick_ebb_start = label_tick;
1176 last_bb = this_basic_block;
1177
1178 FOR_BB_INSNS (this_basic_block, insn)
1179 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1180 {
1181 #ifdef AUTO_INC_DEC
1182 rtx links;
1183 #endif
1184
1185 subst_low_luid = DF_INSN_LUID (insn);
1186 subst_insn = insn;
1187
1188 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1189 insn);
1190 record_dead_and_set_regs (insn);
1191
1192 #ifdef AUTO_INC_DEC
1193 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1194 if (REG_NOTE_KIND (links) == REG_INC)
1195 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1196 insn);
1197 #endif
1198
1199 /* Record the current insn_rtx_cost of this instruction. */
1200 if (NONJUMP_INSN_P (insn))
1201 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1202 optimize_this_for_speed_p);
1203 if (dump_file)
1204 fprintf (dump_file, "insn_cost %d: %d\n",
1205 INSN_UID (insn), INSN_COST (insn));
1206 }
1207 }
1208
1209 nonzero_sign_valid = 1;
1210
1211 /* Now scan all the insns in forward order. */
1212 label_tick = label_tick_ebb_start = 1;
1213 init_reg_last ();
1214 setup_incoming_promotions (first);
1215 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1216 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1217
1218 FOR_EACH_BB_FN (this_basic_block, cfun)
1219 {
1220 rtx_insn *last_combined_insn = NULL;
1221 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1222 last_call_luid = 0;
1223 mem_last_set = -1;
1224
1225 label_tick++;
1226 if (!single_pred_p (this_basic_block)
1227 || single_pred (this_basic_block) != last_bb)
1228 label_tick_ebb_start = label_tick;
1229 last_bb = this_basic_block;
1230
1231 rtl_profile_for_bb (this_basic_block);
1232 for (insn = BB_HEAD (this_basic_block);
1233 insn != NEXT_INSN (BB_END (this_basic_block));
1234 insn = next ? next : NEXT_INSN (insn))
1235 {
1236 next = 0;
1237 if (!NONDEBUG_INSN_P (insn))
1238 continue;
1239
1240 while (last_combined_insn
1241 && INSN_DELETED_P (last_combined_insn))
1242 last_combined_insn = PREV_INSN (last_combined_insn);
1243 if (last_combined_insn == NULL_RTX
1244 || BARRIER_P (last_combined_insn)
1245 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1246 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1247 last_combined_insn = insn;
1248
1249 /* See if we know about function return values before this
1250 insn based upon SUBREG flags. */
1251 check_promoted_subreg (insn, PATTERN (insn));
1252
1253 /* See if we can find hardregs and subreg of pseudos in
1254 narrower modes. This could help turning TRUNCATEs
1255 into SUBREGs. */
1256 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1257
1258 /* Try this insn with each insn it links back to. */
1259
1260 FOR_EACH_LOG_LINK (links, insn)
1261 if ((next = try_combine (insn, links->insn, NULL,
1262 NULL, &new_direct_jump_p,
1263 last_combined_insn)) != 0)
1264 {
1265 statistics_counter_event (cfun, "two-insn combine", 1);
1266 goto retry;
1267 }
1268
1269 /* Try each sequence of three linked insns ending with this one. */
1270
1271 if (max_combine >= 3)
1272 FOR_EACH_LOG_LINK (links, insn)
1273 {
1274 rtx_insn *link = links->insn;
1275
1276 /* If the linked insn has been replaced by a note, then there
1277 is no point in pursuing this chain any further. */
1278 if (NOTE_P (link))
1279 continue;
1280
1281 FOR_EACH_LOG_LINK (nextlinks, link)
1282 if ((next = try_combine (insn, link, nextlinks->insn,
1283 NULL, &new_direct_jump_p,
1284 last_combined_insn)) != 0)
1285 {
1286 statistics_counter_event (cfun, "three-insn combine", 1);
1287 goto retry;
1288 }
1289 }
1290
1291 #ifdef HAVE_cc0
1292 /* Try to combine a jump insn that uses CC0
1293 with a preceding insn that sets CC0, and maybe with its
1294 logical predecessor as well.
1295 This is how we make decrement-and-branch insns.
1296 We need this special code because data flow connections
1297 via CC0 do not get entered in LOG_LINKS. */
1298
1299 if (JUMP_P (insn)
1300 && (prev = prev_nonnote_insn (insn)) != 0
1301 && NONJUMP_INSN_P (prev)
1302 && sets_cc0_p (PATTERN (prev)))
1303 {
1304 if ((next = try_combine (insn, prev, NULL, NULL,
1305 &new_direct_jump_p,
1306 last_combined_insn)) != 0)
1307 goto retry;
1308
1309 FOR_EACH_LOG_LINK (nextlinks, prev)
1310 if ((next = try_combine (insn, prev, nextlinks->insn,
1311 NULL, &new_direct_jump_p,
1312 last_combined_insn)) != 0)
1313 goto retry;
1314 }
1315
1316 /* Do the same for an insn that explicitly references CC0. */
1317 if (NONJUMP_INSN_P (insn)
1318 && (prev = prev_nonnote_insn (insn)) != 0
1319 && NONJUMP_INSN_P (prev)
1320 && sets_cc0_p (PATTERN (prev))
1321 && GET_CODE (PATTERN (insn)) == SET
1322 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1323 {
1324 if ((next = try_combine (insn, prev, NULL, NULL,
1325 &new_direct_jump_p,
1326 last_combined_insn)) != 0)
1327 goto retry;
1328
1329 FOR_EACH_LOG_LINK (nextlinks, prev)
1330 if ((next = try_combine (insn, prev, nextlinks->insn,
1331 NULL, &new_direct_jump_p,
1332 last_combined_insn)) != 0)
1333 goto retry;
1334 }
1335
1336 /* Finally, see if any of the insns that this insn links to
1337 explicitly references CC0. If so, try this insn, that insn,
1338 and its predecessor if it sets CC0. */
1339 FOR_EACH_LOG_LINK (links, insn)
1340 if (NONJUMP_INSN_P (links->insn)
1341 && GET_CODE (PATTERN (links->insn)) == SET
1342 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1343 && (prev = prev_nonnote_insn (links->insn)) != 0
1344 && NONJUMP_INSN_P (prev)
1345 && sets_cc0_p (PATTERN (prev))
1346 && (next = try_combine (insn, links->insn,
1347 prev, NULL, &new_direct_jump_p,
1348 last_combined_insn)) != 0)
1349 goto retry;
1350 #endif
1351
1352 /* Try combining an insn with two different insns whose results it
1353 uses. */
1354 if (max_combine >= 3)
1355 FOR_EACH_LOG_LINK (links, insn)
1356 for (nextlinks = links->next; nextlinks;
1357 nextlinks = nextlinks->next)
1358 if ((next = try_combine (insn, links->insn,
1359 nextlinks->insn, NULL,
1360 &new_direct_jump_p,
1361 last_combined_insn)) != 0)
1362
1363 {
1364 statistics_counter_event (cfun, "three-insn combine", 1);
1365 goto retry;
1366 }
1367
1368 /* Try four-instruction combinations. */
1369 if (max_combine >= 4)
1370 FOR_EACH_LOG_LINK (links, insn)
1371 {
1372 struct insn_link *next1;
1373 rtx_insn *link = links->insn;
1374
1375 /* If the linked insn has been replaced by a note, then there
1376 is no point in pursuing this chain any further. */
1377 if (NOTE_P (link))
1378 continue;
1379
1380 FOR_EACH_LOG_LINK (next1, link)
1381 {
1382 rtx_insn *link1 = next1->insn;
1383 if (NOTE_P (link1))
1384 continue;
1385 /* I0 -> I1 -> I2 -> I3. */
1386 FOR_EACH_LOG_LINK (nextlinks, link1)
1387 if ((next = try_combine (insn, link, link1,
1388 nextlinks->insn,
1389 &new_direct_jump_p,
1390 last_combined_insn)) != 0)
1391 {
1392 statistics_counter_event (cfun, "four-insn combine", 1);
1393 goto retry;
1394 }
1395 /* I0, I1 -> I2, I2 -> I3. */
1396 for (nextlinks = next1->next; nextlinks;
1397 nextlinks = nextlinks->next)
1398 if ((next = try_combine (insn, link, link1,
1399 nextlinks->insn,
1400 &new_direct_jump_p,
1401 last_combined_insn)) != 0)
1402 {
1403 statistics_counter_event (cfun, "four-insn combine", 1);
1404 goto retry;
1405 }
1406 }
1407
1408 for (next1 = links->next; next1; next1 = next1->next)
1409 {
1410 rtx_insn *link1 = next1->insn;
1411 if (NOTE_P (link1))
1412 continue;
1413 /* I0 -> I2; I1, I2 -> I3. */
1414 FOR_EACH_LOG_LINK (nextlinks, link)
1415 if ((next = try_combine (insn, link, link1,
1416 nextlinks->insn,
1417 &new_direct_jump_p,
1418 last_combined_insn)) != 0)
1419 {
1420 statistics_counter_event (cfun, "four-insn combine", 1);
1421 goto retry;
1422 }
1423 /* I0 -> I1; I1, I2 -> I3. */
1424 FOR_EACH_LOG_LINK (nextlinks, link1)
1425 if ((next = try_combine (insn, link, link1,
1426 nextlinks->insn,
1427 &new_direct_jump_p,
1428 last_combined_insn)) != 0)
1429 {
1430 statistics_counter_event (cfun, "four-insn combine", 1);
1431 goto retry;
1432 }
1433 }
1434 }
1435
1436 /* Try this insn with each REG_EQUAL note it links back to. */
1437 FOR_EACH_LOG_LINK (links, insn)
1438 {
1439 rtx set, note;
1440 rtx_insn *temp = links->insn;
1441 if ((set = single_set (temp)) != 0
1442 && (note = find_reg_equal_equiv_note (temp)) != 0
1443 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1444 /* Avoid using a register that may already been marked
1445 dead by an earlier instruction. */
1446 && ! unmentioned_reg_p (note, SET_SRC (set))
1447 && (GET_MODE (note) == VOIDmode
1448 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1449 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1450 {
1451 /* Temporarily replace the set's source with the
1452 contents of the REG_EQUAL note. The insn will
1453 be deleted or recognized by try_combine. */
1454 rtx orig = SET_SRC (set);
1455 SET_SRC (set) = note;
1456 i2mod = temp;
1457 i2mod_old_rhs = copy_rtx (orig);
1458 i2mod_new_rhs = copy_rtx (note);
1459 next = try_combine (insn, i2mod, NULL, NULL,
1460 &new_direct_jump_p,
1461 last_combined_insn);
1462 i2mod = NULL;
1463 if (next)
1464 {
1465 statistics_counter_event (cfun, "insn-with-note combine", 1);
1466 goto retry;
1467 }
1468 SET_SRC (set) = orig;
1469 }
1470 }
1471
1472 if (!NOTE_P (insn))
1473 record_dead_and_set_regs (insn);
1474
1475 retry:
1476 ;
1477 }
1478 }
1479
1480 default_rtl_profile ();
1481 clear_bb_flags ();
1482 new_direct_jump_p |= purge_all_dead_edges ();
1483 delete_noop_moves ();
1484
1485 /* Clean up. */
1486 obstack_free (&insn_link_obstack, NULL);
1487 free (uid_log_links);
1488 free (uid_insn_cost);
1489 reg_stat.release ();
1490
1491 {
1492 struct undo *undo, *next;
1493 for (undo = undobuf.frees; undo; undo = next)
1494 {
1495 next = undo->next;
1496 free (undo);
1497 }
1498 undobuf.frees = 0;
1499 }
1500
1501 total_attempts += combine_attempts;
1502 total_merges += combine_merges;
1503 total_extras += combine_extras;
1504 total_successes += combine_successes;
1505
1506 nonzero_sign_valid = 0;
1507 rtl_hooks = general_rtl_hooks;
1508
1509 /* Make recognizer allow volatile MEMs again. */
1510 init_recog ();
1511
1512 return new_direct_jump_p;
1513 }
1514
1515 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1516
1517 static void
1518 init_reg_last (void)
1519 {
1520 unsigned int i;
1521 reg_stat_type *p;
1522
1523 FOR_EACH_VEC_ELT (reg_stat, i, p)
1524 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1525 }
1526 \f
1527 /* Set up any promoted values for incoming argument registers. */
1528
1529 static void
1530 setup_incoming_promotions (rtx_insn *first)
1531 {
1532 tree arg;
1533 bool strictly_local = false;
1534
1535 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1536 arg = DECL_CHAIN (arg))
1537 {
1538 rtx x, reg = DECL_INCOMING_RTL (arg);
1539 int uns1, uns3;
1540 enum machine_mode mode1, mode2, mode3, mode4;
1541
1542 /* Only continue if the incoming argument is in a register. */
1543 if (!REG_P (reg))
1544 continue;
1545
1546 /* Determine, if possible, whether all call sites of the current
1547 function lie within the current compilation unit. (This does
1548 take into account the exporting of a function via taking its
1549 address, and so forth.) */
1550 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1551
1552 /* The mode and signedness of the argument before any promotions happen
1553 (equal to the mode of the pseudo holding it at that stage). */
1554 mode1 = TYPE_MODE (TREE_TYPE (arg));
1555 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1556
1557 /* The mode and signedness of the argument after any source language and
1558 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1559 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1560 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1561
1562 /* The mode and signedness of the argument as it is actually passed,
1563 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1564 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1565 TREE_TYPE (cfun->decl), 0);
1566
1567 /* The mode of the register in which the argument is being passed. */
1568 mode4 = GET_MODE (reg);
1569
1570 /* Eliminate sign extensions in the callee when:
1571 (a) A mode promotion has occurred; */
1572 if (mode1 == mode3)
1573 continue;
1574 /* (b) The mode of the register is the same as the mode of
1575 the argument as it is passed; */
1576 if (mode3 != mode4)
1577 continue;
1578 /* (c) There's no language level extension; */
1579 if (mode1 == mode2)
1580 ;
1581 /* (c.1) All callers are from the current compilation unit. If that's
1582 the case we don't have to rely on an ABI, we only have to know
1583 what we're generating right now, and we know that we will do the
1584 mode1 to mode2 promotion with the given sign. */
1585 else if (!strictly_local)
1586 continue;
1587 /* (c.2) The combination of the two promotions is useful. This is
1588 true when the signs match, or if the first promotion is unsigned.
1589 In the later case, (sign_extend (zero_extend x)) is the same as
1590 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1591 else if (uns1)
1592 uns3 = true;
1593 else if (uns3)
1594 continue;
1595
1596 /* Record that the value was promoted from mode1 to mode3,
1597 so that any sign extension at the head of the current
1598 function may be eliminated. */
1599 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1600 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1601 record_value_for_reg (reg, first, x);
1602 }
1603 }
1604
1605 /* Called via note_stores. If X is a pseudo that is narrower than
1606 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1607
1608 If we are setting only a portion of X and we can't figure out what
1609 portion, assume all bits will be used since we don't know what will
1610 be happening.
1611
1612 Similarly, set how many bits of X are known to be copies of the sign bit
1613 at all locations in the function. This is the smallest number implied
1614 by any set of X. */
1615
1616 static void
1617 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1618 {
1619 rtx_insn *insn = (rtx_insn *) data;
1620 unsigned int num;
1621
1622 if (REG_P (x)
1623 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1624 /* If this register is undefined at the start of the file, we can't
1625 say what its contents were. */
1626 && ! REGNO_REG_SET_P
1627 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1628 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1629 {
1630 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1631
1632 if (set == 0 || GET_CODE (set) == CLOBBER)
1633 {
1634 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1635 rsp->sign_bit_copies = 1;
1636 return;
1637 }
1638
1639 /* If this register is being initialized using itself, and the
1640 register is uninitialized in this basic block, and there are
1641 no LOG_LINKS which set the register, then part of the
1642 register is uninitialized. In that case we can't assume
1643 anything about the number of nonzero bits.
1644
1645 ??? We could do better if we checked this in
1646 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1647 could avoid making assumptions about the insn which initially
1648 sets the register, while still using the information in other
1649 insns. We would have to be careful to check every insn
1650 involved in the combination. */
1651
1652 if (insn
1653 && reg_referenced_p (x, PATTERN (insn))
1654 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1655 REGNO (x)))
1656 {
1657 struct insn_link *link;
1658
1659 FOR_EACH_LOG_LINK (link, insn)
1660 if (dead_or_set_p (link->insn, x))
1661 break;
1662 if (!link)
1663 {
1664 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1665 rsp->sign_bit_copies = 1;
1666 return;
1667 }
1668 }
1669
1670 /* If this is a complex assignment, see if we can convert it into a
1671 simple assignment. */
1672 set = expand_field_assignment (set);
1673
1674 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1675 set what we know about X. */
1676
1677 if (SET_DEST (set) == x
1678 || (paradoxical_subreg_p (SET_DEST (set))
1679 && SUBREG_REG (SET_DEST (set)) == x))
1680 {
1681 rtx src = SET_SRC (set);
1682
1683 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1684 /* If X is narrower than a word and SRC is a non-negative
1685 constant that would appear negative in the mode of X,
1686 sign-extend it for use in reg_stat[].nonzero_bits because some
1687 machines (maybe most) will actually do the sign-extension
1688 and this is the conservative approach.
1689
1690 ??? For 2.5, try to tighten up the MD files in this regard
1691 instead of this kludge. */
1692
1693 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1694 && CONST_INT_P (src)
1695 && INTVAL (src) > 0
1696 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1697 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1698 #endif
1699
1700 /* Don't call nonzero_bits if it cannot change anything. */
1701 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1702 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1703 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1704 if (rsp->sign_bit_copies == 0
1705 || rsp->sign_bit_copies > num)
1706 rsp->sign_bit_copies = num;
1707 }
1708 else
1709 {
1710 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1711 rsp->sign_bit_copies = 1;
1712 }
1713 }
1714 }
1715 \f
1716 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1717 optionally insns that were previously combined into I3 or that will be
1718 combined into the merger of INSN and I3. The order is PRED, PRED2,
1719 INSN, SUCC, SUCC2, I3.
1720
1721 Return 0 if the combination is not allowed for any reason.
1722
1723 If the combination is allowed, *PDEST will be set to the single
1724 destination of INSN and *PSRC to the single source, and this function
1725 will return 1. */
1726
1727 static int
1728 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1729 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1730 rtx *pdest, rtx *psrc)
1731 {
1732 int i;
1733 const_rtx set = 0;
1734 rtx src, dest;
1735 rtx_insn *p;
1736 #ifdef AUTO_INC_DEC
1737 rtx link;
1738 #endif
1739 bool all_adjacent = true;
1740 int (*is_volatile_p) (const_rtx);
1741
1742 if (succ)
1743 {
1744 if (succ2)
1745 {
1746 if (next_active_insn (succ2) != i3)
1747 all_adjacent = false;
1748 if (next_active_insn (succ) != succ2)
1749 all_adjacent = false;
1750 }
1751 else if (next_active_insn (succ) != i3)
1752 all_adjacent = false;
1753 if (next_active_insn (insn) != succ)
1754 all_adjacent = false;
1755 }
1756 else if (next_active_insn (insn) != i3)
1757 all_adjacent = false;
1758
1759 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1760 or a PARALLEL consisting of such a SET and CLOBBERs.
1761
1762 If INSN has CLOBBER parallel parts, ignore them for our processing.
1763 By definition, these happen during the execution of the insn. When it
1764 is merged with another insn, all bets are off. If they are, in fact,
1765 needed and aren't also supplied in I3, they may be added by
1766 recog_for_combine. Otherwise, it won't match.
1767
1768 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1769 note.
1770
1771 Get the source and destination of INSN. If more than one, can't
1772 combine. */
1773
1774 if (GET_CODE (PATTERN (insn)) == SET)
1775 set = PATTERN (insn);
1776 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1777 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1778 {
1779 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1780 {
1781 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1782
1783 switch (GET_CODE (elt))
1784 {
1785 /* This is important to combine floating point insns
1786 for the SH4 port. */
1787 case USE:
1788 /* Combining an isolated USE doesn't make sense.
1789 We depend here on combinable_i3pat to reject them. */
1790 /* The code below this loop only verifies that the inputs of
1791 the SET in INSN do not change. We call reg_set_between_p
1792 to verify that the REG in the USE does not change between
1793 I3 and INSN.
1794 If the USE in INSN was for a pseudo register, the matching
1795 insn pattern will likely match any register; combining this
1796 with any other USE would only be safe if we knew that the
1797 used registers have identical values, or if there was
1798 something to tell them apart, e.g. different modes. For
1799 now, we forgo such complicated tests and simply disallow
1800 combining of USES of pseudo registers with any other USE. */
1801 if (REG_P (XEXP (elt, 0))
1802 && GET_CODE (PATTERN (i3)) == PARALLEL)
1803 {
1804 rtx i3pat = PATTERN (i3);
1805 int i = XVECLEN (i3pat, 0) - 1;
1806 unsigned int regno = REGNO (XEXP (elt, 0));
1807
1808 do
1809 {
1810 rtx i3elt = XVECEXP (i3pat, 0, i);
1811
1812 if (GET_CODE (i3elt) == USE
1813 && REG_P (XEXP (i3elt, 0))
1814 && (REGNO (XEXP (i3elt, 0)) == regno
1815 ? reg_set_between_p (XEXP (elt, 0),
1816 PREV_INSN (insn), i3)
1817 : regno >= FIRST_PSEUDO_REGISTER))
1818 return 0;
1819 }
1820 while (--i >= 0);
1821 }
1822 break;
1823
1824 /* We can ignore CLOBBERs. */
1825 case CLOBBER:
1826 break;
1827
1828 case SET:
1829 /* Ignore SETs whose result isn't used but not those that
1830 have side-effects. */
1831 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1832 && insn_nothrow_p (insn)
1833 && !side_effects_p (elt))
1834 break;
1835
1836 /* If we have already found a SET, this is a second one and
1837 so we cannot combine with this insn. */
1838 if (set)
1839 return 0;
1840
1841 set = elt;
1842 break;
1843
1844 default:
1845 /* Anything else means we can't combine. */
1846 return 0;
1847 }
1848 }
1849
1850 if (set == 0
1851 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1852 so don't do anything with it. */
1853 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1854 return 0;
1855 }
1856 else
1857 return 0;
1858
1859 if (set == 0)
1860 return 0;
1861
1862 /* The simplification in expand_field_assignment may call back to
1863 get_last_value, so set safe guard here. */
1864 subst_low_luid = DF_INSN_LUID (insn);
1865
1866 set = expand_field_assignment (set);
1867 src = SET_SRC (set), dest = SET_DEST (set);
1868
1869 /* Don't eliminate a store in the stack pointer. */
1870 if (dest == stack_pointer_rtx
1871 /* Don't combine with an insn that sets a register to itself if it has
1872 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1873 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1874 /* Can't merge an ASM_OPERANDS. */
1875 || GET_CODE (src) == ASM_OPERANDS
1876 /* Can't merge a function call. */
1877 || GET_CODE (src) == CALL
1878 /* Don't eliminate a function call argument. */
1879 || (CALL_P (i3)
1880 && (find_reg_fusage (i3, USE, dest)
1881 || (REG_P (dest)
1882 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1883 && global_regs[REGNO (dest)])))
1884 /* Don't substitute into an incremented register. */
1885 || FIND_REG_INC_NOTE (i3, dest)
1886 || (succ && FIND_REG_INC_NOTE (succ, dest))
1887 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1888 /* Don't substitute into a non-local goto, this confuses CFG. */
1889 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1890 /* Make sure that DEST is not used after SUCC but before I3. */
1891 || (!all_adjacent
1892 && ((succ2
1893 && (reg_used_between_p (dest, succ2, i3)
1894 || reg_used_between_p (dest, succ, succ2)))
1895 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1896 /* Make sure that the value that is to be substituted for the register
1897 does not use any registers whose values alter in between. However,
1898 If the insns are adjacent, a use can't cross a set even though we
1899 think it might (this can happen for a sequence of insns each setting
1900 the same destination; last_set of that register might point to
1901 a NOTE). If INSN has a REG_EQUIV note, the register is always
1902 equivalent to the memory so the substitution is valid even if there
1903 are intervening stores. Also, don't move a volatile asm or
1904 UNSPEC_VOLATILE across any other insns. */
1905 || (! all_adjacent
1906 && (((!MEM_P (src)
1907 || ! find_reg_note (insn, REG_EQUIV, src))
1908 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1909 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1910 || GET_CODE (src) == UNSPEC_VOLATILE))
1911 /* Don't combine across a CALL_INSN, because that would possibly
1912 change whether the life span of some REGs crosses calls or not,
1913 and it is a pain to update that information.
1914 Exception: if source is a constant, moving it later can't hurt.
1915 Accept that as a special case. */
1916 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1917 return 0;
1918
1919 /* DEST must either be a REG or CC0. */
1920 if (REG_P (dest))
1921 {
1922 /* If register alignment is being enforced for multi-word items in all
1923 cases except for parameters, it is possible to have a register copy
1924 insn referencing a hard register that is not allowed to contain the
1925 mode being copied and which would not be valid as an operand of most
1926 insns. Eliminate this problem by not combining with such an insn.
1927
1928 Also, on some machines we don't want to extend the life of a hard
1929 register. */
1930
1931 if (REG_P (src)
1932 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1933 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1934 /* Don't extend the life of a hard register unless it is
1935 user variable (if we have few registers) or it can't
1936 fit into the desired register (meaning something special
1937 is going on).
1938 Also avoid substituting a return register into I3, because
1939 reload can't handle a conflict with constraints of other
1940 inputs. */
1941 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1942 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1943 return 0;
1944 }
1945 else if (GET_CODE (dest) != CC0)
1946 return 0;
1947
1948
1949 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1950 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1951 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1952 {
1953 /* Don't substitute for a register intended as a clobberable
1954 operand. */
1955 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1956 if (rtx_equal_p (reg, dest))
1957 return 0;
1958
1959 /* If the clobber represents an earlyclobber operand, we must not
1960 substitute an expression containing the clobbered register.
1961 As we do not analyze the constraint strings here, we have to
1962 make the conservative assumption. However, if the register is
1963 a fixed hard reg, the clobber cannot represent any operand;
1964 we leave it up to the machine description to either accept or
1965 reject use-and-clobber patterns. */
1966 if (!REG_P (reg)
1967 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1968 || !fixed_regs[REGNO (reg)])
1969 if (reg_overlap_mentioned_p (reg, src))
1970 return 0;
1971 }
1972
1973 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1974 or not), reject, unless nothing volatile comes between it and I3 */
1975
1976 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1977 {
1978 /* Make sure neither succ nor succ2 contains a volatile reference. */
1979 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1980 return 0;
1981 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1982 return 0;
1983 /* We'll check insns between INSN and I3 below. */
1984 }
1985
1986 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1987 to be an explicit register variable, and was chosen for a reason. */
1988
1989 if (GET_CODE (src) == ASM_OPERANDS
1990 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1991 return 0;
1992
1993 /* If INSN contains volatile references (specifically volatile MEMs),
1994 we cannot combine across any other volatile references.
1995 Even if INSN doesn't contain volatile references, any intervening
1996 volatile insn might affect machine state. */
1997
1998 is_volatile_p = volatile_refs_p (PATTERN (insn))
1999 ? volatile_refs_p
2000 : volatile_insn_p;
2001
2002 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2003 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2004 return 0;
2005
2006 /* If INSN contains an autoincrement or autodecrement, make sure that
2007 register is not used between there and I3, and not already used in
2008 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2009 Also insist that I3 not be a jump; if it were one
2010 and the incremented register were spilled, we would lose. */
2011
2012 #ifdef AUTO_INC_DEC
2013 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2014 if (REG_NOTE_KIND (link) == REG_INC
2015 && (JUMP_P (i3)
2016 || reg_used_between_p (XEXP (link, 0), insn, i3)
2017 || (pred != NULL_RTX
2018 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2019 || (pred2 != NULL_RTX
2020 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2021 || (succ != NULL_RTX
2022 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2023 || (succ2 != NULL_RTX
2024 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2025 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2026 return 0;
2027 #endif
2028
2029 #ifdef HAVE_cc0
2030 /* Don't combine an insn that follows a CC0-setting insn.
2031 An insn that uses CC0 must not be separated from the one that sets it.
2032 We do, however, allow I2 to follow a CC0-setting insn if that insn
2033 is passed as I1; in that case it will be deleted also.
2034 We also allow combining in this case if all the insns are adjacent
2035 because that would leave the two CC0 insns adjacent as well.
2036 It would be more logical to test whether CC0 occurs inside I1 or I2,
2037 but that would be much slower, and this ought to be equivalent. */
2038
2039 p = prev_nonnote_insn (insn);
2040 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2041 && ! all_adjacent)
2042 return 0;
2043 #endif
2044
2045 /* If we get here, we have passed all the tests and the combination is
2046 to be allowed. */
2047
2048 *pdest = dest;
2049 *psrc = src;
2050
2051 return 1;
2052 }
2053 \f
2054 /* LOC is the location within I3 that contains its pattern or the component
2055 of a PARALLEL of the pattern. We validate that it is valid for combining.
2056
2057 One problem is if I3 modifies its output, as opposed to replacing it
2058 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2059 doing so would produce an insn that is not equivalent to the original insns.
2060
2061 Consider:
2062
2063 (set (reg:DI 101) (reg:DI 100))
2064 (set (subreg:SI (reg:DI 101) 0) <foo>)
2065
2066 This is NOT equivalent to:
2067
2068 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2069 (set (reg:DI 101) (reg:DI 100))])
2070
2071 Not only does this modify 100 (in which case it might still be valid
2072 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2073
2074 We can also run into a problem if I2 sets a register that I1
2075 uses and I1 gets directly substituted into I3 (not via I2). In that
2076 case, we would be getting the wrong value of I2DEST into I3, so we
2077 must reject the combination. This case occurs when I2 and I1 both
2078 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2079 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2080 of a SET must prevent combination from occurring. The same situation
2081 can occur for I0, in which case I0_NOT_IN_SRC is set.
2082
2083 Before doing the above check, we first try to expand a field assignment
2084 into a set of logical operations.
2085
2086 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2087 we place a register that is both set and used within I3. If more than one
2088 such register is detected, we fail.
2089
2090 Return 1 if the combination is valid, zero otherwise. */
2091
2092 static int
2093 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2094 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2095 {
2096 rtx x = *loc;
2097
2098 if (GET_CODE (x) == SET)
2099 {
2100 rtx set = x ;
2101 rtx dest = SET_DEST (set);
2102 rtx src = SET_SRC (set);
2103 rtx inner_dest = dest;
2104 rtx subdest;
2105
2106 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2107 || GET_CODE (inner_dest) == SUBREG
2108 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2109 inner_dest = XEXP (inner_dest, 0);
2110
2111 /* Check for the case where I3 modifies its output, as discussed
2112 above. We don't want to prevent pseudos from being combined
2113 into the address of a MEM, so only prevent the combination if
2114 i1 or i2 set the same MEM. */
2115 if ((inner_dest != dest &&
2116 (!MEM_P (inner_dest)
2117 || rtx_equal_p (i2dest, inner_dest)
2118 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2119 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2120 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2121 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2122 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2123
2124 /* This is the same test done in can_combine_p except we can't test
2125 all_adjacent; we don't have to, since this instruction will stay
2126 in place, thus we are not considering increasing the lifetime of
2127 INNER_DEST.
2128
2129 Also, if this insn sets a function argument, combining it with
2130 something that might need a spill could clobber a previous
2131 function argument; the all_adjacent test in can_combine_p also
2132 checks this; here, we do a more specific test for this case. */
2133
2134 || (REG_P (inner_dest)
2135 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2136 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2137 GET_MODE (inner_dest))))
2138 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2139 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2140 return 0;
2141
2142 /* If DEST is used in I3, it is being killed in this insn, so
2143 record that for later. We have to consider paradoxical
2144 subregs here, since they kill the whole register, but we
2145 ignore partial subregs, STRICT_LOW_PART, etc.
2146 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2147 STACK_POINTER_REGNUM, since these are always considered to be
2148 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2149 subdest = dest;
2150 if (GET_CODE (subdest) == SUBREG
2151 && (GET_MODE_SIZE (GET_MODE (subdest))
2152 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2153 subdest = SUBREG_REG (subdest);
2154 if (pi3dest_killed
2155 && REG_P (subdest)
2156 && reg_referenced_p (subdest, PATTERN (i3))
2157 && REGNO (subdest) != FRAME_POINTER_REGNUM
2158 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2159 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2160 #endif
2161 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2162 && (REGNO (subdest) != ARG_POINTER_REGNUM
2163 || ! fixed_regs [REGNO (subdest)])
2164 #endif
2165 && REGNO (subdest) != STACK_POINTER_REGNUM)
2166 {
2167 if (*pi3dest_killed)
2168 return 0;
2169
2170 *pi3dest_killed = subdest;
2171 }
2172 }
2173
2174 else if (GET_CODE (x) == PARALLEL)
2175 {
2176 int i;
2177
2178 for (i = 0; i < XVECLEN (x, 0); i++)
2179 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2180 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2181 return 0;
2182 }
2183
2184 return 1;
2185 }
2186 \f
2187 /* Return 1 if X is an arithmetic expression that contains a multiplication
2188 and division. We don't count multiplications by powers of two here. */
2189
2190 static int
2191 contains_muldiv (rtx x)
2192 {
2193 switch (GET_CODE (x))
2194 {
2195 case MOD: case DIV: case UMOD: case UDIV:
2196 return 1;
2197
2198 case MULT:
2199 return ! (CONST_INT_P (XEXP (x, 1))
2200 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2201 default:
2202 if (BINARY_P (x))
2203 return contains_muldiv (XEXP (x, 0))
2204 || contains_muldiv (XEXP (x, 1));
2205
2206 if (UNARY_P (x))
2207 return contains_muldiv (XEXP (x, 0));
2208
2209 return 0;
2210 }
2211 }
2212 \f
2213 /* Determine whether INSN can be used in a combination. Return nonzero if
2214 not. This is used in try_combine to detect early some cases where we
2215 can't perform combinations. */
2216
2217 static int
2218 cant_combine_insn_p (rtx_insn *insn)
2219 {
2220 rtx set;
2221 rtx src, dest;
2222
2223 /* If this isn't really an insn, we can't do anything.
2224 This can occur when flow deletes an insn that it has merged into an
2225 auto-increment address. */
2226 if (! INSN_P (insn))
2227 return 1;
2228
2229 /* Never combine loads and stores involving hard regs that are likely
2230 to be spilled. The register allocator can usually handle such
2231 reg-reg moves by tying. If we allow the combiner to make
2232 substitutions of likely-spilled regs, reload might die.
2233 As an exception, we allow combinations involving fixed regs; these are
2234 not available to the register allocator so there's no risk involved. */
2235
2236 set = single_set (insn);
2237 if (! set)
2238 return 0;
2239 src = SET_SRC (set);
2240 dest = SET_DEST (set);
2241 if (GET_CODE (src) == SUBREG)
2242 src = SUBREG_REG (src);
2243 if (GET_CODE (dest) == SUBREG)
2244 dest = SUBREG_REG (dest);
2245 if (REG_P (src) && REG_P (dest)
2246 && ((HARD_REGISTER_P (src)
2247 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2248 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2249 || (HARD_REGISTER_P (dest)
2250 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2251 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2252 return 1;
2253
2254 return 0;
2255 }
2256
2257 struct likely_spilled_retval_info
2258 {
2259 unsigned regno, nregs;
2260 unsigned mask;
2261 };
2262
2263 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2264 hard registers that are known to be written to / clobbered in full. */
2265 static void
2266 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2267 {
2268 struct likely_spilled_retval_info *const info =
2269 (struct likely_spilled_retval_info *) data;
2270 unsigned regno, nregs;
2271 unsigned new_mask;
2272
2273 if (!REG_P (XEXP (set, 0)))
2274 return;
2275 regno = REGNO (x);
2276 if (regno >= info->regno + info->nregs)
2277 return;
2278 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2279 if (regno + nregs <= info->regno)
2280 return;
2281 new_mask = (2U << (nregs - 1)) - 1;
2282 if (regno < info->regno)
2283 new_mask >>= info->regno - regno;
2284 else
2285 new_mask <<= regno - info->regno;
2286 info->mask &= ~new_mask;
2287 }
2288
2289 /* Return nonzero iff part of the return value is live during INSN, and
2290 it is likely spilled. This can happen when more than one insn is needed
2291 to copy the return value, e.g. when we consider to combine into the
2292 second copy insn for a complex value. */
2293
2294 static int
2295 likely_spilled_retval_p (rtx_insn *insn)
2296 {
2297 rtx_insn *use = BB_END (this_basic_block);
2298 rtx reg;
2299 rtx_insn *p;
2300 unsigned regno, nregs;
2301 /* We assume here that no machine mode needs more than
2302 32 hard registers when the value overlaps with a register
2303 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2304 unsigned mask;
2305 struct likely_spilled_retval_info info;
2306
2307 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2308 return 0;
2309 reg = XEXP (PATTERN (use), 0);
2310 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2311 return 0;
2312 regno = REGNO (reg);
2313 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2314 if (nregs == 1)
2315 return 0;
2316 mask = (2U << (nregs - 1)) - 1;
2317
2318 /* Disregard parts of the return value that are set later. */
2319 info.regno = regno;
2320 info.nregs = nregs;
2321 info.mask = mask;
2322 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2323 if (INSN_P (p))
2324 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2325 mask = info.mask;
2326
2327 /* Check if any of the (probably) live return value registers is
2328 likely spilled. */
2329 nregs --;
2330 do
2331 {
2332 if ((mask & 1 << nregs)
2333 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2334 return 1;
2335 } while (nregs--);
2336 return 0;
2337 }
2338
2339 /* Adjust INSN after we made a change to its destination.
2340
2341 Changing the destination can invalidate notes that say something about
2342 the results of the insn and a LOG_LINK pointing to the insn. */
2343
2344 static void
2345 adjust_for_new_dest (rtx_insn *insn)
2346 {
2347 /* For notes, be conservative and simply remove them. */
2348 remove_reg_equal_equiv_notes (insn);
2349
2350 /* The new insn will have a destination that was previously the destination
2351 of an insn just above it. Call distribute_links to make a LOG_LINK from
2352 the next use of that destination. */
2353 distribute_links (alloc_insn_link (insn, NULL));
2354
2355 df_insn_rescan (insn);
2356 }
2357
2358 /* Return TRUE if combine can reuse reg X in mode MODE.
2359 ADDED_SETS is nonzero if the original set is still required. */
2360 static bool
2361 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2362 {
2363 unsigned int regno;
2364
2365 if (!REG_P (x))
2366 return false;
2367
2368 regno = REGNO (x);
2369 /* Allow hard registers if the new mode is legal, and occupies no more
2370 registers than the old mode. */
2371 if (regno < FIRST_PSEUDO_REGISTER)
2372 return (HARD_REGNO_MODE_OK (regno, mode)
2373 && (hard_regno_nregs[regno][GET_MODE (x)]
2374 >= hard_regno_nregs[regno][mode]));
2375
2376 /* Or a pseudo that is only used once. */
2377 return (REG_N_SETS (regno) == 1 && !added_sets
2378 && !REG_USERVAR_P (x));
2379 }
2380
2381
2382 /* Check whether X, the destination of a set, refers to part of
2383 the register specified by REG. */
2384
2385 static bool
2386 reg_subword_p (rtx x, rtx reg)
2387 {
2388 /* Check that reg is an integer mode register. */
2389 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2390 return false;
2391
2392 if (GET_CODE (x) == STRICT_LOW_PART
2393 || GET_CODE (x) == ZERO_EXTRACT)
2394 x = XEXP (x, 0);
2395
2396 return GET_CODE (x) == SUBREG
2397 && SUBREG_REG (x) == reg
2398 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2399 }
2400
2401 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2402 Note that the INSN should be deleted *after* removing dead edges, so
2403 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2404 but not for a (set (pc) (label_ref FOO)). */
2405
2406 static void
2407 update_cfg_for_uncondjump (rtx_insn *insn)
2408 {
2409 basic_block bb = BLOCK_FOR_INSN (insn);
2410 gcc_assert (BB_END (bb) == insn);
2411
2412 purge_dead_edges (bb);
2413
2414 delete_insn (insn);
2415 if (EDGE_COUNT (bb->succs) == 1)
2416 {
2417 rtx_insn *insn;
2418
2419 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2420
2421 /* Remove barriers from the footer if there are any. */
2422 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2423 if (BARRIER_P (insn))
2424 {
2425 if (PREV_INSN (insn))
2426 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2427 else
2428 BB_FOOTER (bb) = NEXT_INSN (insn);
2429 if (NEXT_INSN (insn))
2430 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2431 }
2432 else if (LABEL_P (insn))
2433 break;
2434 }
2435 }
2436
2437 /* Try to combine the insns I0, I1 and I2 into I3.
2438 Here I0, I1 and I2 appear earlier than I3.
2439 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2440 I3.
2441
2442 If we are combining more than two insns and the resulting insn is not
2443 recognized, try splitting it into two insns. If that happens, I2 and I3
2444 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2445 Otherwise, I0, I1 and I2 are pseudo-deleted.
2446
2447 Return 0 if the combination does not work. Then nothing is changed.
2448 If we did the combination, return the insn at which combine should
2449 resume scanning.
2450
2451 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2452 new direct jump instruction.
2453
2454 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2455 been I3 passed to an earlier try_combine within the same basic
2456 block. */
2457
2458 static rtx_insn *
2459 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2460 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2461 {
2462 /* New patterns for I3 and I2, respectively. */
2463 rtx newpat, newi2pat = 0;
2464 rtvec newpat_vec_with_clobbers = 0;
2465 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2466 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2467 dead. */
2468 int added_sets_0, added_sets_1, added_sets_2;
2469 /* Total number of SETs to put into I3. */
2470 int total_sets;
2471 /* Nonzero if I2's or I1's body now appears in I3. */
2472 int i2_is_used = 0, i1_is_used = 0;
2473 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2474 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2475 /* Contains I3 if the destination of I3 is used in its source, which means
2476 that the old life of I3 is being killed. If that usage is placed into
2477 I2 and not in I3, a REG_DEAD note must be made. */
2478 rtx i3dest_killed = 0;
2479 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2480 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2481 /* Copy of SET_SRC of I1 and I0, if needed. */
2482 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2483 /* Set if I2DEST was reused as a scratch register. */
2484 bool i2scratch = false;
2485 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2486 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2487 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2488 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2489 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2490 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2491 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2492 /* Notes that must be added to REG_NOTES in I3 and I2. */
2493 rtx new_i3_notes, new_i2_notes;
2494 /* Notes that we substituted I3 into I2 instead of the normal case. */
2495 int i3_subst_into_i2 = 0;
2496 /* Notes that I1, I2 or I3 is a MULT operation. */
2497 int have_mult = 0;
2498 int swap_i2i3 = 0;
2499 int changed_i3_dest = 0;
2500
2501 int maxreg;
2502 rtx_insn *temp_insn;
2503 rtx temp_expr;
2504 struct insn_link *link;
2505 rtx other_pat = 0;
2506 rtx new_other_notes;
2507 int i;
2508
2509 /* Only try four-insn combinations when there's high likelihood of
2510 success. Look for simple insns, such as loads of constants or
2511 binary operations involving a constant. */
2512 if (i0)
2513 {
2514 int i;
2515 int ngood = 0;
2516 int nshift = 0;
2517
2518 if (!flag_expensive_optimizations)
2519 return 0;
2520
2521 for (i = 0; i < 4; i++)
2522 {
2523 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2524 rtx set = single_set (insn);
2525 rtx src;
2526 if (!set)
2527 continue;
2528 src = SET_SRC (set);
2529 if (CONSTANT_P (src))
2530 {
2531 ngood += 2;
2532 break;
2533 }
2534 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2535 ngood++;
2536 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2537 || GET_CODE (src) == LSHIFTRT)
2538 nshift++;
2539 }
2540 if (ngood < 2 && nshift < 2)
2541 return 0;
2542 }
2543
2544 /* Exit early if one of the insns involved can't be used for
2545 combinations. */
2546 if (cant_combine_insn_p (i3)
2547 || cant_combine_insn_p (i2)
2548 || (i1 && cant_combine_insn_p (i1))
2549 || (i0 && cant_combine_insn_p (i0))
2550 || likely_spilled_retval_p (i3))
2551 return 0;
2552
2553 combine_attempts++;
2554 undobuf.other_insn = 0;
2555
2556 /* Reset the hard register usage information. */
2557 CLEAR_HARD_REG_SET (newpat_used_regs);
2558
2559 if (dump_file && (dump_flags & TDF_DETAILS))
2560 {
2561 if (i0)
2562 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2563 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2564 else if (i1)
2565 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2566 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2567 else
2568 fprintf (dump_file, "\nTrying %d -> %d:\n",
2569 INSN_UID (i2), INSN_UID (i3));
2570 }
2571
2572 /* If multiple insns feed into one of I2 or I3, they can be in any
2573 order. To simplify the code below, reorder them in sequence. */
2574 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2575 temp_insn = i2, i2 = i0, i0 = temp_insn;
2576 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2577 temp_insn = i1, i1 = i0, i0 = temp_insn;
2578 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2579 temp_insn = i1, i1 = i2, i2 = temp_insn;
2580
2581 added_links_insn = 0;
2582
2583 /* First check for one important special case that the code below will
2584 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2585 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2586 we may be able to replace that destination with the destination of I3.
2587 This occurs in the common code where we compute both a quotient and
2588 remainder into a structure, in which case we want to do the computation
2589 directly into the structure to avoid register-register copies.
2590
2591 Note that this case handles both multiple sets in I2 and also cases
2592 where I2 has a number of CLOBBERs inside the PARALLEL.
2593
2594 We make very conservative checks below and only try to handle the
2595 most common cases of this. For example, we only handle the case
2596 where I2 and I3 are adjacent to avoid making difficult register
2597 usage tests. */
2598
2599 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2600 && REG_P (SET_SRC (PATTERN (i3)))
2601 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2602 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2603 && GET_CODE (PATTERN (i2)) == PARALLEL
2604 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2605 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2606 below would need to check what is inside (and reg_overlap_mentioned_p
2607 doesn't support those codes anyway). Don't allow those destinations;
2608 the resulting insn isn't likely to be recognized anyway. */
2609 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2610 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2611 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2612 SET_DEST (PATTERN (i3)))
2613 && next_active_insn (i2) == i3)
2614 {
2615 rtx p2 = PATTERN (i2);
2616
2617 /* Make sure that the destination of I3,
2618 which we are going to substitute into one output of I2,
2619 is not used within another output of I2. We must avoid making this:
2620 (parallel [(set (mem (reg 69)) ...)
2621 (set (reg 69) ...)])
2622 which is not well-defined as to order of actions.
2623 (Besides, reload can't handle output reloads for this.)
2624
2625 The problem can also happen if the dest of I3 is a memory ref,
2626 if another dest in I2 is an indirect memory ref. */
2627 for (i = 0; i < XVECLEN (p2, 0); i++)
2628 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2629 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2630 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2631 SET_DEST (XVECEXP (p2, 0, i))))
2632 break;
2633
2634 if (i == XVECLEN (p2, 0))
2635 for (i = 0; i < XVECLEN (p2, 0); i++)
2636 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2637 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2638 {
2639 combine_merges++;
2640
2641 subst_insn = i3;
2642 subst_low_luid = DF_INSN_LUID (i2);
2643
2644 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2645 i2src = SET_SRC (XVECEXP (p2, 0, i));
2646 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2647 i2dest_killed = dead_or_set_p (i2, i2dest);
2648
2649 /* Replace the dest in I2 with our dest and make the resulting
2650 insn the new pattern for I3. Then skip to where we validate
2651 the pattern. Everything was set up above. */
2652 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2653 newpat = p2;
2654 i3_subst_into_i2 = 1;
2655 goto validate_replacement;
2656 }
2657 }
2658
2659 /* If I2 is setting a pseudo to a constant and I3 is setting some
2660 sub-part of it to another constant, merge them by making a new
2661 constant. */
2662 if (i1 == 0
2663 && (temp_expr = single_set (i2)) != 0
2664 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2665 && GET_CODE (PATTERN (i3)) == SET
2666 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2667 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2668 {
2669 rtx dest = SET_DEST (PATTERN (i3));
2670 int offset = -1;
2671 int width = 0;
2672
2673 if (GET_CODE (dest) == ZERO_EXTRACT)
2674 {
2675 if (CONST_INT_P (XEXP (dest, 1))
2676 && CONST_INT_P (XEXP (dest, 2)))
2677 {
2678 width = INTVAL (XEXP (dest, 1));
2679 offset = INTVAL (XEXP (dest, 2));
2680 dest = XEXP (dest, 0);
2681 if (BITS_BIG_ENDIAN)
2682 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2683 }
2684 }
2685 else
2686 {
2687 if (GET_CODE (dest) == STRICT_LOW_PART)
2688 dest = XEXP (dest, 0);
2689 width = GET_MODE_PRECISION (GET_MODE (dest));
2690 offset = 0;
2691 }
2692
2693 if (offset >= 0)
2694 {
2695 /* If this is the low part, we're done. */
2696 if (subreg_lowpart_p (dest))
2697 ;
2698 /* Handle the case where inner is twice the size of outer. */
2699 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2700 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2701 offset += GET_MODE_PRECISION (GET_MODE (dest));
2702 /* Otherwise give up for now. */
2703 else
2704 offset = -1;
2705 }
2706
2707 if (offset >= 0)
2708 {
2709 rtx inner = SET_SRC (PATTERN (i3));
2710 rtx outer = SET_SRC (temp_expr);
2711
2712 wide_int o
2713 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2714 std::make_pair (inner, GET_MODE (dest)),
2715 offset, width);
2716
2717 combine_merges++;
2718 subst_insn = i3;
2719 subst_low_luid = DF_INSN_LUID (i2);
2720 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2721 i2dest = SET_DEST (temp_expr);
2722 i2dest_killed = dead_or_set_p (i2, i2dest);
2723
2724 /* Replace the source in I2 with the new constant and make the
2725 resulting insn the new pattern for I3. Then skip to where we
2726 validate the pattern. Everything was set up above. */
2727 SUBST (SET_SRC (temp_expr),
2728 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2729
2730 newpat = PATTERN (i2);
2731
2732 /* The dest of I3 has been replaced with the dest of I2. */
2733 changed_i3_dest = 1;
2734 goto validate_replacement;
2735 }
2736 }
2737
2738 #ifndef HAVE_cc0
2739 /* If we have no I1 and I2 looks like:
2740 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2741 (set Y OP)])
2742 make up a dummy I1 that is
2743 (set Y OP)
2744 and change I2 to be
2745 (set (reg:CC X) (compare:CC Y (const_int 0)))
2746
2747 (We can ignore any trailing CLOBBERs.)
2748
2749 This undoes a previous combination and allows us to match a branch-and-
2750 decrement insn. */
2751
2752 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2753 && XVECLEN (PATTERN (i2), 0) >= 2
2754 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2755 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2756 == MODE_CC)
2757 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2758 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2759 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2760 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2761 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2762 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2763 {
2764 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2765 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2766 break;
2767
2768 if (i == 1)
2769 {
2770 /* We make I1 with the same INSN_UID as I2. This gives it
2771 the same DF_INSN_LUID for value tracking. Our fake I1 will
2772 never appear in the insn stream so giving it the same INSN_UID
2773 as I2 will not cause a problem. */
2774
2775 i1 = as_a <rtx_insn *> (
2776 gen_rtx_INSN (VOIDmode, NULL_RTX, i2, BLOCK_FOR_INSN (i2),
2777 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2778 -1, NULL_RTX));
2779 INSN_UID (i1) = INSN_UID (i2);
2780
2781 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2782 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2783 SET_DEST (PATTERN (i1)));
2784 SUBST_LINK (LOG_LINKS (i2), alloc_insn_link (i1, LOG_LINKS (i2)));
2785 }
2786 }
2787 #endif
2788
2789 /* Verify that I2 and I1 are valid for combining. */
2790 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2791 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2792 &i1dest, &i1src))
2793 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2794 &i0dest, &i0src)))
2795 {
2796 undo_all ();
2797 return 0;
2798 }
2799
2800 /* Record whether I2DEST is used in I2SRC and similarly for the other
2801 cases. Knowing this will help in register status updating below. */
2802 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2803 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2804 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2805 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2806 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2807 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2808 i2dest_killed = dead_or_set_p (i2, i2dest);
2809 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2810 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2811
2812 /* For the earlier insns, determine which of the subsequent ones they
2813 feed. */
2814 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2815 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2816 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2817 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2818 && reg_overlap_mentioned_p (i0dest, i2src))));
2819
2820 /* Ensure that I3's pattern can be the destination of combines. */
2821 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2822 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2823 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2824 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2825 &i3dest_killed))
2826 {
2827 undo_all ();
2828 return 0;
2829 }
2830
2831 /* See if any of the insns is a MULT operation. Unless one is, we will
2832 reject a combination that is, since it must be slower. Be conservative
2833 here. */
2834 if (GET_CODE (i2src) == MULT
2835 || (i1 != 0 && GET_CODE (i1src) == MULT)
2836 || (i0 != 0 && GET_CODE (i0src) == MULT)
2837 || (GET_CODE (PATTERN (i3)) == SET
2838 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2839 have_mult = 1;
2840
2841 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2842 We used to do this EXCEPT in one case: I3 has a post-inc in an
2843 output operand. However, that exception can give rise to insns like
2844 mov r3,(r3)+
2845 which is a famous insn on the PDP-11 where the value of r3 used as the
2846 source was model-dependent. Avoid this sort of thing. */
2847
2848 #if 0
2849 if (!(GET_CODE (PATTERN (i3)) == SET
2850 && REG_P (SET_SRC (PATTERN (i3)))
2851 && MEM_P (SET_DEST (PATTERN (i3)))
2852 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2853 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2854 /* It's not the exception. */
2855 #endif
2856 #ifdef AUTO_INC_DEC
2857 {
2858 rtx link;
2859 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2860 if (REG_NOTE_KIND (link) == REG_INC
2861 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2862 || (i1 != 0
2863 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2864 {
2865 undo_all ();
2866 return 0;
2867 }
2868 }
2869 #endif
2870
2871 /* See if the SETs in I1 or I2 need to be kept around in the merged
2872 instruction: whenever the value set there is still needed past I3.
2873 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2874
2875 For the SET in I1, we have two cases: if I1 and I2 independently feed
2876 into I3, the set in I1 needs to be kept around unless I1DEST dies
2877 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2878 in I1 needs to be kept around unless I1DEST dies or is set in either
2879 I2 or I3. The same considerations apply to I0. */
2880
2881 added_sets_2 = !dead_or_set_p (i3, i2dest);
2882
2883 if (i1)
2884 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2885 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2886 else
2887 added_sets_1 = 0;
2888
2889 if (i0)
2890 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2891 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
2892 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
2893 && dead_or_set_p (i2, i0dest)));
2894 else
2895 added_sets_0 = 0;
2896
2897 /* We are about to copy insns for the case where they need to be kept
2898 around. Check that they can be copied in the merged instruction. */
2899
2900 if (targetm.cannot_copy_insn_p
2901 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2902 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2903 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2904 {
2905 undo_all ();
2906 return 0;
2907 }
2908
2909 /* If the set in I2 needs to be kept around, we must make a copy of
2910 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2911 PATTERN (I2), we are only substituting for the original I1DEST, not into
2912 an already-substituted copy. This also prevents making self-referential
2913 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2914 I2DEST. */
2915
2916 if (added_sets_2)
2917 {
2918 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2919 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2920 else
2921 i2pat = copy_rtx (PATTERN (i2));
2922 }
2923
2924 if (added_sets_1)
2925 {
2926 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2927 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2928 else
2929 i1pat = copy_rtx (PATTERN (i1));
2930 }
2931
2932 if (added_sets_0)
2933 {
2934 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2935 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2936 else
2937 i0pat = copy_rtx (PATTERN (i0));
2938 }
2939
2940 combine_merges++;
2941
2942 /* Substitute in the latest insn for the regs set by the earlier ones. */
2943
2944 maxreg = max_reg_num ();
2945
2946 subst_insn = i3;
2947
2948 #ifndef HAVE_cc0
2949 /* Many machines that don't use CC0 have insns that can both perform an
2950 arithmetic operation and set the condition code. These operations will
2951 be represented as a PARALLEL with the first element of the vector
2952 being a COMPARE of an arithmetic operation with the constant zero.
2953 The second element of the vector will set some pseudo to the result
2954 of the same arithmetic operation. If we simplify the COMPARE, we won't
2955 match such a pattern and so will generate an extra insn. Here we test
2956 for this case, where both the comparison and the operation result are
2957 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2958 I2SRC. Later we will make the PARALLEL that contains I2. */
2959
2960 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2961 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2962 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
2963 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2964 {
2965 rtx newpat_dest;
2966 rtx *cc_use_loc = NULL;
2967 rtx_insn *cc_use_insn = NULL;
2968 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
2969 enum machine_mode compare_mode, orig_compare_mode;
2970 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
2971
2972 newpat = PATTERN (i3);
2973 newpat_dest = SET_DEST (newpat);
2974 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
2975
2976 if (undobuf.other_insn == 0
2977 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
2978 &cc_use_insn)))
2979 {
2980 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
2981 compare_code = simplify_compare_const (compare_code,
2982 GET_MODE (i2dest), op0, &op1);
2983 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
2984 }
2985
2986 /* Do the rest only if op1 is const0_rtx, which may be the
2987 result of simplification. */
2988 if (op1 == const0_rtx)
2989 {
2990 /* If a single use of the CC is found, prepare to modify it
2991 when SELECT_CC_MODE returns a new CC-class mode, or when
2992 the above simplify_compare_const() returned a new comparison
2993 operator. undobuf.other_insn is assigned the CC use insn
2994 when modifying it. */
2995 if (cc_use_loc)
2996 {
2997 #ifdef SELECT_CC_MODE
2998 enum machine_mode new_mode
2999 = SELECT_CC_MODE (compare_code, op0, op1);
3000 if (new_mode != orig_compare_mode
3001 && can_change_dest_mode (SET_DEST (newpat),
3002 added_sets_2, new_mode))
3003 {
3004 unsigned int regno = REGNO (newpat_dest);
3005 compare_mode = new_mode;
3006 if (regno < FIRST_PSEUDO_REGISTER)
3007 newpat_dest = gen_rtx_REG (compare_mode, regno);
3008 else
3009 {
3010 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3011 newpat_dest = regno_reg_rtx[regno];
3012 }
3013 }
3014 #endif
3015 /* Cases for modifying the CC-using comparison. */
3016 if (compare_code != orig_compare_code
3017 /* ??? Do we need to verify the zero rtx? */
3018 && XEXP (*cc_use_loc, 1) == const0_rtx)
3019 {
3020 /* Replace cc_use_loc with entire new RTX. */
3021 SUBST (*cc_use_loc,
3022 gen_rtx_fmt_ee (compare_code, compare_mode,
3023 newpat_dest, const0_rtx));
3024 undobuf.other_insn = cc_use_insn;
3025 }
3026 else if (compare_mode != orig_compare_mode)
3027 {
3028 /* Just replace the CC reg with a new mode. */
3029 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3030 undobuf.other_insn = cc_use_insn;
3031 }
3032 }
3033
3034 /* Now we modify the current newpat:
3035 First, SET_DEST(newpat) is updated if the CC mode has been
3036 altered. For targets without SELECT_CC_MODE, this should be
3037 optimized away. */
3038 if (compare_mode != orig_compare_mode)
3039 SUBST (SET_DEST (newpat), newpat_dest);
3040 /* This is always done to propagate i2src into newpat. */
3041 SUBST (SET_SRC (newpat),
3042 gen_rtx_COMPARE (compare_mode, op0, op1));
3043 /* Create new version of i2pat if needed; the below PARALLEL
3044 creation needs this to work correctly. */
3045 if (! rtx_equal_p (i2src, op0))
3046 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3047 i2_is_used = 1;
3048 }
3049 }
3050 #endif
3051
3052 if (i2_is_used == 0)
3053 {
3054 /* It is possible that the source of I2 or I1 may be performing
3055 an unneeded operation, such as a ZERO_EXTEND of something
3056 that is known to have the high part zero. Handle that case
3057 by letting subst look at the inner insns.
3058
3059 Another way to do this would be to have a function that tries
3060 to simplify a single insn instead of merging two or more
3061 insns. We don't do this because of the potential of infinite
3062 loops and because of the potential extra memory required.
3063 However, doing it the way we are is a bit of a kludge and
3064 doesn't catch all cases.
3065
3066 But only do this if -fexpensive-optimizations since it slows
3067 things down and doesn't usually win.
3068
3069 This is not done in the COMPARE case above because the
3070 unmodified I2PAT is used in the PARALLEL and so a pattern
3071 with a modified I2SRC would not match. */
3072
3073 if (flag_expensive_optimizations)
3074 {
3075 /* Pass pc_rtx so no substitutions are done, just
3076 simplifications. */
3077 if (i1)
3078 {
3079 subst_low_luid = DF_INSN_LUID (i1);
3080 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3081 }
3082
3083 subst_low_luid = DF_INSN_LUID (i2);
3084 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3085 }
3086
3087 n_occurrences = 0; /* `subst' counts here */
3088 subst_low_luid = DF_INSN_LUID (i2);
3089
3090 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3091 copy of I2SRC each time we substitute it, in order to avoid creating
3092 self-referential RTL when we will be substituting I1SRC for I1DEST
3093 later. Likewise if I0 feeds into I2, either directly or indirectly
3094 through I1, and I0DEST is in I0SRC. */
3095 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3096 (i1_feeds_i2_n && i1dest_in_i1src)
3097 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3098 && i0dest_in_i0src));
3099 substed_i2 = 1;
3100
3101 /* Record whether I2's body now appears within I3's body. */
3102 i2_is_used = n_occurrences;
3103 }
3104
3105 /* If we already got a failure, don't try to do more. Otherwise, try to
3106 substitute I1 if we have it. */
3107
3108 if (i1 && GET_CODE (newpat) != CLOBBER)
3109 {
3110 /* Check that an autoincrement side-effect on I1 has not been lost.
3111 This happens if I1DEST is mentioned in I2 and dies there, and
3112 has disappeared from the new pattern. */
3113 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3114 && i1_feeds_i2_n
3115 && dead_or_set_p (i2, i1dest)
3116 && !reg_overlap_mentioned_p (i1dest, newpat))
3117 /* Before we can do this substitution, we must redo the test done
3118 above (see detailed comments there) that ensures I1DEST isn't
3119 mentioned in any SETs in NEWPAT that are field assignments. */
3120 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3121 0, 0, 0))
3122 {
3123 undo_all ();
3124 return 0;
3125 }
3126
3127 n_occurrences = 0;
3128 subst_low_luid = DF_INSN_LUID (i1);
3129
3130 /* If the following substitution will modify I1SRC, make a copy of it
3131 for the case where it is substituted for I1DEST in I2PAT later. */
3132 if (added_sets_2 && i1_feeds_i2_n)
3133 i1src_copy = copy_rtx (i1src);
3134
3135 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3136 copy of I1SRC each time we substitute it, in order to avoid creating
3137 self-referential RTL when we will be substituting I0SRC for I0DEST
3138 later. */
3139 newpat = subst (newpat, i1dest, i1src, 0, 0,
3140 i0_feeds_i1_n && i0dest_in_i0src);
3141 substed_i1 = 1;
3142
3143 /* Record whether I1's body now appears within I3's body. */
3144 i1_is_used = n_occurrences;
3145 }
3146
3147 /* Likewise for I0 if we have it. */
3148
3149 if (i0 && GET_CODE (newpat) != CLOBBER)
3150 {
3151 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3152 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3153 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3154 && !reg_overlap_mentioned_p (i0dest, newpat))
3155 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3156 0, 0, 0))
3157 {
3158 undo_all ();
3159 return 0;
3160 }
3161
3162 /* If the following substitution will modify I0SRC, make a copy of it
3163 for the case where it is substituted for I0DEST in I1PAT later. */
3164 if (added_sets_1 && i0_feeds_i1_n)
3165 i0src_copy = copy_rtx (i0src);
3166 /* And a copy for I0DEST in I2PAT substitution. */
3167 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3168 || (i0_feeds_i2_n)))
3169 i0src_copy2 = copy_rtx (i0src);
3170
3171 n_occurrences = 0;
3172 subst_low_luid = DF_INSN_LUID (i0);
3173 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3174 substed_i0 = 1;
3175 }
3176
3177 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3178 to count all the ways that I2SRC and I1SRC can be used. */
3179 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3180 && i2_is_used + added_sets_2 > 1)
3181 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3182 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3183 > 1))
3184 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3185 && (n_occurrences + added_sets_0
3186 + (added_sets_1 && i0_feeds_i1_n)
3187 + (added_sets_2 && i0_feeds_i2_n)
3188 > 1))
3189 /* Fail if we tried to make a new register. */
3190 || max_reg_num () != maxreg
3191 /* Fail if we couldn't do something and have a CLOBBER. */
3192 || GET_CODE (newpat) == CLOBBER
3193 /* Fail if this new pattern is a MULT and we didn't have one before
3194 at the outer level. */
3195 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3196 && ! have_mult))
3197 {
3198 undo_all ();
3199 return 0;
3200 }
3201
3202 /* If the actions of the earlier insns must be kept
3203 in addition to substituting them into the latest one,
3204 we must make a new PARALLEL for the latest insn
3205 to hold additional the SETs. */
3206
3207 if (added_sets_0 || added_sets_1 || added_sets_2)
3208 {
3209 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3210 combine_extras++;
3211
3212 if (GET_CODE (newpat) == PARALLEL)
3213 {
3214 rtvec old = XVEC (newpat, 0);
3215 total_sets = XVECLEN (newpat, 0) + extra_sets;
3216 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3217 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3218 sizeof (old->elem[0]) * old->num_elem);
3219 }
3220 else
3221 {
3222 rtx old = newpat;
3223 total_sets = 1 + extra_sets;
3224 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3225 XVECEXP (newpat, 0, 0) = old;
3226 }
3227
3228 if (added_sets_0)
3229 XVECEXP (newpat, 0, --total_sets) = i0pat;
3230
3231 if (added_sets_1)
3232 {
3233 rtx t = i1pat;
3234 if (i0_feeds_i1_n)
3235 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3236
3237 XVECEXP (newpat, 0, --total_sets) = t;
3238 }
3239 if (added_sets_2)
3240 {
3241 rtx t = i2pat;
3242 if (i1_feeds_i2_n)
3243 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3244 i0_feeds_i1_n && i0dest_in_i0src);
3245 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3246 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3247
3248 XVECEXP (newpat, 0, --total_sets) = t;
3249 }
3250 }
3251
3252 validate_replacement:
3253
3254 /* Note which hard regs this insn has as inputs. */
3255 mark_used_regs_combine (newpat);
3256
3257 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3258 consider splitting this pattern, we might need these clobbers. */
3259 if (i1 && GET_CODE (newpat) == PARALLEL
3260 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3261 {
3262 int len = XVECLEN (newpat, 0);
3263
3264 newpat_vec_with_clobbers = rtvec_alloc (len);
3265 for (i = 0; i < len; i++)
3266 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3267 }
3268
3269 /* Is the result of combination a valid instruction? */
3270 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3271
3272 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3273 the second SET's destination is a register that is unused and isn't
3274 marked as an instruction that might trap in an EH region. In that case,
3275 we just need the first SET. This can occur when simplifying a divmod
3276 insn. We *must* test for this case here because the code below that
3277 splits two independent SETs doesn't handle this case correctly when it
3278 updates the register status.
3279
3280 It's pointless doing this if we originally had two sets, one from
3281 i3, and one from i2. Combining then splitting the parallel results
3282 in the original i2 again plus an invalid insn (which we delete).
3283 The net effect is only to move instructions around, which makes
3284 debug info less accurate.
3285
3286 Also check the case where the first SET's destination is unused.
3287 That would not cause incorrect code, but does cause an unneeded
3288 insn to remain. */
3289
3290 if (insn_code_number < 0
3291 && !(added_sets_2 && i1 == 0)
3292 && GET_CODE (newpat) == PARALLEL
3293 && XVECLEN (newpat, 0) == 2
3294 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3295 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3296 && asm_noperands (newpat) < 0)
3297 {
3298 rtx set0 = XVECEXP (newpat, 0, 0);
3299 rtx set1 = XVECEXP (newpat, 0, 1);
3300
3301 if (((REG_P (SET_DEST (set1))
3302 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3303 || (GET_CODE (SET_DEST (set1)) == SUBREG
3304 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3305 && insn_nothrow_p (i3)
3306 && !side_effects_p (SET_SRC (set1)))
3307 {
3308 newpat = set0;
3309 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3310 }
3311
3312 else if (((REG_P (SET_DEST (set0))
3313 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3314 || (GET_CODE (SET_DEST (set0)) == SUBREG
3315 && find_reg_note (i3, REG_UNUSED,
3316 SUBREG_REG (SET_DEST (set0)))))
3317 && insn_nothrow_p (i3)
3318 && !side_effects_p (SET_SRC (set0)))
3319 {
3320 newpat = set1;
3321 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3322
3323 if (insn_code_number >= 0)
3324 changed_i3_dest = 1;
3325 }
3326 }
3327
3328 /* If we were combining three insns and the result is a simple SET
3329 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3330 insns. There are two ways to do this. It can be split using a
3331 machine-specific method (like when you have an addition of a large
3332 constant) or by combine in the function find_split_point. */
3333
3334 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3335 && asm_noperands (newpat) < 0)
3336 {
3337 rtx parallel, *split;
3338 rtx_insn *m_split_insn;
3339
3340 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3341 use I2DEST as a scratch register will help. In the latter case,
3342 convert I2DEST to the mode of the source of NEWPAT if we can. */
3343
3344 m_split_insn = combine_split_insns (newpat, i3);
3345
3346 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3347 inputs of NEWPAT. */
3348
3349 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3350 possible to try that as a scratch reg. This would require adding
3351 more code to make it work though. */
3352
3353 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3354 {
3355 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3356
3357 /* First try to split using the original register as a
3358 scratch register. */
3359 parallel = gen_rtx_PARALLEL (VOIDmode,
3360 gen_rtvec (2, newpat,
3361 gen_rtx_CLOBBER (VOIDmode,
3362 i2dest)));
3363 m_split_insn = combine_split_insns (parallel, i3);
3364
3365 /* If that didn't work, try changing the mode of I2DEST if
3366 we can. */
3367 if (m_split_insn == 0
3368 && new_mode != GET_MODE (i2dest)
3369 && new_mode != VOIDmode
3370 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3371 {
3372 enum machine_mode old_mode = GET_MODE (i2dest);
3373 rtx ni2dest;
3374
3375 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3376 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3377 else
3378 {
3379 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3380 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3381 }
3382
3383 parallel = (gen_rtx_PARALLEL
3384 (VOIDmode,
3385 gen_rtvec (2, newpat,
3386 gen_rtx_CLOBBER (VOIDmode,
3387 ni2dest))));
3388 m_split_insn = combine_split_insns (parallel, i3);
3389
3390 if (m_split_insn == 0
3391 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3392 {
3393 struct undo *buf;
3394
3395 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3396 buf = undobuf.undos;
3397 undobuf.undos = buf->next;
3398 buf->next = undobuf.frees;
3399 undobuf.frees = buf;
3400 }
3401 }
3402
3403 i2scratch = m_split_insn != 0;
3404 }
3405
3406 /* If recog_for_combine has discarded clobbers, try to use them
3407 again for the split. */
3408 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3409 {
3410 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3411 m_split_insn = combine_split_insns (parallel, i3);
3412 }
3413
3414 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3415 {
3416 rtx m_split_pat = PATTERN (m_split_insn);
3417 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3418 if (insn_code_number >= 0)
3419 newpat = m_split_pat;
3420 }
3421 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3422 && (next_nonnote_nondebug_insn (i2) == i3
3423 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3424 {
3425 rtx i2set, i3set;
3426 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3427 newi2pat = PATTERN (m_split_insn);
3428
3429 i3set = single_set (NEXT_INSN (m_split_insn));
3430 i2set = single_set (m_split_insn);
3431
3432 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3433
3434 /* If I2 or I3 has multiple SETs, we won't know how to track
3435 register status, so don't use these insns. If I2's destination
3436 is used between I2 and I3, we also can't use these insns. */
3437
3438 if (i2_code_number >= 0 && i2set && i3set
3439 && (next_nonnote_nondebug_insn (i2) == i3
3440 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3441 insn_code_number = recog_for_combine (&newi3pat, i3,
3442 &new_i3_notes);
3443 if (insn_code_number >= 0)
3444 newpat = newi3pat;
3445
3446 /* It is possible that both insns now set the destination of I3.
3447 If so, we must show an extra use of it. */
3448
3449 if (insn_code_number >= 0)
3450 {
3451 rtx new_i3_dest = SET_DEST (i3set);
3452 rtx new_i2_dest = SET_DEST (i2set);
3453
3454 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3455 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3456 || GET_CODE (new_i3_dest) == SUBREG)
3457 new_i3_dest = XEXP (new_i3_dest, 0);
3458
3459 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3460 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3461 || GET_CODE (new_i2_dest) == SUBREG)
3462 new_i2_dest = XEXP (new_i2_dest, 0);
3463
3464 if (REG_P (new_i3_dest)
3465 && REG_P (new_i2_dest)
3466 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3467 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3468 }
3469 }
3470
3471 /* If we can split it and use I2DEST, go ahead and see if that
3472 helps things be recognized. Verify that none of the registers
3473 are set between I2 and I3. */
3474 if (insn_code_number < 0
3475 && (split = find_split_point (&newpat, i3, false)) != 0
3476 #ifdef HAVE_cc0
3477 && REG_P (i2dest)
3478 #endif
3479 /* We need I2DEST in the proper mode. If it is a hard register
3480 or the only use of a pseudo, we can change its mode.
3481 Make sure we don't change a hard register to have a mode that
3482 isn't valid for it, or change the number of registers. */
3483 && (GET_MODE (*split) == GET_MODE (i2dest)
3484 || GET_MODE (*split) == VOIDmode
3485 || can_change_dest_mode (i2dest, added_sets_2,
3486 GET_MODE (*split)))
3487 && (next_nonnote_nondebug_insn (i2) == i3
3488 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3489 /* We can't overwrite I2DEST if its value is still used by
3490 NEWPAT. */
3491 && ! reg_referenced_p (i2dest, newpat))
3492 {
3493 rtx newdest = i2dest;
3494 enum rtx_code split_code = GET_CODE (*split);
3495 enum machine_mode split_mode = GET_MODE (*split);
3496 bool subst_done = false;
3497 newi2pat = NULL_RTX;
3498
3499 i2scratch = true;
3500
3501 /* *SPLIT may be part of I2SRC, so make sure we have the
3502 original expression around for later debug processing.
3503 We should not need I2SRC any more in other cases. */
3504 if (MAY_HAVE_DEBUG_INSNS)
3505 i2src = copy_rtx (i2src);
3506 else
3507 i2src = NULL;
3508
3509 /* Get NEWDEST as a register in the proper mode. We have already
3510 validated that we can do this. */
3511 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3512 {
3513 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3514 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3515 else
3516 {
3517 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3518 newdest = regno_reg_rtx[REGNO (i2dest)];
3519 }
3520 }
3521
3522 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3523 an ASHIFT. This can occur if it was inside a PLUS and hence
3524 appeared to be a memory address. This is a kludge. */
3525 if (split_code == MULT
3526 && CONST_INT_P (XEXP (*split, 1))
3527 && INTVAL (XEXP (*split, 1)) > 0
3528 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3529 {
3530 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3531 XEXP (*split, 0), GEN_INT (i)));
3532 /* Update split_code because we may not have a multiply
3533 anymore. */
3534 split_code = GET_CODE (*split);
3535 }
3536
3537 #ifdef INSN_SCHEDULING
3538 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3539 be written as a ZERO_EXTEND. */
3540 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3541 {
3542 #ifdef LOAD_EXTEND_OP
3543 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3544 what it really is. */
3545 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3546 == SIGN_EXTEND)
3547 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3548 SUBREG_REG (*split)));
3549 else
3550 #endif
3551 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3552 SUBREG_REG (*split)));
3553 }
3554 #endif
3555
3556 /* Attempt to split binary operators using arithmetic identities. */
3557 if (BINARY_P (SET_SRC (newpat))
3558 && split_mode == GET_MODE (SET_SRC (newpat))
3559 && ! side_effects_p (SET_SRC (newpat)))
3560 {
3561 rtx setsrc = SET_SRC (newpat);
3562 enum machine_mode mode = GET_MODE (setsrc);
3563 enum rtx_code code = GET_CODE (setsrc);
3564 rtx src_op0 = XEXP (setsrc, 0);
3565 rtx src_op1 = XEXP (setsrc, 1);
3566
3567 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3568 if (rtx_equal_p (src_op0, src_op1))
3569 {
3570 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3571 SUBST (XEXP (setsrc, 0), newdest);
3572 SUBST (XEXP (setsrc, 1), newdest);
3573 subst_done = true;
3574 }
3575 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3576 else if ((code == PLUS || code == MULT)
3577 && GET_CODE (src_op0) == code
3578 && GET_CODE (XEXP (src_op0, 0)) == code
3579 && (INTEGRAL_MODE_P (mode)
3580 || (FLOAT_MODE_P (mode)
3581 && flag_unsafe_math_optimizations)))
3582 {
3583 rtx p = XEXP (XEXP (src_op0, 0), 0);
3584 rtx q = XEXP (XEXP (src_op0, 0), 1);
3585 rtx r = XEXP (src_op0, 1);
3586 rtx s = src_op1;
3587
3588 /* Split both "((X op Y) op X) op Y" and
3589 "((X op Y) op Y) op X" as "T op T" where T is
3590 "X op Y". */
3591 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3592 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3593 {
3594 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3595 XEXP (src_op0, 0));
3596 SUBST (XEXP (setsrc, 0), newdest);
3597 SUBST (XEXP (setsrc, 1), newdest);
3598 subst_done = true;
3599 }
3600 /* Split "((X op X) op Y) op Y)" as "T op T" where
3601 T is "X op Y". */
3602 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3603 {
3604 rtx tmp = simplify_gen_binary (code, mode, p, r);
3605 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3606 SUBST (XEXP (setsrc, 0), newdest);
3607 SUBST (XEXP (setsrc, 1), newdest);
3608 subst_done = true;
3609 }
3610 }
3611 }
3612
3613 if (!subst_done)
3614 {
3615 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3616 SUBST (*split, newdest);
3617 }
3618
3619 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3620
3621 /* recog_for_combine might have added CLOBBERs to newi2pat.
3622 Make sure NEWPAT does not depend on the clobbered regs. */
3623 if (GET_CODE (newi2pat) == PARALLEL)
3624 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3625 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3626 {
3627 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3628 if (reg_overlap_mentioned_p (reg, newpat))
3629 {
3630 undo_all ();
3631 return 0;
3632 }
3633 }
3634
3635 /* If the split point was a MULT and we didn't have one before,
3636 don't use one now. */
3637 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3638 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3639 }
3640 }
3641
3642 /* Check for a case where we loaded from memory in a narrow mode and
3643 then sign extended it, but we need both registers. In that case,
3644 we have a PARALLEL with both loads from the same memory location.
3645 We can split this into a load from memory followed by a register-register
3646 copy. This saves at least one insn, more if register allocation can
3647 eliminate the copy.
3648
3649 We cannot do this if the destination of the first assignment is a
3650 condition code register or cc0. We eliminate this case by making sure
3651 the SET_DEST and SET_SRC have the same mode.
3652
3653 We cannot do this if the destination of the second assignment is
3654 a register that we have already assumed is zero-extended. Similarly
3655 for a SUBREG of such a register. */
3656
3657 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3658 && GET_CODE (newpat) == PARALLEL
3659 && XVECLEN (newpat, 0) == 2
3660 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3661 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3662 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3663 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3664 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3665 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3666 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3667 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3668 DF_INSN_LUID (i2))
3669 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3670 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3671 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3672 (REG_P (temp_expr)
3673 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3674 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3675 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3676 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3677 != GET_MODE_MASK (word_mode))))
3678 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3679 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3680 (REG_P (temp_expr)
3681 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3682 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3683 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3684 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3685 != GET_MODE_MASK (word_mode)))))
3686 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3687 SET_SRC (XVECEXP (newpat, 0, 1)))
3688 && ! find_reg_note (i3, REG_UNUSED,
3689 SET_DEST (XVECEXP (newpat, 0, 0))))
3690 {
3691 rtx ni2dest;
3692
3693 newi2pat = XVECEXP (newpat, 0, 0);
3694 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3695 newpat = XVECEXP (newpat, 0, 1);
3696 SUBST (SET_SRC (newpat),
3697 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3698 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3699
3700 if (i2_code_number >= 0)
3701 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3702
3703 if (insn_code_number >= 0)
3704 swap_i2i3 = 1;
3705 }
3706
3707 /* Similarly, check for a case where we have a PARALLEL of two independent
3708 SETs but we started with three insns. In this case, we can do the sets
3709 as two separate insns. This case occurs when some SET allows two
3710 other insns to combine, but the destination of that SET is still live. */
3711
3712 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3713 && GET_CODE (newpat) == PARALLEL
3714 && XVECLEN (newpat, 0) == 2
3715 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3716 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3717 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3718 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3719 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3720 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3721 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3722 XVECEXP (newpat, 0, 0))
3723 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3724 XVECEXP (newpat, 0, 1))
3725 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3726 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3727 {
3728 rtx set0 = XVECEXP (newpat, 0, 0);
3729 rtx set1 = XVECEXP (newpat, 0, 1);
3730
3731 /* Normally, it doesn't matter which of the two is done first,
3732 but the one that references cc0 can't be the second, and
3733 one which uses any regs/memory set in between i2 and i3 can't
3734 be first. The PARALLEL might also have been pre-existing in i3,
3735 so we need to make sure that we won't wrongly hoist a SET to i2
3736 that would conflict with a death note present in there. */
3737 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3738 && !(REG_P (SET_DEST (set1))
3739 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3740 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3741 && find_reg_note (i2, REG_DEAD,
3742 SUBREG_REG (SET_DEST (set1))))
3743 #ifdef HAVE_cc0
3744 && !reg_referenced_p (cc0_rtx, set0)
3745 #endif
3746 /* If I3 is a jump, ensure that set0 is a jump so that
3747 we do not create invalid RTL. */
3748 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3749 )
3750 {
3751 newi2pat = set1;
3752 newpat = set0;
3753 }
3754 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3755 && !(REG_P (SET_DEST (set0))
3756 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3757 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3758 && find_reg_note (i2, REG_DEAD,
3759 SUBREG_REG (SET_DEST (set0))))
3760 #ifdef HAVE_cc0
3761 && !reg_referenced_p (cc0_rtx, set1)
3762 #endif
3763 /* If I3 is a jump, ensure that set1 is a jump so that
3764 we do not create invalid RTL. */
3765 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3766 )
3767 {
3768 newi2pat = set0;
3769 newpat = set1;
3770 }
3771 else
3772 {
3773 undo_all ();
3774 return 0;
3775 }
3776
3777 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3778
3779 if (i2_code_number >= 0)
3780 {
3781 /* recog_for_combine might have added CLOBBERs to newi2pat.
3782 Make sure NEWPAT does not depend on the clobbered regs. */
3783 if (GET_CODE (newi2pat) == PARALLEL)
3784 {
3785 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3786 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3787 {
3788 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3789 if (reg_overlap_mentioned_p (reg, newpat))
3790 {
3791 undo_all ();
3792 return 0;
3793 }
3794 }
3795 }
3796
3797 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3798 }
3799 }
3800
3801 /* If it still isn't recognized, fail and change things back the way they
3802 were. */
3803 if ((insn_code_number < 0
3804 /* Is the result a reasonable ASM_OPERANDS? */
3805 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3806 {
3807 undo_all ();
3808 return 0;
3809 }
3810
3811 /* If we had to change another insn, make sure it is valid also. */
3812 if (undobuf.other_insn)
3813 {
3814 CLEAR_HARD_REG_SET (newpat_used_regs);
3815
3816 other_pat = PATTERN (undobuf.other_insn);
3817 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3818 &new_other_notes);
3819
3820 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3821 {
3822 undo_all ();
3823 return 0;
3824 }
3825 }
3826
3827 #ifdef HAVE_cc0
3828 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3829 they are adjacent to each other or not. */
3830 {
3831 rtx_insn *p = prev_nonnote_insn (i3);
3832 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3833 && sets_cc0_p (newi2pat))
3834 {
3835 undo_all ();
3836 return 0;
3837 }
3838 }
3839 #endif
3840
3841 /* Only allow this combination if insn_rtx_costs reports that the
3842 replacement instructions are cheaper than the originals. */
3843 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3844 {
3845 undo_all ();
3846 return 0;
3847 }
3848
3849 if (MAY_HAVE_DEBUG_INSNS)
3850 {
3851 struct undo *undo;
3852
3853 for (undo = undobuf.undos; undo; undo = undo->next)
3854 if (undo->kind == UNDO_MODE)
3855 {
3856 rtx reg = *undo->where.r;
3857 enum machine_mode new_mode = GET_MODE (reg);
3858 enum machine_mode old_mode = undo->old_contents.m;
3859
3860 /* Temporarily revert mode back. */
3861 adjust_reg_mode (reg, old_mode);
3862
3863 if (reg == i2dest && i2scratch)
3864 {
3865 /* If we used i2dest as a scratch register with a
3866 different mode, substitute it for the original
3867 i2src while its original mode is temporarily
3868 restored, and then clear i2scratch so that we don't
3869 do it again later. */
3870 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3871 this_basic_block);
3872 i2scratch = false;
3873 /* Put back the new mode. */
3874 adjust_reg_mode (reg, new_mode);
3875 }
3876 else
3877 {
3878 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3879 rtx_insn *first, *last;
3880
3881 if (reg == i2dest)
3882 {
3883 first = i2;
3884 last = last_combined_insn;
3885 }
3886 else
3887 {
3888 first = i3;
3889 last = undobuf.other_insn;
3890 gcc_assert (last);
3891 if (DF_INSN_LUID (last)
3892 < DF_INSN_LUID (last_combined_insn))
3893 last = last_combined_insn;
3894 }
3895
3896 /* We're dealing with a reg that changed mode but not
3897 meaning, so we want to turn it into a subreg for
3898 the new mode. However, because of REG sharing and
3899 because its mode had already changed, we have to do
3900 it in two steps. First, replace any debug uses of
3901 reg, with its original mode temporarily restored,
3902 with this copy we have created; then, replace the
3903 copy with the SUBREG of the original shared reg,
3904 once again changed to the new mode. */
3905 propagate_for_debug (first, last, reg, tempreg,
3906 this_basic_block);
3907 adjust_reg_mode (reg, new_mode);
3908 propagate_for_debug (first, last, tempreg,
3909 lowpart_subreg (old_mode, reg, new_mode),
3910 this_basic_block);
3911 }
3912 }
3913 }
3914
3915 /* If we will be able to accept this, we have made a
3916 change to the destination of I3. This requires us to
3917 do a few adjustments. */
3918
3919 if (changed_i3_dest)
3920 {
3921 PATTERN (i3) = newpat;
3922 adjust_for_new_dest (i3);
3923 }
3924
3925 /* We now know that we can do this combination. Merge the insns and
3926 update the status of registers and LOG_LINKS. */
3927
3928 if (undobuf.other_insn)
3929 {
3930 rtx note, next;
3931
3932 PATTERN (undobuf.other_insn) = other_pat;
3933
3934 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
3935 ensure that they are still valid. Then add any non-duplicate
3936 notes added by recog_for_combine. */
3937 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3938 {
3939 next = XEXP (note, 1);
3940
3941 if ((REG_NOTE_KIND (note) == REG_DEAD
3942 && !reg_referenced_p (XEXP (note, 0),
3943 PATTERN (undobuf.other_insn)))
3944 ||(REG_NOTE_KIND (note) == REG_UNUSED
3945 && !reg_set_p (XEXP (note, 0),
3946 PATTERN (undobuf.other_insn))))
3947 remove_note (undobuf.other_insn, note);
3948 }
3949
3950 distribute_notes (new_other_notes, undobuf.other_insn,
3951 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
3952 NULL_RTX);
3953 }
3954
3955 if (swap_i2i3)
3956 {
3957 rtx_insn *insn;
3958 struct insn_link *link;
3959 rtx ni2dest;
3960
3961 /* I3 now uses what used to be its destination and which is now
3962 I2's destination. This requires us to do a few adjustments. */
3963 PATTERN (i3) = newpat;
3964 adjust_for_new_dest (i3);
3965
3966 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3967 so we still will.
3968
3969 However, some later insn might be using I2's dest and have
3970 a LOG_LINK pointing at I3. We must remove this link.
3971 The simplest way to remove the link is to point it at I1,
3972 which we know will be a NOTE. */
3973
3974 /* newi2pat is usually a SET here; however, recog_for_combine might
3975 have added some clobbers. */
3976 if (GET_CODE (newi2pat) == PARALLEL)
3977 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3978 else
3979 ni2dest = SET_DEST (newi2pat);
3980
3981 for (insn = NEXT_INSN (i3);
3982 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
3983 || insn != BB_HEAD (this_basic_block->next_bb));
3984 insn = NEXT_INSN (insn))
3985 {
3986 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3987 {
3988 FOR_EACH_LOG_LINK (link, insn)
3989 if (link->insn == i3)
3990 link->insn = i1;
3991
3992 break;
3993 }
3994 }
3995 }
3996
3997 {
3998 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3999 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4000 rtx midnotes = 0;
4001 int from_luid;
4002 /* Compute which registers we expect to eliminate. newi2pat may be setting
4003 either i3dest or i2dest, so we must check it. Also, i1dest may be the
4004 same as i3dest, in which case newi2pat may be setting i1dest. */
4005 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4006 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4007 || !i2dest_killed
4008 ? 0 : i2dest);
4009 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4010 || (newi2pat && reg_set_p (i1dest, newi2pat))
4011 || !i1dest_killed
4012 ? 0 : i1dest);
4013 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4014 || (newi2pat && reg_set_p (i0dest, newi2pat))
4015 || !i0dest_killed
4016 ? 0 : i0dest);
4017
4018 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4019 clear them. */
4020 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4021 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4022 if (i1)
4023 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4024 if (i0)
4025 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4026
4027 /* Ensure that we do not have something that should not be shared but
4028 occurs multiple times in the new insns. Check this by first
4029 resetting all the `used' flags and then copying anything is shared. */
4030
4031 reset_used_flags (i3notes);
4032 reset_used_flags (i2notes);
4033 reset_used_flags (i1notes);
4034 reset_used_flags (i0notes);
4035 reset_used_flags (newpat);
4036 reset_used_flags (newi2pat);
4037 if (undobuf.other_insn)
4038 reset_used_flags (PATTERN (undobuf.other_insn));
4039
4040 i3notes = copy_rtx_if_shared (i3notes);
4041 i2notes = copy_rtx_if_shared (i2notes);
4042 i1notes = copy_rtx_if_shared (i1notes);
4043 i0notes = copy_rtx_if_shared (i0notes);
4044 newpat = copy_rtx_if_shared (newpat);
4045 newi2pat = copy_rtx_if_shared (newi2pat);
4046 if (undobuf.other_insn)
4047 reset_used_flags (PATTERN (undobuf.other_insn));
4048
4049 INSN_CODE (i3) = insn_code_number;
4050 PATTERN (i3) = newpat;
4051
4052 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4053 {
4054 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4055
4056 reset_used_flags (call_usage);
4057 call_usage = copy_rtx (call_usage);
4058
4059 if (substed_i2)
4060 {
4061 /* I2SRC must still be meaningful at this point. Some splitting
4062 operations can invalidate I2SRC, but those operations do not
4063 apply to calls. */
4064 gcc_assert (i2src);
4065 replace_rtx (call_usage, i2dest, i2src);
4066 }
4067
4068 if (substed_i1)
4069 replace_rtx (call_usage, i1dest, i1src);
4070 if (substed_i0)
4071 replace_rtx (call_usage, i0dest, i0src);
4072
4073 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4074 }
4075
4076 if (undobuf.other_insn)
4077 INSN_CODE (undobuf.other_insn) = other_code_number;
4078
4079 /* We had one special case above where I2 had more than one set and
4080 we replaced a destination of one of those sets with the destination
4081 of I3. In that case, we have to update LOG_LINKS of insns later
4082 in this basic block. Note that this (expensive) case is rare.
4083
4084 Also, in this case, we must pretend that all REG_NOTEs for I2
4085 actually came from I3, so that REG_UNUSED notes from I2 will be
4086 properly handled. */
4087
4088 if (i3_subst_into_i2)
4089 {
4090 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4091 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4092 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4093 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4094 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4095 && ! find_reg_note (i2, REG_UNUSED,
4096 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4097 for (temp_insn = NEXT_INSN (i2);
4098 temp_insn
4099 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4100 || BB_HEAD (this_basic_block) != temp_insn);
4101 temp_insn = NEXT_INSN (temp_insn))
4102 if (temp_insn != i3 && INSN_P (temp_insn))
4103 FOR_EACH_LOG_LINK (link, temp_insn)
4104 if (link->insn == i2)
4105 link->insn = i3;
4106
4107 if (i3notes)
4108 {
4109 rtx link = i3notes;
4110 while (XEXP (link, 1))
4111 link = XEXP (link, 1);
4112 XEXP (link, 1) = i2notes;
4113 }
4114 else
4115 i3notes = i2notes;
4116 i2notes = 0;
4117 }
4118
4119 LOG_LINKS (i3) = NULL;
4120 REG_NOTES (i3) = 0;
4121 LOG_LINKS (i2) = NULL;
4122 REG_NOTES (i2) = 0;
4123
4124 if (newi2pat)
4125 {
4126 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4127 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4128 this_basic_block);
4129 INSN_CODE (i2) = i2_code_number;
4130 PATTERN (i2) = newi2pat;
4131 }
4132 else
4133 {
4134 if (MAY_HAVE_DEBUG_INSNS && i2src)
4135 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4136 this_basic_block);
4137 SET_INSN_DELETED (i2);
4138 }
4139
4140 if (i1)
4141 {
4142 LOG_LINKS (i1) = NULL;
4143 REG_NOTES (i1) = 0;
4144 if (MAY_HAVE_DEBUG_INSNS)
4145 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4146 this_basic_block);
4147 SET_INSN_DELETED (i1);
4148 }
4149
4150 if (i0)
4151 {
4152 LOG_LINKS (i0) = NULL;
4153 REG_NOTES (i0) = 0;
4154 if (MAY_HAVE_DEBUG_INSNS)
4155 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4156 this_basic_block);
4157 SET_INSN_DELETED (i0);
4158 }
4159
4160 /* Get death notes for everything that is now used in either I3 or
4161 I2 and used to die in a previous insn. If we built two new
4162 patterns, move from I1 to I2 then I2 to I3 so that we get the
4163 proper movement on registers that I2 modifies. */
4164
4165 if (i0)
4166 from_luid = DF_INSN_LUID (i0);
4167 else if (i1)
4168 from_luid = DF_INSN_LUID (i1);
4169 else
4170 from_luid = DF_INSN_LUID (i2);
4171 if (newi2pat)
4172 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4173 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4174
4175 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4176 if (i3notes)
4177 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4178 elim_i2, elim_i1, elim_i0);
4179 if (i2notes)
4180 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4181 elim_i2, elim_i1, elim_i0);
4182 if (i1notes)
4183 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4184 elim_i2, elim_i1, elim_i0);
4185 if (i0notes)
4186 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4187 elim_i2, elim_i1, elim_i0);
4188 if (midnotes)
4189 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4190 elim_i2, elim_i1, elim_i0);
4191
4192 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4193 know these are REG_UNUSED and want them to go to the desired insn,
4194 so we always pass it as i3. */
4195
4196 if (newi2pat && new_i2_notes)
4197 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4198 NULL_RTX);
4199
4200 if (new_i3_notes)
4201 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4202 NULL_RTX);
4203
4204 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4205 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4206 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4207 in that case, it might delete I2. Similarly for I2 and I1.
4208 Show an additional death due to the REG_DEAD note we make here. If
4209 we discard it in distribute_notes, we will decrement it again. */
4210
4211 if (i3dest_killed)
4212 {
4213 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4214 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4215 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4216 elim_i1, elim_i0);
4217 else
4218 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4219 elim_i2, elim_i1, elim_i0);
4220 }
4221
4222 if (i2dest_in_i2src)
4223 {
4224 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4225 if (newi2pat && reg_set_p (i2dest, newi2pat))
4226 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4227 NULL_RTX, NULL_RTX);
4228 else
4229 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4230 NULL_RTX, NULL_RTX, NULL_RTX);
4231 }
4232
4233 if (i1dest_in_i1src)
4234 {
4235 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4236 if (newi2pat && reg_set_p (i1dest, newi2pat))
4237 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4238 NULL_RTX, NULL_RTX);
4239 else
4240 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4241 NULL_RTX, NULL_RTX, NULL_RTX);
4242 }
4243
4244 if (i0dest_in_i0src)
4245 {
4246 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4247 if (newi2pat && reg_set_p (i0dest, newi2pat))
4248 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4249 NULL_RTX, NULL_RTX);
4250 else
4251 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4252 NULL_RTX, NULL_RTX, NULL_RTX);
4253 }
4254
4255 distribute_links (i3links);
4256 distribute_links (i2links);
4257 distribute_links (i1links);
4258 distribute_links (i0links);
4259
4260 if (REG_P (i2dest))
4261 {
4262 struct insn_link *link;
4263 rtx_insn *i2_insn = 0;
4264 rtx i2_val = 0, set;
4265
4266 /* The insn that used to set this register doesn't exist, and
4267 this life of the register may not exist either. See if one of
4268 I3's links points to an insn that sets I2DEST. If it does,
4269 that is now the last known value for I2DEST. If we don't update
4270 this and I2 set the register to a value that depended on its old
4271 contents, we will get confused. If this insn is used, thing
4272 will be set correctly in combine_instructions. */
4273 FOR_EACH_LOG_LINK (link, i3)
4274 if ((set = single_set (link->insn)) != 0
4275 && rtx_equal_p (i2dest, SET_DEST (set)))
4276 i2_insn = link->insn, i2_val = SET_SRC (set);
4277
4278 record_value_for_reg (i2dest, i2_insn, i2_val);
4279
4280 /* If the reg formerly set in I2 died only once and that was in I3,
4281 zero its use count so it won't make `reload' do any work. */
4282 if (! added_sets_2
4283 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4284 && ! i2dest_in_i2src)
4285 INC_REG_N_SETS (REGNO (i2dest), -1);
4286 }
4287
4288 if (i1 && REG_P (i1dest))
4289 {
4290 struct insn_link *link;
4291 rtx_insn *i1_insn = 0;
4292 rtx i1_val = 0, set;
4293
4294 FOR_EACH_LOG_LINK (link, i3)
4295 if ((set = single_set (link->insn)) != 0
4296 && rtx_equal_p (i1dest, SET_DEST (set)))
4297 i1_insn = link->insn, i1_val = SET_SRC (set);
4298
4299 record_value_for_reg (i1dest, i1_insn, i1_val);
4300
4301 if (! added_sets_1 && ! i1dest_in_i1src)
4302 INC_REG_N_SETS (REGNO (i1dest), -1);
4303 }
4304
4305 if (i0 && REG_P (i0dest))
4306 {
4307 struct insn_link *link;
4308 rtx_insn *i0_insn = 0;
4309 rtx i0_val = 0, set;
4310
4311 FOR_EACH_LOG_LINK (link, i3)
4312 if ((set = single_set (link->insn)) != 0
4313 && rtx_equal_p (i0dest, SET_DEST (set)))
4314 i0_insn = link->insn, i0_val = SET_SRC (set);
4315
4316 record_value_for_reg (i0dest, i0_insn, i0_val);
4317
4318 if (! added_sets_0 && ! i0dest_in_i0src)
4319 INC_REG_N_SETS (REGNO (i0dest), -1);
4320 }
4321
4322 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4323 been made to this insn. The order is important, because newi2pat
4324 can affect nonzero_bits of newpat. */
4325 if (newi2pat)
4326 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4327 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4328 }
4329
4330 if (undobuf.other_insn != NULL_RTX)
4331 {
4332 if (dump_file)
4333 {
4334 fprintf (dump_file, "modifying other_insn ");
4335 dump_insn_slim (dump_file, undobuf.other_insn);
4336 }
4337 df_insn_rescan (undobuf.other_insn);
4338 }
4339
4340 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4341 {
4342 if (dump_file)
4343 {
4344 fprintf (dump_file, "modifying insn i0 ");
4345 dump_insn_slim (dump_file, i0);
4346 }
4347 df_insn_rescan (i0);
4348 }
4349
4350 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4351 {
4352 if (dump_file)
4353 {
4354 fprintf (dump_file, "modifying insn i1 ");
4355 dump_insn_slim (dump_file, i1);
4356 }
4357 df_insn_rescan (i1);
4358 }
4359
4360 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4361 {
4362 if (dump_file)
4363 {
4364 fprintf (dump_file, "modifying insn i2 ");
4365 dump_insn_slim (dump_file, i2);
4366 }
4367 df_insn_rescan (i2);
4368 }
4369
4370 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4371 {
4372 if (dump_file)
4373 {
4374 fprintf (dump_file, "modifying insn i3 ");
4375 dump_insn_slim (dump_file, i3);
4376 }
4377 df_insn_rescan (i3);
4378 }
4379
4380 /* Set new_direct_jump_p if a new return or simple jump instruction
4381 has been created. Adjust the CFG accordingly. */
4382 if (returnjump_p (i3) || any_uncondjump_p (i3))
4383 {
4384 *new_direct_jump_p = 1;
4385 mark_jump_label (PATTERN (i3), i3, 0);
4386 update_cfg_for_uncondjump (i3);
4387 }
4388
4389 if (undobuf.other_insn != NULL_RTX
4390 && (returnjump_p (undobuf.other_insn)
4391 || any_uncondjump_p (undobuf.other_insn)))
4392 {
4393 *new_direct_jump_p = 1;
4394 update_cfg_for_uncondjump (undobuf.other_insn);
4395 }
4396
4397 /* A noop might also need cleaning up of CFG, if it comes from the
4398 simplification of a jump. */
4399 if (JUMP_P (i3)
4400 && GET_CODE (newpat) == SET
4401 && SET_SRC (newpat) == pc_rtx
4402 && SET_DEST (newpat) == pc_rtx)
4403 {
4404 *new_direct_jump_p = 1;
4405 update_cfg_for_uncondjump (i3);
4406 }
4407
4408 if (undobuf.other_insn != NULL_RTX
4409 && JUMP_P (undobuf.other_insn)
4410 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4411 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4412 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4413 {
4414 *new_direct_jump_p = 1;
4415 update_cfg_for_uncondjump (undobuf.other_insn);
4416 }
4417
4418 combine_successes++;
4419 undo_commit ();
4420
4421 if (added_links_insn
4422 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4423 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4424 return added_links_insn;
4425 else
4426 return newi2pat ? i2 : i3;
4427 }
4428 \f
4429 /* Undo all the modifications recorded in undobuf. */
4430
4431 static void
4432 undo_all (void)
4433 {
4434 struct undo *undo, *next;
4435
4436 for (undo = undobuf.undos; undo; undo = next)
4437 {
4438 next = undo->next;
4439 switch (undo->kind)
4440 {
4441 case UNDO_RTX:
4442 *undo->where.r = undo->old_contents.r;
4443 break;
4444 case UNDO_INT:
4445 *undo->where.i = undo->old_contents.i;
4446 break;
4447 case UNDO_MODE:
4448 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4449 break;
4450 case UNDO_LINKS:
4451 *undo->where.l = undo->old_contents.l;
4452 break;
4453 default:
4454 gcc_unreachable ();
4455 }
4456
4457 undo->next = undobuf.frees;
4458 undobuf.frees = undo;
4459 }
4460
4461 undobuf.undos = 0;
4462 }
4463
4464 /* We've committed to accepting the changes we made. Move all
4465 of the undos to the free list. */
4466
4467 static void
4468 undo_commit (void)
4469 {
4470 struct undo *undo, *next;
4471
4472 for (undo = undobuf.undos; undo; undo = next)
4473 {
4474 next = undo->next;
4475 undo->next = undobuf.frees;
4476 undobuf.frees = undo;
4477 }
4478 undobuf.undos = 0;
4479 }
4480 \f
4481 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4482 where we have an arithmetic expression and return that point. LOC will
4483 be inside INSN.
4484
4485 try_combine will call this function to see if an insn can be split into
4486 two insns. */
4487
4488 static rtx *
4489 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4490 {
4491 rtx x = *loc;
4492 enum rtx_code code = GET_CODE (x);
4493 rtx *split;
4494 unsigned HOST_WIDE_INT len = 0;
4495 HOST_WIDE_INT pos = 0;
4496 int unsignedp = 0;
4497 rtx inner = NULL_RTX;
4498
4499 /* First special-case some codes. */
4500 switch (code)
4501 {
4502 case SUBREG:
4503 #ifdef INSN_SCHEDULING
4504 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4505 point. */
4506 if (MEM_P (SUBREG_REG (x)))
4507 return loc;
4508 #endif
4509 return find_split_point (&SUBREG_REG (x), insn, false);
4510
4511 case MEM:
4512 #ifdef HAVE_lo_sum
4513 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4514 using LO_SUM and HIGH. */
4515 if (GET_CODE (XEXP (x, 0)) == CONST
4516 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4517 {
4518 enum machine_mode address_mode = get_address_mode (x);
4519
4520 SUBST (XEXP (x, 0),
4521 gen_rtx_LO_SUM (address_mode,
4522 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4523 XEXP (x, 0)));
4524 return &XEXP (XEXP (x, 0), 0);
4525 }
4526 #endif
4527
4528 /* If we have a PLUS whose second operand is a constant and the
4529 address is not valid, perhaps will can split it up using
4530 the machine-specific way to split large constants. We use
4531 the first pseudo-reg (one of the virtual regs) as a placeholder;
4532 it will not remain in the result. */
4533 if (GET_CODE (XEXP (x, 0)) == PLUS
4534 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4535 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4536 MEM_ADDR_SPACE (x)))
4537 {
4538 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4539 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4540 XEXP (x, 0)),
4541 subst_insn);
4542
4543 /* This should have produced two insns, each of which sets our
4544 placeholder. If the source of the second is a valid address,
4545 we can make put both sources together and make a split point
4546 in the middle. */
4547
4548 if (seq
4549 && NEXT_INSN (seq) != NULL_RTX
4550 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4551 && NONJUMP_INSN_P (seq)
4552 && GET_CODE (PATTERN (seq)) == SET
4553 && SET_DEST (PATTERN (seq)) == reg
4554 && ! reg_mentioned_p (reg,
4555 SET_SRC (PATTERN (seq)))
4556 && NONJUMP_INSN_P (NEXT_INSN (seq))
4557 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4558 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4559 && memory_address_addr_space_p
4560 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4561 MEM_ADDR_SPACE (x)))
4562 {
4563 rtx src1 = SET_SRC (PATTERN (seq));
4564 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4565
4566 /* Replace the placeholder in SRC2 with SRC1. If we can
4567 find where in SRC2 it was placed, that can become our
4568 split point and we can replace this address with SRC2.
4569 Just try two obvious places. */
4570
4571 src2 = replace_rtx (src2, reg, src1);
4572 split = 0;
4573 if (XEXP (src2, 0) == src1)
4574 split = &XEXP (src2, 0);
4575 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4576 && XEXP (XEXP (src2, 0), 0) == src1)
4577 split = &XEXP (XEXP (src2, 0), 0);
4578
4579 if (split)
4580 {
4581 SUBST (XEXP (x, 0), src2);
4582 return split;
4583 }
4584 }
4585
4586 /* If that didn't work, perhaps the first operand is complex and
4587 needs to be computed separately, so make a split point there.
4588 This will occur on machines that just support REG + CONST
4589 and have a constant moved through some previous computation. */
4590
4591 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4592 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4593 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4594 return &XEXP (XEXP (x, 0), 0);
4595 }
4596
4597 /* If we have a PLUS whose first operand is complex, try computing it
4598 separately by making a split there. */
4599 if (GET_CODE (XEXP (x, 0)) == PLUS
4600 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4601 MEM_ADDR_SPACE (x))
4602 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4603 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4604 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4605 return &XEXP (XEXP (x, 0), 0);
4606 break;
4607
4608 case SET:
4609 #ifdef HAVE_cc0
4610 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4611 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4612 we need to put the operand into a register. So split at that
4613 point. */
4614
4615 if (SET_DEST (x) == cc0_rtx
4616 && GET_CODE (SET_SRC (x)) != COMPARE
4617 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4618 && !OBJECT_P (SET_SRC (x))
4619 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4620 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4621 return &SET_SRC (x);
4622 #endif
4623
4624 /* See if we can split SET_SRC as it stands. */
4625 split = find_split_point (&SET_SRC (x), insn, true);
4626 if (split && split != &SET_SRC (x))
4627 return split;
4628
4629 /* See if we can split SET_DEST as it stands. */
4630 split = find_split_point (&SET_DEST (x), insn, false);
4631 if (split && split != &SET_DEST (x))
4632 return split;
4633
4634 /* See if this is a bitfield assignment with everything constant. If
4635 so, this is an IOR of an AND, so split it into that. */
4636 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4637 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4638 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4639 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4640 && CONST_INT_P (SET_SRC (x))
4641 && ((INTVAL (XEXP (SET_DEST (x), 1))
4642 + INTVAL (XEXP (SET_DEST (x), 2)))
4643 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4644 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4645 {
4646 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4647 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4648 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4649 rtx dest = XEXP (SET_DEST (x), 0);
4650 enum machine_mode mode = GET_MODE (dest);
4651 unsigned HOST_WIDE_INT mask
4652 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4653 rtx or_mask;
4654
4655 if (BITS_BIG_ENDIAN)
4656 pos = GET_MODE_PRECISION (mode) - len - pos;
4657
4658 or_mask = gen_int_mode (src << pos, mode);
4659 if (src == mask)
4660 SUBST (SET_SRC (x),
4661 simplify_gen_binary (IOR, mode, dest, or_mask));
4662 else
4663 {
4664 rtx negmask = gen_int_mode (~(mask << pos), mode);
4665 SUBST (SET_SRC (x),
4666 simplify_gen_binary (IOR, mode,
4667 simplify_gen_binary (AND, mode,
4668 dest, negmask),
4669 or_mask));
4670 }
4671
4672 SUBST (SET_DEST (x), dest);
4673
4674 split = find_split_point (&SET_SRC (x), insn, true);
4675 if (split && split != &SET_SRC (x))
4676 return split;
4677 }
4678
4679 /* Otherwise, see if this is an operation that we can split into two.
4680 If so, try to split that. */
4681 code = GET_CODE (SET_SRC (x));
4682
4683 switch (code)
4684 {
4685 case AND:
4686 /* If we are AND'ing with a large constant that is only a single
4687 bit and the result is only being used in a context where we
4688 need to know if it is zero or nonzero, replace it with a bit
4689 extraction. This will avoid the large constant, which might
4690 have taken more than one insn to make. If the constant were
4691 not a valid argument to the AND but took only one insn to make,
4692 this is no worse, but if it took more than one insn, it will
4693 be better. */
4694
4695 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4696 && REG_P (XEXP (SET_SRC (x), 0))
4697 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4698 && REG_P (SET_DEST (x))
4699 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4700 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4701 && XEXP (*split, 0) == SET_DEST (x)
4702 && XEXP (*split, 1) == const0_rtx)
4703 {
4704 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4705 XEXP (SET_SRC (x), 0),
4706 pos, NULL_RTX, 1, 1, 0, 0);
4707 if (extraction != 0)
4708 {
4709 SUBST (SET_SRC (x), extraction);
4710 return find_split_point (loc, insn, false);
4711 }
4712 }
4713 break;
4714
4715 case NE:
4716 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4717 is known to be on, this can be converted into a NEG of a shift. */
4718 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4719 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4720 && 1 <= (pos = exact_log2
4721 (nonzero_bits (XEXP (SET_SRC (x), 0),
4722 GET_MODE (XEXP (SET_SRC (x), 0))))))
4723 {
4724 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4725
4726 SUBST (SET_SRC (x),
4727 gen_rtx_NEG (mode,
4728 gen_rtx_LSHIFTRT (mode,
4729 XEXP (SET_SRC (x), 0),
4730 GEN_INT (pos))));
4731
4732 split = find_split_point (&SET_SRC (x), insn, true);
4733 if (split && split != &SET_SRC (x))
4734 return split;
4735 }
4736 break;
4737
4738 case SIGN_EXTEND:
4739 inner = XEXP (SET_SRC (x), 0);
4740
4741 /* We can't optimize if either mode is a partial integer
4742 mode as we don't know how many bits are significant
4743 in those modes. */
4744 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4745 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4746 break;
4747
4748 pos = 0;
4749 len = GET_MODE_PRECISION (GET_MODE (inner));
4750 unsignedp = 0;
4751 break;
4752
4753 case SIGN_EXTRACT:
4754 case ZERO_EXTRACT:
4755 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4756 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4757 {
4758 inner = XEXP (SET_SRC (x), 0);
4759 len = INTVAL (XEXP (SET_SRC (x), 1));
4760 pos = INTVAL (XEXP (SET_SRC (x), 2));
4761
4762 if (BITS_BIG_ENDIAN)
4763 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4764 unsignedp = (code == ZERO_EXTRACT);
4765 }
4766 break;
4767
4768 default:
4769 break;
4770 }
4771
4772 if (len && pos >= 0
4773 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4774 {
4775 enum machine_mode mode = GET_MODE (SET_SRC (x));
4776
4777 /* For unsigned, we have a choice of a shift followed by an
4778 AND or two shifts. Use two shifts for field sizes where the
4779 constant might be too large. We assume here that we can
4780 always at least get 8-bit constants in an AND insn, which is
4781 true for every current RISC. */
4782
4783 if (unsignedp && len <= 8)
4784 {
4785 unsigned HOST_WIDE_INT mask
4786 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4787 SUBST (SET_SRC (x),
4788 gen_rtx_AND (mode,
4789 gen_rtx_LSHIFTRT
4790 (mode, gen_lowpart (mode, inner),
4791 GEN_INT (pos)),
4792 gen_int_mode (mask, mode)));
4793
4794 split = find_split_point (&SET_SRC (x), insn, true);
4795 if (split && split != &SET_SRC (x))
4796 return split;
4797 }
4798 else
4799 {
4800 SUBST (SET_SRC (x),
4801 gen_rtx_fmt_ee
4802 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4803 gen_rtx_ASHIFT (mode,
4804 gen_lowpart (mode, inner),
4805 GEN_INT (GET_MODE_PRECISION (mode)
4806 - len - pos)),
4807 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4808
4809 split = find_split_point (&SET_SRC (x), insn, true);
4810 if (split && split != &SET_SRC (x))
4811 return split;
4812 }
4813 }
4814
4815 /* See if this is a simple operation with a constant as the second
4816 operand. It might be that this constant is out of range and hence
4817 could be used as a split point. */
4818 if (BINARY_P (SET_SRC (x))
4819 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4820 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4821 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4822 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4823 return &XEXP (SET_SRC (x), 1);
4824
4825 /* Finally, see if this is a simple operation with its first operand
4826 not in a register. The operation might require this operand in a
4827 register, so return it as a split point. We can always do this
4828 because if the first operand were another operation, we would have
4829 already found it as a split point. */
4830 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4831 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4832 return &XEXP (SET_SRC (x), 0);
4833
4834 return 0;
4835
4836 case AND:
4837 case IOR:
4838 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4839 it is better to write this as (not (ior A B)) so we can split it.
4840 Similarly for IOR. */
4841 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4842 {
4843 SUBST (*loc,
4844 gen_rtx_NOT (GET_MODE (x),
4845 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4846 GET_MODE (x),
4847 XEXP (XEXP (x, 0), 0),
4848 XEXP (XEXP (x, 1), 0))));
4849 return find_split_point (loc, insn, set_src);
4850 }
4851
4852 /* Many RISC machines have a large set of logical insns. If the
4853 second operand is a NOT, put it first so we will try to split the
4854 other operand first. */
4855 if (GET_CODE (XEXP (x, 1)) == NOT)
4856 {
4857 rtx tem = XEXP (x, 0);
4858 SUBST (XEXP (x, 0), XEXP (x, 1));
4859 SUBST (XEXP (x, 1), tem);
4860 }
4861 break;
4862
4863 case PLUS:
4864 case MINUS:
4865 /* Canonicalization can produce (minus A (mult B C)), where C is a
4866 constant. It may be better to try splitting (plus (mult B -C) A)
4867 instead if this isn't a multiply by a power of two. */
4868 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4869 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4870 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4871 {
4872 enum machine_mode mode = GET_MODE (x);
4873 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4874 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4875 SUBST (*loc, gen_rtx_PLUS (mode,
4876 gen_rtx_MULT (mode,
4877 XEXP (XEXP (x, 1), 0),
4878 gen_int_mode (other_int,
4879 mode)),
4880 XEXP (x, 0)));
4881 return find_split_point (loc, insn, set_src);
4882 }
4883
4884 /* Split at a multiply-accumulate instruction. However if this is
4885 the SET_SRC, we likely do not have such an instruction and it's
4886 worthless to try this split. */
4887 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4888 return loc;
4889
4890 default:
4891 break;
4892 }
4893
4894 /* Otherwise, select our actions depending on our rtx class. */
4895 switch (GET_RTX_CLASS (code))
4896 {
4897 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4898 case RTX_TERNARY:
4899 split = find_split_point (&XEXP (x, 2), insn, false);
4900 if (split)
4901 return split;
4902 /* ... fall through ... */
4903 case RTX_BIN_ARITH:
4904 case RTX_COMM_ARITH:
4905 case RTX_COMPARE:
4906 case RTX_COMM_COMPARE:
4907 split = find_split_point (&XEXP (x, 1), insn, false);
4908 if (split)
4909 return split;
4910 /* ... fall through ... */
4911 case RTX_UNARY:
4912 /* Some machines have (and (shift ...) ...) insns. If X is not
4913 an AND, but XEXP (X, 0) is, use it as our split point. */
4914 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4915 return &XEXP (x, 0);
4916
4917 split = find_split_point (&XEXP (x, 0), insn, false);
4918 if (split)
4919 return split;
4920 return loc;
4921
4922 default:
4923 /* Otherwise, we don't have a split point. */
4924 return 0;
4925 }
4926 }
4927 \f
4928 /* Throughout X, replace FROM with TO, and return the result.
4929 The result is TO if X is FROM;
4930 otherwise the result is X, but its contents may have been modified.
4931 If they were modified, a record was made in undobuf so that
4932 undo_all will (among other things) return X to its original state.
4933
4934 If the number of changes necessary is too much to record to undo,
4935 the excess changes are not made, so the result is invalid.
4936 The changes already made can still be undone.
4937 undobuf.num_undo is incremented for such changes, so by testing that
4938 the caller can tell whether the result is valid.
4939
4940 `n_occurrences' is incremented each time FROM is replaced.
4941
4942 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4943
4944 IN_COND is nonzero if we are at the top level of a condition.
4945
4946 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4947 by copying if `n_occurrences' is nonzero. */
4948
4949 static rtx
4950 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4951 {
4952 enum rtx_code code = GET_CODE (x);
4953 enum machine_mode op0_mode = VOIDmode;
4954 const char *fmt;
4955 int len, i;
4956 rtx new_rtx;
4957
4958 /* Two expressions are equal if they are identical copies of a shared
4959 RTX or if they are both registers with the same register number
4960 and mode. */
4961
4962 #define COMBINE_RTX_EQUAL_P(X,Y) \
4963 ((X) == (Y) \
4964 || (REG_P (X) && REG_P (Y) \
4965 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4966
4967 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4968 {
4969 n_occurrences++;
4970 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4971 }
4972
4973 /* If X and FROM are the same register but different modes, they
4974 will not have been seen as equal above. However, the log links code
4975 will make a LOG_LINKS entry for that case. If we do nothing, we
4976 will try to rerecognize our original insn and, when it succeeds,
4977 we will delete the feeding insn, which is incorrect.
4978
4979 So force this insn not to match in this (rare) case. */
4980 if (! in_dest && code == REG && REG_P (from)
4981 && reg_overlap_mentioned_p (x, from))
4982 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4983
4984 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4985 of which may contain things that can be combined. */
4986 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4987 return x;
4988
4989 /* It is possible to have a subexpression appear twice in the insn.
4990 Suppose that FROM is a register that appears within TO.
4991 Then, after that subexpression has been scanned once by `subst',
4992 the second time it is scanned, TO may be found. If we were
4993 to scan TO here, we would find FROM within it and create a
4994 self-referent rtl structure which is completely wrong. */
4995 if (COMBINE_RTX_EQUAL_P (x, to))
4996 return to;
4997
4998 /* Parallel asm_operands need special attention because all of the
4999 inputs are shared across the arms. Furthermore, unsharing the
5000 rtl results in recognition failures. Failure to handle this case
5001 specially can result in circular rtl.
5002
5003 Solve this by doing a normal pass across the first entry of the
5004 parallel, and only processing the SET_DESTs of the subsequent
5005 entries. Ug. */
5006
5007 if (code == PARALLEL
5008 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5009 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5010 {
5011 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5012
5013 /* If this substitution failed, this whole thing fails. */
5014 if (GET_CODE (new_rtx) == CLOBBER
5015 && XEXP (new_rtx, 0) == const0_rtx)
5016 return new_rtx;
5017
5018 SUBST (XVECEXP (x, 0, 0), new_rtx);
5019
5020 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5021 {
5022 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5023
5024 if (!REG_P (dest)
5025 && GET_CODE (dest) != CC0
5026 && GET_CODE (dest) != PC)
5027 {
5028 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5029
5030 /* If this substitution failed, this whole thing fails. */
5031 if (GET_CODE (new_rtx) == CLOBBER
5032 && XEXP (new_rtx, 0) == const0_rtx)
5033 return new_rtx;
5034
5035 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5036 }
5037 }
5038 }
5039 else
5040 {
5041 len = GET_RTX_LENGTH (code);
5042 fmt = GET_RTX_FORMAT (code);
5043
5044 /* We don't need to process a SET_DEST that is a register, CC0,
5045 or PC, so set up to skip this common case. All other cases
5046 where we want to suppress replacing something inside a
5047 SET_SRC are handled via the IN_DEST operand. */
5048 if (code == SET
5049 && (REG_P (SET_DEST (x))
5050 || GET_CODE (SET_DEST (x)) == CC0
5051 || GET_CODE (SET_DEST (x)) == PC))
5052 fmt = "ie";
5053
5054 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5055 constant. */
5056 if (fmt[0] == 'e')
5057 op0_mode = GET_MODE (XEXP (x, 0));
5058
5059 for (i = 0; i < len; i++)
5060 {
5061 if (fmt[i] == 'E')
5062 {
5063 int j;
5064 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5065 {
5066 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5067 {
5068 new_rtx = (unique_copy && n_occurrences
5069 ? copy_rtx (to) : to);
5070 n_occurrences++;
5071 }
5072 else
5073 {
5074 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5075 unique_copy);
5076
5077 /* If this substitution failed, this whole thing
5078 fails. */
5079 if (GET_CODE (new_rtx) == CLOBBER
5080 && XEXP (new_rtx, 0) == const0_rtx)
5081 return new_rtx;
5082 }
5083
5084 SUBST (XVECEXP (x, i, j), new_rtx);
5085 }
5086 }
5087 else if (fmt[i] == 'e')
5088 {
5089 /* If this is a register being set, ignore it. */
5090 new_rtx = XEXP (x, i);
5091 if (in_dest
5092 && i == 0
5093 && (((code == SUBREG || code == ZERO_EXTRACT)
5094 && REG_P (new_rtx))
5095 || code == STRICT_LOW_PART))
5096 ;
5097
5098 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5099 {
5100 /* In general, don't install a subreg involving two
5101 modes not tieable. It can worsen register
5102 allocation, and can even make invalid reload
5103 insns, since the reg inside may need to be copied
5104 from in the outside mode, and that may be invalid
5105 if it is an fp reg copied in integer mode.
5106
5107 We allow two exceptions to this: It is valid if
5108 it is inside another SUBREG and the mode of that
5109 SUBREG and the mode of the inside of TO is
5110 tieable and it is valid if X is a SET that copies
5111 FROM to CC0. */
5112
5113 if (GET_CODE (to) == SUBREG
5114 && ! MODES_TIEABLE_P (GET_MODE (to),
5115 GET_MODE (SUBREG_REG (to)))
5116 && ! (code == SUBREG
5117 && MODES_TIEABLE_P (GET_MODE (x),
5118 GET_MODE (SUBREG_REG (to))))
5119 #ifdef HAVE_cc0
5120 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5121 #endif
5122 )
5123 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5124
5125 #ifdef CANNOT_CHANGE_MODE_CLASS
5126 if (code == SUBREG
5127 && REG_P (to)
5128 && REGNO (to) < FIRST_PSEUDO_REGISTER
5129 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
5130 GET_MODE (to),
5131 GET_MODE (x)))
5132 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5133 #endif
5134
5135 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5136 n_occurrences++;
5137 }
5138 else
5139 /* If we are in a SET_DEST, suppress most cases unless we
5140 have gone inside a MEM, in which case we want to
5141 simplify the address. We assume here that things that
5142 are actually part of the destination have their inner
5143 parts in the first expression. This is true for SUBREG,
5144 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5145 things aside from REG and MEM that should appear in a
5146 SET_DEST. */
5147 new_rtx = subst (XEXP (x, i), from, to,
5148 (((in_dest
5149 && (code == SUBREG || code == STRICT_LOW_PART
5150 || code == ZERO_EXTRACT))
5151 || code == SET)
5152 && i == 0),
5153 code == IF_THEN_ELSE && i == 0,
5154 unique_copy);
5155
5156 /* If we found that we will have to reject this combination,
5157 indicate that by returning the CLOBBER ourselves, rather than
5158 an expression containing it. This will speed things up as
5159 well as prevent accidents where two CLOBBERs are considered
5160 to be equal, thus producing an incorrect simplification. */
5161
5162 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5163 return new_rtx;
5164
5165 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5166 {
5167 enum machine_mode mode = GET_MODE (x);
5168
5169 x = simplify_subreg (GET_MODE (x), new_rtx,
5170 GET_MODE (SUBREG_REG (x)),
5171 SUBREG_BYTE (x));
5172 if (! x)
5173 x = gen_rtx_CLOBBER (mode, const0_rtx);
5174 }
5175 else if (CONST_SCALAR_INT_P (new_rtx)
5176 && GET_CODE (x) == ZERO_EXTEND)
5177 {
5178 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5179 new_rtx, GET_MODE (XEXP (x, 0)));
5180 gcc_assert (x);
5181 }
5182 else
5183 SUBST (XEXP (x, i), new_rtx);
5184 }
5185 }
5186 }
5187
5188 /* Check if we are loading something from the constant pool via float
5189 extension; in this case we would undo compress_float_constant
5190 optimization and degenerate constant load to an immediate value. */
5191 if (GET_CODE (x) == FLOAT_EXTEND
5192 && MEM_P (XEXP (x, 0))
5193 && MEM_READONLY_P (XEXP (x, 0)))
5194 {
5195 rtx tmp = avoid_constant_pool_reference (x);
5196 if (x != tmp)
5197 return x;
5198 }
5199
5200 /* Try to simplify X. If the simplification changed the code, it is likely
5201 that further simplification will help, so loop, but limit the number
5202 of repetitions that will be performed. */
5203
5204 for (i = 0; i < 4; i++)
5205 {
5206 /* If X is sufficiently simple, don't bother trying to do anything
5207 with it. */
5208 if (code != CONST_INT && code != REG && code != CLOBBER)
5209 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5210
5211 if (GET_CODE (x) == code)
5212 break;
5213
5214 code = GET_CODE (x);
5215
5216 /* We no longer know the original mode of operand 0 since we
5217 have changed the form of X) */
5218 op0_mode = VOIDmode;
5219 }
5220
5221 return x;
5222 }
5223 \f
5224 /* Simplify X, a piece of RTL. We just operate on the expression at the
5225 outer level; call `subst' to simplify recursively. Return the new
5226 expression.
5227
5228 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5229 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5230 of a condition. */
5231
5232 static rtx
5233 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5234 int in_cond)
5235 {
5236 enum rtx_code code = GET_CODE (x);
5237 enum machine_mode mode = GET_MODE (x);
5238 rtx temp;
5239 int i;
5240
5241 /* If this is a commutative operation, put a constant last and a complex
5242 expression first. We don't need to do this for comparisons here. */
5243 if (COMMUTATIVE_ARITH_P (x)
5244 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5245 {
5246 temp = XEXP (x, 0);
5247 SUBST (XEXP (x, 0), XEXP (x, 1));
5248 SUBST (XEXP (x, 1), temp);
5249 }
5250
5251 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5252 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5253 things. Check for cases where both arms are testing the same
5254 condition.
5255
5256 Don't do anything if all operands are very simple. */
5257
5258 if ((BINARY_P (x)
5259 && ((!OBJECT_P (XEXP (x, 0))
5260 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5261 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5262 || (!OBJECT_P (XEXP (x, 1))
5263 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5264 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5265 || (UNARY_P (x)
5266 && (!OBJECT_P (XEXP (x, 0))
5267 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5268 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5269 {
5270 rtx cond, true_rtx, false_rtx;
5271
5272 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5273 if (cond != 0
5274 /* If everything is a comparison, what we have is highly unlikely
5275 to be simpler, so don't use it. */
5276 && ! (COMPARISON_P (x)
5277 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5278 {
5279 rtx cop1 = const0_rtx;
5280 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5281
5282 if (cond_code == NE && COMPARISON_P (cond))
5283 return x;
5284
5285 /* Simplify the alternative arms; this may collapse the true and
5286 false arms to store-flag values. Be careful to use copy_rtx
5287 here since true_rtx or false_rtx might share RTL with x as a
5288 result of the if_then_else_cond call above. */
5289 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5290 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5291
5292 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5293 is unlikely to be simpler. */
5294 if (general_operand (true_rtx, VOIDmode)
5295 && general_operand (false_rtx, VOIDmode))
5296 {
5297 enum rtx_code reversed;
5298
5299 /* Restarting if we generate a store-flag expression will cause
5300 us to loop. Just drop through in this case. */
5301
5302 /* If the result values are STORE_FLAG_VALUE and zero, we can
5303 just make the comparison operation. */
5304 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5305 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5306 cond, cop1);
5307 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5308 && ((reversed = reversed_comparison_code_parts
5309 (cond_code, cond, cop1, NULL))
5310 != UNKNOWN))
5311 x = simplify_gen_relational (reversed, mode, VOIDmode,
5312 cond, cop1);
5313
5314 /* Likewise, we can make the negate of a comparison operation
5315 if the result values are - STORE_FLAG_VALUE and zero. */
5316 else if (CONST_INT_P (true_rtx)
5317 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5318 && false_rtx == const0_rtx)
5319 x = simplify_gen_unary (NEG, mode,
5320 simplify_gen_relational (cond_code,
5321 mode, VOIDmode,
5322 cond, cop1),
5323 mode);
5324 else if (CONST_INT_P (false_rtx)
5325 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5326 && true_rtx == const0_rtx
5327 && ((reversed = reversed_comparison_code_parts
5328 (cond_code, cond, cop1, NULL))
5329 != UNKNOWN))
5330 x = simplify_gen_unary (NEG, mode,
5331 simplify_gen_relational (reversed,
5332 mode, VOIDmode,
5333 cond, cop1),
5334 mode);
5335 else
5336 return gen_rtx_IF_THEN_ELSE (mode,
5337 simplify_gen_relational (cond_code,
5338 mode,
5339 VOIDmode,
5340 cond,
5341 cop1),
5342 true_rtx, false_rtx);
5343
5344 code = GET_CODE (x);
5345 op0_mode = VOIDmode;
5346 }
5347 }
5348 }
5349
5350 /* Try to fold this expression in case we have constants that weren't
5351 present before. */
5352 temp = 0;
5353 switch (GET_RTX_CLASS (code))
5354 {
5355 case RTX_UNARY:
5356 if (op0_mode == VOIDmode)
5357 op0_mode = GET_MODE (XEXP (x, 0));
5358 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5359 break;
5360 case RTX_COMPARE:
5361 case RTX_COMM_COMPARE:
5362 {
5363 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5364 if (cmp_mode == VOIDmode)
5365 {
5366 cmp_mode = GET_MODE (XEXP (x, 1));
5367 if (cmp_mode == VOIDmode)
5368 cmp_mode = op0_mode;
5369 }
5370 temp = simplify_relational_operation (code, mode, cmp_mode,
5371 XEXP (x, 0), XEXP (x, 1));
5372 }
5373 break;
5374 case RTX_COMM_ARITH:
5375 case RTX_BIN_ARITH:
5376 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5377 break;
5378 case RTX_BITFIELD_OPS:
5379 case RTX_TERNARY:
5380 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5381 XEXP (x, 1), XEXP (x, 2));
5382 break;
5383 default:
5384 break;
5385 }
5386
5387 if (temp)
5388 {
5389 x = temp;
5390 code = GET_CODE (temp);
5391 op0_mode = VOIDmode;
5392 mode = GET_MODE (temp);
5393 }
5394
5395 /* First see if we can apply the inverse distributive law. */
5396 if (code == PLUS || code == MINUS
5397 || code == AND || code == IOR || code == XOR)
5398 {
5399 x = apply_distributive_law (x);
5400 code = GET_CODE (x);
5401 op0_mode = VOIDmode;
5402 }
5403
5404 /* If CODE is an associative operation not otherwise handled, see if we
5405 can associate some operands. This can win if they are constants or
5406 if they are logically related (i.e. (a & b) & a). */
5407 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5408 || code == AND || code == IOR || code == XOR
5409 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5410 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5411 || (flag_associative_math && FLOAT_MODE_P (mode))))
5412 {
5413 if (GET_CODE (XEXP (x, 0)) == code)
5414 {
5415 rtx other = XEXP (XEXP (x, 0), 0);
5416 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5417 rtx inner_op1 = XEXP (x, 1);
5418 rtx inner;
5419
5420 /* Make sure we pass the constant operand if any as the second
5421 one if this is a commutative operation. */
5422 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5423 {
5424 rtx tem = inner_op0;
5425 inner_op0 = inner_op1;
5426 inner_op1 = tem;
5427 }
5428 inner = simplify_binary_operation (code == MINUS ? PLUS
5429 : code == DIV ? MULT
5430 : code,
5431 mode, inner_op0, inner_op1);
5432
5433 /* For commutative operations, try the other pair if that one
5434 didn't simplify. */
5435 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5436 {
5437 other = XEXP (XEXP (x, 0), 1);
5438 inner = simplify_binary_operation (code, mode,
5439 XEXP (XEXP (x, 0), 0),
5440 XEXP (x, 1));
5441 }
5442
5443 if (inner)
5444 return simplify_gen_binary (code, mode, other, inner);
5445 }
5446 }
5447
5448 /* A little bit of algebraic simplification here. */
5449 switch (code)
5450 {
5451 case MEM:
5452 /* Ensure that our address has any ASHIFTs converted to MULT in case
5453 address-recognizing predicates are called later. */
5454 temp = make_compound_operation (XEXP (x, 0), MEM);
5455 SUBST (XEXP (x, 0), temp);
5456 break;
5457
5458 case SUBREG:
5459 if (op0_mode == VOIDmode)
5460 op0_mode = GET_MODE (SUBREG_REG (x));
5461
5462 /* See if this can be moved to simplify_subreg. */
5463 if (CONSTANT_P (SUBREG_REG (x))
5464 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5465 /* Don't call gen_lowpart if the inner mode
5466 is VOIDmode and we cannot simplify it, as SUBREG without
5467 inner mode is invalid. */
5468 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5469 || gen_lowpart_common (mode, SUBREG_REG (x))))
5470 return gen_lowpart (mode, SUBREG_REG (x));
5471
5472 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5473 break;
5474 {
5475 rtx temp;
5476 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5477 SUBREG_BYTE (x));
5478 if (temp)
5479 return temp;
5480
5481 /* If op is known to have all lower bits zero, the result is zero. */
5482 if (!in_dest
5483 && SCALAR_INT_MODE_P (mode)
5484 && SCALAR_INT_MODE_P (op0_mode)
5485 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5486 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5487 && HWI_COMPUTABLE_MODE_P (op0_mode)
5488 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5489 & GET_MODE_MASK (mode)) == 0)
5490 return CONST0_RTX (mode);
5491 }
5492
5493 /* Don't change the mode of the MEM if that would change the meaning
5494 of the address. */
5495 if (MEM_P (SUBREG_REG (x))
5496 && (MEM_VOLATILE_P (SUBREG_REG (x))
5497 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5498 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5499 return gen_rtx_CLOBBER (mode, const0_rtx);
5500
5501 /* Note that we cannot do any narrowing for non-constants since
5502 we might have been counting on using the fact that some bits were
5503 zero. We now do this in the SET. */
5504
5505 break;
5506
5507 case NEG:
5508 temp = expand_compound_operation (XEXP (x, 0));
5509
5510 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5511 replaced by (lshiftrt X C). This will convert
5512 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5513
5514 if (GET_CODE (temp) == ASHIFTRT
5515 && CONST_INT_P (XEXP (temp, 1))
5516 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5517 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5518 INTVAL (XEXP (temp, 1)));
5519
5520 /* If X has only a single bit that might be nonzero, say, bit I, convert
5521 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5522 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5523 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5524 or a SUBREG of one since we'd be making the expression more
5525 complex if it was just a register. */
5526
5527 if (!REG_P (temp)
5528 && ! (GET_CODE (temp) == SUBREG
5529 && REG_P (SUBREG_REG (temp)))
5530 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5531 {
5532 rtx temp1 = simplify_shift_const
5533 (NULL_RTX, ASHIFTRT, mode,
5534 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5535 GET_MODE_PRECISION (mode) - 1 - i),
5536 GET_MODE_PRECISION (mode) - 1 - i);
5537
5538 /* If all we did was surround TEMP with the two shifts, we
5539 haven't improved anything, so don't use it. Otherwise,
5540 we are better off with TEMP1. */
5541 if (GET_CODE (temp1) != ASHIFTRT
5542 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5543 || XEXP (XEXP (temp1, 0), 0) != temp)
5544 return temp1;
5545 }
5546 break;
5547
5548 case TRUNCATE:
5549 /* We can't handle truncation to a partial integer mode here
5550 because we don't know the real bitsize of the partial
5551 integer mode. */
5552 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5553 break;
5554
5555 if (HWI_COMPUTABLE_MODE_P (mode))
5556 SUBST (XEXP (x, 0),
5557 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5558 GET_MODE_MASK (mode), 0));
5559
5560 /* We can truncate a constant value and return it. */
5561 if (CONST_INT_P (XEXP (x, 0)))
5562 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5563
5564 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5565 whose value is a comparison can be replaced with a subreg if
5566 STORE_FLAG_VALUE permits. */
5567 if (HWI_COMPUTABLE_MODE_P (mode)
5568 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5569 && (temp = get_last_value (XEXP (x, 0)))
5570 && COMPARISON_P (temp))
5571 return gen_lowpart (mode, XEXP (x, 0));
5572 break;
5573
5574 case CONST:
5575 /* (const (const X)) can become (const X). Do it this way rather than
5576 returning the inner CONST since CONST can be shared with a
5577 REG_EQUAL note. */
5578 if (GET_CODE (XEXP (x, 0)) == CONST)
5579 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5580 break;
5581
5582 #ifdef HAVE_lo_sum
5583 case LO_SUM:
5584 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5585 can add in an offset. find_split_point will split this address up
5586 again if it doesn't match. */
5587 if (GET_CODE (XEXP (x, 0)) == HIGH
5588 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5589 return XEXP (x, 1);
5590 break;
5591 #endif
5592
5593 case PLUS:
5594 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5595 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5596 bit-field and can be replaced by either a sign_extend or a
5597 sign_extract. The `and' may be a zero_extend and the two
5598 <c>, -<c> constants may be reversed. */
5599 if (GET_CODE (XEXP (x, 0)) == XOR
5600 && CONST_INT_P (XEXP (x, 1))
5601 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5602 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5603 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5604 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5605 && HWI_COMPUTABLE_MODE_P (mode)
5606 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5607 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5608 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5609 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5610 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5611 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5612 == (unsigned int) i + 1))))
5613 return simplify_shift_const
5614 (NULL_RTX, ASHIFTRT, mode,
5615 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5616 XEXP (XEXP (XEXP (x, 0), 0), 0),
5617 GET_MODE_PRECISION (mode) - (i + 1)),
5618 GET_MODE_PRECISION (mode) - (i + 1));
5619
5620 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5621 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5622 the bitsize of the mode - 1. This allows simplification of
5623 "a = (b & 8) == 0;" */
5624 if (XEXP (x, 1) == constm1_rtx
5625 && !REG_P (XEXP (x, 0))
5626 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5627 && REG_P (SUBREG_REG (XEXP (x, 0))))
5628 && nonzero_bits (XEXP (x, 0), mode) == 1)
5629 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5630 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5631 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5632 GET_MODE_PRECISION (mode) - 1),
5633 GET_MODE_PRECISION (mode) - 1);
5634
5635 /* If we are adding two things that have no bits in common, convert
5636 the addition into an IOR. This will often be further simplified,
5637 for example in cases like ((a & 1) + (a & 2)), which can
5638 become a & 3. */
5639
5640 if (HWI_COMPUTABLE_MODE_P (mode)
5641 && (nonzero_bits (XEXP (x, 0), mode)
5642 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5643 {
5644 /* Try to simplify the expression further. */
5645 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5646 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5647
5648 /* If we could, great. If not, do not go ahead with the IOR
5649 replacement, since PLUS appears in many special purpose
5650 address arithmetic instructions. */
5651 if (GET_CODE (temp) != CLOBBER
5652 && (GET_CODE (temp) != IOR
5653 || ((XEXP (temp, 0) != XEXP (x, 0)
5654 || XEXP (temp, 1) != XEXP (x, 1))
5655 && (XEXP (temp, 0) != XEXP (x, 1)
5656 || XEXP (temp, 1) != XEXP (x, 0)))))
5657 return temp;
5658 }
5659 break;
5660
5661 case MINUS:
5662 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5663 (and <foo> (const_int pow2-1)) */
5664 if (GET_CODE (XEXP (x, 1)) == AND
5665 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5666 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5667 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5668 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5669 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5670 break;
5671
5672 case MULT:
5673 /* If we have (mult (plus A B) C), apply the distributive law and then
5674 the inverse distributive law to see if things simplify. This
5675 occurs mostly in addresses, often when unrolling loops. */
5676
5677 if (GET_CODE (XEXP (x, 0)) == PLUS)
5678 {
5679 rtx result = distribute_and_simplify_rtx (x, 0);
5680 if (result)
5681 return result;
5682 }
5683
5684 /* Try simplify a*(b/c) as (a*b)/c. */
5685 if (FLOAT_MODE_P (mode) && flag_associative_math
5686 && GET_CODE (XEXP (x, 0)) == DIV)
5687 {
5688 rtx tem = simplify_binary_operation (MULT, mode,
5689 XEXP (XEXP (x, 0), 0),
5690 XEXP (x, 1));
5691 if (tem)
5692 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5693 }
5694 break;
5695
5696 case UDIV:
5697 /* If this is a divide by a power of two, treat it as a shift if
5698 its first operand is a shift. */
5699 if (CONST_INT_P (XEXP (x, 1))
5700 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5701 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5702 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5703 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5704 || GET_CODE (XEXP (x, 0)) == ROTATE
5705 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5706 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5707 break;
5708
5709 case EQ: case NE:
5710 case GT: case GTU: case GE: case GEU:
5711 case LT: case LTU: case LE: case LEU:
5712 case UNEQ: case LTGT:
5713 case UNGT: case UNGE:
5714 case UNLT: case UNLE:
5715 case UNORDERED: case ORDERED:
5716 /* If the first operand is a condition code, we can't do anything
5717 with it. */
5718 if (GET_CODE (XEXP (x, 0)) == COMPARE
5719 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5720 && ! CC0_P (XEXP (x, 0))))
5721 {
5722 rtx op0 = XEXP (x, 0);
5723 rtx op1 = XEXP (x, 1);
5724 enum rtx_code new_code;
5725
5726 if (GET_CODE (op0) == COMPARE)
5727 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5728
5729 /* Simplify our comparison, if possible. */
5730 new_code = simplify_comparison (code, &op0, &op1);
5731
5732 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5733 if only the low-order bit is possibly nonzero in X (such as when
5734 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5735 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5736 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5737 (plus X 1).
5738
5739 Remove any ZERO_EXTRACT we made when thinking this was a
5740 comparison. It may now be simpler to use, e.g., an AND. If a
5741 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5742 the call to make_compound_operation in the SET case.
5743
5744 Don't apply these optimizations if the caller would
5745 prefer a comparison rather than a value.
5746 E.g., for the condition in an IF_THEN_ELSE most targets need
5747 an explicit comparison. */
5748
5749 if (in_cond)
5750 ;
5751
5752 else if (STORE_FLAG_VALUE == 1
5753 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5754 && op1 == const0_rtx
5755 && mode == GET_MODE (op0)
5756 && nonzero_bits (op0, mode) == 1)
5757 return gen_lowpart (mode,
5758 expand_compound_operation (op0));
5759
5760 else if (STORE_FLAG_VALUE == 1
5761 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5762 && op1 == const0_rtx
5763 && mode == GET_MODE (op0)
5764 && (num_sign_bit_copies (op0, mode)
5765 == GET_MODE_PRECISION (mode)))
5766 {
5767 op0 = expand_compound_operation (op0);
5768 return simplify_gen_unary (NEG, mode,
5769 gen_lowpart (mode, op0),
5770 mode);
5771 }
5772
5773 else if (STORE_FLAG_VALUE == 1
5774 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5775 && op1 == const0_rtx
5776 && mode == GET_MODE (op0)
5777 && nonzero_bits (op0, mode) == 1)
5778 {
5779 op0 = expand_compound_operation (op0);
5780 return simplify_gen_binary (XOR, mode,
5781 gen_lowpart (mode, op0),
5782 const1_rtx);
5783 }
5784
5785 else if (STORE_FLAG_VALUE == 1
5786 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5787 && op1 == const0_rtx
5788 && mode == GET_MODE (op0)
5789 && (num_sign_bit_copies (op0, mode)
5790 == GET_MODE_PRECISION (mode)))
5791 {
5792 op0 = expand_compound_operation (op0);
5793 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5794 }
5795
5796 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5797 those above. */
5798 if (in_cond)
5799 ;
5800
5801 else if (STORE_FLAG_VALUE == -1
5802 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5803 && op1 == const0_rtx
5804 && (num_sign_bit_copies (op0, mode)
5805 == GET_MODE_PRECISION (mode)))
5806 return gen_lowpart (mode,
5807 expand_compound_operation (op0));
5808
5809 else if (STORE_FLAG_VALUE == -1
5810 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5811 && op1 == const0_rtx
5812 && mode == GET_MODE (op0)
5813 && nonzero_bits (op0, mode) == 1)
5814 {
5815 op0 = expand_compound_operation (op0);
5816 return simplify_gen_unary (NEG, mode,
5817 gen_lowpart (mode, op0),
5818 mode);
5819 }
5820
5821 else if (STORE_FLAG_VALUE == -1
5822 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5823 && op1 == const0_rtx
5824 && mode == GET_MODE (op0)
5825 && (num_sign_bit_copies (op0, mode)
5826 == GET_MODE_PRECISION (mode)))
5827 {
5828 op0 = expand_compound_operation (op0);
5829 return simplify_gen_unary (NOT, mode,
5830 gen_lowpart (mode, op0),
5831 mode);
5832 }
5833
5834 /* If X is 0/1, (eq X 0) is X-1. */
5835 else if (STORE_FLAG_VALUE == -1
5836 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5837 && op1 == const0_rtx
5838 && mode == GET_MODE (op0)
5839 && nonzero_bits (op0, mode) == 1)
5840 {
5841 op0 = expand_compound_operation (op0);
5842 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5843 }
5844
5845 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5846 one bit that might be nonzero, we can convert (ne x 0) to
5847 (ashift x c) where C puts the bit in the sign bit. Remove any
5848 AND with STORE_FLAG_VALUE when we are done, since we are only
5849 going to test the sign bit. */
5850 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5851 && HWI_COMPUTABLE_MODE_P (mode)
5852 && val_signbit_p (mode, STORE_FLAG_VALUE)
5853 && op1 == const0_rtx
5854 && mode == GET_MODE (op0)
5855 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5856 {
5857 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5858 expand_compound_operation (op0),
5859 GET_MODE_PRECISION (mode) - 1 - i);
5860 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5861 return XEXP (x, 0);
5862 else
5863 return x;
5864 }
5865
5866 /* If the code changed, return a whole new comparison.
5867 We also need to avoid using SUBST in cases where
5868 simplify_comparison has widened a comparison with a CONST_INT,
5869 since in that case the wider CONST_INT may fail the sanity
5870 checks in do_SUBST. */
5871 if (new_code != code
5872 || (CONST_INT_P (op1)
5873 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
5874 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
5875 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5876
5877 /* Otherwise, keep this operation, but maybe change its operands.
5878 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5879 SUBST (XEXP (x, 0), op0);
5880 SUBST (XEXP (x, 1), op1);
5881 }
5882 break;
5883
5884 case IF_THEN_ELSE:
5885 return simplify_if_then_else (x);
5886
5887 case ZERO_EXTRACT:
5888 case SIGN_EXTRACT:
5889 case ZERO_EXTEND:
5890 case SIGN_EXTEND:
5891 /* If we are processing SET_DEST, we are done. */
5892 if (in_dest)
5893 return x;
5894
5895 return expand_compound_operation (x);
5896
5897 case SET:
5898 return simplify_set (x);
5899
5900 case AND:
5901 case IOR:
5902 return simplify_logical (x);
5903
5904 case ASHIFT:
5905 case LSHIFTRT:
5906 case ASHIFTRT:
5907 case ROTATE:
5908 case ROTATERT:
5909 /* If this is a shift by a constant amount, simplify it. */
5910 if (CONST_INT_P (XEXP (x, 1)))
5911 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5912 INTVAL (XEXP (x, 1)));
5913
5914 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5915 SUBST (XEXP (x, 1),
5916 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5917 ((unsigned HOST_WIDE_INT) 1
5918 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5919 - 1,
5920 0));
5921 break;
5922
5923 default:
5924 break;
5925 }
5926
5927 return x;
5928 }
5929 \f
5930 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5931
5932 static rtx
5933 simplify_if_then_else (rtx x)
5934 {
5935 enum machine_mode mode = GET_MODE (x);
5936 rtx cond = XEXP (x, 0);
5937 rtx true_rtx = XEXP (x, 1);
5938 rtx false_rtx = XEXP (x, 2);
5939 enum rtx_code true_code = GET_CODE (cond);
5940 int comparison_p = COMPARISON_P (cond);
5941 rtx temp;
5942 int i;
5943 enum rtx_code false_code;
5944 rtx reversed;
5945
5946 /* Simplify storing of the truth value. */
5947 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5948 return simplify_gen_relational (true_code, mode, VOIDmode,
5949 XEXP (cond, 0), XEXP (cond, 1));
5950
5951 /* Also when the truth value has to be reversed. */
5952 if (comparison_p
5953 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5954 && (reversed = reversed_comparison (cond, mode)))
5955 return reversed;
5956
5957 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5958 in it is being compared against certain values. Get the true and false
5959 comparisons and see if that says anything about the value of each arm. */
5960
5961 if (comparison_p
5962 && ((false_code = reversed_comparison_code (cond, NULL))
5963 != UNKNOWN)
5964 && REG_P (XEXP (cond, 0)))
5965 {
5966 HOST_WIDE_INT nzb;
5967 rtx from = XEXP (cond, 0);
5968 rtx true_val = XEXP (cond, 1);
5969 rtx false_val = true_val;
5970 int swapped = 0;
5971
5972 /* If FALSE_CODE is EQ, swap the codes and arms. */
5973
5974 if (false_code == EQ)
5975 {
5976 swapped = 1, true_code = EQ, false_code = NE;
5977 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5978 }
5979
5980 /* If we are comparing against zero and the expression being tested has
5981 only a single bit that might be nonzero, that is its value when it is
5982 not equal to zero. Similarly if it is known to be -1 or 0. */
5983
5984 if (true_code == EQ && true_val == const0_rtx
5985 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5986 {
5987 false_code = EQ;
5988 false_val = gen_int_mode (nzb, GET_MODE (from));
5989 }
5990 else if (true_code == EQ && true_val == const0_rtx
5991 && (num_sign_bit_copies (from, GET_MODE (from))
5992 == GET_MODE_PRECISION (GET_MODE (from))))
5993 {
5994 false_code = EQ;
5995 false_val = constm1_rtx;
5996 }
5997
5998 /* Now simplify an arm if we know the value of the register in the
5999 branch and it is used in the arm. Be careful due to the potential
6000 of locally-shared RTL. */
6001
6002 if (reg_mentioned_p (from, true_rtx))
6003 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6004 from, true_val),
6005 pc_rtx, pc_rtx, 0, 0, 0);
6006 if (reg_mentioned_p (from, false_rtx))
6007 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6008 from, false_val),
6009 pc_rtx, pc_rtx, 0, 0, 0);
6010
6011 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6012 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6013
6014 true_rtx = XEXP (x, 1);
6015 false_rtx = XEXP (x, 2);
6016 true_code = GET_CODE (cond);
6017 }
6018
6019 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6020 reversed, do so to avoid needing two sets of patterns for
6021 subtract-and-branch insns. Similarly if we have a constant in the true
6022 arm, the false arm is the same as the first operand of the comparison, or
6023 the false arm is more complicated than the true arm. */
6024
6025 if (comparison_p
6026 && reversed_comparison_code (cond, NULL) != UNKNOWN
6027 && (true_rtx == pc_rtx
6028 || (CONSTANT_P (true_rtx)
6029 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6030 || true_rtx == const0_rtx
6031 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6032 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6033 && !OBJECT_P (false_rtx))
6034 || reg_mentioned_p (true_rtx, false_rtx)
6035 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6036 {
6037 true_code = reversed_comparison_code (cond, NULL);
6038 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6039 SUBST (XEXP (x, 1), false_rtx);
6040 SUBST (XEXP (x, 2), true_rtx);
6041
6042 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6043 cond = XEXP (x, 0);
6044
6045 /* It is possible that the conditional has been simplified out. */
6046 true_code = GET_CODE (cond);
6047 comparison_p = COMPARISON_P (cond);
6048 }
6049
6050 /* If the two arms are identical, we don't need the comparison. */
6051
6052 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6053 return true_rtx;
6054
6055 /* Convert a == b ? b : a to "a". */
6056 if (true_code == EQ && ! side_effects_p (cond)
6057 && !HONOR_NANS (mode)
6058 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6059 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6060 return false_rtx;
6061 else if (true_code == NE && ! side_effects_p (cond)
6062 && !HONOR_NANS (mode)
6063 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6064 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6065 return true_rtx;
6066
6067 /* Look for cases where we have (abs x) or (neg (abs X)). */
6068
6069 if (GET_MODE_CLASS (mode) == MODE_INT
6070 && comparison_p
6071 && XEXP (cond, 1) == const0_rtx
6072 && GET_CODE (false_rtx) == NEG
6073 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6074 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6075 && ! side_effects_p (true_rtx))
6076 switch (true_code)
6077 {
6078 case GT:
6079 case GE:
6080 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6081 case LT:
6082 case LE:
6083 return
6084 simplify_gen_unary (NEG, mode,
6085 simplify_gen_unary (ABS, mode, true_rtx, mode),
6086 mode);
6087 default:
6088 break;
6089 }
6090
6091 /* Look for MIN or MAX. */
6092
6093 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6094 && comparison_p
6095 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6096 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6097 && ! side_effects_p (cond))
6098 switch (true_code)
6099 {
6100 case GE:
6101 case GT:
6102 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6103 case LE:
6104 case LT:
6105 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6106 case GEU:
6107 case GTU:
6108 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6109 case LEU:
6110 case LTU:
6111 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6112 default:
6113 break;
6114 }
6115
6116 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6117 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6118 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6119 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6120 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6121 neither 1 or -1, but it isn't worth checking for. */
6122
6123 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6124 && comparison_p
6125 && GET_MODE_CLASS (mode) == MODE_INT
6126 && ! side_effects_p (x))
6127 {
6128 rtx t = make_compound_operation (true_rtx, SET);
6129 rtx f = make_compound_operation (false_rtx, SET);
6130 rtx cond_op0 = XEXP (cond, 0);
6131 rtx cond_op1 = XEXP (cond, 1);
6132 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6133 enum machine_mode m = mode;
6134 rtx z = 0, c1 = NULL_RTX;
6135
6136 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6137 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6138 || GET_CODE (t) == ASHIFT
6139 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6140 && rtx_equal_p (XEXP (t, 0), f))
6141 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6142
6143 /* If an identity-zero op is commutative, check whether there
6144 would be a match if we swapped the operands. */
6145 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6146 || GET_CODE (t) == XOR)
6147 && rtx_equal_p (XEXP (t, 1), f))
6148 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6149 else if (GET_CODE (t) == SIGN_EXTEND
6150 && (GET_CODE (XEXP (t, 0)) == PLUS
6151 || GET_CODE (XEXP (t, 0)) == MINUS
6152 || GET_CODE (XEXP (t, 0)) == IOR
6153 || GET_CODE (XEXP (t, 0)) == XOR
6154 || GET_CODE (XEXP (t, 0)) == ASHIFT
6155 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6156 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6157 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6158 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6159 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6160 && (num_sign_bit_copies (f, GET_MODE (f))
6161 > (unsigned int)
6162 (GET_MODE_PRECISION (mode)
6163 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6164 {
6165 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6166 extend_op = SIGN_EXTEND;
6167 m = GET_MODE (XEXP (t, 0));
6168 }
6169 else if (GET_CODE (t) == SIGN_EXTEND
6170 && (GET_CODE (XEXP (t, 0)) == PLUS
6171 || GET_CODE (XEXP (t, 0)) == IOR
6172 || GET_CODE (XEXP (t, 0)) == XOR)
6173 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6174 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6175 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6176 && (num_sign_bit_copies (f, GET_MODE (f))
6177 > (unsigned int)
6178 (GET_MODE_PRECISION (mode)
6179 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6180 {
6181 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6182 extend_op = SIGN_EXTEND;
6183 m = GET_MODE (XEXP (t, 0));
6184 }
6185 else if (GET_CODE (t) == ZERO_EXTEND
6186 && (GET_CODE (XEXP (t, 0)) == PLUS
6187 || GET_CODE (XEXP (t, 0)) == MINUS
6188 || GET_CODE (XEXP (t, 0)) == IOR
6189 || GET_CODE (XEXP (t, 0)) == XOR
6190 || GET_CODE (XEXP (t, 0)) == ASHIFT
6191 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6192 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6193 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6194 && HWI_COMPUTABLE_MODE_P (mode)
6195 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6196 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6197 && ((nonzero_bits (f, GET_MODE (f))
6198 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6199 == 0))
6200 {
6201 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6202 extend_op = ZERO_EXTEND;
6203 m = GET_MODE (XEXP (t, 0));
6204 }
6205 else if (GET_CODE (t) == ZERO_EXTEND
6206 && (GET_CODE (XEXP (t, 0)) == PLUS
6207 || GET_CODE (XEXP (t, 0)) == IOR
6208 || GET_CODE (XEXP (t, 0)) == XOR)
6209 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6210 && HWI_COMPUTABLE_MODE_P (mode)
6211 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6212 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6213 && ((nonzero_bits (f, GET_MODE (f))
6214 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6215 == 0))
6216 {
6217 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6218 extend_op = ZERO_EXTEND;
6219 m = GET_MODE (XEXP (t, 0));
6220 }
6221
6222 if (z)
6223 {
6224 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6225 cond_op0, cond_op1),
6226 pc_rtx, pc_rtx, 0, 0, 0);
6227 temp = simplify_gen_binary (MULT, m, temp,
6228 simplify_gen_binary (MULT, m, c1,
6229 const_true_rtx));
6230 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6231 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6232
6233 if (extend_op != UNKNOWN)
6234 temp = simplify_gen_unary (extend_op, mode, temp, m);
6235
6236 return temp;
6237 }
6238 }
6239
6240 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6241 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6242 negation of a single bit, we can convert this operation to a shift. We
6243 can actually do this more generally, but it doesn't seem worth it. */
6244
6245 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6246 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6247 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6248 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6249 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6250 == GET_MODE_PRECISION (mode))
6251 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6252 return
6253 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6254 gen_lowpart (mode, XEXP (cond, 0)), i);
6255
6256 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6257 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6258 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6259 && GET_MODE (XEXP (cond, 0)) == mode
6260 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6261 == nonzero_bits (XEXP (cond, 0), mode)
6262 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6263 return XEXP (cond, 0);
6264
6265 return x;
6266 }
6267 \f
6268 /* Simplify X, a SET expression. Return the new expression. */
6269
6270 static rtx
6271 simplify_set (rtx x)
6272 {
6273 rtx src = SET_SRC (x);
6274 rtx dest = SET_DEST (x);
6275 enum machine_mode mode
6276 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6277 rtx_insn *other_insn;
6278 rtx *cc_use;
6279
6280 /* (set (pc) (return)) gets written as (return). */
6281 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6282 return src;
6283
6284 /* Now that we know for sure which bits of SRC we are using, see if we can
6285 simplify the expression for the object knowing that we only need the
6286 low-order bits. */
6287
6288 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6289 {
6290 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6291 SUBST (SET_SRC (x), src);
6292 }
6293
6294 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6295 the comparison result and try to simplify it unless we already have used
6296 undobuf.other_insn. */
6297 if ((GET_MODE_CLASS (mode) == MODE_CC
6298 || GET_CODE (src) == COMPARE
6299 || CC0_P (dest))
6300 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6301 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6302 && COMPARISON_P (*cc_use)
6303 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6304 {
6305 enum rtx_code old_code = GET_CODE (*cc_use);
6306 enum rtx_code new_code;
6307 rtx op0, op1, tmp;
6308 int other_changed = 0;
6309 rtx inner_compare = NULL_RTX;
6310 enum machine_mode compare_mode = GET_MODE (dest);
6311
6312 if (GET_CODE (src) == COMPARE)
6313 {
6314 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6315 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6316 {
6317 inner_compare = op0;
6318 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6319 }
6320 }
6321 else
6322 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6323
6324 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6325 op0, op1);
6326 if (!tmp)
6327 new_code = old_code;
6328 else if (!CONSTANT_P (tmp))
6329 {
6330 new_code = GET_CODE (tmp);
6331 op0 = XEXP (tmp, 0);
6332 op1 = XEXP (tmp, 1);
6333 }
6334 else
6335 {
6336 rtx pat = PATTERN (other_insn);
6337 undobuf.other_insn = other_insn;
6338 SUBST (*cc_use, tmp);
6339
6340 /* Attempt to simplify CC user. */
6341 if (GET_CODE (pat) == SET)
6342 {
6343 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6344 if (new_rtx != NULL_RTX)
6345 SUBST (SET_SRC (pat), new_rtx);
6346 }
6347
6348 /* Convert X into a no-op move. */
6349 SUBST (SET_DEST (x), pc_rtx);
6350 SUBST (SET_SRC (x), pc_rtx);
6351 return x;
6352 }
6353
6354 /* Simplify our comparison, if possible. */
6355 new_code = simplify_comparison (new_code, &op0, &op1);
6356
6357 #ifdef SELECT_CC_MODE
6358 /* If this machine has CC modes other than CCmode, check to see if we
6359 need to use a different CC mode here. */
6360 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6361 compare_mode = GET_MODE (op0);
6362 else if (inner_compare
6363 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6364 && new_code == old_code
6365 && op0 == XEXP (inner_compare, 0)
6366 && op1 == XEXP (inner_compare, 1))
6367 compare_mode = GET_MODE (inner_compare);
6368 else
6369 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6370
6371 #ifndef HAVE_cc0
6372 /* If the mode changed, we have to change SET_DEST, the mode in the
6373 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6374 a hard register, just build new versions with the proper mode. If it
6375 is a pseudo, we lose unless it is only time we set the pseudo, in
6376 which case we can safely change its mode. */
6377 if (compare_mode != GET_MODE (dest))
6378 {
6379 if (can_change_dest_mode (dest, 0, compare_mode))
6380 {
6381 unsigned int regno = REGNO (dest);
6382 rtx new_dest;
6383
6384 if (regno < FIRST_PSEUDO_REGISTER)
6385 new_dest = gen_rtx_REG (compare_mode, regno);
6386 else
6387 {
6388 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6389 new_dest = regno_reg_rtx[regno];
6390 }
6391
6392 SUBST (SET_DEST (x), new_dest);
6393 SUBST (XEXP (*cc_use, 0), new_dest);
6394 other_changed = 1;
6395
6396 dest = new_dest;
6397 }
6398 }
6399 #endif /* cc0 */
6400 #endif /* SELECT_CC_MODE */
6401
6402 /* If the code changed, we have to build a new comparison in
6403 undobuf.other_insn. */
6404 if (new_code != old_code)
6405 {
6406 int other_changed_previously = other_changed;
6407 unsigned HOST_WIDE_INT mask;
6408 rtx old_cc_use = *cc_use;
6409
6410 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6411 dest, const0_rtx));
6412 other_changed = 1;
6413
6414 /* If the only change we made was to change an EQ into an NE or
6415 vice versa, OP0 has only one bit that might be nonzero, and OP1
6416 is zero, check if changing the user of the condition code will
6417 produce a valid insn. If it won't, we can keep the original code
6418 in that insn by surrounding our operation with an XOR. */
6419
6420 if (((old_code == NE && new_code == EQ)
6421 || (old_code == EQ && new_code == NE))
6422 && ! other_changed_previously && op1 == const0_rtx
6423 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6424 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6425 {
6426 rtx pat = PATTERN (other_insn), note = 0;
6427
6428 if ((recog_for_combine (&pat, other_insn, &note) < 0
6429 && ! check_asm_operands (pat)))
6430 {
6431 *cc_use = old_cc_use;
6432 other_changed = 0;
6433
6434 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6435 gen_int_mode (mask,
6436 GET_MODE (op0)));
6437 }
6438 }
6439 }
6440
6441 if (other_changed)
6442 undobuf.other_insn = other_insn;
6443
6444 /* Otherwise, if we didn't previously have a COMPARE in the
6445 correct mode, we need one. */
6446 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6447 {
6448 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6449 src = SET_SRC (x);
6450 }
6451 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6452 {
6453 SUBST (SET_SRC (x), op0);
6454 src = SET_SRC (x);
6455 }
6456 /* Otherwise, update the COMPARE if needed. */
6457 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6458 {
6459 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6460 src = SET_SRC (x);
6461 }
6462 }
6463 else
6464 {
6465 /* Get SET_SRC in a form where we have placed back any
6466 compound expressions. Then do the checks below. */
6467 src = make_compound_operation (src, SET);
6468 SUBST (SET_SRC (x), src);
6469 }
6470
6471 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6472 and X being a REG or (subreg (reg)), we may be able to convert this to
6473 (set (subreg:m2 x) (op)).
6474
6475 We can always do this if M1 is narrower than M2 because that means that
6476 we only care about the low bits of the result.
6477
6478 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6479 perform a narrower operation than requested since the high-order bits will
6480 be undefined. On machine where it is defined, this transformation is safe
6481 as long as M1 and M2 have the same number of words. */
6482
6483 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6484 && !OBJECT_P (SUBREG_REG (src))
6485 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6486 / UNITS_PER_WORD)
6487 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6488 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6489 #ifndef WORD_REGISTER_OPERATIONS
6490 && (GET_MODE_SIZE (GET_MODE (src))
6491 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6492 #endif
6493 #ifdef CANNOT_CHANGE_MODE_CLASS
6494 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6495 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6496 GET_MODE (SUBREG_REG (src)),
6497 GET_MODE (src)))
6498 #endif
6499 && (REG_P (dest)
6500 || (GET_CODE (dest) == SUBREG
6501 && REG_P (SUBREG_REG (dest)))))
6502 {
6503 SUBST (SET_DEST (x),
6504 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6505 dest));
6506 SUBST (SET_SRC (x), SUBREG_REG (src));
6507
6508 src = SET_SRC (x), dest = SET_DEST (x);
6509 }
6510
6511 #ifdef HAVE_cc0
6512 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6513 in SRC. */
6514 if (dest == cc0_rtx
6515 && GET_CODE (src) == SUBREG
6516 && subreg_lowpart_p (src)
6517 && (GET_MODE_PRECISION (GET_MODE (src))
6518 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6519 {
6520 rtx inner = SUBREG_REG (src);
6521 enum machine_mode inner_mode = GET_MODE (inner);
6522
6523 /* Here we make sure that we don't have a sign bit on. */
6524 if (val_signbit_known_clear_p (GET_MODE (src),
6525 nonzero_bits (inner, inner_mode)))
6526 {
6527 SUBST (SET_SRC (x), inner);
6528 src = SET_SRC (x);
6529 }
6530 }
6531 #endif
6532
6533 #ifdef LOAD_EXTEND_OP
6534 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6535 would require a paradoxical subreg. Replace the subreg with a
6536 zero_extend to avoid the reload that would otherwise be required. */
6537
6538 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6539 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6540 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6541 && SUBREG_BYTE (src) == 0
6542 && paradoxical_subreg_p (src)
6543 && MEM_P (SUBREG_REG (src)))
6544 {
6545 SUBST (SET_SRC (x),
6546 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6547 GET_MODE (src), SUBREG_REG (src)));
6548
6549 src = SET_SRC (x);
6550 }
6551 #endif
6552
6553 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6554 are comparing an item known to be 0 or -1 against 0, use a logical
6555 operation instead. Check for one of the arms being an IOR of the other
6556 arm with some value. We compute three terms to be IOR'ed together. In
6557 practice, at most two will be nonzero. Then we do the IOR's. */
6558
6559 if (GET_CODE (dest) != PC
6560 && GET_CODE (src) == IF_THEN_ELSE
6561 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6562 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6563 && XEXP (XEXP (src, 0), 1) == const0_rtx
6564 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6565 #ifdef HAVE_conditional_move
6566 && ! can_conditionally_move_p (GET_MODE (src))
6567 #endif
6568 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6569 GET_MODE (XEXP (XEXP (src, 0), 0)))
6570 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6571 && ! side_effects_p (src))
6572 {
6573 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6574 ? XEXP (src, 1) : XEXP (src, 2));
6575 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6576 ? XEXP (src, 2) : XEXP (src, 1));
6577 rtx term1 = const0_rtx, term2, term3;
6578
6579 if (GET_CODE (true_rtx) == IOR
6580 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6581 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6582 else if (GET_CODE (true_rtx) == IOR
6583 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6584 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6585 else if (GET_CODE (false_rtx) == IOR
6586 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6587 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6588 else if (GET_CODE (false_rtx) == IOR
6589 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6590 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6591
6592 term2 = simplify_gen_binary (AND, GET_MODE (src),
6593 XEXP (XEXP (src, 0), 0), true_rtx);
6594 term3 = simplify_gen_binary (AND, GET_MODE (src),
6595 simplify_gen_unary (NOT, GET_MODE (src),
6596 XEXP (XEXP (src, 0), 0),
6597 GET_MODE (src)),
6598 false_rtx);
6599
6600 SUBST (SET_SRC (x),
6601 simplify_gen_binary (IOR, GET_MODE (src),
6602 simplify_gen_binary (IOR, GET_MODE (src),
6603 term1, term2),
6604 term3));
6605
6606 src = SET_SRC (x);
6607 }
6608
6609 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6610 whole thing fail. */
6611 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6612 return src;
6613 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6614 return dest;
6615 else
6616 /* Convert this into a field assignment operation, if possible. */
6617 return make_field_assignment (x);
6618 }
6619 \f
6620 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6621 result. */
6622
6623 static rtx
6624 simplify_logical (rtx x)
6625 {
6626 enum machine_mode mode = GET_MODE (x);
6627 rtx op0 = XEXP (x, 0);
6628 rtx op1 = XEXP (x, 1);
6629
6630 switch (GET_CODE (x))
6631 {
6632 case AND:
6633 /* We can call simplify_and_const_int only if we don't lose
6634 any (sign) bits when converting INTVAL (op1) to
6635 "unsigned HOST_WIDE_INT". */
6636 if (CONST_INT_P (op1)
6637 && (HWI_COMPUTABLE_MODE_P (mode)
6638 || INTVAL (op1) > 0))
6639 {
6640 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6641 if (GET_CODE (x) != AND)
6642 return x;
6643
6644 op0 = XEXP (x, 0);
6645 op1 = XEXP (x, 1);
6646 }
6647
6648 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6649 apply the distributive law and then the inverse distributive
6650 law to see if things simplify. */
6651 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6652 {
6653 rtx result = distribute_and_simplify_rtx (x, 0);
6654 if (result)
6655 return result;
6656 }
6657 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6658 {
6659 rtx result = distribute_and_simplify_rtx (x, 1);
6660 if (result)
6661 return result;
6662 }
6663 break;
6664
6665 case IOR:
6666 /* If we have (ior (and A B) C), apply the distributive law and then
6667 the inverse distributive law to see if things simplify. */
6668
6669 if (GET_CODE (op0) == AND)
6670 {
6671 rtx result = distribute_and_simplify_rtx (x, 0);
6672 if (result)
6673 return result;
6674 }
6675
6676 if (GET_CODE (op1) == AND)
6677 {
6678 rtx result = distribute_and_simplify_rtx (x, 1);
6679 if (result)
6680 return result;
6681 }
6682 break;
6683
6684 default:
6685 gcc_unreachable ();
6686 }
6687
6688 return x;
6689 }
6690 \f
6691 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6692 operations" because they can be replaced with two more basic operations.
6693 ZERO_EXTEND is also considered "compound" because it can be replaced with
6694 an AND operation, which is simpler, though only one operation.
6695
6696 The function expand_compound_operation is called with an rtx expression
6697 and will convert it to the appropriate shifts and AND operations,
6698 simplifying at each stage.
6699
6700 The function make_compound_operation is called to convert an expression
6701 consisting of shifts and ANDs into the equivalent compound expression.
6702 It is the inverse of this function, loosely speaking. */
6703
6704 static rtx
6705 expand_compound_operation (rtx x)
6706 {
6707 unsigned HOST_WIDE_INT pos = 0, len;
6708 int unsignedp = 0;
6709 unsigned int modewidth;
6710 rtx tem;
6711
6712 switch (GET_CODE (x))
6713 {
6714 case ZERO_EXTEND:
6715 unsignedp = 1;
6716 case SIGN_EXTEND:
6717 /* We can't necessarily use a const_int for a multiword mode;
6718 it depends on implicitly extending the value.
6719 Since we don't know the right way to extend it,
6720 we can't tell whether the implicit way is right.
6721
6722 Even for a mode that is no wider than a const_int,
6723 we can't win, because we need to sign extend one of its bits through
6724 the rest of it, and we don't know which bit. */
6725 if (CONST_INT_P (XEXP (x, 0)))
6726 return x;
6727
6728 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6729 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6730 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6731 reloaded. If not for that, MEM's would very rarely be safe.
6732
6733 Reject MODEs bigger than a word, because we might not be able
6734 to reference a two-register group starting with an arbitrary register
6735 (and currently gen_lowpart might crash for a SUBREG). */
6736
6737 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6738 return x;
6739
6740 /* Reject MODEs that aren't scalar integers because turning vector
6741 or complex modes into shifts causes problems. */
6742
6743 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6744 return x;
6745
6746 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6747 /* If the inner object has VOIDmode (the only way this can happen
6748 is if it is an ASM_OPERANDS), we can't do anything since we don't
6749 know how much masking to do. */
6750 if (len == 0)
6751 return x;
6752
6753 break;
6754
6755 case ZERO_EXTRACT:
6756 unsignedp = 1;
6757
6758 /* ... fall through ... */
6759
6760 case SIGN_EXTRACT:
6761 /* If the operand is a CLOBBER, just return it. */
6762 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6763 return XEXP (x, 0);
6764
6765 if (!CONST_INT_P (XEXP (x, 1))
6766 || !CONST_INT_P (XEXP (x, 2))
6767 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6768 return x;
6769
6770 /* Reject MODEs that aren't scalar integers because turning vector
6771 or complex modes into shifts causes problems. */
6772
6773 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6774 return x;
6775
6776 len = INTVAL (XEXP (x, 1));
6777 pos = INTVAL (XEXP (x, 2));
6778
6779 /* This should stay within the object being extracted, fail otherwise. */
6780 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6781 return x;
6782
6783 if (BITS_BIG_ENDIAN)
6784 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6785
6786 break;
6787
6788 default:
6789 return x;
6790 }
6791 /* Convert sign extension to zero extension, if we know that the high
6792 bit is not set, as this is easier to optimize. It will be converted
6793 back to cheaper alternative in make_extraction. */
6794 if (GET_CODE (x) == SIGN_EXTEND
6795 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6796 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6797 & ~(((unsigned HOST_WIDE_INT)
6798 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6799 >> 1))
6800 == 0)))
6801 {
6802 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6803 rtx temp2 = expand_compound_operation (temp);
6804
6805 /* Make sure this is a profitable operation. */
6806 if (set_src_cost (x, optimize_this_for_speed_p)
6807 > set_src_cost (temp2, optimize_this_for_speed_p))
6808 return temp2;
6809 else if (set_src_cost (x, optimize_this_for_speed_p)
6810 > set_src_cost (temp, optimize_this_for_speed_p))
6811 return temp;
6812 else
6813 return x;
6814 }
6815
6816 /* We can optimize some special cases of ZERO_EXTEND. */
6817 if (GET_CODE (x) == ZERO_EXTEND)
6818 {
6819 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6820 know that the last value didn't have any inappropriate bits
6821 set. */
6822 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6823 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6824 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6825 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6826 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6827 return XEXP (XEXP (x, 0), 0);
6828
6829 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6830 if (GET_CODE (XEXP (x, 0)) == SUBREG
6831 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6832 && subreg_lowpart_p (XEXP (x, 0))
6833 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6834 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6835 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6836 return SUBREG_REG (XEXP (x, 0));
6837
6838 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6839 is a comparison and STORE_FLAG_VALUE permits. This is like
6840 the first case, but it works even when GET_MODE (x) is larger
6841 than HOST_WIDE_INT. */
6842 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6843 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6844 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6845 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6846 <= HOST_BITS_PER_WIDE_INT)
6847 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6848 return XEXP (XEXP (x, 0), 0);
6849
6850 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6851 if (GET_CODE (XEXP (x, 0)) == SUBREG
6852 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6853 && subreg_lowpart_p (XEXP (x, 0))
6854 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6855 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6856 <= HOST_BITS_PER_WIDE_INT)
6857 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6858 return SUBREG_REG (XEXP (x, 0));
6859
6860 }
6861
6862 /* If we reach here, we want to return a pair of shifts. The inner
6863 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6864 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6865 logical depending on the value of UNSIGNEDP.
6866
6867 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6868 converted into an AND of a shift.
6869
6870 We must check for the case where the left shift would have a negative
6871 count. This can happen in a case like (x >> 31) & 255 on machines
6872 that can't shift by a constant. On those machines, we would first
6873 combine the shift with the AND to produce a variable-position
6874 extraction. Then the constant of 31 would be substituted in
6875 to produce such a position. */
6876
6877 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6878 if (modewidth >= pos + len)
6879 {
6880 enum machine_mode mode = GET_MODE (x);
6881 tem = gen_lowpart (mode, XEXP (x, 0));
6882 if (!tem || GET_CODE (tem) == CLOBBER)
6883 return x;
6884 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6885 tem, modewidth - pos - len);
6886 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6887 mode, tem, modewidth - len);
6888 }
6889 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6890 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6891 simplify_shift_const (NULL_RTX, LSHIFTRT,
6892 GET_MODE (x),
6893 XEXP (x, 0), pos),
6894 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6895 else
6896 /* Any other cases we can't handle. */
6897 return x;
6898
6899 /* If we couldn't do this for some reason, return the original
6900 expression. */
6901 if (GET_CODE (tem) == CLOBBER)
6902 return x;
6903
6904 return tem;
6905 }
6906 \f
6907 /* X is a SET which contains an assignment of one object into
6908 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6909 or certain SUBREGS). If possible, convert it into a series of
6910 logical operations.
6911
6912 We half-heartedly support variable positions, but do not at all
6913 support variable lengths. */
6914
6915 static const_rtx
6916 expand_field_assignment (const_rtx x)
6917 {
6918 rtx inner;
6919 rtx pos; /* Always counts from low bit. */
6920 int len;
6921 rtx mask, cleared, masked;
6922 enum machine_mode compute_mode;
6923
6924 /* Loop until we find something we can't simplify. */
6925 while (1)
6926 {
6927 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6928 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6929 {
6930 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6931 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
6932 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6933 }
6934 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6935 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6936 {
6937 inner = XEXP (SET_DEST (x), 0);
6938 len = INTVAL (XEXP (SET_DEST (x), 1));
6939 pos = XEXP (SET_DEST (x), 2);
6940
6941 /* A constant position should stay within the width of INNER. */
6942 if (CONST_INT_P (pos)
6943 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
6944 break;
6945
6946 if (BITS_BIG_ENDIAN)
6947 {
6948 if (CONST_INT_P (pos))
6949 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
6950 - INTVAL (pos));
6951 else if (GET_CODE (pos) == MINUS
6952 && CONST_INT_P (XEXP (pos, 1))
6953 && (INTVAL (XEXP (pos, 1))
6954 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
6955 /* If position is ADJUST - X, new position is X. */
6956 pos = XEXP (pos, 0);
6957 else
6958 {
6959 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
6960 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6961 gen_int_mode (prec - len,
6962 GET_MODE (pos)),
6963 pos);
6964 }
6965 }
6966 }
6967
6968 /* A SUBREG between two modes that occupy the same numbers of words
6969 can be done by moving the SUBREG to the source. */
6970 else if (GET_CODE (SET_DEST (x)) == SUBREG
6971 /* We need SUBREGs to compute nonzero_bits properly. */
6972 && nonzero_sign_valid
6973 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6974 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6975 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6976 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6977 {
6978 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6979 gen_lowpart
6980 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6981 SET_SRC (x)));
6982 continue;
6983 }
6984 else
6985 break;
6986
6987 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6988 inner = SUBREG_REG (inner);
6989
6990 compute_mode = GET_MODE (inner);
6991
6992 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6993 if (! SCALAR_INT_MODE_P (compute_mode))
6994 {
6995 enum machine_mode imode;
6996
6997 /* Don't do anything for vector or complex integral types. */
6998 if (! FLOAT_MODE_P (compute_mode))
6999 break;
7000
7001 /* Try to find an integral mode to pun with. */
7002 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7003 if (imode == BLKmode)
7004 break;
7005
7006 compute_mode = imode;
7007 inner = gen_lowpart (imode, inner);
7008 }
7009
7010 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7011 if (len >= HOST_BITS_PER_WIDE_INT)
7012 break;
7013
7014 /* Now compute the equivalent expression. Make a copy of INNER
7015 for the SET_DEST in case it is a MEM into which we will substitute;
7016 we don't want shared RTL in that case. */
7017 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7018 compute_mode);
7019 cleared = simplify_gen_binary (AND, compute_mode,
7020 simplify_gen_unary (NOT, compute_mode,
7021 simplify_gen_binary (ASHIFT,
7022 compute_mode,
7023 mask, pos),
7024 compute_mode),
7025 inner);
7026 masked = simplify_gen_binary (ASHIFT, compute_mode,
7027 simplify_gen_binary (
7028 AND, compute_mode,
7029 gen_lowpart (compute_mode, SET_SRC (x)),
7030 mask),
7031 pos);
7032
7033 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7034 simplify_gen_binary (IOR, compute_mode,
7035 cleared, masked));
7036 }
7037
7038 return x;
7039 }
7040 \f
7041 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7042 it is an RTX that represents the (variable) starting position; otherwise,
7043 POS is the (constant) starting bit position. Both are counted from the LSB.
7044
7045 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7046
7047 IN_DEST is nonzero if this is a reference in the destination of a SET.
7048 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7049 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7050 be used.
7051
7052 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7053 ZERO_EXTRACT should be built even for bits starting at bit 0.
7054
7055 MODE is the desired mode of the result (if IN_DEST == 0).
7056
7057 The result is an RTX for the extraction or NULL_RTX if the target
7058 can't handle it. */
7059
7060 static rtx
7061 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7062 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7063 int in_dest, int in_compare)
7064 {
7065 /* This mode describes the size of the storage area
7066 to fetch the overall value from. Within that, we
7067 ignore the POS lowest bits, etc. */
7068 enum machine_mode is_mode = GET_MODE (inner);
7069 enum machine_mode inner_mode;
7070 enum machine_mode wanted_inner_mode;
7071 enum machine_mode wanted_inner_reg_mode = word_mode;
7072 enum machine_mode pos_mode = word_mode;
7073 enum machine_mode extraction_mode = word_mode;
7074 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7075 rtx new_rtx = 0;
7076 rtx orig_pos_rtx = pos_rtx;
7077 HOST_WIDE_INT orig_pos;
7078
7079 if (pos_rtx && CONST_INT_P (pos_rtx))
7080 pos = INTVAL (pos_rtx), pos_rtx = 0;
7081
7082 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7083 {
7084 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7085 consider just the QI as the memory to extract from.
7086 The subreg adds or removes high bits; its mode is
7087 irrelevant to the meaning of this extraction,
7088 since POS and LEN count from the lsb. */
7089 if (MEM_P (SUBREG_REG (inner)))
7090 is_mode = GET_MODE (SUBREG_REG (inner));
7091 inner = SUBREG_REG (inner);
7092 }
7093 else if (GET_CODE (inner) == ASHIFT
7094 && CONST_INT_P (XEXP (inner, 1))
7095 && pos_rtx == 0 && pos == 0
7096 && len > UINTVAL (XEXP (inner, 1)))
7097 {
7098 /* We're extracting the least significant bits of an rtx
7099 (ashift X (const_int C)), where LEN > C. Extract the
7100 least significant (LEN - C) bits of X, giving an rtx
7101 whose mode is MODE, then shift it left C times. */
7102 new_rtx = make_extraction (mode, XEXP (inner, 0),
7103 0, 0, len - INTVAL (XEXP (inner, 1)),
7104 unsignedp, in_dest, in_compare);
7105 if (new_rtx != 0)
7106 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7107 }
7108 else if (GET_CODE (inner) == TRUNCATE)
7109 inner = XEXP (inner, 0);
7110
7111 inner_mode = GET_MODE (inner);
7112
7113 /* See if this can be done without an extraction. We never can if the
7114 width of the field is not the same as that of some integer mode. For
7115 registers, we can only avoid the extraction if the position is at the
7116 low-order bit and this is either not in the destination or we have the
7117 appropriate STRICT_LOW_PART operation available.
7118
7119 For MEM, we can avoid an extract if the field starts on an appropriate
7120 boundary and we can change the mode of the memory reference. */
7121
7122 if (tmode != BLKmode
7123 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7124 && !MEM_P (inner)
7125 && (inner_mode == tmode
7126 || !REG_P (inner)
7127 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7128 || reg_truncated_to_mode (tmode, inner))
7129 && (! in_dest
7130 || (REG_P (inner)
7131 && have_insn_for (STRICT_LOW_PART, tmode))))
7132 || (MEM_P (inner) && pos_rtx == 0
7133 && (pos
7134 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7135 : BITS_PER_UNIT)) == 0
7136 /* We can't do this if we are widening INNER_MODE (it
7137 may not be aligned, for one thing). */
7138 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7139 && (inner_mode == tmode
7140 || (! mode_dependent_address_p (XEXP (inner, 0),
7141 MEM_ADDR_SPACE (inner))
7142 && ! MEM_VOLATILE_P (inner))))))
7143 {
7144 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7145 field. If the original and current mode are the same, we need not
7146 adjust the offset. Otherwise, we do if bytes big endian.
7147
7148 If INNER is not a MEM, get a piece consisting of just the field
7149 of interest (in this case POS % BITS_PER_WORD must be 0). */
7150
7151 if (MEM_P (inner))
7152 {
7153 HOST_WIDE_INT offset;
7154
7155 /* POS counts from lsb, but make OFFSET count in memory order. */
7156 if (BYTES_BIG_ENDIAN)
7157 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7158 else
7159 offset = pos / BITS_PER_UNIT;
7160
7161 new_rtx = adjust_address_nv (inner, tmode, offset);
7162 }
7163 else if (REG_P (inner))
7164 {
7165 if (tmode != inner_mode)
7166 {
7167 /* We can't call gen_lowpart in a DEST since we
7168 always want a SUBREG (see below) and it would sometimes
7169 return a new hard register. */
7170 if (pos || in_dest)
7171 {
7172 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7173
7174 if (WORDS_BIG_ENDIAN
7175 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7176 final_word = ((GET_MODE_SIZE (inner_mode)
7177 - GET_MODE_SIZE (tmode))
7178 / UNITS_PER_WORD) - final_word;
7179
7180 final_word *= UNITS_PER_WORD;
7181 if (BYTES_BIG_ENDIAN &&
7182 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7183 final_word += (GET_MODE_SIZE (inner_mode)
7184 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7185
7186 /* Avoid creating invalid subregs, for example when
7187 simplifying (x>>32)&255. */
7188 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7189 return NULL_RTX;
7190
7191 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7192 }
7193 else
7194 new_rtx = gen_lowpart (tmode, inner);
7195 }
7196 else
7197 new_rtx = inner;
7198 }
7199 else
7200 new_rtx = force_to_mode (inner, tmode,
7201 len >= HOST_BITS_PER_WIDE_INT
7202 ? ~(unsigned HOST_WIDE_INT) 0
7203 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7204 0);
7205
7206 /* If this extraction is going into the destination of a SET,
7207 make a STRICT_LOW_PART unless we made a MEM. */
7208
7209 if (in_dest)
7210 return (MEM_P (new_rtx) ? new_rtx
7211 : (GET_CODE (new_rtx) != SUBREG
7212 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7213 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7214
7215 if (mode == tmode)
7216 return new_rtx;
7217
7218 if (CONST_SCALAR_INT_P (new_rtx))
7219 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7220 mode, new_rtx, tmode);
7221
7222 /* If we know that no extraneous bits are set, and that the high
7223 bit is not set, convert the extraction to the cheaper of
7224 sign and zero extension, that are equivalent in these cases. */
7225 if (flag_expensive_optimizations
7226 && (HWI_COMPUTABLE_MODE_P (tmode)
7227 && ((nonzero_bits (new_rtx, tmode)
7228 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7229 == 0)))
7230 {
7231 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7232 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7233
7234 /* Prefer ZERO_EXTENSION, since it gives more information to
7235 backends. */
7236 if (set_src_cost (temp, optimize_this_for_speed_p)
7237 <= set_src_cost (temp1, optimize_this_for_speed_p))
7238 return temp;
7239 return temp1;
7240 }
7241
7242 /* Otherwise, sign- or zero-extend unless we already are in the
7243 proper mode. */
7244
7245 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7246 mode, new_rtx));
7247 }
7248
7249 /* Unless this is a COMPARE or we have a funny memory reference,
7250 don't do anything with zero-extending field extracts starting at
7251 the low-order bit since they are simple AND operations. */
7252 if (pos_rtx == 0 && pos == 0 && ! in_dest
7253 && ! in_compare && unsignedp)
7254 return 0;
7255
7256 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7257 if the position is not a constant and the length is not 1. In all
7258 other cases, we would only be going outside our object in cases when
7259 an original shift would have been undefined. */
7260 if (MEM_P (inner)
7261 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7262 || (pos_rtx != 0 && len != 1)))
7263 return 0;
7264
7265 enum extraction_pattern pattern = (in_dest ? EP_insv
7266 : unsignedp ? EP_extzv : EP_extv);
7267
7268 /* If INNER is not from memory, we want it to have the mode of a register
7269 extraction pattern's structure operand, or word_mode if there is no
7270 such pattern. The same applies to extraction_mode and pos_mode
7271 and their respective operands.
7272
7273 For memory, assume that the desired extraction_mode and pos_mode
7274 are the same as for a register operation, since at present we don't
7275 have named patterns for aligned memory structures. */
7276 struct extraction_insn insn;
7277 if (get_best_reg_extraction_insn (&insn, pattern,
7278 GET_MODE_BITSIZE (inner_mode), mode))
7279 {
7280 wanted_inner_reg_mode = insn.struct_mode;
7281 pos_mode = insn.pos_mode;
7282 extraction_mode = insn.field_mode;
7283 }
7284
7285 /* Never narrow an object, since that might not be safe. */
7286
7287 if (mode != VOIDmode
7288 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7289 extraction_mode = mode;
7290
7291 if (!MEM_P (inner))
7292 wanted_inner_mode = wanted_inner_reg_mode;
7293 else
7294 {
7295 /* Be careful not to go beyond the extracted object and maintain the
7296 natural alignment of the memory. */
7297 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7298 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7299 > GET_MODE_BITSIZE (wanted_inner_mode))
7300 {
7301 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7302 gcc_assert (wanted_inner_mode != VOIDmode);
7303 }
7304 }
7305
7306 orig_pos = pos;
7307
7308 if (BITS_BIG_ENDIAN)
7309 {
7310 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7311 BITS_BIG_ENDIAN style. If position is constant, compute new
7312 position. Otherwise, build subtraction.
7313 Note that POS is relative to the mode of the original argument.
7314 If it's a MEM we need to recompute POS relative to that.
7315 However, if we're extracting from (or inserting into) a register,
7316 we want to recompute POS relative to wanted_inner_mode. */
7317 int width = (MEM_P (inner)
7318 ? GET_MODE_BITSIZE (is_mode)
7319 : GET_MODE_BITSIZE (wanted_inner_mode));
7320
7321 if (pos_rtx == 0)
7322 pos = width - len - pos;
7323 else
7324 pos_rtx
7325 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7326 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7327 pos_rtx);
7328 /* POS may be less than 0 now, but we check for that below.
7329 Note that it can only be less than 0 if !MEM_P (inner). */
7330 }
7331
7332 /* If INNER has a wider mode, and this is a constant extraction, try to
7333 make it smaller and adjust the byte to point to the byte containing
7334 the value. */
7335 if (wanted_inner_mode != VOIDmode
7336 && inner_mode != wanted_inner_mode
7337 && ! pos_rtx
7338 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7339 && MEM_P (inner)
7340 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7341 && ! MEM_VOLATILE_P (inner))
7342 {
7343 int offset = 0;
7344
7345 /* The computations below will be correct if the machine is big
7346 endian in both bits and bytes or little endian in bits and bytes.
7347 If it is mixed, we must adjust. */
7348
7349 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7350 adjust OFFSET to compensate. */
7351 if (BYTES_BIG_ENDIAN
7352 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7353 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7354
7355 /* We can now move to the desired byte. */
7356 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7357 * GET_MODE_SIZE (wanted_inner_mode);
7358 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7359
7360 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7361 && is_mode != wanted_inner_mode)
7362 offset = (GET_MODE_SIZE (is_mode)
7363 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7364
7365 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7366 }
7367
7368 /* If INNER is not memory, get it into the proper mode. If we are changing
7369 its mode, POS must be a constant and smaller than the size of the new
7370 mode. */
7371 else if (!MEM_P (inner))
7372 {
7373 /* On the LHS, don't create paradoxical subregs implicitely truncating
7374 the register unless TRULY_NOOP_TRUNCATION. */
7375 if (in_dest
7376 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7377 wanted_inner_mode))
7378 return NULL_RTX;
7379
7380 if (GET_MODE (inner) != wanted_inner_mode
7381 && (pos_rtx != 0
7382 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7383 return NULL_RTX;
7384
7385 if (orig_pos < 0)
7386 return NULL_RTX;
7387
7388 inner = force_to_mode (inner, wanted_inner_mode,
7389 pos_rtx
7390 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7391 ? ~(unsigned HOST_WIDE_INT) 0
7392 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7393 << orig_pos),
7394 0);
7395 }
7396
7397 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7398 have to zero extend. Otherwise, we can just use a SUBREG. */
7399 if (pos_rtx != 0
7400 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7401 {
7402 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7403 GET_MODE (pos_rtx));
7404
7405 /* If we know that no extraneous bits are set, and that the high
7406 bit is not set, convert extraction to cheaper one - either
7407 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7408 cases. */
7409 if (flag_expensive_optimizations
7410 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7411 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7412 & ~(((unsigned HOST_WIDE_INT)
7413 GET_MODE_MASK (GET_MODE (pos_rtx)))
7414 >> 1))
7415 == 0)))
7416 {
7417 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7418 GET_MODE (pos_rtx));
7419
7420 /* Prefer ZERO_EXTENSION, since it gives more information to
7421 backends. */
7422 if (set_src_cost (temp1, optimize_this_for_speed_p)
7423 < set_src_cost (temp, optimize_this_for_speed_p))
7424 temp = temp1;
7425 }
7426 pos_rtx = temp;
7427 }
7428
7429 /* Make POS_RTX unless we already have it and it is correct. If we don't
7430 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7431 be a CONST_INT. */
7432 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7433 pos_rtx = orig_pos_rtx;
7434
7435 else if (pos_rtx == 0)
7436 pos_rtx = GEN_INT (pos);
7437
7438 /* Make the required operation. See if we can use existing rtx. */
7439 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7440 extraction_mode, inner, GEN_INT (len), pos_rtx);
7441 if (! in_dest)
7442 new_rtx = gen_lowpart (mode, new_rtx);
7443
7444 return new_rtx;
7445 }
7446 \f
7447 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7448 with any other operations in X. Return X without that shift if so. */
7449
7450 static rtx
7451 extract_left_shift (rtx x, int count)
7452 {
7453 enum rtx_code code = GET_CODE (x);
7454 enum machine_mode mode = GET_MODE (x);
7455 rtx tem;
7456
7457 switch (code)
7458 {
7459 case ASHIFT:
7460 /* This is the shift itself. If it is wide enough, we will return
7461 either the value being shifted if the shift count is equal to
7462 COUNT or a shift for the difference. */
7463 if (CONST_INT_P (XEXP (x, 1))
7464 && INTVAL (XEXP (x, 1)) >= count)
7465 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7466 INTVAL (XEXP (x, 1)) - count);
7467 break;
7468
7469 case NEG: case NOT:
7470 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7471 return simplify_gen_unary (code, mode, tem, mode);
7472
7473 break;
7474
7475 case PLUS: case IOR: case XOR: case AND:
7476 /* If we can safely shift this constant and we find the inner shift,
7477 make a new operation. */
7478 if (CONST_INT_P (XEXP (x, 1))
7479 && (UINTVAL (XEXP (x, 1))
7480 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7481 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7482 {
7483 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7484 return simplify_gen_binary (code, mode, tem,
7485 gen_int_mode (val, mode));
7486 }
7487 break;
7488
7489 default:
7490 break;
7491 }
7492
7493 return 0;
7494 }
7495 \f
7496 /* Look at the expression rooted at X. Look for expressions
7497 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7498 Form these expressions.
7499
7500 Return the new rtx, usually just X.
7501
7502 Also, for machines like the VAX that don't have logical shift insns,
7503 try to convert logical to arithmetic shift operations in cases where
7504 they are equivalent. This undoes the canonicalizations to logical
7505 shifts done elsewhere.
7506
7507 We try, as much as possible, to re-use rtl expressions to save memory.
7508
7509 IN_CODE says what kind of expression we are processing. Normally, it is
7510 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7511 being kludges), it is MEM. When processing the arguments of a comparison
7512 or a COMPARE against zero, it is COMPARE. */
7513
7514 rtx
7515 make_compound_operation (rtx x, enum rtx_code in_code)
7516 {
7517 enum rtx_code code = GET_CODE (x);
7518 enum machine_mode mode = GET_MODE (x);
7519 int mode_width = GET_MODE_PRECISION (mode);
7520 rtx rhs, lhs;
7521 enum rtx_code next_code;
7522 int i, j;
7523 rtx new_rtx = 0;
7524 rtx tem;
7525 const char *fmt;
7526
7527 /* Select the code to be used in recursive calls. Once we are inside an
7528 address, we stay there. If we have a comparison, set to COMPARE,
7529 but once inside, go back to our default of SET. */
7530
7531 next_code = (code == MEM ? MEM
7532 : ((code == PLUS || code == MINUS)
7533 && SCALAR_INT_MODE_P (mode)) ? MEM
7534 : ((code == COMPARE || COMPARISON_P (x))
7535 && XEXP (x, 1) == const0_rtx) ? COMPARE
7536 : in_code == COMPARE ? SET : in_code);
7537
7538 /* Process depending on the code of this operation. If NEW is set
7539 nonzero, it will be returned. */
7540
7541 switch (code)
7542 {
7543 case ASHIFT:
7544 /* Convert shifts by constants into multiplications if inside
7545 an address. */
7546 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7547 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7548 && INTVAL (XEXP (x, 1)) >= 0
7549 && SCALAR_INT_MODE_P (mode))
7550 {
7551 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7552 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7553
7554 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7555 if (GET_CODE (new_rtx) == NEG)
7556 {
7557 new_rtx = XEXP (new_rtx, 0);
7558 multval = -multval;
7559 }
7560 multval = trunc_int_for_mode (multval, mode);
7561 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7562 }
7563 break;
7564
7565 case PLUS:
7566 lhs = XEXP (x, 0);
7567 rhs = XEXP (x, 1);
7568 lhs = make_compound_operation (lhs, next_code);
7569 rhs = make_compound_operation (rhs, next_code);
7570 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7571 && SCALAR_INT_MODE_P (mode))
7572 {
7573 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7574 XEXP (lhs, 1));
7575 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7576 }
7577 else if (GET_CODE (lhs) == MULT
7578 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7579 {
7580 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7581 simplify_gen_unary (NEG, mode,
7582 XEXP (lhs, 1),
7583 mode));
7584 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7585 }
7586 else
7587 {
7588 SUBST (XEXP (x, 0), lhs);
7589 SUBST (XEXP (x, 1), rhs);
7590 goto maybe_swap;
7591 }
7592 x = gen_lowpart (mode, new_rtx);
7593 goto maybe_swap;
7594
7595 case MINUS:
7596 lhs = XEXP (x, 0);
7597 rhs = XEXP (x, 1);
7598 lhs = make_compound_operation (lhs, next_code);
7599 rhs = make_compound_operation (rhs, next_code);
7600 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7601 && SCALAR_INT_MODE_P (mode))
7602 {
7603 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7604 XEXP (rhs, 1));
7605 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7606 }
7607 else if (GET_CODE (rhs) == MULT
7608 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7609 {
7610 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7611 simplify_gen_unary (NEG, mode,
7612 XEXP (rhs, 1),
7613 mode));
7614 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7615 }
7616 else
7617 {
7618 SUBST (XEXP (x, 0), lhs);
7619 SUBST (XEXP (x, 1), rhs);
7620 return x;
7621 }
7622 return gen_lowpart (mode, new_rtx);
7623
7624 case AND:
7625 /* If the second operand is not a constant, we can't do anything
7626 with it. */
7627 if (!CONST_INT_P (XEXP (x, 1)))
7628 break;
7629
7630 /* If the constant is a power of two minus one and the first operand
7631 is a logical right shift, make an extraction. */
7632 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7633 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7634 {
7635 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7636 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7637 0, in_code == COMPARE);
7638 }
7639
7640 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7641 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7642 && subreg_lowpart_p (XEXP (x, 0))
7643 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7644 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7645 {
7646 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7647 next_code);
7648 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7649 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7650 0, in_code == COMPARE);
7651 }
7652 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7653 else if ((GET_CODE (XEXP (x, 0)) == XOR
7654 || GET_CODE (XEXP (x, 0)) == IOR)
7655 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7656 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7657 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7658 {
7659 /* Apply the distributive law, and then try to make extractions. */
7660 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7661 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7662 XEXP (x, 1)),
7663 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7664 XEXP (x, 1)));
7665 new_rtx = make_compound_operation (new_rtx, in_code);
7666 }
7667
7668 /* If we are have (and (rotate X C) M) and C is larger than the number
7669 of bits in M, this is an extraction. */
7670
7671 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7672 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7673 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7674 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7675 {
7676 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7677 new_rtx = make_extraction (mode, new_rtx,
7678 (GET_MODE_PRECISION (mode)
7679 - INTVAL (XEXP (XEXP (x, 0), 1))),
7680 NULL_RTX, i, 1, 0, in_code == COMPARE);
7681 }
7682
7683 /* On machines without logical shifts, if the operand of the AND is
7684 a logical shift and our mask turns off all the propagated sign
7685 bits, we can replace the logical shift with an arithmetic shift. */
7686 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7687 && !have_insn_for (LSHIFTRT, mode)
7688 && have_insn_for (ASHIFTRT, mode)
7689 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7690 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7691 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7692 && mode_width <= HOST_BITS_PER_WIDE_INT)
7693 {
7694 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7695
7696 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7697 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7698 SUBST (XEXP (x, 0),
7699 gen_rtx_ASHIFTRT (mode,
7700 make_compound_operation
7701 (XEXP (XEXP (x, 0), 0), next_code),
7702 XEXP (XEXP (x, 0), 1)));
7703 }
7704
7705 /* If the constant is one less than a power of two, this might be
7706 representable by an extraction even if no shift is present.
7707 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7708 we are in a COMPARE. */
7709 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7710 new_rtx = make_extraction (mode,
7711 make_compound_operation (XEXP (x, 0),
7712 next_code),
7713 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7714
7715 /* If we are in a comparison and this is an AND with a power of two,
7716 convert this into the appropriate bit extract. */
7717 else if (in_code == COMPARE
7718 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7719 new_rtx = make_extraction (mode,
7720 make_compound_operation (XEXP (x, 0),
7721 next_code),
7722 i, NULL_RTX, 1, 1, 0, 1);
7723
7724 break;
7725
7726 case LSHIFTRT:
7727 /* If the sign bit is known to be zero, replace this with an
7728 arithmetic shift. */
7729 if (have_insn_for (ASHIFTRT, mode)
7730 && ! have_insn_for (LSHIFTRT, mode)
7731 && mode_width <= HOST_BITS_PER_WIDE_INT
7732 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7733 {
7734 new_rtx = gen_rtx_ASHIFTRT (mode,
7735 make_compound_operation (XEXP (x, 0),
7736 next_code),
7737 XEXP (x, 1));
7738 break;
7739 }
7740
7741 /* ... fall through ... */
7742
7743 case ASHIFTRT:
7744 lhs = XEXP (x, 0);
7745 rhs = XEXP (x, 1);
7746
7747 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7748 this is a SIGN_EXTRACT. */
7749 if (CONST_INT_P (rhs)
7750 && GET_CODE (lhs) == ASHIFT
7751 && CONST_INT_P (XEXP (lhs, 1))
7752 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7753 && INTVAL (XEXP (lhs, 1)) >= 0
7754 && INTVAL (rhs) < mode_width)
7755 {
7756 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7757 new_rtx = make_extraction (mode, new_rtx,
7758 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7759 NULL_RTX, mode_width - INTVAL (rhs),
7760 code == LSHIFTRT, 0, in_code == COMPARE);
7761 break;
7762 }
7763
7764 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7765 If so, try to merge the shifts into a SIGN_EXTEND. We could
7766 also do this for some cases of SIGN_EXTRACT, but it doesn't
7767 seem worth the effort; the case checked for occurs on Alpha. */
7768
7769 if (!OBJECT_P (lhs)
7770 && ! (GET_CODE (lhs) == SUBREG
7771 && (OBJECT_P (SUBREG_REG (lhs))))
7772 && CONST_INT_P (rhs)
7773 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7774 && INTVAL (rhs) < mode_width
7775 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7776 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7777 0, NULL_RTX, mode_width - INTVAL (rhs),
7778 code == LSHIFTRT, 0, in_code == COMPARE);
7779
7780 break;
7781
7782 case SUBREG:
7783 /* Call ourselves recursively on the inner expression. If we are
7784 narrowing the object and it has a different RTL code from
7785 what it originally did, do this SUBREG as a force_to_mode. */
7786 {
7787 rtx inner = SUBREG_REG (x), simplified;
7788 enum rtx_code subreg_code = in_code;
7789
7790 /* If in_code is COMPARE, it isn't always safe to pass it through
7791 to the recursive make_compound_operation call. */
7792 if (subreg_code == COMPARE
7793 && (!subreg_lowpart_p (x)
7794 || GET_CODE (inner) == SUBREG
7795 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7796 is (const_int 0), rather than
7797 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7798 || (GET_CODE (inner) == AND
7799 && CONST_INT_P (XEXP (inner, 1))
7800 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7801 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7802 >= GET_MODE_BITSIZE (mode))))
7803 subreg_code = SET;
7804
7805 tem = make_compound_operation (inner, subreg_code);
7806
7807 simplified
7808 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7809 if (simplified)
7810 tem = simplified;
7811
7812 if (GET_CODE (tem) != GET_CODE (inner)
7813 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7814 && subreg_lowpart_p (x))
7815 {
7816 rtx newer
7817 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7818
7819 /* If we have something other than a SUBREG, we might have
7820 done an expansion, so rerun ourselves. */
7821 if (GET_CODE (newer) != SUBREG)
7822 newer = make_compound_operation (newer, in_code);
7823
7824 /* force_to_mode can expand compounds. If it just re-expanded the
7825 compound, use gen_lowpart to convert to the desired mode. */
7826 if (rtx_equal_p (newer, x)
7827 /* Likewise if it re-expanded the compound only partially.
7828 This happens for SUBREG of ZERO_EXTRACT if they extract
7829 the same number of bits. */
7830 || (GET_CODE (newer) == SUBREG
7831 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7832 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7833 && GET_CODE (inner) == AND
7834 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7835 return gen_lowpart (GET_MODE (x), tem);
7836
7837 return newer;
7838 }
7839
7840 if (simplified)
7841 return tem;
7842 }
7843 break;
7844
7845 default:
7846 break;
7847 }
7848
7849 if (new_rtx)
7850 {
7851 x = gen_lowpart (mode, new_rtx);
7852 code = GET_CODE (x);
7853 }
7854
7855 /* Now recursively process each operand of this operation. We need to
7856 handle ZERO_EXTEND specially so that we don't lose track of the
7857 inner mode. */
7858 if (GET_CODE (x) == ZERO_EXTEND)
7859 {
7860 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7861 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7862 new_rtx, GET_MODE (XEXP (x, 0)));
7863 if (tem)
7864 return tem;
7865 SUBST (XEXP (x, 0), new_rtx);
7866 return x;
7867 }
7868
7869 fmt = GET_RTX_FORMAT (code);
7870 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7871 if (fmt[i] == 'e')
7872 {
7873 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7874 SUBST (XEXP (x, i), new_rtx);
7875 }
7876 else if (fmt[i] == 'E')
7877 for (j = 0; j < XVECLEN (x, i); j++)
7878 {
7879 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7880 SUBST (XVECEXP (x, i, j), new_rtx);
7881 }
7882
7883 maybe_swap:
7884 /* If this is a commutative operation, the changes to the operands
7885 may have made it noncanonical. */
7886 if (COMMUTATIVE_ARITH_P (x)
7887 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7888 {
7889 tem = XEXP (x, 0);
7890 SUBST (XEXP (x, 0), XEXP (x, 1));
7891 SUBST (XEXP (x, 1), tem);
7892 }
7893
7894 return x;
7895 }
7896 \f
7897 /* Given M see if it is a value that would select a field of bits
7898 within an item, but not the entire word. Return -1 if not.
7899 Otherwise, return the starting position of the field, where 0 is the
7900 low-order bit.
7901
7902 *PLEN is set to the length of the field. */
7903
7904 static int
7905 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7906 {
7907 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7908 int pos = m ? ctz_hwi (m) : -1;
7909 int len = 0;
7910
7911 if (pos >= 0)
7912 /* Now shift off the low-order zero bits and see if we have a
7913 power of two minus 1. */
7914 len = exact_log2 ((m >> pos) + 1);
7915
7916 if (len <= 0)
7917 pos = -1;
7918
7919 *plen = len;
7920 return pos;
7921 }
7922 \f
7923 /* If X refers to a register that equals REG in value, replace these
7924 references with REG. */
7925 static rtx
7926 canon_reg_for_combine (rtx x, rtx reg)
7927 {
7928 rtx op0, op1, op2;
7929 const char *fmt;
7930 int i;
7931 bool copied;
7932
7933 enum rtx_code code = GET_CODE (x);
7934 switch (GET_RTX_CLASS (code))
7935 {
7936 case RTX_UNARY:
7937 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7938 if (op0 != XEXP (x, 0))
7939 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7940 GET_MODE (reg));
7941 break;
7942
7943 case RTX_BIN_ARITH:
7944 case RTX_COMM_ARITH:
7945 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7946 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7947 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7948 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7949 break;
7950
7951 case RTX_COMPARE:
7952 case RTX_COMM_COMPARE:
7953 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7954 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7955 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7956 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7957 GET_MODE (op0), op0, op1);
7958 break;
7959
7960 case RTX_TERNARY:
7961 case RTX_BITFIELD_OPS:
7962 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7963 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7964 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7965 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7966 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7967 GET_MODE (op0), op0, op1, op2);
7968
7969 case RTX_OBJ:
7970 if (REG_P (x))
7971 {
7972 if (rtx_equal_p (get_last_value (reg), x)
7973 || rtx_equal_p (reg, get_last_value (x)))
7974 return reg;
7975 else
7976 break;
7977 }
7978
7979 /* fall through */
7980
7981 default:
7982 fmt = GET_RTX_FORMAT (code);
7983 copied = false;
7984 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7985 if (fmt[i] == 'e')
7986 {
7987 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7988 if (op != XEXP (x, i))
7989 {
7990 if (!copied)
7991 {
7992 copied = true;
7993 x = copy_rtx (x);
7994 }
7995 XEXP (x, i) = op;
7996 }
7997 }
7998 else if (fmt[i] == 'E')
7999 {
8000 int j;
8001 for (j = 0; j < XVECLEN (x, i); j++)
8002 {
8003 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8004 if (op != XVECEXP (x, i, j))
8005 {
8006 if (!copied)
8007 {
8008 copied = true;
8009 x = copy_rtx (x);
8010 }
8011 XVECEXP (x, i, j) = op;
8012 }
8013 }
8014 }
8015
8016 break;
8017 }
8018
8019 return x;
8020 }
8021
8022 /* Return X converted to MODE. If the value is already truncated to
8023 MODE we can just return a subreg even though in the general case we
8024 would need an explicit truncation. */
8025
8026 static rtx
8027 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
8028 {
8029 if (!CONST_INT_P (x)
8030 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8031 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8032 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8033 {
8034 /* Bit-cast X into an integer mode. */
8035 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8036 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8037 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8038 x, GET_MODE (x));
8039 }
8040
8041 return gen_lowpart (mode, x);
8042 }
8043
8044 /* See if X can be simplified knowing that we will only refer to it in
8045 MODE and will only refer to those bits that are nonzero in MASK.
8046 If other bits are being computed or if masking operations are done
8047 that select a superset of the bits in MASK, they can sometimes be
8048 ignored.
8049
8050 Return a possibly simplified expression, but always convert X to
8051 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8052
8053 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8054 are all off in X. This is used when X will be complemented, by either
8055 NOT, NEG, or XOR. */
8056
8057 static rtx
8058 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
8059 int just_select)
8060 {
8061 enum rtx_code code = GET_CODE (x);
8062 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8063 enum machine_mode op_mode;
8064 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8065 rtx op0, op1, temp;
8066
8067 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8068 code below will do the wrong thing since the mode of such an
8069 expression is VOIDmode.
8070
8071 Also do nothing if X is a CLOBBER; this can happen if X was
8072 the return value from a call to gen_lowpart. */
8073 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8074 return x;
8075
8076 /* We want to perform the operation in its present mode unless we know
8077 that the operation is valid in MODE, in which case we do the operation
8078 in MODE. */
8079 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8080 && have_insn_for (code, mode))
8081 ? mode : GET_MODE (x));
8082
8083 /* It is not valid to do a right-shift in a narrower mode
8084 than the one it came in with. */
8085 if ((code == LSHIFTRT || code == ASHIFTRT)
8086 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8087 op_mode = GET_MODE (x);
8088
8089 /* Truncate MASK to fit OP_MODE. */
8090 if (op_mode)
8091 mask &= GET_MODE_MASK (op_mode);
8092
8093 /* When we have an arithmetic operation, or a shift whose count we
8094 do not know, we need to assume that all bits up to the highest-order
8095 bit in MASK will be needed. This is how we form such a mask. */
8096 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8097 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8098 else
8099 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8100 - 1);
8101
8102 /* Determine what bits of X are guaranteed to be (non)zero. */
8103 nonzero = nonzero_bits (x, mode);
8104
8105 /* If none of the bits in X are needed, return a zero. */
8106 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8107 x = const0_rtx;
8108
8109 /* If X is a CONST_INT, return a new one. Do this here since the
8110 test below will fail. */
8111 if (CONST_INT_P (x))
8112 {
8113 if (SCALAR_INT_MODE_P (mode))
8114 return gen_int_mode (INTVAL (x) & mask, mode);
8115 else
8116 {
8117 x = GEN_INT (INTVAL (x) & mask);
8118 return gen_lowpart_common (mode, x);
8119 }
8120 }
8121
8122 /* If X is narrower than MODE and we want all the bits in X's mode, just
8123 get X in the proper mode. */
8124 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8125 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8126 return gen_lowpart (mode, x);
8127
8128 /* We can ignore the effect of a SUBREG if it narrows the mode or
8129 if the constant masks to zero all the bits the mode doesn't have. */
8130 if (GET_CODE (x) == SUBREG
8131 && subreg_lowpart_p (x)
8132 && ((GET_MODE_SIZE (GET_MODE (x))
8133 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8134 || (0 == (mask
8135 & GET_MODE_MASK (GET_MODE (x))
8136 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8137 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8138
8139 /* The arithmetic simplifications here only work for scalar integer modes. */
8140 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8141 return gen_lowpart_or_truncate (mode, x);
8142
8143 switch (code)
8144 {
8145 case CLOBBER:
8146 /* If X is a (clobber (const_int)), return it since we know we are
8147 generating something that won't match. */
8148 return x;
8149
8150 case SIGN_EXTEND:
8151 case ZERO_EXTEND:
8152 case ZERO_EXTRACT:
8153 case SIGN_EXTRACT:
8154 x = expand_compound_operation (x);
8155 if (GET_CODE (x) != code)
8156 return force_to_mode (x, mode, mask, next_select);
8157 break;
8158
8159 case TRUNCATE:
8160 /* Similarly for a truncate. */
8161 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8162
8163 case AND:
8164 /* If this is an AND with a constant, convert it into an AND
8165 whose constant is the AND of that constant with MASK. If it
8166 remains an AND of MASK, delete it since it is redundant. */
8167
8168 if (CONST_INT_P (XEXP (x, 1)))
8169 {
8170 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8171 mask & INTVAL (XEXP (x, 1)));
8172
8173 /* If X is still an AND, see if it is an AND with a mask that
8174 is just some low-order bits. If so, and it is MASK, we don't
8175 need it. */
8176
8177 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8178 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8179 == mask))
8180 x = XEXP (x, 0);
8181
8182 /* If it remains an AND, try making another AND with the bits
8183 in the mode mask that aren't in MASK turned on. If the
8184 constant in the AND is wide enough, this might make a
8185 cheaper constant. */
8186
8187 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8188 && GET_MODE_MASK (GET_MODE (x)) != mask
8189 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8190 {
8191 unsigned HOST_WIDE_INT cval
8192 = UINTVAL (XEXP (x, 1))
8193 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8194 rtx y;
8195
8196 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8197 gen_int_mode (cval, GET_MODE (x)));
8198 if (set_src_cost (y, optimize_this_for_speed_p)
8199 < set_src_cost (x, optimize_this_for_speed_p))
8200 x = y;
8201 }
8202
8203 break;
8204 }
8205
8206 goto binop;
8207
8208 case PLUS:
8209 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8210 low-order bits (as in an alignment operation) and FOO is already
8211 aligned to that boundary, mask C1 to that boundary as well.
8212 This may eliminate that PLUS and, later, the AND. */
8213
8214 {
8215 unsigned int width = GET_MODE_PRECISION (mode);
8216 unsigned HOST_WIDE_INT smask = mask;
8217
8218 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8219 number, sign extend it. */
8220
8221 if (width < HOST_BITS_PER_WIDE_INT
8222 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8223 smask |= HOST_WIDE_INT_M1U << width;
8224
8225 if (CONST_INT_P (XEXP (x, 1))
8226 && exact_log2 (- smask) >= 0
8227 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8228 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8229 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8230 (INTVAL (XEXP (x, 1)) & smask)),
8231 mode, smask, next_select);
8232 }
8233
8234 /* ... fall through ... */
8235
8236 case MULT:
8237 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8238 most significant bit in MASK since carries from those bits will
8239 affect the bits we are interested in. */
8240 mask = fuller_mask;
8241 goto binop;
8242
8243 case MINUS:
8244 /* If X is (minus C Y) where C's least set bit is larger than any bit
8245 in the mask, then we may replace with (neg Y). */
8246 if (CONST_INT_P (XEXP (x, 0))
8247 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8248 {
8249 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8250 GET_MODE (x));
8251 return force_to_mode (x, mode, mask, next_select);
8252 }
8253
8254 /* Similarly, if C contains every bit in the fuller_mask, then we may
8255 replace with (not Y). */
8256 if (CONST_INT_P (XEXP (x, 0))
8257 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8258 {
8259 x = simplify_gen_unary (NOT, GET_MODE (x),
8260 XEXP (x, 1), GET_MODE (x));
8261 return force_to_mode (x, mode, mask, next_select);
8262 }
8263
8264 mask = fuller_mask;
8265 goto binop;
8266
8267 case IOR:
8268 case XOR:
8269 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8270 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8271 operation which may be a bitfield extraction. Ensure that the
8272 constant we form is not wider than the mode of X. */
8273
8274 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8275 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8276 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8277 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8278 && CONST_INT_P (XEXP (x, 1))
8279 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8280 + floor_log2 (INTVAL (XEXP (x, 1))))
8281 < GET_MODE_PRECISION (GET_MODE (x)))
8282 && (UINTVAL (XEXP (x, 1))
8283 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8284 {
8285 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8286 << INTVAL (XEXP (XEXP (x, 0), 1)),
8287 GET_MODE (x));
8288 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8289 XEXP (XEXP (x, 0), 0), temp);
8290 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8291 XEXP (XEXP (x, 0), 1));
8292 return force_to_mode (x, mode, mask, next_select);
8293 }
8294
8295 binop:
8296 /* For most binary operations, just propagate into the operation and
8297 change the mode if we have an operation of that mode. */
8298
8299 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8300 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8301
8302 /* If we ended up truncating both operands, truncate the result of the
8303 operation instead. */
8304 if (GET_CODE (op0) == TRUNCATE
8305 && GET_CODE (op1) == TRUNCATE)
8306 {
8307 op0 = XEXP (op0, 0);
8308 op1 = XEXP (op1, 0);
8309 }
8310
8311 op0 = gen_lowpart_or_truncate (op_mode, op0);
8312 op1 = gen_lowpart_or_truncate (op_mode, op1);
8313
8314 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8315 x = simplify_gen_binary (code, op_mode, op0, op1);
8316 break;
8317
8318 case ASHIFT:
8319 /* For left shifts, do the same, but just for the first operand.
8320 However, we cannot do anything with shifts where we cannot
8321 guarantee that the counts are smaller than the size of the mode
8322 because such a count will have a different meaning in a
8323 wider mode. */
8324
8325 if (! (CONST_INT_P (XEXP (x, 1))
8326 && INTVAL (XEXP (x, 1)) >= 0
8327 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8328 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8329 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8330 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8331 break;
8332
8333 /* If the shift count is a constant and we can do arithmetic in
8334 the mode of the shift, refine which bits we need. Otherwise, use the
8335 conservative form of the mask. */
8336 if (CONST_INT_P (XEXP (x, 1))
8337 && INTVAL (XEXP (x, 1)) >= 0
8338 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8339 && HWI_COMPUTABLE_MODE_P (op_mode))
8340 mask >>= INTVAL (XEXP (x, 1));
8341 else
8342 mask = fuller_mask;
8343
8344 op0 = gen_lowpart_or_truncate (op_mode,
8345 force_to_mode (XEXP (x, 0), op_mode,
8346 mask, next_select));
8347
8348 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8349 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8350 break;
8351
8352 case LSHIFTRT:
8353 /* Here we can only do something if the shift count is a constant,
8354 this shift constant is valid for the host, and we can do arithmetic
8355 in OP_MODE. */
8356
8357 if (CONST_INT_P (XEXP (x, 1))
8358 && INTVAL (XEXP (x, 1)) >= 0
8359 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8360 && HWI_COMPUTABLE_MODE_P (op_mode))
8361 {
8362 rtx inner = XEXP (x, 0);
8363 unsigned HOST_WIDE_INT inner_mask;
8364
8365 /* Select the mask of the bits we need for the shift operand. */
8366 inner_mask = mask << INTVAL (XEXP (x, 1));
8367
8368 /* We can only change the mode of the shift if we can do arithmetic
8369 in the mode of the shift and INNER_MASK is no wider than the
8370 width of X's mode. */
8371 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8372 op_mode = GET_MODE (x);
8373
8374 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8375
8376 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8377 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8378 }
8379
8380 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8381 shift and AND produces only copies of the sign bit (C2 is one less
8382 than a power of two), we can do this with just a shift. */
8383
8384 if (GET_CODE (x) == LSHIFTRT
8385 && CONST_INT_P (XEXP (x, 1))
8386 /* The shift puts one of the sign bit copies in the least significant
8387 bit. */
8388 && ((INTVAL (XEXP (x, 1))
8389 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8390 >= GET_MODE_PRECISION (GET_MODE (x)))
8391 && exact_log2 (mask + 1) >= 0
8392 /* Number of bits left after the shift must be more than the mask
8393 needs. */
8394 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8395 <= GET_MODE_PRECISION (GET_MODE (x)))
8396 /* Must be more sign bit copies than the mask needs. */
8397 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8398 >= exact_log2 (mask + 1)))
8399 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8400 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8401 - exact_log2 (mask + 1)));
8402
8403 goto shiftrt;
8404
8405 case ASHIFTRT:
8406 /* If we are just looking for the sign bit, we don't need this shift at
8407 all, even if it has a variable count. */
8408 if (val_signbit_p (GET_MODE (x), mask))
8409 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8410
8411 /* If this is a shift by a constant, get a mask that contains those bits
8412 that are not copies of the sign bit. We then have two cases: If
8413 MASK only includes those bits, this can be a logical shift, which may
8414 allow simplifications. If MASK is a single-bit field not within
8415 those bits, we are requesting a copy of the sign bit and hence can
8416 shift the sign bit to the appropriate location. */
8417
8418 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8419 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8420 {
8421 int i;
8422
8423 /* If the considered data is wider than HOST_WIDE_INT, we can't
8424 represent a mask for all its bits in a single scalar.
8425 But we only care about the lower bits, so calculate these. */
8426
8427 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8428 {
8429 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8430
8431 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8432 is the number of bits a full-width mask would have set.
8433 We need only shift if these are fewer than nonzero can
8434 hold. If not, we must keep all bits set in nonzero. */
8435
8436 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8437 < HOST_BITS_PER_WIDE_INT)
8438 nonzero >>= INTVAL (XEXP (x, 1))
8439 + HOST_BITS_PER_WIDE_INT
8440 - GET_MODE_PRECISION (GET_MODE (x)) ;
8441 }
8442 else
8443 {
8444 nonzero = GET_MODE_MASK (GET_MODE (x));
8445 nonzero >>= INTVAL (XEXP (x, 1));
8446 }
8447
8448 if ((mask & ~nonzero) == 0)
8449 {
8450 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8451 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8452 if (GET_CODE (x) != ASHIFTRT)
8453 return force_to_mode (x, mode, mask, next_select);
8454 }
8455
8456 else if ((i = exact_log2 (mask)) >= 0)
8457 {
8458 x = simplify_shift_const
8459 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8460 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8461
8462 if (GET_CODE (x) != ASHIFTRT)
8463 return force_to_mode (x, mode, mask, next_select);
8464 }
8465 }
8466
8467 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8468 even if the shift count isn't a constant. */
8469 if (mask == 1)
8470 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8471 XEXP (x, 0), XEXP (x, 1));
8472
8473 shiftrt:
8474
8475 /* If this is a zero- or sign-extension operation that just affects bits
8476 we don't care about, remove it. Be sure the call above returned
8477 something that is still a shift. */
8478
8479 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8480 && CONST_INT_P (XEXP (x, 1))
8481 && INTVAL (XEXP (x, 1)) >= 0
8482 && (INTVAL (XEXP (x, 1))
8483 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8484 && GET_CODE (XEXP (x, 0)) == ASHIFT
8485 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8486 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8487 next_select);
8488
8489 break;
8490
8491 case ROTATE:
8492 case ROTATERT:
8493 /* If the shift count is constant and we can do computations
8494 in the mode of X, compute where the bits we care about are.
8495 Otherwise, we can't do anything. Don't change the mode of
8496 the shift or propagate MODE into the shift, though. */
8497 if (CONST_INT_P (XEXP (x, 1))
8498 && INTVAL (XEXP (x, 1)) >= 0)
8499 {
8500 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8501 GET_MODE (x),
8502 gen_int_mode (mask, GET_MODE (x)),
8503 XEXP (x, 1));
8504 if (temp && CONST_INT_P (temp))
8505 x = simplify_gen_binary (code, GET_MODE (x),
8506 force_to_mode (XEXP (x, 0), GET_MODE (x),
8507 INTVAL (temp), next_select),
8508 XEXP (x, 1));
8509 }
8510 break;
8511
8512 case NEG:
8513 /* If we just want the low-order bit, the NEG isn't needed since it
8514 won't change the low-order bit. */
8515 if (mask == 1)
8516 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8517
8518 /* We need any bits less significant than the most significant bit in
8519 MASK since carries from those bits will affect the bits we are
8520 interested in. */
8521 mask = fuller_mask;
8522 goto unop;
8523
8524 case NOT:
8525 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8526 same as the XOR case above. Ensure that the constant we form is not
8527 wider than the mode of X. */
8528
8529 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8530 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8531 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8532 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8533 < GET_MODE_PRECISION (GET_MODE (x)))
8534 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8535 {
8536 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8537 GET_MODE (x));
8538 temp = simplify_gen_binary (XOR, GET_MODE (x),
8539 XEXP (XEXP (x, 0), 0), temp);
8540 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8541 temp, XEXP (XEXP (x, 0), 1));
8542
8543 return force_to_mode (x, mode, mask, next_select);
8544 }
8545
8546 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8547 use the full mask inside the NOT. */
8548 mask = fuller_mask;
8549
8550 unop:
8551 op0 = gen_lowpart_or_truncate (op_mode,
8552 force_to_mode (XEXP (x, 0), mode, mask,
8553 next_select));
8554 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8555 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8556 break;
8557
8558 case NE:
8559 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8560 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8561 which is equal to STORE_FLAG_VALUE. */
8562 if ((mask & ~STORE_FLAG_VALUE) == 0
8563 && XEXP (x, 1) == const0_rtx
8564 && GET_MODE (XEXP (x, 0)) == mode
8565 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8566 && (nonzero_bits (XEXP (x, 0), mode)
8567 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8568 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8569
8570 break;
8571
8572 case IF_THEN_ELSE:
8573 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8574 written in a narrower mode. We play it safe and do not do so. */
8575
8576 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8577 force_to_mode (XEXP (x, 1), mode,
8578 mask, next_select));
8579 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8580 force_to_mode (XEXP (x, 2), mode,
8581 mask, next_select));
8582 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8583 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8584 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8585 op0, op1);
8586 break;
8587
8588 default:
8589 break;
8590 }
8591
8592 /* Ensure we return a value of the proper mode. */
8593 return gen_lowpart_or_truncate (mode, x);
8594 }
8595 \f
8596 /* Return nonzero if X is an expression that has one of two values depending on
8597 whether some other value is zero or nonzero. In that case, we return the
8598 value that is being tested, *PTRUE is set to the value if the rtx being
8599 returned has a nonzero value, and *PFALSE is set to the other alternative.
8600
8601 If we return zero, we set *PTRUE and *PFALSE to X. */
8602
8603 static rtx
8604 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8605 {
8606 enum machine_mode mode = GET_MODE (x);
8607 enum rtx_code code = GET_CODE (x);
8608 rtx cond0, cond1, true0, true1, false0, false1;
8609 unsigned HOST_WIDE_INT nz;
8610
8611 /* If we are comparing a value against zero, we are done. */
8612 if ((code == NE || code == EQ)
8613 && XEXP (x, 1) == const0_rtx)
8614 {
8615 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8616 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8617 return XEXP (x, 0);
8618 }
8619
8620 /* If this is a unary operation whose operand has one of two values, apply
8621 our opcode to compute those values. */
8622 else if (UNARY_P (x)
8623 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8624 {
8625 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8626 *pfalse = simplify_gen_unary (code, mode, false0,
8627 GET_MODE (XEXP (x, 0)));
8628 return cond0;
8629 }
8630
8631 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8632 make can't possibly match and would suppress other optimizations. */
8633 else if (code == COMPARE)
8634 ;
8635
8636 /* If this is a binary operation, see if either side has only one of two
8637 values. If either one does or if both do and they are conditional on
8638 the same value, compute the new true and false values. */
8639 else if (BINARY_P (x))
8640 {
8641 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8642 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8643
8644 if ((cond0 != 0 || cond1 != 0)
8645 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8646 {
8647 /* If if_then_else_cond returned zero, then true/false are the
8648 same rtl. We must copy one of them to prevent invalid rtl
8649 sharing. */
8650 if (cond0 == 0)
8651 true0 = copy_rtx (true0);
8652 else if (cond1 == 0)
8653 true1 = copy_rtx (true1);
8654
8655 if (COMPARISON_P (x))
8656 {
8657 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8658 true0, true1);
8659 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8660 false0, false1);
8661 }
8662 else
8663 {
8664 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8665 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8666 }
8667
8668 return cond0 ? cond0 : cond1;
8669 }
8670
8671 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8672 operands is zero when the other is nonzero, and vice-versa,
8673 and STORE_FLAG_VALUE is 1 or -1. */
8674
8675 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8676 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8677 || code == UMAX)
8678 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8679 {
8680 rtx op0 = XEXP (XEXP (x, 0), 1);
8681 rtx op1 = XEXP (XEXP (x, 1), 1);
8682
8683 cond0 = XEXP (XEXP (x, 0), 0);
8684 cond1 = XEXP (XEXP (x, 1), 0);
8685
8686 if (COMPARISON_P (cond0)
8687 && COMPARISON_P (cond1)
8688 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8689 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8690 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8691 || ((swap_condition (GET_CODE (cond0))
8692 == reversed_comparison_code (cond1, NULL))
8693 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8694 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8695 && ! side_effects_p (x))
8696 {
8697 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8698 *pfalse = simplify_gen_binary (MULT, mode,
8699 (code == MINUS
8700 ? simplify_gen_unary (NEG, mode,
8701 op1, mode)
8702 : op1),
8703 const_true_rtx);
8704 return cond0;
8705 }
8706 }
8707
8708 /* Similarly for MULT, AND and UMIN, except that for these the result
8709 is always zero. */
8710 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8711 && (code == MULT || code == AND || code == UMIN)
8712 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8713 {
8714 cond0 = XEXP (XEXP (x, 0), 0);
8715 cond1 = XEXP (XEXP (x, 1), 0);
8716
8717 if (COMPARISON_P (cond0)
8718 && COMPARISON_P (cond1)
8719 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8720 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8721 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8722 || ((swap_condition (GET_CODE (cond0))
8723 == reversed_comparison_code (cond1, NULL))
8724 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8725 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8726 && ! side_effects_p (x))
8727 {
8728 *ptrue = *pfalse = const0_rtx;
8729 return cond0;
8730 }
8731 }
8732 }
8733
8734 else if (code == IF_THEN_ELSE)
8735 {
8736 /* If we have IF_THEN_ELSE already, extract the condition and
8737 canonicalize it if it is NE or EQ. */
8738 cond0 = XEXP (x, 0);
8739 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8740 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8741 return XEXP (cond0, 0);
8742 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8743 {
8744 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8745 return XEXP (cond0, 0);
8746 }
8747 else
8748 return cond0;
8749 }
8750
8751 /* If X is a SUBREG, we can narrow both the true and false values
8752 if the inner expression, if there is a condition. */
8753 else if (code == SUBREG
8754 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8755 &true0, &false0)))
8756 {
8757 true0 = simplify_gen_subreg (mode, true0,
8758 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8759 false0 = simplify_gen_subreg (mode, false0,
8760 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8761 if (true0 && false0)
8762 {
8763 *ptrue = true0;
8764 *pfalse = false0;
8765 return cond0;
8766 }
8767 }
8768
8769 /* If X is a constant, this isn't special and will cause confusions
8770 if we treat it as such. Likewise if it is equivalent to a constant. */
8771 else if (CONSTANT_P (x)
8772 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8773 ;
8774
8775 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8776 will be least confusing to the rest of the compiler. */
8777 else if (mode == BImode)
8778 {
8779 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8780 return x;
8781 }
8782
8783 /* If X is known to be either 0 or -1, those are the true and
8784 false values when testing X. */
8785 else if (x == constm1_rtx || x == const0_rtx
8786 || (mode != VOIDmode
8787 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8788 {
8789 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8790 return x;
8791 }
8792
8793 /* Likewise for 0 or a single bit. */
8794 else if (HWI_COMPUTABLE_MODE_P (mode)
8795 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8796 {
8797 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8798 return x;
8799 }
8800
8801 /* Otherwise fail; show no condition with true and false values the same. */
8802 *ptrue = *pfalse = x;
8803 return 0;
8804 }
8805 \f
8806 /* Return the value of expression X given the fact that condition COND
8807 is known to be true when applied to REG as its first operand and VAL
8808 as its second. X is known to not be shared and so can be modified in
8809 place.
8810
8811 We only handle the simplest cases, and specifically those cases that
8812 arise with IF_THEN_ELSE expressions. */
8813
8814 static rtx
8815 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8816 {
8817 enum rtx_code code = GET_CODE (x);
8818 rtx temp;
8819 const char *fmt;
8820 int i, j;
8821
8822 if (side_effects_p (x))
8823 return x;
8824
8825 /* If either operand of the condition is a floating point value,
8826 then we have to avoid collapsing an EQ comparison. */
8827 if (cond == EQ
8828 && rtx_equal_p (x, reg)
8829 && ! FLOAT_MODE_P (GET_MODE (x))
8830 && ! FLOAT_MODE_P (GET_MODE (val)))
8831 return val;
8832
8833 if (cond == UNEQ && rtx_equal_p (x, reg))
8834 return val;
8835
8836 /* If X is (abs REG) and we know something about REG's relationship
8837 with zero, we may be able to simplify this. */
8838
8839 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8840 switch (cond)
8841 {
8842 case GE: case GT: case EQ:
8843 return XEXP (x, 0);
8844 case LT: case LE:
8845 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8846 XEXP (x, 0),
8847 GET_MODE (XEXP (x, 0)));
8848 default:
8849 break;
8850 }
8851
8852 /* The only other cases we handle are MIN, MAX, and comparisons if the
8853 operands are the same as REG and VAL. */
8854
8855 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8856 {
8857 if (rtx_equal_p (XEXP (x, 0), val))
8858 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8859
8860 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8861 {
8862 if (COMPARISON_P (x))
8863 {
8864 if (comparison_dominates_p (cond, code))
8865 return const_true_rtx;
8866
8867 code = reversed_comparison_code (x, NULL);
8868 if (code != UNKNOWN
8869 && comparison_dominates_p (cond, code))
8870 return const0_rtx;
8871 else
8872 return x;
8873 }
8874 else if (code == SMAX || code == SMIN
8875 || code == UMIN || code == UMAX)
8876 {
8877 int unsignedp = (code == UMIN || code == UMAX);
8878
8879 /* Do not reverse the condition when it is NE or EQ.
8880 This is because we cannot conclude anything about
8881 the value of 'SMAX (x, y)' when x is not equal to y,
8882 but we can when x equals y. */
8883 if ((code == SMAX || code == UMAX)
8884 && ! (cond == EQ || cond == NE))
8885 cond = reverse_condition (cond);
8886
8887 switch (cond)
8888 {
8889 case GE: case GT:
8890 return unsignedp ? x : XEXP (x, 1);
8891 case LE: case LT:
8892 return unsignedp ? x : XEXP (x, 0);
8893 case GEU: case GTU:
8894 return unsignedp ? XEXP (x, 1) : x;
8895 case LEU: case LTU:
8896 return unsignedp ? XEXP (x, 0) : x;
8897 default:
8898 break;
8899 }
8900 }
8901 }
8902 }
8903 else if (code == SUBREG)
8904 {
8905 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8906 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8907
8908 if (SUBREG_REG (x) != r)
8909 {
8910 /* We must simplify subreg here, before we lose track of the
8911 original inner_mode. */
8912 new_rtx = simplify_subreg (GET_MODE (x), r,
8913 inner_mode, SUBREG_BYTE (x));
8914 if (new_rtx)
8915 return new_rtx;
8916 else
8917 SUBST (SUBREG_REG (x), r);
8918 }
8919
8920 return x;
8921 }
8922 /* We don't have to handle SIGN_EXTEND here, because even in the
8923 case of replacing something with a modeless CONST_INT, a
8924 CONST_INT is already (supposed to be) a valid sign extension for
8925 its narrower mode, which implies it's already properly
8926 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8927 story is different. */
8928 else if (code == ZERO_EXTEND)
8929 {
8930 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8931 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8932
8933 if (XEXP (x, 0) != r)
8934 {
8935 /* We must simplify the zero_extend here, before we lose
8936 track of the original inner_mode. */
8937 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8938 r, inner_mode);
8939 if (new_rtx)
8940 return new_rtx;
8941 else
8942 SUBST (XEXP (x, 0), r);
8943 }
8944
8945 return x;
8946 }
8947
8948 fmt = GET_RTX_FORMAT (code);
8949 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8950 {
8951 if (fmt[i] == 'e')
8952 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8953 else if (fmt[i] == 'E')
8954 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8955 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8956 cond, reg, val));
8957 }
8958
8959 return x;
8960 }
8961 \f
8962 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8963 assignment as a field assignment. */
8964
8965 static int
8966 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8967 {
8968 if (x == y || rtx_equal_p (x, y))
8969 return 1;
8970
8971 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8972 return 0;
8973
8974 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8975 Note that all SUBREGs of MEM are paradoxical; otherwise they
8976 would have been rewritten. */
8977 if (MEM_P (x) && GET_CODE (y) == SUBREG
8978 && MEM_P (SUBREG_REG (y))
8979 && rtx_equal_p (SUBREG_REG (y),
8980 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8981 return 1;
8982
8983 if (MEM_P (y) && GET_CODE (x) == SUBREG
8984 && MEM_P (SUBREG_REG (x))
8985 && rtx_equal_p (SUBREG_REG (x),
8986 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8987 return 1;
8988
8989 /* We used to see if get_last_value of X and Y were the same but that's
8990 not correct. In one direction, we'll cause the assignment to have
8991 the wrong destination and in the case, we'll import a register into this
8992 insn that might have already have been dead. So fail if none of the
8993 above cases are true. */
8994 return 0;
8995 }
8996 \f
8997 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8998 Return that assignment if so.
8999
9000 We only handle the most common cases. */
9001
9002 static rtx
9003 make_field_assignment (rtx x)
9004 {
9005 rtx dest = SET_DEST (x);
9006 rtx src = SET_SRC (x);
9007 rtx assign;
9008 rtx rhs, lhs;
9009 HOST_WIDE_INT c1;
9010 HOST_WIDE_INT pos;
9011 unsigned HOST_WIDE_INT len;
9012 rtx other;
9013 enum machine_mode mode;
9014
9015 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9016 a clear of a one-bit field. We will have changed it to
9017 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9018 for a SUBREG. */
9019
9020 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9021 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9022 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9023 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9024 {
9025 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9026 1, 1, 1, 0);
9027 if (assign != 0)
9028 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9029 return x;
9030 }
9031
9032 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9033 && subreg_lowpart_p (XEXP (src, 0))
9034 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9035 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9036 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9037 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9038 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9039 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9040 {
9041 assign = make_extraction (VOIDmode, dest, 0,
9042 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9043 1, 1, 1, 0);
9044 if (assign != 0)
9045 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9046 return x;
9047 }
9048
9049 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9050 one-bit field. */
9051 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9052 && XEXP (XEXP (src, 0), 0) == const1_rtx
9053 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9054 {
9055 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9056 1, 1, 1, 0);
9057 if (assign != 0)
9058 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9059 return x;
9060 }
9061
9062 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9063 SRC is an AND with all bits of that field set, then we can discard
9064 the AND. */
9065 if (GET_CODE (dest) == ZERO_EXTRACT
9066 && CONST_INT_P (XEXP (dest, 1))
9067 && GET_CODE (src) == AND
9068 && CONST_INT_P (XEXP (src, 1)))
9069 {
9070 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9071 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9072 unsigned HOST_WIDE_INT ze_mask;
9073
9074 if (width >= HOST_BITS_PER_WIDE_INT)
9075 ze_mask = -1;
9076 else
9077 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9078
9079 /* Complete overlap. We can remove the source AND. */
9080 if ((and_mask & ze_mask) == ze_mask)
9081 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9082
9083 /* Partial overlap. We can reduce the source AND. */
9084 if ((and_mask & ze_mask) != and_mask)
9085 {
9086 mode = GET_MODE (src);
9087 src = gen_rtx_AND (mode, XEXP (src, 0),
9088 gen_int_mode (and_mask & ze_mask, mode));
9089 return gen_rtx_SET (VOIDmode, dest, src);
9090 }
9091 }
9092
9093 /* The other case we handle is assignments into a constant-position
9094 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9095 a mask that has all one bits except for a group of zero bits and
9096 OTHER is known to have zeros where C1 has ones, this is such an
9097 assignment. Compute the position and length from C1. Shift OTHER
9098 to the appropriate position, force it to the required mode, and
9099 make the extraction. Check for the AND in both operands. */
9100
9101 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9102 return x;
9103
9104 rhs = expand_compound_operation (XEXP (src, 0));
9105 lhs = expand_compound_operation (XEXP (src, 1));
9106
9107 if (GET_CODE (rhs) == AND
9108 && CONST_INT_P (XEXP (rhs, 1))
9109 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9110 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9111 else if (GET_CODE (lhs) == AND
9112 && CONST_INT_P (XEXP (lhs, 1))
9113 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9114 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9115 else
9116 return x;
9117
9118 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9119 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9120 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9121 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9122 return x;
9123
9124 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9125 if (assign == 0)
9126 return x;
9127
9128 /* The mode to use for the source is the mode of the assignment, or of
9129 what is inside a possible STRICT_LOW_PART. */
9130 mode = (GET_CODE (assign) == STRICT_LOW_PART
9131 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9132
9133 /* Shift OTHER right POS places and make it the source, restricting it
9134 to the proper length and mode. */
9135
9136 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9137 GET_MODE (src),
9138 other, pos),
9139 dest);
9140 src = force_to_mode (src, mode,
9141 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9142 ? ~(unsigned HOST_WIDE_INT) 0
9143 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9144 0);
9145
9146 /* If SRC is masked by an AND that does not make a difference in
9147 the value being stored, strip it. */
9148 if (GET_CODE (assign) == ZERO_EXTRACT
9149 && CONST_INT_P (XEXP (assign, 1))
9150 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9151 && GET_CODE (src) == AND
9152 && CONST_INT_P (XEXP (src, 1))
9153 && UINTVAL (XEXP (src, 1))
9154 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9155 src = XEXP (src, 0);
9156
9157 return gen_rtx_SET (VOIDmode, assign, src);
9158 }
9159 \f
9160 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9161 if so. */
9162
9163 static rtx
9164 apply_distributive_law (rtx x)
9165 {
9166 enum rtx_code code = GET_CODE (x);
9167 enum rtx_code inner_code;
9168 rtx lhs, rhs, other;
9169 rtx tem;
9170
9171 /* Distributivity is not true for floating point as it can change the
9172 value. So we don't do it unless -funsafe-math-optimizations. */
9173 if (FLOAT_MODE_P (GET_MODE (x))
9174 && ! flag_unsafe_math_optimizations)
9175 return x;
9176
9177 /* The outer operation can only be one of the following: */
9178 if (code != IOR && code != AND && code != XOR
9179 && code != PLUS && code != MINUS)
9180 return x;
9181
9182 lhs = XEXP (x, 0);
9183 rhs = XEXP (x, 1);
9184
9185 /* If either operand is a primitive we can't do anything, so get out
9186 fast. */
9187 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9188 return x;
9189
9190 lhs = expand_compound_operation (lhs);
9191 rhs = expand_compound_operation (rhs);
9192 inner_code = GET_CODE (lhs);
9193 if (inner_code != GET_CODE (rhs))
9194 return x;
9195
9196 /* See if the inner and outer operations distribute. */
9197 switch (inner_code)
9198 {
9199 case LSHIFTRT:
9200 case ASHIFTRT:
9201 case AND:
9202 case IOR:
9203 /* These all distribute except over PLUS. */
9204 if (code == PLUS || code == MINUS)
9205 return x;
9206 break;
9207
9208 case MULT:
9209 if (code != PLUS && code != MINUS)
9210 return x;
9211 break;
9212
9213 case ASHIFT:
9214 /* This is also a multiply, so it distributes over everything. */
9215 break;
9216
9217 /* This used to handle SUBREG, but this turned out to be counter-
9218 productive, since (subreg (op ...)) usually is not handled by
9219 insn patterns, and this "optimization" therefore transformed
9220 recognizable patterns into unrecognizable ones. Therefore the
9221 SUBREG case was removed from here.
9222
9223 It is possible that distributing SUBREG over arithmetic operations
9224 leads to an intermediate result than can then be optimized further,
9225 e.g. by moving the outer SUBREG to the other side of a SET as done
9226 in simplify_set. This seems to have been the original intent of
9227 handling SUBREGs here.
9228
9229 However, with current GCC this does not appear to actually happen,
9230 at least on major platforms. If some case is found where removing
9231 the SUBREG case here prevents follow-on optimizations, distributing
9232 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9233
9234 default:
9235 return x;
9236 }
9237
9238 /* Set LHS and RHS to the inner operands (A and B in the example
9239 above) and set OTHER to the common operand (C in the example).
9240 There is only one way to do this unless the inner operation is
9241 commutative. */
9242 if (COMMUTATIVE_ARITH_P (lhs)
9243 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9244 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9245 else if (COMMUTATIVE_ARITH_P (lhs)
9246 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9247 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9248 else if (COMMUTATIVE_ARITH_P (lhs)
9249 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9250 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9251 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9252 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9253 else
9254 return x;
9255
9256 /* Form the new inner operation, seeing if it simplifies first. */
9257 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9258
9259 /* There is one exception to the general way of distributing:
9260 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9261 if (code == XOR && inner_code == IOR)
9262 {
9263 inner_code = AND;
9264 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9265 }
9266
9267 /* We may be able to continuing distributing the result, so call
9268 ourselves recursively on the inner operation before forming the
9269 outer operation, which we return. */
9270 return simplify_gen_binary (inner_code, GET_MODE (x),
9271 apply_distributive_law (tem), other);
9272 }
9273
9274 /* See if X is of the form (* (+ A B) C), and if so convert to
9275 (+ (* A C) (* B C)) and try to simplify.
9276
9277 Most of the time, this results in no change. However, if some of
9278 the operands are the same or inverses of each other, simplifications
9279 will result.
9280
9281 For example, (and (ior A B) (not B)) can occur as the result of
9282 expanding a bit field assignment. When we apply the distributive
9283 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9284 which then simplifies to (and (A (not B))).
9285
9286 Note that no checks happen on the validity of applying the inverse
9287 distributive law. This is pointless since we can do it in the
9288 few places where this routine is called.
9289
9290 N is the index of the term that is decomposed (the arithmetic operation,
9291 i.e. (+ A B) in the first example above). !N is the index of the term that
9292 is distributed, i.e. of C in the first example above. */
9293 static rtx
9294 distribute_and_simplify_rtx (rtx x, int n)
9295 {
9296 enum machine_mode mode;
9297 enum rtx_code outer_code, inner_code;
9298 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9299
9300 /* Distributivity is not true for floating point as it can change the
9301 value. So we don't do it unless -funsafe-math-optimizations. */
9302 if (FLOAT_MODE_P (GET_MODE (x))
9303 && ! flag_unsafe_math_optimizations)
9304 return NULL_RTX;
9305
9306 decomposed = XEXP (x, n);
9307 if (!ARITHMETIC_P (decomposed))
9308 return NULL_RTX;
9309
9310 mode = GET_MODE (x);
9311 outer_code = GET_CODE (x);
9312 distributed = XEXP (x, !n);
9313
9314 inner_code = GET_CODE (decomposed);
9315 inner_op0 = XEXP (decomposed, 0);
9316 inner_op1 = XEXP (decomposed, 1);
9317
9318 /* Special case (and (xor B C) (not A)), which is equivalent to
9319 (xor (ior A B) (ior A C)) */
9320 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9321 {
9322 distributed = XEXP (distributed, 0);
9323 outer_code = IOR;
9324 }
9325
9326 if (n == 0)
9327 {
9328 /* Distribute the second term. */
9329 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9330 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9331 }
9332 else
9333 {
9334 /* Distribute the first term. */
9335 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9336 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9337 }
9338
9339 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9340 new_op0, new_op1));
9341 if (GET_CODE (tmp) != outer_code
9342 && (set_src_cost (tmp, optimize_this_for_speed_p)
9343 < set_src_cost (x, optimize_this_for_speed_p)))
9344 return tmp;
9345
9346 return NULL_RTX;
9347 }
9348 \f
9349 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9350 in MODE. Return an equivalent form, if different from (and VAROP
9351 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9352
9353 static rtx
9354 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9355 unsigned HOST_WIDE_INT constop)
9356 {
9357 unsigned HOST_WIDE_INT nonzero;
9358 unsigned HOST_WIDE_INT orig_constop;
9359 rtx orig_varop;
9360 int i;
9361
9362 orig_varop = varop;
9363 orig_constop = constop;
9364 if (GET_CODE (varop) == CLOBBER)
9365 return NULL_RTX;
9366
9367 /* Simplify VAROP knowing that we will be only looking at some of the
9368 bits in it.
9369
9370 Note by passing in CONSTOP, we guarantee that the bits not set in
9371 CONSTOP are not significant and will never be examined. We must
9372 ensure that is the case by explicitly masking out those bits
9373 before returning. */
9374 varop = force_to_mode (varop, mode, constop, 0);
9375
9376 /* If VAROP is a CLOBBER, we will fail so return it. */
9377 if (GET_CODE (varop) == CLOBBER)
9378 return varop;
9379
9380 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9381 to VAROP and return the new constant. */
9382 if (CONST_INT_P (varop))
9383 return gen_int_mode (INTVAL (varop) & constop, mode);
9384
9385 /* See what bits may be nonzero in VAROP. Unlike the general case of
9386 a call to nonzero_bits, here we don't care about bits outside
9387 MODE. */
9388
9389 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9390
9391 /* Turn off all bits in the constant that are known to already be zero.
9392 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9393 which is tested below. */
9394
9395 constop &= nonzero;
9396
9397 /* If we don't have any bits left, return zero. */
9398 if (constop == 0)
9399 return const0_rtx;
9400
9401 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9402 a power of two, we can replace this with an ASHIFT. */
9403 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9404 && (i = exact_log2 (constop)) >= 0)
9405 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9406
9407 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9408 or XOR, then try to apply the distributive law. This may eliminate
9409 operations if either branch can be simplified because of the AND.
9410 It may also make some cases more complex, but those cases probably
9411 won't match a pattern either with or without this. */
9412
9413 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9414 return
9415 gen_lowpart
9416 (mode,
9417 apply_distributive_law
9418 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9419 simplify_and_const_int (NULL_RTX,
9420 GET_MODE (varop),
9421 XEXP (varop, 0),
9422 constop),
9423 simplify_and_const_int (NULL_RTX,
9424 GET_MODE (varop),
9425 XEXP (varop, 1),
9426 constop))));
9427
9428 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9429 the AND and see if one of the operands simplifies to zero. If so, we
9430 may eliminate it. */
9431
9432 if (GET_CODE (varop) == PLUS
9433 && exact_log2 (constop + 1) >= 0)
9434 {
9435 rtx o0, o1;
9436
9437 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9438 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9439 if (o0 == const0_rtx)
9440 return o1;
9441 if (o1 == const0_rtx)
9442 return o0;
9443 }
9444
9445 /* Make a SUBREG if necessary. If we can't make it, fail. */
9446 varop = gen_lowpart (mode, varop);
9447 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9448 return NULL_RTX;
9449
9450 /* If we are only masking insignificant bits, return VAROP. */
9451 if (constop == nonzero)
9452 return varop;
9453
9454 if (varop == orig_varop && constop == orig_constop)
9455 return NULL_RTX;
9456
9457 /* Otherwise, return an AND. */
9458 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9459 }
9460
9461
9462 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9463 in MODE.
9464
9465 Return an equivalent form, if different from X. Otherwise, return X. If
9466 X is zero, we are to always construct the equivalent form. */
9467
9468 static rtx
9469 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9470 unsigned HOST_WIDE_INT constop)
9471 {
9472 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9473 if (tem)
9474 return tem;
9475
9476 if (!x)
9477 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9478 gen_int_mode (constop, mode));
9479 if (GET_MODE (x) != mode)
9480 x = gen_lowpart (mode, x);
9481 return x;
9482 }
9483 \f
9484 /* Given a REG, X, compute which bits in X can be nonzero.
9485 We don't care about bits outside of those defined in MODE.
9486
9487 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9488 a shift, AND, or zero_extract, we can do better. */
9489
9490 static rtx
9491 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9492 const_rtx known_x ATTRIBUTE_UNUSED,
9493 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9494 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9495 unsigned HOST_WIDE_INT *nonzero)
9496 {
9497 rtx tem;
9498 reg_stat_type *rsp;
9499
9500 /* If X is a register whose nonzero bits value is current, use it.
9501 Otherwise, if X is a register whose value we can find, use that
9502 value. Otherwise, use the previously-computed global nonzero bits
9503 for this register. */
9504
9505 rsp = &reg_stat[REGNO (x)];
9506 if (rsp->last_set_value != 0
9507 && (rsp->last_set_mode == mode
9508 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9509 && GET_MODE_CLASS (mode) == MODE_INT))
9510 && ((rsp->last_set_label >= label_tick_ebb_start
9511 && rsp->last_set_label < label_tick)
9512 || (rsp->last_set_label == label_tick
9513 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9514 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9515 && REG_N_SETS (REGNO (x)) == 1
9516 && !REGNO_REG_SET_P
9517 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9518 REGNO (x)))))
9519 {
9520 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9521
9522 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9523 /* We don't know anything about the upper bits. */
9524 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9525
9526 *nonzero &= mask;
9527 return NULL;
9528 }
9529
9530 tem = get_last_value (x);
9531
9532 if (tem)
9533 {
9534 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9535 /* If X is narrower than MODE and TEM is a non-negative
9536 constant that would appear negative in the mode of X,
9537 sign-extend it for use in reg_nonzero_bits because some
9538 machines (maybe most) will actually do the sign-extension
9539 and this is the conservative approach.
9540
9541 ??? For 2.5, try to tighten up the MD files in this regard
9542 instead of this kludge. */
9543
9544 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9545 && CONST_INT_P (tem)
9546 && INTVAL (tem) > 0
9547 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9548 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9549 #endif
9550 return tem;
9551 }
9552 else if (nonzero_sign_valid && rsp->nonzero_bits)
9553 {
9554 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9555
9556 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9557 /* We don't know anything about the upper bits. */
9558 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9559
9560 *nonzero &= mask;
9561 }
9562
9563 return NULL;
9564 }
9565
9566 /* Return the number of bits at the high-order end of X that are known to
9567 be equal to the sign bit. X will be used in mode MODE; if MODE is
9568 VOIDmode, X will be used in its own mode. The returned value will always
9569 be between 1 and the number of bits in MODE. */
9570
9571 static rtx
9572 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9573 const_rtx known_x ATTRIBUTE_UNUSED,
9574 enum machine_mode known_mode
9575 ATTRIBUTE_UNUSED,
9576 unsigned int known_ret ATTRIBUTE_UNUSED,
9577 unsigned int *result)
9578 {
9579 rtx tem;
9580 reg_stat_type *rsp;
9581
9582 rsp = &reg_stat[REGNO (x)];
9583 if (rsp->last_set_value != 0
9584 && rsp->last_set_mode == mode
9585 && ((rsp->last_set_label >= label_tick_ebb_start
9586 && rsp->last_set_label < label_tick)
9587 || (rsp->last_set_label == label_tick
9588 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9589 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9590 && REG_N_SETS (REGNO (x)) == 1
9591 && !REGNO_REG_SET_P
9592 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9593 REGNO (x)))))
9594 {
9595 *result = rsp->last_set_sign_bit_copies;
9596 return NULL;
9597 }
9598
9599 tem = get_last_value (x);
9600 if (tem != 0)
9601 return tem;
9602
9603 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9604 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9605 *result = rsp->sign_bit_copies;
9606
9607 return NULL;
9608 }
9609 \f
9610 /* Return the number of "extended" bits there are in X, when interpreted
9611 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9612 unsigned quantities, this is the number of high-order zero bits.
9613 For signed quantities, this is the number of copies of the sign bit
9614 minus 1. In both case, this function returns the number of "spare"
9615 bits. For example, if two quantities for which this function returns
9616 at least 1 are added, the addition is known not to overflow.
9617
9618 This function will always return 0 unless called during combine, which
9619 implies that it must be called from a define_split. */
9620
9621 unsigned int
9622 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9623 {
9624 if (nonzero_sign_valid == 0)
9625 return 0;
9626
9627 return (unsignedp
9628 ? (HWI_COMPUTABLE_MODE_P (mode)
9629 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9630 - floor_log2 (nonzero_bits (x, mode)))
9631 : 0)
9632 : num_sign_bit_copies (x, mode) - 1);
9633 }
9634
9635 /* This function is called from `simplify_shift_const' to merge two
9636 outer operations. Specifically, we have already found that we need
9637 to perform operation *POP0 with constant *PCONST0 at the outermost
9638 position. We would now like to also perform OP1 with constant CONST1
9639 (with *POP0 being done last).
9640
9641 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9642 the resulting operation. *PCOMP_P is set to 1 if we would need to
9643 complement the innermost operand, otherwise it is unchanged.
9644
9645 MODE is the mode in which the operation will be done. No bits outside
9646 the width of this mode matter. It is assumed that the width of this mode
9647 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9648
9649 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9650 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9651 result is simply *PCONST0.
9652
9653 If the resulting operation cannot be expressed as one operation, we
9654 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9655
9656 static int
9657 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
9658 {
9659 enum rtx_code op0 = *pop0;
9660 HOST_WIDE_INT const0 = *pconst0;
9661
9662 const0 &= GET_MODE_MASK (mode);
9663 const1 &= GET_MODE_MASK (mode);
9664
9665 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9666 if (op0 == AND)
9667 const1 &= const0;
9668
9669 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9670 if OP0 is SET. */
9671
9672 if (op1 == UNKNOWN || op0 == SET)
9673 return 1;
9674
9675 else if (op0 == UNKNOWN)
9676 op0 = op1, const0 = const1;
9677
9678 else if (op0 == op1)
9679 {
9680 switch (op0)
9681 {
9682 case AND:
9683 const0 &= const1;
9684 break;
9685 case IOR:
9686 const0 |= const1;
9687 break;
9688 case XOR:
9689 const0 ^= const1;
9690 break;
9691 case PLUS:
9692 const0 += const1;
9693 break;
9694 case NEG:
9695 op0 = UNKNOWN;
9696 break;
9697 default:
9698 break;
9699 }
9700 }
9701
9702 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9703 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9704 return 0;
9705
9706 /* If the two constants aren't the same, we can't do anything. The
9707 remaining six cases can all be done. */
9708 else if (const0 != const1)
9709 return 0;
9710
9711 else
9712 switch (op0)
9713 {
9714 case IOR:
9715 if (op1 == AND)
9716 /* (a & b) | b == b */
9717 op0 = SET;
9718 else /* op1 == XOR */
9719 /* (a ^ b) | b == a | b */
9720 {;}
9721 break;
9722
9723 case XOR:
9724 if (op1 == AND)
9725 /* (a & b) ^ b == (~a) & b */
9726 op0 = AND, *pcomp_p = 1;
9727 else /* op1 == IOR */
9728 /* (a | b) ^ b == a & ~b */
9729 op0 = AND, const0 = ~const0;
9730 break;
9731
9732 case AND:
9733 if (op1 == IOR)
9734 /* (a | b) & b == b */
9735 op0 = SET;
9736 else /* op1 == XOR */
9737 /* (a ^ b) & b) == (~a) & b */
9738 *pcomp_p = 1;
9739 break;
9740 default:
9741 break;
9742 }
9743
9744 /* Check for NO-OP cases. */
9745 const0 &= GET_MODE_MASK (mode);
9746 if (const0 == 0
9747 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9748 op0 = UNKNOWN;
9749 else if (const0 == 0 && op0 == AND)
9750 op0 = SET;
9751 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9752 && op0 == AND)
9753 op0 = UNKNOWN;
9754
9755 *pop0 = op0;
9756
9757 /* ??? Slightly redundant with the above mask, but not entirely.
9758 Moving this above means we'd have to sign-extend the mode mask
9759 for the final test. */
9760 if (op0 != UNKNOWN && op0 != NEG)
9761 *pconst0 = trunc_int_for_mode (const0, mode);
9762
9763 return 1;
9764 }
9765 \f
9766 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9767 the shift in. The original shift operation CODE is performed on OP in
9768 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9769 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9770 result of the shift is subject to operation OUTER_CODE with operand
9771 OUTER_CONST. */
9772
9773 static enum machine_mode
9774 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9775 enum machine_mode orig_mode, enum machine_mode mode,
9776 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9777 {
9778 if (orig_mode == mode)
9779 return mode;
9780 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9781
9782 /* In general we can't perform in wider mode for right shift and rotate. */
9783 switch (code)
9784 {
9785 case ASHIFTRT:
9786 /* We can still widen if the bits brought in from the left are identical
9787 to the sign bit of ORIG_MODE. */
9788 if (num_sign_bit_copies (op, mode)
9789 > (unsigned) (GET_MODE_PRECISION (mode)
9790 - GET_MODE_PRECISION (orig_mode)))
9791 return mode;
9792 return orig_mode;
9793
9794 case LSHIFTRT:
9795 /* Similarly here but with zero bits. */
9796 if (HWI_COMPUTABLE_MODE_P (mode)
9797 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9798 return mode;
9799
9800 /* We can also widen if the bits brought in will be masked off. This
9801 operation is performed in ORIG_MODE. */
9802 if (outer_code == AND)
9803 {
9804 int care_bits = low_bitmask_len (orig_mode, outer_const);
9805
9806 if (care_bits >= 0
9807 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9808 return mode;
9809 }
9810 /* fall through */
9811
9812 case ROTATE:
9813 return orig_mode;
9814
9815 case ROTATERT:
9816 gcc_unreachable ();
9817
9818 default:
9819 return mode;
9820 }
9821 }
9822
9823 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9824 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9825 if we cannot simplify it. Otherwise, return a simplified value.
9826
9827 The shift is normally computed in the widest mode we find in VAROP, as
9828 long as it isn't a different number of words than RESULT_MODE. Exceptions
9829 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9830
9831 static rtx
9832 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9833 rtx varop, int orig_count)
9834 {
9835 enum rtx_code orig_code = code;
9836 rtx orig_varop = varop;
9837 int count;
9838 enum machine_mode mode = result_mode;
9839 enum machine_mode shift_mode, tmode;
9840 unsigned int mode_words
9841 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9842 /* We form (outer_op (code varop count) (outer_const)). */
9843 enum rtx_code outer_op = UNKNOWN;
9844 HOST_WIDE_INT outer_const = 0;
9845 int complement_p = 0;
9846 rtx new_rtx, x;
9847
9848 /* Make sure and truncate the "natural" shift on the way in. We don't
9849 want to do this inside the loop as it makes it more difficult to
9850 combine shifts. */
9851 if (SHIFT_COUNT_TRUNCATED)
9852 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9853
9854 /* If we were given an invalid count, don't do anything except exactly
9855 what was requested. */
9856
9857 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9858 return NULL_RTX;
9859
9860 count = orig_count;
9861
9862 /* Unless one of the branches of the `if' in this loop does a `continue',
9863 we will `break' the loop after the `if'. */
9864
9865 while (count != 0)
9866 {
9867 /* If we have an operand of (clobber (const_int 0)), fail. */
9868 if (GET_CODE (varop) == CLOBBER)
9869 return NULL_RTX;
9870
9871 /* Convert ROTATERT to ROTATE. */
9872 if (code == ROTATERT)
9873 {
9874 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9875 code = ROTATE;
9876 if (VECTOR_MODE_P (result_mode))
9877 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9878 else
9879 count = bitsize - count;
9880 }
9881
9882 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9883 mode, outer_op, outer_const);
9884
9885 /* Handle cases where the count is greater than the size of the mode
9886 minus 1. For ASHIFT, use the size minus one as the count (this can
9887 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9888 take the count modulo the size. For other shifts, the result is
9889 zero.
9890
9891 Since these shifts are being produced by the compiler by combining
9892 multiple operations, each of which are defined, we know what the
9893 result is supposed to be. */
9894
9895 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
9896 {
9897 if (code == ASHIFTRT)
9898 count = GET_MODE_PRECISION (shift_mode) - 1;
9899 else if (code == ROTATE || code == ROTATERT)
9900 count %= GET_MODE_PRECISION (shift_mode);
9901 else
9902 {
9903 /* We can't simply return zero because there may be an
9904 outer op. */
9905 varop = const0_rtx;
9906 count = 0;
9907 break;
9908 }
9909 }
9910
9911 /* If we discovered we had to complement VAROP, leave. Making a NOT
9912 here would cause an infinite loop. */
9913 if (complement_p)
9914 break;
9915
9916 /* An arithmetic right shift of a quantity known to be -1 or 0
9917 is a no-op. */
9918 if (code == ASHIFTRT
9919 && (num_sign_bit_copies (varop, shift_mode)
9920 == GET_MODE_PRECISION (shift_mode)))
9921 {
9922 count = 0;
9923 break;
9924 }
9925
9926 /* If we are doing an arithmetic right shift and discarding all but
9927 the sign bit copies, this is equivalent to doing a shift by the
9928 bitsize minus one. Convert it into that shift because it will often
9929 allow other simplifications. */
9930
9931 if (code == ASHIFTRT
9932 && (count + num_sign_bit_copies (varop, shift_mode)
9933 >= GET_MODE_PRECISION (shift_mode)))
9934 count = GET_MODE_PRECISION (shift_mode) - 1;
9935
9936 /* We simplify the tests below and elsewhere by converting
9937 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9938 `make_compound_operation' will convert it to an ASHIFTRT for
9939 those machines (such as VAX) that don't have an LSHIFTRT. */
9940 if (code == ASHIFTRT
9941 && val_signbit_known_clear_p (shift_mode,
9942 nonzero_bits (varop, shift_mode)))
9943 code = LSHIFTRT;
9944
9945 if (((code == LSHIFTRT
9946 && HWI_COMPUTABLE_MODE_P (shift_mode)
9947 && !(nonzero_bits (varop, shift_mode) >> count))
9948 || (code == ASHIFT
9949 && HWI_COMPUTABLE_MODE_P (shift_mode)
9950 && !((nonzero_bits (varop, shift_mode) << count)
9951 & GET_MODE_MASK (shift_mode))))
9952 && !side_effects_p (varop))
9953 varop = const0_rtx;
9954
9955 switch (GET_CODE (varop))
9956 {
9957 case SIGN_EXTEND:
9958 case ZERO_EXTEND:
9959 case SIGN_EXTRACT:
9960 case ZERO_EXTRACT:
9961 new_rtx = expand_compound_operation (varop);
9962 if (new_rtx != varop)
9963 {
9964 varop = new_rtx;
9965 continue;
9966 }
9967 break;
9968
9969 case MEM:
9970 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9971 minus the width of a smaller mode, we can do this with a
9972 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9973 if ((code == ASHIFTRT || code == LSHIFTRT)
9974 && ! mode_dependent_address_p (XEXP (varop, 0),
9975 MEM_ADDR_SPACE (varop))
9976 && ! MEM_VOLATILE_P (varop)
9977 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9978 MODE_INT, 1)) != BLKmode)
9979 {
9980 new_rtx = adjust_address_nv (varop, tmode,
9981 BYTES_BIG_ENDIAN ? 0
9982 : count / BITS_PER_UNIT);
9983
9984 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9985 : ZERO_EXTEND, mode, new_rtx);
9986 count = 0;
9987 continue;
9988 }
9989 break;
9990
9991 case SUBREG:
9992 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9993 the same number of words as what we've seen so far. Then store
9994 the widest mode in MODE. */
9995 if (subreg_lowpart_p (varop)
9996 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9997 > GET_MODE_SIZE (GET_MODE (varop)))
9998 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9999 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10000 == mode_words
10001 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10002 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10003 {
10004 varop = SUBREG_REG (varop);
10005 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10006 mode = GET_MODE (varop);
10007 continue;
10008 }
10009 break;
10010
10011 case MULT:
10012 /* Some machines use MULT instead of ASHIFT because MULT
10013 is cheaper. But it is still better on those machines to
10014 merge two shifts into one. */
10015 if (CONST_INT_P (XEXP (varop, 1))
10016 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10017 {
10018 varop
10019 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10020 XEXP (varop, 0),
10021 GEN_INT (exact_log2 (
10022 UINTVAL (XEXP (varop, 1)))));
10023 continue;
10024 }
10025 break;
10026
10027 case UDIV:
10028 /* Similar, for when divides are cheaper. */
10029 if (CONST_INT_P (XEXP (varop, 1))
10030 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10031 {
10032 varop
10033 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10034 XEXP (varop, 0),
10035 GEN_INT (exact_log2 (
10036 UINTVAL (XEXP (varop, 1)))));
10037 continue;
10038 }
10039 break;
10040
10041 case ASHIFTRT:
10042 /* If we are extracting just the sign bit of an arithmetic
10043 right shift, that shift is not needed. However, the sign
10044 bit of a wider mode may be different from what would be
10045 interpreted as the sign bit in a narrower mode, so, if
10046 the result is narrower, don't discard the shift. */
10047 if (code == LSHIFTRT
10048 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10049 && (GET_MODE_BITSIZE (result_mode)
10050 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10051 {
10052 varop = XEXP (varop, 0);
10053 continue;
10054 }
10055
10056 /* ... fall through ... */
10057
10058 case LSHIFTRT:
10059 case ASHIFT:
10060 case ROTATE:
10061 /* Here we have two nested shifts. The result is usually the
10062 AND of a new shift with a mask. We compute the result below. */
10063 if (CONST_INT_P (XEXP (varop, 1))
10064 && INTVAL (XEXP (varop, 1)) >= 0
10065 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10066 && HWI_COMPUTABLE_MODE_P (result_mode)
10067 && HWI_COMPUTABLE_MODE_P (mode)
10068 && !VECTOR_MODE_P (result_mode))
10069 {
10070 enum rtx_code first_code = GET_CODE (varop);
10071 unsigned int first_count = INTVAL (XEXP (varop, 1));
10072 unsigned HOST_WIDE_INT mask;
10073 rtx mask_rtx;
10074
10075 /* We have one common special case. We can't do any merging if
10076 the inner code is an ASHIFTRT of a smaller mode. However, if
10077 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10078 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10079 we can convert it to
10080 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10081 This simplifies certain SIGN_EXTEND operations. */
10082 if (code == ASHIFT && first_code == ASHIFTRT
10083 && count == (GET_MODE_PRECISION (result_mode)
10084 - GET_MODE_PRECISION (GET_MODE (varop))))
10085 {
10086 /* C3 has the low-order C1 bits zero. */
10087
10088 mask = GET_MODE_MASK (mode)
10089 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10090
10091 varop = simplify_and_const_int (NULL_RTX, result_mode,
10092 XEXP (varop, 0), mask);
10093 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10094 varop, count);
10095 count = first_count;
10096 code = ASHIFTRT;
10097 continue;
10098 }
10099
10100 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10101 than C1 high-order bits equal to the sign bit, we can convert
10102 this to either an ASHIFT or an ASHIFTRT depending on the
10103 two counts.
10104
10105 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10106
10107 if (code == ASHIFTRT && first_code == ASHIFT
10108 && GET_MODE (varop) == shift_mode
10109 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10110 > first_count))
10111 {
10112 varop = XEXP (varop, 0);
10113 count -= first_count;
10114 if (count < 0)
10115 {
10116 count = -count;
10117 code = ASHIFT;
10118 }
10119
10120 continue;
10121 }
10122
10123 /* There are some cases we can't do. If CODE is ASHIFTRT,
10124 we can only do this if FIRST_CODE is also ASHIFTRT.
10125
10126 We can't do the case when CODE is ROTATE and FIRST_CODE is
10127 ASHIFTRT.
10128
10129 If the mode of this shift is not the mode of the outer shift,
10130 we can't do this if either shift is a right shift or ROTATE.
10131
10132 Finally, we can't do any of these if the mode is too wide
10133 unless the codes are the same.
10134
10135 Handle the case where the shift codes are the same
10136 first. */
10137
10138 if (code == first_code)
10139 {
10140 if (GET_MODE (varop) != result_mode
10141 && (code == ASHIFTRT || code == LSHIFTRT
10142 || code == ROTATE))
10143 break;
10144
10145 count += first_count;
10146 varop = XEXP (varop, 0);
10147 continue;
10148 }
10149
10150 if (code == ASHIFTRT
10151 || (code == ROTATE && first_code == ASHIFTRT)
10152 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10153 || (GET_MODE (varop) != result_mode
10154 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10155 || first_code == ROTATE
10156 || code == ROTATE)))
10157 break;
10158
10159 /* To compute the mask to apply after the shift, shift the
10160 nonzero bits of the inner shift the same way the
10161 outer shift will. */
10162
10163 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10164 result_mode);
10165
10166 mask_rtx
10167 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10168 GEN_INT (count));
10169
10170 /* Give up if we can't compute an outer operation to use. */
10171 if (mask_rtx == 0
10172 || !CONST_INT_P (mask_rtx)
10173 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10174 INTVAL (mask_rtx),
10175 result_mode, &complement_p))
10176 break;
10177
10178 /* If the shifts are in the same direction, we add the
10179 counts. Otherwise, we subtract them. */
10180 if ((code == ASHIFTRT || code == LSHIFTRT)
10181 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10182 count += first_count;
10183 else
10184 count -= first_count;
10185
10186 /* If COUNT is positive, the new shift is usually CODE,
10187 except for the two exceptions below, in which case it is
10188 FIRST_CODE. If the count is negative, FIRST_CODE should
10189 always be used */
10190 if (count > 0
10191 && ((first_code == ROTATE && code == ASHIFT)
10192 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10193 code = first_code;
10194 else if (count < 0)
10195 code = first_code, count = -count;
10196
10197 varop = XEXP (varop, 0);
10198 continue;
10199 }
10200
10201 /* If we have (A << B << C) for any shift, we can convert this to
10202 (A << C << B). This wins if A is a constant. Only try this if
10203 B is not a constant. */
10204
10205 else if (GET_CODE (varop) == code
10206 && CONST_INT_P (XEXP (varop, 0))
10207 && !CONST_INT_P (XEXP (varop, 1)))
10208 {
10209 rtx new_rtx = simplify_const_binary_operation (code, mode,
10210 XEXP (varop, 0),
10211 GEN_INT (count));
10212 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10213 count = 0;
10214 continue;
10215 }
10216 break;
10217
10218 case NOT:
10219 if (VECTOR_MODE_P (mode))
10220 break;
10221
10222 /* Make this fit the case below. */
10223 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10224 continue;
10225
10226 case IOR:
10227 case AND:
10228 case XOR:
10229 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10230 with C the size of VAROP - 1 and the shift is logical if
10231 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10232 we have an (le X 0) operation. If we have an arithmetic shift
10233 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10234 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10235
10236 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10237 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10238 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10239 && (code == LSHIFTRT || code == ASHIFTRT)
10240 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10241 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10242 {
10243 count = 0;
10244 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10245 const0_rtx);
10246
10247 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10248 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10249
10250 continue;
10251 }
10252
10253 /* If we have (shift (logical)), move the logical to the outside
10254 to allow it to possibly combine with another logical and the
10255 shift to combine with another shift. This also canonicalizes to
10256 what a ZERO_EXTRACT looks like. Also, some machines have
10257 (and (shift)) insns. */
10258
10259 if (CONST_INT_P (XEXP (varop, 1))
10260 /* We can't do this if we have (ashiftrt (xor)) and the
10261 constant has its sign bit set in shift_mode. */
10262 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10263 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10264 shift_mode))
10265 && (new_rtx = simplify_const_binary_operation
10266 (code, result_mode,
10267 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10268 GEN_INT (count))) != 0
10269 && CONST_INT_P (new_rtx)
10270 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10271 INTVAL (new_rtx), result_mode, &complement_p))
10272 {
10273 varop = XEXP (varop, 0);
10274 continue;
10275 }
10276
10277 /* If we can't do that, try to simplify the shift in each arm of the
10278 logical expression, make a new logical expression, and apply
10279 the inverse distributive law. This also can't be done
10280 for some (ashiftrt (xor)). */
10281 if (CONST_INT_P (XEXP (varop, 1))
10282 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10283 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10284 shift_mode)))
10285 {
10286 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10287 XEXP (varop, 0), count);
10288 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10289 XEXP (varop, 1), count);
10290
10291 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10292 lhs, rhs);
10293 varop = apply_distributive_law (varop);
10294
10295 count = 0;
10296 continue;
10297 }
10298 break;
10299
10300 case EQ:
10301 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10302 says that the sign bit can be tested, FOO has mode MODE, C is
10303 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10304 that may be nonzero. */
10305 if (code == LSHIFTRT
10306 && XEXP (varop, 1) == const0_rtx
10307 && GET_MODE (XEXP (varop, 0)) == result_mode
10308 && count == (GET_MODE_PRECISION (result_mode) - 1)
10309 && HWI_COMPUTABLE_MODE_P (result_mode)
10310 && STORE_FLAG_VALUE == -1
10311 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10312 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10313 &complement_p))
10314 {
10315 varop = XEXP (varop, 0);
10316 count = 0;
10317 continue;
10318 }
10319 break;
10320
10321 case NEG:
10322 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10323 than the number of bits in the mode is equivalent to A. */
10324 if (code == LSHIFTRT
10325 && count == (GET_MODE_PRECISION (result_mode) - 1)
10326 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10327 {
10328 varop = XEXP (varop, 0);
10329 count = 0;
10330 continue;
10331 }
10332
10333 /* NEG commutes with ASHIFT since it is multiplication. Move the
10334 NEG outside to allow shifts to combine. */
10335 if (code == ASHIFT
10336 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10337 &complement_p))
10338 {
10339 varop = XEXP (varop, 0);
10340 continue;
10341 }
10342 break;
10343
10344 case PLUS:
10345 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10346 is one less than the number of bits in the mode is
10347 equivalent to (xor A 1). */
10348 if (code == LSHIFTRT
10349 && count == (GET_MODE_PRECISION (result_mode) - 1)
10350 && XEXP (varop, 1) == constm1_rtx
10351 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10352 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10353 &complement_p))
10354 {
10355 count = 0;
10356 varop = XEXP (varop, 0);
10357 continue;
10358 }
10359
10360 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10361 that might be nonzero in BAR are those being shifted out and those
10362 bits are known zero in FOO, we can replace the PLUS with FOO.
10363 Similarly in the other operand order. This code occurs when
10364 we are computing the size of a variable-size array. */
10365
10366 if ((code == ASHIFTRT || code == LSHIFTRT)
10367 && count < HOST_BITS_PER_WIDE_INT
10368 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10369 && (nonzero_bits (XEXP (varop, 1), result_mode)
10370 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10371 {
10372 varop = XEXP (varop, 0);
10373 continue;
10374 }
10375 else if ((code == ASHIFTRT || code == LSHIFTRT)
10376 && count < HOST_BITS_PER_WIDE_INT
10377 && HWI_COMPUTABLE_MODE_P (result_mode)
10378 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10379 >> count)
10380 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10381 & nonzero_bits (XEXP (varop, 1),
10382 result_mode)))
10383 {
10384 varop = XEXP (varop, 1);
10385 continue;
10386 }
10387
10388 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10389 if (code == ASHIFT
10390 && CONST_INT_P (XEXP (varop, 1))
10391 && (new_rtx = simplify_const_binary_operation
10392 (ASHIFT, result_mode,
10393 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10394 GEN_INT (count))) != 0
10395 && CONST_INT_P (new_rtx)
10396 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10397 INTVAL (new_rtx), result_mode, &complement_p))
10398 {
10399 varop = XEXP (varop, 0);
10400 continue;
10401 }
10402
10403 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10404 signbit', and attempt to change the PLUS to an XOR and move it to
10405 the outer operation as is done above in the AND/IOR/XOR case
10406 leg for shift(logical). See details in logical handling above
10407 for reasoning in doing so. */
10408 if (code == LSHIFTRT
10409 && CONST_INT_P (XEXP (varop, 1))
10410 && mode_signbit_p (result_mode, XEXP (varop, 1))
10411 && (new_rtx = simplify_const_binary_operation
10412 (code, result_mode,
10413 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10414 GEN_INT (count))) != 0
10415 && CONST_INT_P (new_rtx)
10416 && merge_outer_ops (&outer_op, &outer_const, XOR,
10417 INTVAL (new_rtx), result_mode, &complement_p))
10418 {
10419 varop = XEXP (varop, 0);
10420 continue;
10421 }
10422
10423 break;
10424
10425 case MINUS:
10426 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10427 with C the size of VAROP - 1 and the shift is logical if
10428 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10429 we have a (gt X 0) operation. If the shift is arithmetic with
10430 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10431 we have a (neg (gt X 0)) operation. */
10432
10433 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10434 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10435 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10436 && (code == LSHIFTRT || code == ASHIFTRT)
10437 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10438 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10439 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10440 {
10441 count = 0;
10442 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10443 const0_rtx);
10444
10445 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10446 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10447
10448 continue;
10449 }
10450 break;
10451
10452 case TRUNCATE:
10453 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10454 if the truncate does not affect the value. */
10455 if (code == LSHIFTRT
10456 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10457 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10458 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10459 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10460 - GET_MODE_PRECISION (GET_MODE (varop)))))
10461 {
10462 rtx varop_inner = XEXP (varop, 0);
10463
10464 varop_inner
10465 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10466 XEXP (varop_inner, 0),
10467 GEN_INT
10468 (count + INTVAL (XEXP (varop_inner, 1))));
10469 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10470 count = 0;
10471 continue;
10472 }
10473 break;
10474
10475 default:
10476 break;
10477 }
10478
10479 break;
10480 }
10481
10482 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10483 outer_op, outer_const);
10484
10485 /* We have now finished analyzing the shift. The result should be
10486 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10487 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10488 to the result of the shift. OUTER_CONST is the relevant constant,
10489 but we must turn off all bits turned off in the shift. */
10490
10491 if (outer_op == UNKNOWN
10492 && orig_code == code && orig_count == count
10493 && varop == orig_varop
10494 && shift_mode == GET_MODE (varop))
10495 return NULL_RTX;
10496
10497 /* Make a SUBREG if necessary. If we can't make it, fail. */
10498 varop = gen_lowpart (shift_mode, varop);
10499 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10500 return NULL_RTX;
10501
10502 /* If we have an outer operation and we just made a shift, it is
10503 possible that we could have simplified the shift were it not
10504 for the outer operation. So try to do the simplification
10505 recursively. */
10506
10507 if (outer_op != UNKNOWN)
10508 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10509 else
10510 x = NULL_RTX;
10511
10512 if (x == NULL_RTX)
10513 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10514
10515 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10516 turn off all the bits that the shift would have turned off. */
10517 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10518 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10519 GET_MODE_MASK (result_mode) >> orig_count);
10520
10521 /* Do the remainder of the processing in RESULT_MODE. */
10522 x = gen_lowpart_or_truncate (result_mode, x);
10523
10524 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10525 operation. */
10526 if (complement_p)
10527 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10528
10529 if (outer_op != UNKNOWN)
10530 {
10531 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10532 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10533 outer_const = trunc_int_for_mode (outer_const, result_mode);
10534
10535 if (outer_op == AND)
10536 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10537 else if (outer_op == SET)
10538 {
10539 /* This means that we have determined that the result is
10540 equivalent to a constant. This should be rare. */
10541 if (!side_effects_p (x))
10542 x = GEN_INT (outer_const);
10543 }
10544 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10545 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10546 else
10547 x = simplify_gen_binary (outer_op, result_mode, x,
10548 GEN_INT (outer_const));
10549 }
10550
10551 return x;
10552 }
10553
10554 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10555 The result of the shift is RESULT_MODE. If we cannot simplify it,
10556 return X or, if it is NULL, synthesize the expression with
10557 simplify_gen_binary. Otherwise, return a simplified value.
10558
10559 The shift is normally computed in the widest mode we find in VAROP, as
10560 long as it isn't a different number of words than RESULT_MODE. Exceptions
10561 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10562
10563 static rtx
10564 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10565 rtx varop, int count)
10566 {
10567 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10568 if (tem)
10569 return tem;
10570
10571 if (!x)
10572 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10573 if (GET_MODE (x) != result_mode)
10574 x = gen_lowpart (result_mode, x);
10575 return x;
10576 }
10577
10578 \f
10579 /* Like recog, but we receive the address of a pointer to a new pattern.
10580 We try to match the rtx that the pointer points to.
10581 If that fails, we may try to modify or replace the pattern,
10582 storing the replacement into the same pointer object.
10583
10584 Modifications include deletion or addition of CLOBBERs.
10585
10586 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10587 the CLOBBERs are placed.
10588
10589 The value is the final insn code from the pattern ultimately matched,
10590 or -1. */
10591
10592 static int
10593 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10594 {
10595 rtx pat = *pnewpat;
10596 rtx pat_without_clobbers;
10597 int insn_code_number;
10598 int num_clobbers_to_add = 0;
10599 int i;
10600 rtx notes = NULL_RTX;
10601 rtx old_notes, old_pat;
10602 int old_icode;
10603
10604 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10605 we use to indicate that something didn't match. If we find such a
10606 thing, force rejection. */
10607 if (GET_CODE (pat) == PARALLEL)
10608 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10609 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10610 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10611 return -1;
10612
10613 old_pat = PATTERN (insn);
10614 old_notes = REG_NOTES (insn);
10615 PATTERN (insn) = pat;
10616 REG_NOTES (insn) = NULL_RTX;
10617
10618 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10619 if (dump_file && (dump_flags & TDF_DETAILS))
10620 {
10621 if (insn_code_number < 0)
10622 fputs ("Failed to match this instruction:\n", dump_file);
10623 else
10624 fputs ("Successfully matched this instruction:\n", dump_file);
10625 print_rtl_single (dump_file, pat);
10626 }
10627
10628 /* If it isn't, there is the possibility that we previously had an insn
10629 that clobbered some register as a side effect, but the combined
10630 insn doesn't need to do that. So try once more without the clobbers
10631 unless this represents an ASM insn. */
10632
10633 if (insn_code_number < 0 && ! check_asm_operands (pat)
10634 && GET_CODE (pat) == PARALLEL)
10635 {
10636 int pos;
10637
10638 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10639 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10640 {
10641 if (i != pos)
10642 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10643 pos++;
10644 }
10645
10646 SUBST_INT (XVECLEN (pat, 0), pos);
10647
10648 if (pos == 1)
10649 pat = XVECEXP (pat, 0, 0);
10650
10651 PATTERN (insn) = pat;
10652 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10653 if (dump_file && (dump_flags & TDF_DETAILS))
10654 {
10655 if (insn_code_number < 0)
10656 fputs ("Failed to match this instruction:\n", dump_file);
10657 else
10658 fputs ("Successfully matched this instruction:\n", dump_file);
10659 print_rtl_single (dump_file, pat);
10660 }
10661 }
10662
10663 pat_without_clobbers = pat;
10664
10665 PATTERN (insn) = old_pat;
10666 REG_NOTES (insn) = old_notes;
10667
10668 /* Recognize all noop sets, these will be killed by followup pass. */
10669 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10670 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10671
10672 /* If we had any clobbers to add, make a new pattern than contains
10673 them. Then check to make sure that all of them are dead. */
10674 if (num_clobbers_to_add)
10675 {
10676 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10677 rtvec_alloc (GET_CODE (pat) == PARALLEL
10678 ? (XVECLEN (pat, 0)
10679 + num_clobbers_to_add)
10680 : num_clobbers_to_add + 1));
10681
10682 if (GET_CODE (pat) == PARALLEL)
10683 for (i = 0; i < XVECLEN (pat, 0); i++)
10684 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10685 else
10686 XVECEXP (newpat, 0, 0) = pat;
10687
10688 add_clobbers (newpat, insn_code_number);
10689
10690 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10691 i < XVECLEN (newpat, 0); i++)
10692 {
10693 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10694 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10695 return -1;
10696 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10697 {
10698 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10699 notes = alloc_reg_note (REG_UNUSED,
10700 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10701 }
10702 }
10703 pat = newpat;
10704 }
10705
10706 if (insn_code_number >= 0
10707 && insn_code_number != NOOP_MOVE_INSN_CODE)
10708 {
10709 old_pat = PATTERN (insn);
10710 old_notes = REG_NOTES (insn);
10711 old_icode = INSN_CODE (insn);
10712 PATTERN (insn) = pat;
10713 REG_NOTES (insn) = notes;
10714
10715 /* Allow targets to reject combined insn. */
10716 if (!targetm.legitimate_combined_insn (insn))
10717 {
10718 if (dump_file && (dump_flags & TDF_DETAILS))
10719 fputs ("Instruction not appropriate for target.",
10720 dump_file);
10721
10722 /* Callers expect recog_for_combine to strip
10723 clobbers from the pattern on failure. */
10724 pat = pat_without_clobbers;
10725 notes = NULL_RTX;
10726
10727 insn_code_number = -1;
10728 }
10729
10730 PATTERN (insn) = old_pat;
10731 REG_NOTES (insn) = old_notes;
10732 INSN_CODE (insn) = old_icode;
10733 }
10734
10735 *pnewpat = pat;
10736 *pnotes = notes;
10737
10738 return insn_code_number;
10739 }
10740 \f
10741 /* Like gen_lowpart_general but for use by combine. In combine it
10742 is not possible to create any new pseudoregs. However, it is
10743 safe to create invalid memory addresses, because combine will
10744 try to recognize them and all they will do is make the combine
10745 attempt fail.
10746
10747 If for some reason this cannot do its job, an rtx
10748 (clobber (const_int 0)) is returned.
10749 An insn containing that will not be recognized. */
10750
10751 static rtx
10752 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10753 {
10754 enum machine_mode imode = GET_MODE (x);
10755 unsigned int osize = GET_MODE_SIZE (omode);
10756 unsigned int isize = GET_MODE_SIZE (imode);
10757 rtx result;
10758
10759 if (omode == imode)
10760 return x;
10761
10762 /* We can only support MODE being wider than a word if X is a
10763 constant integer or has a mode the same size. */
10764 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10765 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10766 goto fail;
10767
10768 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10769 won't know what to do. So we will strip off the SUBREG here and
10770 process normally. */
10771 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10772 {
10773 x = SUBREG_REG (x);
10774
10775 /* For use in case we fall down into the address adjustments
10776 further below, we need to adjust the known mode and size of
10777 x; imode and isize, since we just adjusted x. */
10778 imode = GET_MODE (x);
10779
10780 if (imode == omode)
10781 return x;
10782
10783 isize = GET_MODE_SIZE (imode);
10784 }
10785
10786 result = gen_lowpart_common (omode, x);
10787
10788 if (result)
10789 return result;
10790
10791 if (MEM_P (x))
10792 {
10793 int offset = 0;
10794
10795 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10796 address. */
10797 if (MEM_VOLATILE_P (x)
10798 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10799 goto fail;
10800
10801 /* If we want to refer to something bigger than the original memref,
10802 generate a paradoxical subreg instead. That will force a reload
10803 of the original memref X. */
10804 if (isize < osize)
10805 return gen_rtx_SUBREG (omode, x, 0);
10806
10807 if (WORDS_BIG_ENDIAN)
10808 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10809
10810 /* Adjust the address so that the address-after-the-data is
10811 unchanged. */
10812 if (BYTES_BIG_ENDIAN)
10813 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10814
10815 return adjust_address_nv (x, omode, offset);
10816 }
10817
10818 /* If X is a comparison operator, rewrite it in a new mode. This
10819 probably won't match, but may allow further simplifications. */
10820 else if (COMPARISON_P (x))
10821 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10822
10823 /* If we couldn't simplify X any other way, just enclose it in a
10824 SUBREG. Normally, this SUBREG won't match, but some patterns may
10825 include an explicit SUBREG or we may simplify it further in combine. */
10826 else
10827 {
10828 int offset = 0;
10829 rtx res;
10830
10831 offset = subreg_lowpart_offset (omode, imode);
10832 if (imode == VOIDmode)
10833 {
10834 imode = int_mode_for_mode (omode);
10835 x = gen_lowpart_common (imode, x);
10836 if (x == NULL)
10837 goto fail;
10838 }
10839 res = simplify_gen_subreg (omode, x, imode, offset);
10840 if (res)
10841 return res;
10842 }
10843
10844 fail:
10845 return gen_rtx_CLOBBER (omode, const0_rtx);
10846 }
10847 \f
10848 /* Try to simplify a comparison between OP0 and a constant OP1,
10849 where CODE is the comparison code that will be tested, into a
10850 (CODE OP0 const0_rtx) form.
10851
10852 The result is a possibly different comparison code to use.
10853 *POP1 may be updated. */
10854
10855 static enum rtx_code
10856 simplify_compare_const (enum rtx_code code, enum machine_mode mode,
10857 rtx op0, rtx *pop1)
10858 {
10859 unsigned int mode_width = GET_MODE_PRECISION (mode);
10860 HOST_WIDE_INT const_op = INTVAL (*pop1);
10861
10862 /* Get the constant we are comparing against and turn off all bits
10863 not on in our mode. */
10864 if (mode != VOIDmode)
10865 const_op = trunc_int_for_mode (const_op, mode);
10866
10867 /* If we are comparing against a constant power of two and the value
10868 being compared can only have that single bit nonzero (e.g., it was
10869 `and'ed with that bit), we can replace this with a comparison
10870 with zero. */
10871 if (const_op
10872 && (code == EQ || code == NE || code == GE || code == GEU
10873 || code == LT || code == LTU)
10874 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10875 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
10876 && (nonzero_bits (op0, mode)
10877 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
10878 {
10879 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10880 const_op = 0;
10881 }
10882
10883 /* Similarly, if we are comparing a value known to be either -1 or
10884 0 with -1, change it to the opposite comparison against zero. */
10885 if (const_op == -1
10886 && (code == EQ || code == NE || code == GT || code == LE
10887 || code == GEU || code == LTU)
10888 && num_sign_bit_copies (op0, mode) == mode_width)
10889 {
10890 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10891 const_op = 0;
10892 }
10893
10894 /* Do some canonicalizations based on the comparison code. We prefer
10895 comparisons against zero and then prefer equality comparisons.
10896 If we can reduce the size of a constant, we will do that too. */
10897 switch (code)
10898 {
10899 case LT:
10900 /* < C is equivalent to <= (C - 1) */
10901 if (const_op > 0)
10902 {
10903 const_op -= 1;
10904 code = LE;
10905 /* ... fall through to LE case below. */
10906 }
10907 else
10908 break;
10909
10910 case LE:
10911 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10912 if (const_op < 0)
10913 {
10914 const_op += 1;
10915 code = LT;
10916 }
10917
10918 /* If we are doing a <= 0 comparison on a value known to have
10919 a zero sign bit, we can replace this with == 0. */
10920 else if (const_op == 0
10921 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10922 && (nonzero_bits (op0, mode)
10923 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10924 == 0)
10925 code = EQ;
10926 break;
10927
10928 case GE:
10929 /* >= C is equivalent to > (C - 1). */
10930 if (const_op > 0)
10931 {
10932 const_op -= 1;
10933 code = GT;
10934 /* ... fall through to GT below. */
10935 }
10936 else
10937 break;
10938
10939 case GT:
10940 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10941 if (const_op < 0)
10942 {
10943 const_op += 1;
10944 code = GE;
10945 }
10946
10947 /* If we are doing a > 0 comparison on a value known to have
10948 a zero sign bit, we can replace this with != 0. */
10949 else if (const_op == 0
10950 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10951 && (nonzero_bits (op0, mode)
10952 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10953 == 0)
10954 code = NE;
10955 break;
10956
10957 case LTU:
10958 /* < C is equivalent to <= (C - 1). */
10959 if (const_op > 0)
10960 {
10961 const_op -= 1;
10962 code = LEU;
10963 /* ... fall through ... */
10964 }
10965 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10966 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10967 && (unsigned HOST_WIDE_INT) const_op
10968 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10969 {
10970 const_op = 0;
10971 code = GE;
10972 break;
10973 }
10974 else
10975 break;
10976
10977 case LEU:
10978 /* unsigned <= 0 is equivalent to == 0 */
10979 if (const_op == 0)
10980 code = EQ;
10981 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10982 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10983 && (unsigned HOST_WIDE_INT) const_op
10984 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10985 {
10986 const_op = 0;
10987 code = GE;
10988 }
10989 break;
10990
10991 case GEU:
10992 /* >= C is equivalent to > (C - 1). */
10993 if (const_op > 1)
10994 {
10995 const_op -= 1;
10996 code = GTU;
10997 /* ... fall through ... */
10998 }
10999
11000 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11001 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11002 && (unsigned HOST_WIDE_INT) const_op
11003 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11004 {
11005 const_op = 0;
11006 code = LT;
11007 break;
11008 }
11009 else
11010 break;
11011
11012 case GTU:
11013 /* unsigned > 0 is equivalent to != 0 */
11014 if (const_op == 0)
11015 code = NE;
11016 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11017 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11018 && (unsigned HOST_WIDE_INT) const_op
11019 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11020 {
11021 const_op = 0;
11022 code = LT;
11023 }
11024 break;
11025
11026 default:
11027 break;
11028 }
11029
11030 *pop1 = GEN_INT (const_op);
11031 return code;
11032 }
11033 \f
11034 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11035 comparison code that will be tested.
11036
11037 The result is a possibly different comparison code to use. *POP0 and
11038 *POP1 may be updated.
11039
11040 It is possible that we might detect that a comparison is either always
11041 true or always false. However, we do not perform general constant
11042 folding in combine, so this knowledge isn't useful. Such tautologies
11043 should have been detected earlier. Hence we ignore all such cases. */
11044
11045 static enum rtx_code
11046 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11047 {
11048 rtx op0 = *pop0;
11049 rtx op1 = *pop1;
11050 rtx tem, tem1;
11051 int i;
11052 enum machine_mode mode, tmode;
11053
11054 /* Try a few ways of applying the same transformation to both operands. */
11055 while (1)
11056 {
11057 #ifndef WORD_REGISTER_OPERATIONS
11058 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11059 so check specially. */
11060 if (code != GTU && code != GEU && code != LTU && code != LEU
11061 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11062 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11063 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11064 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11065 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11066 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11067 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11068 && CONST_INT_P (XEXP (op0, 1))
11069 && XEXP (op0, 1) == XEXP (op1, 1)
11070 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11071 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11072 && (INTVAL (XEXP (op0, 1))
11073 == (GET_MODE_PRECISION (GET_MODE (op0))
11074 - (GET_MODE_PRECISION
11075 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11076 {
11077 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11078 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11079 }
11080 #endif
11081
11082 /* If both operands are the same constant shift, see if we can ignore the
11083 shift. We can if the shift is a rotate or if the bits shifted out of
11084 this shift are known to be zero for both inputs and if the type of
11085 comparison is compatible with the shift. */
11086 if (GET_CODE (op0) == GET_CODE (op1)
11087 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11088 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11089 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11090 && (code != GT && code != LT && code != GE && code != LE))
11091 || (GET_CODE (op0) == ASHIFTRT
11092 && (code != GTU && code != LTU
11093 && code != GEU && code != LEU)))
11094 && CONST_INT_P (XEXP (op0, 1))
11095 && INTVAL (XEXP (op0, 1)) >= 0
11096 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11097 && XEXP (op0, 1) == XEXP (op1, 1))
11098 {
11099 enum machine_mode mode = GET_MODE (op0);
11100 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11101 int shift_count = INTVAL (XEXP (op0, 1));
11102
11103 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11104 mask &= (mask >> shift_count) << shift_count;
11105 else if (GET_CODE (op0) == ASHIFT)
11106 mask = (mask & (mask << shift_count)) >> shift_count;
11107
11108 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11109 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11110 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11111 else
11112 break;
11113 }
11114
11115 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11116 SUBREGs are of the same mode, and, in both cases, the AND would
11117 be redundant if the comparison was done in the narrower mode,
11118 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11119 and the operand's possibly nonzero bits are 0xffffff01; in that case
11120 if we only care about QImode, we don't need the AND). This case
11121 occurs if the output mode of an scc insn is not SImode and
11122 STORE_FLAG_VALUE == 1 (e.g., the 386).
11123
11124 Similarly, check for a case where the AND's are ZERO_EXTEND
11125 operations from some narrower mode even though a SUBREG is not
11126 present. */
11127
11128 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11129 && CONST_INT_P (XEXP (op0, 1))
11130 && CONST_INT_P (XEXP (op1, 1)))
11131 {
11132 rtx inner_op0 = XEXP (op0, 0);
11133 rtx inner_op1 = XEXP (op1, 0);
11134 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11135 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11136 int changed = 0;
11137
11138 if (paradoxical_subreg_p (inner_op0)
11139 && GET_CODE (inner_op1) == SUBREG
11140 && (GET_MODE (SUBREG_REG (inner_op0))
11141 == GET_MODE (SUBREG_REG (inner_op1)))
11142 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11143 <= HOST_BITS_PER_WIDE_INT)
11144 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11145 GET_MODE (SUBREG_REG (inner_op0)))))
11146 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11147 GET_MODE (SUBREG_REG (inner_op1))))))
11148 {
11149 op0 = SUBREG_REG (inner_op0);
11150 op1 = SUBREG_REG (inner_op1);
11151
11152 /* The resulting comparison is always unsigned since we masked
11153 off the original sign bit. */
11154 code = unsigned_condition (code);
11155
11156 changed = 1;
11157 }
11158
11159 else if (c0 == c1)
11160 for (tmode = GET_CLASS_NARROWEST_MODE
11161 (GET_MODE_CLASS (GET_MODE (op0)));
11162 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11163 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11164 {
11165 op0 = gen_lowpart (tmode, inner_op0);
11166 op1 = gen_lowpart (tmode, inner_op1);
11167 code = unsigned_condition (code);
11168 changed = 1;
11169 break;
11170 }
11171
11172 if (! changed)
11173 break;
11174 }
11175
11176 /* If both operands are NOT, we can strip off the outer operation
11177 and adjust the comparison code for swapped operands; similarly for
11178 NEG, except that this must be an equality comparison. */
11179 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11180 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11181 && (code == EQ || code == NE)))
11182 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11183
11184 else
11185 break;
11186 }
11187
11188 /* If the first operand is a constant, swap the operands and adjust the
11189 comparison code appropriately, but don't do this if the second operand
11190 is already a constant integer. */
11191 if (swap_commutative_operands_p (op0, op1))
11192 {
11193 tem = op0, op0 = op1, op1 = tem;
11194 code = swap_condition (code);
11195 }
11196
11197 /* We now enter a loop during which we will try to simplify the comparison.
11198 For the most part, we only are concerned with comparisons with zero,
11199 but some things may really be comparisons with zero but not start
11200 out looking that way. */
11201
11202 while (CONST_INT_P (op1))
11203 {
11204 enum machine_mode mode = GET_MODE (op0);
11205 unsigned int mode_width = GET_MODE_PRECISION (mode);
11206 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11207 int equality_comparison_p;
11208 int sign_bit_comparison_p;
11209 int unsigned_comparison_p;
11210 HOST_WIDE_INT const_op;
11211
11212 /* We only want to handle integral modes. This catches VOIDmode,
11213 CCmode, and the floating-point modes. An exception is that we
11214 can handle VOIDmode if OP0 is a COMPARE or a comparison
11215 operation. */
11216
11217 if (GET_MODE_CLASS (mode) != MODE_INT
11218 && ! (mode == VOIDmode
11219 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11220 break;
11221
11222 /* Try to simplify the compare to constant, possibly changing the
11223 comparison op, and/or changing op1 to zero. */
11224 code = simplify_compare_const (code, mode, op0, &op1);
11225 const_op = INTVAL (op1);
11226
11227 /* Compute some predicates to simplify code below. */
11228
11229 equality_comparison_p = (code == EQ || code == NE);
11230 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11231 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11232 || code == GEU);
11233
11234 /* If this is a sign bit comparison and we can do arithmetic in
11235 MODE, say that we will only be needing the sign bit of OP0. */
11236 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11237 op0 = force_to_mode (op0, mode,
11238 (unsigned HOST_WIDE_INT) 1
11239 << (GET_MODE_PRECISION (mode) - 1),
11240 0);
11241
11242 /* Now try cases based on the opcode of OP0. If none of the cases
11243 does a "continue", we exit this loop immediately after the
11244 switch. */
11245
11246 switch (GET_CODE (op0))
11247 {
11248 case ZERO_EXTRACT:
11249 /* If we are extracting a single bit from a variable position in
11250 a constant that has only a single bit set and are comparing it
11251 with zero, we can convert this into an equality comparison
11252 between the position and the location of the single bit. */
11253 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11254 have already reduced the shift count modulo the word size. */
11255 if (!SHIFT_COUNT_TRUNCATED
11256 && CONST_INT_P (XEXP (op0, 0))
11257 && XEXP (op0, 1) == const1_rtx
11258 && equality_comparison_p && const_op == 0
11259 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11260 {
11261 if (BITS_BIG_ENDIAN)
11262 i = BITS_PER_WORD - 1 - i;
11263
11264 op0 = XEXP (op0, 2);
11265 op1 = GEN_INT (i);
11266 const_op = i;
11267
11268 /* Result is nonzero iff shift count is equal to I. */
11269 code = reverse_condition (code);
11270 continue;
11271 }
11272
11273 /* ... fall through ... */
11274
11275 case SIGN_EXTRACT:
11276 tem = expand_compound_operation (op0);
11277 if (tem != op0)
11278 {
11279 op0 = tem;
11280 continue;
11281 }
11282 break;
11283
11284 case NOT:
11285 /* If testing for equality, we can take the NOT of the constant. */
11286 if (equality_comparison_p
11287 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11288 {
11289 op0 = XEXP (op0, 0);
11290 op1 = tem;
11291 continue;
11292 }
11293
11294 /* If just looking at the sign bit, reverse the sense of the
11295 comparison. */
11296 if (sign_bit_comparison_p)
11297 {
11298 op0 = XEXP (op0, 0);
11299 code = (code == GE ? LT : GE);
11300 continue;
11301 }
11302 break;
11303
11304 case NEG:
11305 /* If testing for equality, we can take the NEG of the constant. */
11306 if (equality_comparison_p
11307 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11308 {
11309 op0 = XEXP (op0, 0);
11310 op1 = tem;
11311 continue;
11312 }
11313
11314 /* The remaining cases only apply to comparisons with zero. */
11315 if (const_op != 0)
11316 break;
11317
11318 /* When X is ABS or is known positive,
11319 (neg X) is < 0 if and only if X != 0. */
11320
11321 if (sign_bit_comparison_p
11322 && (GET_CODE (XEXP (op0, 0)) == ABS
11323 || (mode_width <= HOST_BITS_PER_WIDE_INT
11324 && (nonzero_bits (XEXP (op0, 0), mode)
11325 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11326 == 0)))
11327 {
11328 op0 = XEXP (op0, 0);
11329 code = (code == LT ? NE : EQ);
11330 continue;
11331 }
11332
11333 /* If we have NEG of something whose two high-order bits are the
11334 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11335 if (num_sign_bit_copies (op0, mode) >= 2)
11336 {
11337 op0 = XEXP (op0, 0);
11338 code = swap_condition (code);
11339 continue;
11340 }
11341 break;
11342
11343 case ROTATE:
11344 /* If we are testing equality and our count is a constant, we
11345 can perform the inverse operation on our RHS. */
11346 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11347 && (tem = simplify_binary_operation (ROTATERT, mode,
11348 op1, XEXP (op0, 1))) != 0)
11349 {
11350 op0 = XEXP (op0, 0);
11351 op1 = tem;
11352 continue;
11353 }
11354
11355 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11356 a particular bit. Convert it to an AND of a constant of that
11357 bit. This will be converted into a ZERO_EXTRACT. */
11358 if (const_op == 0 && sign_bit_comparison_p
11359 && CONST_INT_P (XEXP (op0, 1))
11360 && mode_width <= HOST_BITS_PER_WIDE_INT)
11361 {
11362 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11363 ((unsigned HOST_WIDE_INT) 1
11364 << (mode_width - 1
11365 - INTVAL (XEXP (op0, 1)))));
11366 code = (code == LT ? NE : EQ);
11367 continue;
11368 }
11369
11370 /* Fall through. */
11371
11372 case ABS:
11373 /* ABS is ignorable inside an equality comparison with zero. */
11374 if (const_op == 0 && equality_comparison_p)
11375 {
11376 op0 = XEXP (op0, 0);
11377 continue;
11378 }
11379 break;
11380
11381 case SIGN_EXTEND:
11382 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11383 (compare FOO CONST) if CONST fits in FOO's mode and we
11384 are either testing inequality or have an unsigned
11385 comparison with ZERO_EXTEND or a signed comparison with
11386 SIGN_EXTEND. But don't do it if we don't have a compare
11387 insn of the given mode, since we'd have to revert it
11388 later on, and then we wouldn't know whether to sign- or
11389 zero-extend. */
11390 mode = GET_MODE (XEXP (op0, 0));
11391 if (GET_MODE_CLASS (mode) == MODE_INT
11392 && ! unsigned_comparison_p
11393 && HWI_COMPUTABLE_MODE_P (mode)
11394 && trunc_int_for_mode (const_op, mode) == const_op
11395 && have_insn_for (COMPARE, mode))
11396 {
11397 op0 = XEXP (op0, 0);
11398 continue;
11399 }
11400 break;
11401
11402 case SUBREG:
11403 /* Check for the case where we are comparing A - C1 with C2, that is
11404
11405 (subreg:MODE (plus (A) (-C1))) op (C2)
11406
11407 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11408 comparison in the wider mode. One of the following two conditions
11409 must be true in order for this to be valid:
11410
11411 1. The mode extension results in the same bit pattern being added
11412 on both sides and the comparison is equality or unsigned. As
11413 C2 has been truncated to fit in MODE, the pattern can only be
11414 all 0s or all 1s.
11415
11416 2. The mode extension results in the sign bit being copied on
11417 each side.
11418
11419 The difficulty here is that we have predicates for A but not for
11420 (A - C1) so we need to check that C1 is within proper bounds so
11421 as to perturbate A as little as possible. */
11422
11423 if (mode_width <= HOST_BITS_PER_WIDE_INT
11424 && subreg_lowpart_p (op0)
11425 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11426 && GET_CODE (SUBREG_REG (op0)) == PLUS
11427 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11428 {
11429 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11430 rtx a = XEXP (SUBREG_REG (op0), 0);
11431 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11432
11433 if ((c1 > 0
11434 && (unsigned HOST_WIDE_INT) c1
11435 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11436 && (equality_comparison_p || unsigned_comparison_p)
11437 /* (A - C1) zero-extends if it is positive and sign-extends
11438 if it is negative, C2 both zero- and sign-extends. */
11439 && ((0 == (nonzero_bits (a, inner_mode)
11440 & ~GET_MODE_MASK (mode))
11441 && const_op >= 0)
11442 /* (A - C1) sign-extends if it is positive and 1-extends
11443 if it is negative, C2 both sign- and 1-extends. */
11444 || (num_sign_bit_copies (a, inner_mode)
11445 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11446 - mode_width)
11447 && const_op < 0)))
11448 || ((unsigned HOST_WIDE_INT) c1
11449 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11450 /* (A - C1) always sign-extends, like C2. */
11451 && num_sign_bit_copies (a, inner_mode)
11452 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11453 - (mode_width - 1))))
11454 {
11455 op0 = SUBREG_REG (op0);
11456 continue;
11457 }
11458 }
11459
11460 /* If the inner mode is narrower and we are extracting the low part,
11461 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11462 if (subreg_lowpart_p (op0)
11463 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11464 /* Fall through */ ;
11465 else
11466 break;
11467
11468 /* ... fall through ... */
11469
11470 case ZERO_EXTEND:
11471 mode = GET_MODE (XEXP (op0, 0));
11472 if (GET_MODE_CLASS (mode) == MODE_INT
11473 && (unsigned_comparison_p || equality_comparison_p)
11474 && HWI_COMPUTABLE_MODE_P (mode)
11475 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11476 && const_op >= 0
11477 && have_insn_for (COMPARE, mode))
11478 {
11479 op0 = XEXP (op0, 0);
11480 continue;
11481 }
11482 break;
11483
11484 case PLUS:
11485 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11486 this for equality comparisons due to pathological cases involving
11487 overflows. */
11488 if (equality_comparison_p
11489 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11490 op1, XEXP (op0, 1))))
11491 {
11492 op0 = XEXP (op0, 0);
11493 op1 = tem;
11494 continue;
11495 }
11496
11497 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11498 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11499 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11500 {
11501 op0 = XEXP (XEXP (op0, 0), 0);
11502 code = (code == LT ? EQ : NE);
11503 continue;
11504 }
11505 break;
11506
11507 case MINUS:
11508 /* We used to optimize signed comparisons against zero, but that
11509 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11510 arrive here as equality comparisons, or (GEU, LTU) are
11511 optimized away. No need to special-case them. */
11512
11513 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11514 (eq B (minus A C)), whichever simplifies. We can only do
11515 this for equality comparisons due to pathological cases involving
11516 overflows. */
11517 if (equality_comparison_p
11518 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11519 XEXP (op0, 1), op1)))
11520 {
11521 op0 = XEXP (op0, 0);
11522 op1 = tem;
11523 continue;
11524 }
11525
11526 if (equality_comparison_p
11527 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11528 XEXP (op0, 0), op1)))
11529 {
11530 op0 = XEXP (op0, 1);
11531 op1 = tem;
11532 continue;
11533 }
11534
11535 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11536 of bits in X minus 1, is one iff X > 0. */
11537 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11538 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11539 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11540 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11541 {
11542 op0 = XEXP (op0, 1);
11543 code = (code == GE ? LE : GT);
11544 continue;
11545 }
11546 break;
11547
11548 case XOR:
11549 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11550 if C is zero or B is a constant. */
11551 if (equality_comparison_p
11552 && 0 != (tem = simplify_binary_operation (XOR, mode,
11553 XEXP (op0, 1), op1)))
11554 {
11555 op0 = XEXP (op0, 0);
11556 op1 = tem;
11557 continue;
11558 }
11559 break;
11560
11561 case EQ: case NE:
11562 case UNEQ: case LTGT:
11563 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11564 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11565 case UNORDERED: case ORDERED:
11566 /* We can't do anything if OP0 is a condition code value, rather
11567 than an actual data value. */
11568 if (const_op != 0
11569 || CC0_P (XEXP (op0, 0))
11570 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11571 break;
11572
11573 /* Get the two operands being compared. */
11574 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11575 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11576 else
11577 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11578
11579 /* Check for the cases where we simply want the result of the
11580 earlier test or the opposite of that result. */
11581 if (code == NE || code == EQ
11582 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11583 && (code == LT || code == GE)))
11584 {
11585 enum rtx_code new_code;
11586 if (code == LT || code == NE)
11587 new_code = GET_CODE (op0);
11588 else
11589 new_code = reversed_comparison_code (op0, NULL);
11590
11591 if (new_code != UNKNOWN)
11592 {
11593 code = new_code;
11594 op0 = tem;
11595 op1 = tem1;
11596 continue;
11597 }
11598 }
11599 break;
11600
11601 case IOR:
11602 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11603 iff X <= 0. */
11604 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11605 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11606 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11607 {
11608 op0 = XEXP (op0, 1);
11609 code = (code == GE ? GT : LE);
11610 continue;
11611 }
11612 break;
11613
11614 case AND:
11615 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11616 will be converted to a ZERO_EXTRACT later. */
11617 if (const_op == 0 && equality_comparison_p
11618 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11619 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11620 {
11621 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11622 XEXP (XEXP (op0, 0), 1));
11623 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11624 continue;
11625 }
11626
11627 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11628 zero and X is a comparison and C1 and C2 describe only bits set
11629 in STORE_FLAG_VALUE, we can compare with X. */
11630 if (const_op == 0 && equality_comparison_p
11631 && mode_width <= HOST_BITS_PER_WIDE_INT
11632 && CONST_INT_P (XEXP (op0, 1))
11633 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11634 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11635 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11636 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11637 {
11638 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11639 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11640 if ((~STORE_FLAG_VALUE & mask) == 0
11641 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11642 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11643 && COMPARISON_P (tem))))
11644 {
11645 op0 = XEXP (XEXP (op0, 0), 0);
11646 continue;
11647 }
11648 }
11649
11650 /* If we are doing an equality comparison of an AND of a bit equal
11651 to the sign bit, replace this with a LT or GE comparison of
11652 the underlying value. */
11653 if (equality_comparison_p
11654 && const_op == 0
11655 && CONST_INT_P (XEXP (op0, 1))
11656 && mode_width <= HOST_BITS_PER_WIDE_INT
11657 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11658 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11659 {
11660 op0 = XEXP (op0, 0);
11661 code = (code == EQ ? GE : LT);
11662 continue;
11663 }
11664
11665 /* If this AND operation is really a ZERO_EXTEND from a narrower
11666 mode, the constant fits within that mode, and this is either an
11667 equality or unsigned comparison, try to do this comparison in
11668 the narrower mode.
11669
11670 Note that in:
11671
11672 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11673 -> (ne:DI (reg:SI 4) (const_int 0))
11674
11675 unless TRULY_NOOP_TRUNCATION allows it or the register is
11676 known to hold a value of the required mode the
11677 transformation is invalid. */
11678 if ((equality_comparison_p || unsigned_comparison_p)
11679 && CONST_INT_P (XEXP (op0, 1))
11680 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11681 & GET_MODE_MASK (mode))
11682 + 1)) >= 0
11683 && const_op >> i == 0
11684 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11685 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11686 || (REG_P (XEXP (op0, 0))
11687 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11688 {
11689 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11690 continue;
11691 }
11692
11693 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11694 fits in both M1 and M2 and the SUBREG is either paradoxical
11695 or represents the low part, permute the SUBREG and the AND
11696 and try again. */
11697 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11698 {
11699 unsigned HOST_WIDE_INT c1;
11700 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11701 /* Require an integral mode, to avoid creating something like
11702 (AND:SF ...). */
11703 if (SCALAR_INT_MODE_P (tmode)
11704 /* It is unsafe to commute the AND into the SUBREG if the
11705 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11706 not defined. As originally written the upper bits
11707 have a defined value due to the AND operation.
11708 However, if we commute the AND inside the SUBREG then
11709 they no longer have defined values and the meaning of
11710 the code has been changed. */
11711 && (0
11712 #ifdef WORD_REGISTER_OPERATIONS
11713 || (mode_width > GET_MODE_PRECISION (tmode)
11714 && mode_width <= BITS_PER_WORD)
11715 #endif
11716 || (mode_width <= GET_MODE_PRECISION (tmode)
11717 && subreg_lowpart_p (XEXP (op0, 0))))
11718 && CONST_INT_P (XEXP (op0, 1))
11719 && mode_width <= HOST_BITS_PER_WIDE_INT
11720 && HWI_COMPUTABLE_MODE_P (tmode)
11721 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11722 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11723 && c1 != mask
11724 && c1 != GET_MODE_MASK (tmode))
11725 {
11726 op0 = simplify_gen_binary (AND, tmode,
11727 SUBREG_REG (XEXP (op0, 0)),
11728 gen_int_mode (c1, tmode));
11729 op0 = gen_lowpart (mode, op0);
11730 continue;
11731 }
11732 }
11733
11734 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11735 if (const_op == 0 && equality_comparison_p
11736 && XEXP (op0, 1) == const1_rtx
11737 && GET_CODE (XEXP (op0, 0)) == NOT)
11738 {
11739 op0 = simplify_and_const_int (NULL_RTX, mode,
11740 XEXP (XEXP (op0, 0), 0), 1);
11741 code = (code == NE ? EQ : NE);
11742 continue;
11743 }
11744
11745 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11746 (eq (and (lshiftrt X) 1) 0).
11747 Also handle the case where (not X) is expressed using xor. */
11748 if (const_op == 0 && equality_comparison_p
11749 && XEXP (op0, 1) == const1_rtx
11750 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11751 {
11752 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11753 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11754
11755 if (GET_CODE (shift_op) == NOT
11756 || (GET_CODE (shift_op) == XOR
11757 && CONST_INT_P (XEXP (shift_op, 1))
11758 && CONST_INT_P (shift_count)
11759 && HWI_COMPUTABLE_MODE_P (mode)
11760 && (UINTVAL (XEXP (shift_op, 1))
11761 == (unsigned HOST_WIDE_INT) 1
11762 << INTVAL (shift_count))))
11763 {
11764 op0
11765 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11766 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11767 code = (code == NE ? EQ : NE);
11768 continue;
11769 }
11770 }
11771 break;
11772
11773 case ASHIFT:
11774 /* If we have (compare (ashift FOO N) (const_int C)) and
11775 the high order N bits of FOO (N+1 if an inequality comparison)
11776 are known to be zero, we can do this by comparing FOO with C
11777 shifted right N bits so long as the low-order N bits of C are
11778 zero. */
11779 if (CONST_INT_P (XEXP (op0, 1))
11780 && INTVAL (XEXP (op0, 1)) >= 0
11781 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11782 < HOST_BITS_PER_WIDE_INT)
11783 && (((unsigned HOST_WIDE_INT) const_op
11784 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11785 - 1)) == 0)
11786 && mode_width <= HOST_BITS_PER_WIDE_INT
11787 && (nonzero_bits (XEXP (op0, 0), mode)
11788 & ~(mask >> (INTVAL (XEXP (op0, 1))
11789 + ! equality_comparison_p))) == 0)
11790 {
11791 /* We must perform a logical shift, not an arithmetic one,
11792 as we want the top N bits of C to be zero. */
11793 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11794
11795 temp >>= INTVAL (XEXP (op0, 1));
11796 op1 = gen_int_mode (temp, mode);
11797 op0 = XEXP (op0, 0);
11798 continue;
11799 }
11800
11801 /* If we are doing a sign bit comparison, it means we are testing
11802 a particular bit. Convert it to the appropriate AND. */
11803 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11804 && mode_width <= HOST_BITS_PER_WIDE_INT)
11805 {
11806 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11807 ((unsigned HOST_WIDE_INT) 1
11808 << (mode_width - 1
11809 - INTVAL (XEXP (op0, 1)))));
11810 code = (code == LT ? NE : EQ);
11811 continue;
11812 }
11813
11814 /* If this an equality comparison with zero and we are shifting
11815 the low bit to the sign bit, we can convert this to an AND of the
11816 low-order bit. */
11817 if (const_op == 0 && equality_comparison_p
11818 && CONST_INT_P (XEXP (op0, 1))
11819 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11820 {
11821 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11822 continue;
11823 }
11824 break;
11825
11826 case ASHIFTRT:
11827 /* If this is an equality comparison with zero, we can do this
11828 as a logical shift, which might be much simpler. */
11829 if (equality_comparison_p && const_op == 0
11830 && CONST_INT_P (XEXP (op0, 1)))
11831 {
11832 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11833 XEXP (op0, 0),
11834 INTVAL (XEXP (op0, 1)));
11835 continue;
11836 }
11837
11838 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11839 do the comparison in a narrower mode. */
11840 if (! unsigned_comparison_p
11841 && CONST_INT_P (XEXP (op0, 1))
11842 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11843 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11844 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11845 MODE_INT, 1)) != BLKmode
11846 && (((unsigned HOST_WIDE_INT) const_op
11847 + (GET_MODE_MASK (tmode) >> 1) + 1)
11848 <= GET_MODE_MASK (tmode)))
11849 {
11850 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11851 continue;
11852 }
11853
11854 /* Likewise if OP0 is a PLUS of a sign extension with a
11855 constant, which is usually represented with the PLUS
11856 between the shifts. */
11857 if (! unsigned_comparison_p
11858 && CONST_INT_P (XEXP (op0, 1))
11859 && GET_CODE (XEXP (op0, 0)) == PLUS
11860 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11861 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11862 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11863 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11864 MODE_INT, 1)) != BLKmode
11865 && (((unsigned HOST_WIDE_INT) const_op
11866 + (GET_MODE_MASK (tmode) >> 1) + 1)
11867 <= GET_MODE_MASK (tmode)))
11868 {
11869 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11870 rtx add_const = XEXP (XEXP (op0, 0), 1);
11871 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11872 add_const, XEXP (op0, 1));
11873
11874 op0 = simplify_gen_binary (PLUS, tmode,
11875 gen_lowpart (tmode, inner),
11876 new_const);
11877 continue;
11878 }
11879
11880 /* ... fall through ... */
11881 case LSHIFTRT:
11882 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11883 the low order N bits of FOO are known to be zero, we can do this
11884 by comparing FOO with C shifted left N bits so long as no
11885 overflow occurs. Even if the low order N bits of FOO aren't known
11886 to be zero, if the comparison is >= or < we can use the same
11887 optimization and for > or <= by setting all the low
11888 order N bits in the comparison constant. */
11889 if (CONST_INT_P (XEXP (op0, 1))
11890 && INTVAL (XEXP (op0, 1)) > 0
11891 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11892 && mode_width <= HOST_BITS_PER_WIDE_INT
11893 && (((unsigned HOST_WIDE_INT) const_op
11894 + (GET_CODE (op0) != LSHIFTRT
11895 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11896 + 1)
11897 : 0))
11898 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11899 {
11900 unsigned HOST_WIDE_INT low_bits
11901 = (nonzero_bits (XEXP (op0, 0), mode)
11902 & (((unsigned HOST_WIDE_INT) 1
11903 << INTVAL (XEXP (op0, 1))) - 1));
11904 if (low_bits == 0 || !equality_comparison_p)
11905 {
11906 /* If the shift was logical, then we must make the condition
11907 unsigned. */
11908 if (GET_CODE (op0) == LSHIFTRT)
11909 code = unsigned_condition (code);
11910
11911 const_op <<= INTVAL (XEXP (op0, 1));
11912 if (low_bits != 0
11913 && (code == GT || code == GTU
11914 || code == LE || code == LEU))
11915 const_op
11916 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11917 op1 = GEN_INT (const_op);
11918 op0 = XEXP (op0, 0);
11919 continue;
11920 }
11921 }
11922
11923 /* If we are using this shift to extract just the sign bit, we
11924 can replace this with an LT or GE comparison. */
11925 if (const_op == 0
11926 && (equality_comparison_p || sign_bit_comparison_p)
11927 && CONST_INT_P (XEXP (op0, 1))
11928 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11929 {
11930 op0 = XEXP (op0, 0);
11931 code = (code == NE || code == GT ? LT : GE);
11932 continue;
11933 }
11934 break;
11935
11936 default:
11937 break;
11938 }
11939
11940 break;
11941 }
11942
11943 /* Now make any compound operations involved in this comparison. Then,
11944 check for an outmost SUBREG on OP0 that is not doing anything or is
11945 paradoxical. The latter transformation must only be performed when
11946 it is known that the "extra" bits will be the same in op0 and op1 or
11947 that they don't matter. There are three cases to consider:
11948
11949 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11950 care bits and we can assume they have any convenient value. So
11951 making the transformation is safe.
11952
11953 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11954 In this case the upper bits of op0 are undefined. We should not make
11955 the simplification in that case as we do not know the contents of
11956 those bits.
11957
11958 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11959 UNKNOWN. In that case we know those bits are zeros or ones. We must
11960 also be sure that they are the same as the upper bits of op1.
11961
11962 We can never remove a SUBREG for a non-equality comparison because
11963 the sign bit is in a different place in the underlying object. */
11964
11965 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11966 op1 = make_compound_operation (op1, SET);
11967
11968 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11969 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11970 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11971 && (code == NE || code == EQ))
11972 {
11973 if (paradoxical_subreg_p (op0))
11974 {
11975 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11976 implemented. */
11977 if (REG_P (SUBREG_REG (op0)))
11978 {
11979 op0 = SUBREG_REG (op0);
11980 op1 = gen_lowpart (GET_MODE (op0), op1);
11981 }
11982 }
11983 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
11984 <= HOST_BITS_PER_WIDE_INT)
11985 && (nonzero_bits (SUBREG_REG (op0),
11986 GET_MODE (SUBREG_REG (op0)))
11987 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11988 {
11989 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11990
11991 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11992 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11993 op0 = SUBREG_REG (op0), op1 = tem;
11994 }
11995 }
11996
11997 /* We now do the opposite procedure: Some machines don't have compare
11998 insns in all modes. If OP0's mode is an integer mode smaller than a
11999 word and we can't do a compare in that mode, see if there is a larger
12000 mode for which we can do the compare. There are a number of cases in
12001 which we can use the wider mode. */
12002
12003 mode = GET_MODE (op0);
12004 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12005 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12006 && ! have_insn_for (COMPARE, mode))
12007 for (tmode = GET_MODE_WIDER_MODE (mode);
12008 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12009 tmode = GET_MODE_WIDER_MODE (tmode))
12010 if (have_insn_for (COMPARE, tmode))
12011 {
12012 int zero_extended;
12013
12014 /* If this is a test for negative, we can make an explicit
12015 test of the sign bit. Test this first so we can use
12016 a paradoxical subreg to extend OP0. */
12017
12018 if (op1 == const0_rtx && (code == LT || code == GE)
12019 && HWI_COMPUTABLE_MODE_P (mode))
12020 {
12021 unsigned HOST_WIDE_INT sign
12022 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12023 op0 = simplify_gen_binary (AND, tmode,
12024 gen_lowpart (tmode, op0),
12025 gen_int_mode (sign, tmode));
12026 code = (code == LT) ? NE : EQ;
12027 break;
12028 }
12029
12030 /* If the only nonzero bits in OP0 and OP1 are those in the
12031 narrower mode and this is an equality or unsigned comparison,
12032 we can use the wider mode. Similarly for sign-extended
12033 values, in which case it is true for all comparisons. */
12034 zero_extended = ((code == EQ || code == NE
12035 || code == GEU || code == GTU
12036 || code == LEU || code == LTU)
12037 && (nonzero_bits (op0, tmode)
12038 & ~GET_MODE_MASK (mode)) == 0
12039 && ((CONST_INT_P (op1)
12040 || (nonzero_bits (op1, tmode)
12041 & ~GET_MODE_MASK (mode)) == 0)));
12042
12043 if (zero_extended
12044 || ((num_sign_bit_copies (op0, tmode)
12045 > (unsigned int) (GET_MODE_PRECISION (tmode)
12046 - GET_MODE_PRECISION (mode)))
12047 && (num_sign_bit_copies (op1, tmode)
12048 > (unsigned int) (GET_MODE_PRECISION (tmode)
12049 - GET_MODE_PRECISION (mode)))))
12050 {
12051 /* If OP0 is an AND and we don't have an AND in MODE either,
12052 make a new AND in the proper mode. */
12053 if (GET_CODE (op0) == AND
12054 && !have_insn_for (AND, mode))
12055 op0 = simplify_gen_binary (AND, tmode,
12056 gen_lowpart (tmode,
12057 XEXP (op0, 0)),
12058 gen_lowpart (tmode,
12059 XEXP (op0, 1)));
12060 else
12061 {
12062 if (zero_extended)
12063 {
12064 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12065 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12066 }
12067 else
12068 {
12069 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12070 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12071 }
12072 break;
12073 }
12074 }
12075 }
12076
12077 /* We may have changed the comparison operands. Re-canonicalize. */
12078 if (swap_commutative_operands_p (op0, op1))
12079 {
12080 tem = op0, op0 = op1, op1 = tem;
12081 code = swap_condition (code);
12082 }
12083
12084 /* If this machine only supports a subset of valid comparisons, see if we
12085 can convert an unsupported one into a supported one. */
12086 target_canonicalize_comparison (&code, &op0, &op1, 0);
12087
12088 *pop0 = op0;
12089 *pop1 = op1;
12090
12091 return code;
12092 }
12093 \f
12094 /* Utility function for record_value_for_reg. Count number of
12095 rtxs in X. */
12096 static int
12097 count_rtxs (rtx x)
12098 {
12099 enum rtx_code code = GET_CODE (x);
12100 const char *fmt;
12101 int i, j, ret = 1;
12102
12103 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12104 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12105 {
12106 rtx x0 = XEXP (x, 0);
12107 rtx x1 = XEXP (x, 1);
12108
12109 if (x0 == x1)
12110 return 1 + 2 * count_rtxs (x0);
12111
12112 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12113 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12114 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12115 return 2 + 2 * count_rtxs (x0)
12116 + count_rtxs (x == XEXP (x1, 0)
12117 ? XEXP (x1, 1) : XEXP (x1, 0));
12118
12119 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12120 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12121 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12122 return 2 + 2 * count_rtxs (x1)
12123 + count_rtxs (x == XEXP (x0, 0)
12124 ? XEXP (x0, 1) : XEXP (x0, 0));
12125 }
12126
12127 fmt = GET_RTX_FORMAT (code);
12128 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12129 if (fmt[i] == 'e')
12130 ret += count_rtxs (XEXP (x, i));
12131 else if (fmt[i] == 'E')
12132 for (j = 0; j < XVECLEN (x, i); j++)
12133 ret += count_rtxs (XVECEXP (x, i, j));
12134
12135 return ret;
12136 }
12137 \f
12138 /* Utility function for following routine. Called when X is part of a value
12139 being stored into last_set_value. Sets last_set_table_tick
12140 for each register mentioned. Similar to mention_regs in cse.c */
12141
12142 static void
12143 update_table_tick (rtx x)
12144 {
12145 enum rtx_code code = GET_CODE (x);
12146 const char *fmt = GET_RTX_FORMAT (code);
12147 int i, j;
12148
12149 if (code == REG)
12150 {
12151 unsigned int regno = REGNO (x);
12152 unsigned int endregno = END_REGNO (x);
12153 unsigned int r;
12154
12155 for (r = regno; r < endregno; r++)
12156 {
12157 reg_stat_type *rsp = &reg_stat[r];
12158 rsp->last_set_table_tick = label_tick;
12159 }
12160
12161 return;
12162 }
12163
12164 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12165 if (fmt[i] == 'e')
12166 {
12167 /* Check for identical subexpressions. If x contains
12168 identical subexpression we only have to traverse one of
12169 them. */
12170 if (i == 0 && ARITHMETIC_P (x))
12171 {
12172 /* Note that at this point x1 has already been
12173 processed. */
12174 rtx x0 = XEXP (x, 0);
12175 rtx x1 = XEXP (x, 1);
12176
12177 /* If x0 and x1 are identical then there is no need to
12178 process x0. */
12179 if (x0 == x1)
12180 break;
12181
12182 /* If x0 is identical to a subexpression of x1 then while
12183 processing x1, x0 has already been processed. Thus we
12184 are done with x. */
12185 if (ARITHMETIC_P (x1)
12186 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12187 break;
12188
12189 /* If x1 is identical to a subexpression of x0 then we
12190 still have to process the rest of x0. */
12191 if (ARITHMETIC_P (x0)
12192 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12193 {
12194 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12195 break;
12196 }
12197 }
12198
12199 update_table_tick (XEXP (x, i));
12200 }
12201 else if (fmt[i] == 'E')
12202 for (j = 0; j < XVECLEN (x, i); j++)
12203 update_table_tick (XVECEXP (x, i, j));
12204 }
12205
12206 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12207 are saying that the register is clobbered and we no longer know its
12208 value. If INSN is zero, don't update reg_stat[].last_set; this is
12209 only permitted with VALUE also zero and is used to invalidate the
12210 register. */
12211
12212 static void
12213 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12214 {
12215 unsigned int regno = REGNO (reg);
12216 unsigned int endregno = END_REGNO (reg);
12217 unsigned int i;
12218 reg_stat_type *rsp;
12219
12220 /* If VALUE contains REG and we have a previous value for REG, substitute
12221 the previous value. */
12222 if (value && insn && reg_overlap_mentioned_p (reg, value))
12223 {
12224 rtx tem;
12225
12226 /* Set things up so get_last_value is allowed to see anything set up to
12227 our insn. */
12228 subst_low_luid = DF_INSN_LUID (insn);
12229 tem = get_last_value (reg);
12230
12231 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12232 it isn't going to be useful and will take a lot of time to process,
12233 so just use the CLOBBER. */
12234
12235 if (tem)
12236 {
12237 if (ARITHMETIC_P (tem)
12238 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12239 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12240 tem = XEXP (tem, 0);
12241 else if (count_occurrences (value, reg, 1) >= 2)
12242 {
12243 /* If there are two or more occurrences of REG in VALUE,
12244 prevent the value from growing too much. */
12245 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12246 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12247 }
12248
12249 value = replace_rtx (copy_rtx (value), reg, tem);
12250 }
12251 }
12252
12253 /* For each register modified, show we don't know its value, that
12254 we don't know about its bitwise content, that its value has been
12255 updated, and that we don't know the location of the death of the
12256 register. */
12257 for (i = regno; i < endregno; i++)
12258 {
12259 rsp = &reg_stat[i];
12260
12261 if (insn)
12262 rsp->last_set = insn;
12263
12264 rsp->last_set_value = 0;
12265 rsp->last_set_mode = VOIDmode;
12266 rsp->last_set_nonzero_bits = 0;
12267 rsp->last_set_sign_bit_copies = 0;
12268 rsp->last_death = 0;
12269 rsp->truncated_to_mode = VOIDmode;
12270 }
12271
12272 /* Mark registers that are being referenced in this value. */
12273 if (value)
12274 update_table_tick (value);
12275
12276 /* Now update the status of each register being set.
12277 If someone is using this register in this block, set this register
12278 to invalid since we will get confused between the two lives in this
12279 basic block. This makes using this register always invalid. In cse, we
12280 scan the table to invalidate all entries using this register, but this
12281 is too much work for us. */
12282
12283 for (i = regno; i < endregno; i++)
12284 {
12285 rsp = &reg_stat[i];
12286 rsp->last_set_label = label_tick;
12287 if (!insn
12288 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12289 rsp->last_set_invalid = 1;
12290 else
12291 rsp->last_set_invalid = 0;
12292 }
12293
12294 /* The value being assigned might refer to X (like in "x++;"). In that
12295 case, we must replace it with (clobber (const_int 0)) to prevent
12296 infinite loops. */
12297 rsp = &reg_stat[regno];
12298 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12299 {
12300 value = copy_rtx (value);
12301 if (!get_last_value_validate (&value, insn, label_tick, 1))
12302 value = 0;
12303 }
12304
12305 /* For the main register being modified, update the value, the mode, the
12306 nonzero bits, and the number of sign bit copies. */
12307
12308 rsp->last_set_value = value;
12309
12310 if (value)
12311 {
12312 enum machine_mode mode = GET_MODE (reg);
12313 subst_low_luid = DF_INSN_LUID (insn);
12314 rsp->last_set_mode = mode;
12315 if (GET_MODE_CLASS (mode) == MODE_INT
12316 && HWI_COMPUTABLE_MODE_P (mode))
12317 mode = nonzero_bits_mode;
12318 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12319 rsp->last_set_sign_bit_copies
12320 = num_sign_bit_copies (value, GET_MODE (reg));
12321 }
12322 }
12323
12324 /* Called via note_stores from record_dead_and_set_regs to handle one
12325 SET or CLOBBER in an insn. DATA is the instruction in which the
12326 set is occurring. */
12327
12328 static void
12329 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12330 {
12331 rtx_insn *record_dead_insn = (rtx_insn *) data;
12332
12333 if (GET_CODE (dest) == SUBREG)
12334 dest = SUBREG_REG (dest);
12335
12336 if (!record_dead_insn)
12337 {
12338 if (REG_P (dest))
12339 record_value_for_reg (dest, NULL, NULL_RTX);
12340 return;
12341 }
12342
12343 if (REG_P (dest))
12344 {
12345 /* If we are setting the whole register, we know its value. Otherwise
12346 show that we don't know the value. We can handle SUBREG in
12347 some cases. */
12348 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12349 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12350 else if (GET_CODE (setter) == SET
12351 && GET_CODE (SET_DEST (setter)) == SUBREG
12352 && SUBREG_REG (SET_DEST (setter)) == dest
12353 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12354 && subreg_lowpart_p (SET_DEST (setter)))
12355 record_value_for_reg (dest, record_dead_insn,
12356 gen_lowpart (GET_MODE (dest),
12357 SET_SRC (setter)));
12358 else
12359 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12360 }
12361 else if (MEM_P (dest)
12362 /* Ignore pushes, they clobber nothing. */
12363 && ! push_operand (dest, GET_MODE (dest)))
12364 mem_last_set = DF_INSN_LUID (record_dead_insn);
12365 }
12366
12367 /* Update the records of when each REG was most recently set or killed
12368 for the things done by INSN. This is the last thing done in processing
12369 INSN in the combiner loop.
12370
12371 We update reg_stat[], in particular fields last_set, last_set_value,
12372 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12373 last_death, and also the similar information mem_last_set (which insn
12374 most recently modified memory) and last_call_luid (which insn was the
12375 most recent subroutine call). */
12376
12377 static void
12378 record_dead_and_set_regs (rtx_insn *insn)
12379 {
12380 rtx link;
12381 unsigned int i;
12382
12383 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12384 {
12385 if (REG_NOTE_KIND (link) == REG_DEAD
12386 && REG_P (XEXP (link, 0)))
12387 {
12388 unsigned int regno = REGNO (XEXP (link, 0));
12389 unsigned int endregno = END_REGNO (XEXP (link, 0));
12390
12391 for (i = regno; i < endregno; i++)
12392 {
12393 reg_stat_type *rsp;
12394
12395 rsp = &reg_stat[i];
12396 rsp->last_death = insn;
12397 }
12398 }
12399 else if (REG_NOTE_KIND (link) == REG_INC)
12400 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12401 }
12402
12403 if (CALL_P (insn))
12404 {
12405 hard_reg_set_iterator hrsi;
12406 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12407 {
12408 reg_stat_type *rsp;
12409
12410 rsp = &reg_stat[i];
12411 rsp->last_set_invalid = 1;
12412 rsp->last_set = insn;
12413 rsp->last_set_value = 0;
12414 rsp->last_set_mode = VOIDmode;
12415 rsp->last_set_nonzero_bits = 0;
12416 rsp->last_set_sign_bit_copies = 0;
12417 rsp->last_death = 0;
12418 rsp->truncated_to_mode = VOIDmode;
12419 }
12420
12421 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12422
12423 /* We can't combine into a call pattern. Remember, though, that
12424 the return value register is set at this LUID. We could
12425 still replace a register with the return value from the
12426 wrong subroutine call! */
12427 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12428 }
12429 else
12430 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12431 }
12432
12433 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12434 register present in the SUBREG, so for each such SUBREG go back and
12435 adjust nonzero and sign bit information of the registers that are
12436 known to have some zero/sign bits set.
12437
12438 This is needed because when combine blows the SUBREGs away, the
12439 information on zero/sign bits is lost and further combines can be
12440 missed because of that. */
12441
12442 static void
12443 record_promoted_value (rtx_insn *insn, rtx subreg)
12444 {
12445 struct insn_link *links;
12446 rtx set;
12447 unsigned int regno = REGNO (SUBREG_REG (subreg));
12448 enum machine_mode mode = GET_MODE (subreg);
12449
12450 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12451 return;
12452
12453 for (links = LOG_LINKS (insn); links;)
12454 {
12455 reg_stat_type *rsp;
12456
12457 insn = links->insn;
12458 set = single_set (insn);
12459
12460 if (! set || !REG_P (SET_DEST (set))
12461 || REGNO (SET_DEST (set)) != regno
12462 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12463 {
12464 links = links->next;
12465 continue;
12466 }
12467
12468 rsp = &reg_stat[regno];
12469 if (rsp->last_set == insn)
12470 {
12471 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12472 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12473 }
12474
12475 if (REG_P (SET_SRC (set)))
12476 {
12477 regno = REGNO (SET_SRC (set));
12478 links = LOG_LINKS (insn);
12479 }
12480 else
12481 break;
12482 }
12483 }
12484
12485 /* Check if X, a register, is known to contain a value already
12486 truncated to MODE. In this case we can use a subreg to refer to
12487 the truncated value even though in the generic case we would need
12488 an explicit truncation. */
12489
12490 static bool
12491 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12492 {
12493 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12494 enum machine_mode truncated = rsp->truncated_to_mode;
12495
12496 if (truncated == 0
12497 || rsp->truncation_label < label_tick_ebb_start)
12498 return false;
12499 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12500 return true;
12501 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12502 return true;
12503 return false;
12504 }
12505
12506 /* If X is a hard reg or a subreg record the mode that the register is
12507 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12508 to turn a truncate into a subreg using this information. Return true
12509 if traversing X is complete. */
12510
12511 static bool
12512 record_truncated_value (rtx x)
12513 {
12514 enum machine_mode truncated_mode;
12515 reg_stat_type *rsp;
12516
12517 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12518 {
12519 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12520 truncated_mode = GET_MODE (x);
12521
12522 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12523 return true;
12524
12525 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12526 return true;
12527
12528 x = SUBREG_REG (x);
12529 }
12530 /* ??? For hard-regs we now record everything. We might be able to
12531 optimize this using last_set_mode. */
12532 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12533 truncated_mode = GET_MODE (x);
12534 else
12535 return false;
12536
12537 rsp = &reg_stat[REGNO (x)];
12538 if (rsp->truncated_to_mode == 0
12539 || rsp->truncation_label < label_tick_ebb_start
12540 || (GET_MODE_SIZE (truncated_mode)
12541 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12542 {
12543 rsp->truncated_to_mode = truncated_mode;
12544 rsp->truncation_label = label_tick;
12545 }
12546
12547 return true;
12548 }
12549
12550 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12551 the modes they are used in. This can help truning TRUNCATEs into
12552 SUBREGs. */
12553
12554 static void
12555 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12556 {
12557 subrtx_var_iterator::array_type array;
12558 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12559 if (record_truncated_value (*iter))
12560 iter.skip_subrtxes ();
12561 }
12562
12563 /* Scan X for promoted SUBREGs. For each one found,
12564 note what it implies to the registers used in it. */
12565
12566 static void
12567 check_promoted_subreg (rtx_insn *insn, rtx x)
12568 {
12569 if (GET_CODE (x) == SUBREG
12570 && SUBREG_PROMOTED_VAR_P (x)
12571 && REG_P (SUBREG_REG (x)))
12572 record_promoted_value (insn, x);
12573 else
12574 {
12575 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12576 int i, j;
12577
12578 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12579 switch (format[i])
12580 {
12581 case 'e':
12582 check_promoted_subreg (insn, XEXP (x, i));
12583 break;
12584 case 'V':
12585 case 'E':
12586 if (XVEC (x, i) != 0)
12587 for (j = 0; j < XVECLEN (x, i); j++)
12588 check_promoted_subreg (insn, XVECEXP (x, i, j));
12589 break;
12590 }
12591 }
12592 }
12593 \f
12594 /* Verify that all the registers and memory references mentioned in *LOC are
12595 still valid. *LOC was part of a value set in INSN when label_tick was
12596 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12597 the invalid references with (clobber (const_int 0)) and return 1. This
12598 replacement is useful because we often can get useful information about
12599 the form of a value (e.g., if it was produced by a shift that always
12600 produces -1 or 0) even though we don't know exactly what registers it
12601 was produced from. */
12602
12603 static int
12604 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12605 {
12606 rtx x = *loc;
12607 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12608 int len = GET_RTX_LENGTH (GET_CODE (x));
12609 int i, j;
12610
12611 if (REG_P (x))
12612 {
12613 unsigned int regno = REGNO (x);
12614 unsigned int endregno = END_REGNO (x);
12615 unsigned int j;
12616
12617 for (j = regno; j < endregno; j++)
12618 {
12619 reg_stat_type *rsp = &reg_stat[j];
12620 if (rsp->last_set_invalid
12621 /* If this is a pseudo-register that was only set once and not
12622 live at the beginning of the function, it is always valid. */
12623 || (! (regno >= FIRST_PSEUDO_REGISTER
12624 && REG_N_SETS (regno) == 1
12625 && (!REGNO_REG_SET_P
12626 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12627 regno)))
12628 && rsp->last_set_label > tick))
12629 {
12630 if (replace)
12631 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12632 return replace;
12633 }
12634 }
12635
12636 return 1;
12637 }
12638 /* If this is a memory reference, make sure that there were no stores after
12639 it that might have clobbered the value. We don't have alias info, so we
12640 assume any store invalidates it. Moreover, we only have local UIDs, so
12641 we also assume that there were stores in the intervening basic blocks. */
12642 else if (MEM_P (x) && !MEM_READONLY_P (x)
12643 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12644 {
12645 if (replace)
12646 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12647 return replace;
12648 }
12649
12650 for (i = 0; i < len; i++)
12651 {
12652 if (fmt[i] == 'e')
12653 {
12654 /* Check for identical subexpressions. If x contains
12655 identical subexpression we only have to traverse one of
12656 them. */
12657 if (i == 1 && ARITHMETIC_P (x))
12658 {
12659 /* Note that at this point x0 has already been checked
12660 and found valid. */
12661 rtx x0 = XEXP (x, 0);
12662 rtx x1 = XEXP (x, 1);
12663
12664 /* If x0 and x1 are identical then x is also valid. */
12665 if (x0 == x1)
12666 return 1;
12667
12668 /* If x1 is identical to a subexpression of x0 then
12669 while checking x0, x1 has already been checked. Thus
12670 it is valid and so as x. */
12671 if (ARITHMETIC_P (x0)
12672 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12673 return 1;
12674
12675 /* If x0 is identical to a subexpression of x1 then x is
12676 valid iff the rest of x1 is valid. */
12677 if (ARITHMETIC_P (x1)
12678 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12679 return
12680 get_last_value_validate (&XEXP (x1,
12681 x0 == XEXP (x1, 0) ? 1 : 0),
12682 insn, tick, replace);
12683 }
12684
12685 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12686 replace) == 0)
12687 return 0;
12688 }
12689 else if (fmt[i] == 'E')
12690 for (j = 0; j < XVECLEN (x, i); j++)
12691 if (get_last_value_validate (&XVECEXP (x, i, j),
12692 insn, tick, replace) == 0)
12693 return 0;
12694 }
12695
12696 /* If we haven't found a reason for it to be invalid, it is valid. */
12697 return 1;
12698 }
12699
12700 /* Get the last value assigned to X, if known. Some registers
12701 in the value may be replaced with (clobber (const_int 0)) if their value
12702 is known longer known reliably. */
12703
12704 static rtx
12705 get_last_value (const_rtx x)
12706 {
12707 unsigned int regno;
12708 rtx value;
12709 reg_stat_type *rsp;
12710
12711 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12712 then convert it to the desired mode. If this is a paradoxical SUBREG,
12713 we cannot predict what values the "extra" bits might have. */
12714 if (GET_CODE (x) == SUBREG
12715 && subreg_lowpart_p (x)
12716 && !paradoxical_subreg_p (x)
12717 && (value = get_last_value (SUBREG_REG (x))) != 0)
12718 return gen_lowpart (GET_MODE (x), value);
12719
12720 if (!REG_P (x))
12721 return 0;
12722
12723 regno = REGNO (x);
12724 rsp = &reg_stat[regno];
12725 value = rsp->last_set_value;
12726
12727 /* If we don't have a value, or if it isn't for this basic block and
12728 it's either a hard register, set more than once, or it's a live
12729 at the beginning of the function, return 0.
12730
12731 Because if it's not live at the beginning of the function then the reg
12732 is always set before being used (is never used without being set).
12733 And, if it's set only once, and it's always set before use, then all
12734 uses must have the same last value, even if it's not from this basic
12735 block. */
12736
12737 if (value == 0
12738 || (rsp->last_set_label < label_tick_ebb_start
12739 && (regno < FIRST_PSEUDO_REGISTER
12740 || REG_N_SETS (regno) != 1
12741 || REGNO_REG_SET_P
12742 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12743 return 0;
12744
12745 /* If the value was set in a later insn than the ones we are processing,
12746 we can't use it even if the register was only set once. */
12747 if (rsp->last_set_label == label_tick
12748 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12749 return 0;
12750
12751 /* If the value has all its registers valid, return it. */
12752 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12753 return value;
12754
12755 /* Otherwise, make a copy and replace any invalid register with
12756 (clobber (const_int 0)). If that fails for some reason, return 0. */
12757
12758 value = copy_rtx (value);
12759 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12760 return value;
12761
12762 return 0;
12763 }
12764 \f
12765 /* Return nonzero if expression X refers to a REG or to memory
12766 that is set in an instruction more recent than FROM_LUID. */
12767
12768 static int
12769 use_crosses_set_p (const_rtx x, int from_luid)
12770 {
12771 const char *fmt;
12772 int i;
12773 enum rtx_code code = GET_CODE (x);
12774
12775 if (code == REG)
12776 {
12777 unsigned int regno = REGNO (x);
12778 unsigned endreg = END_REGNO (x);
12779
12780 #ifdef PUSH_ROUNDING
12781 /* Don't allow uses of the stack pointer to be moved,
12782 because we don't know whether the move crosses a push insn. */
12783 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12784 return 1;
12785 #endif
12786 for (; regno < endreg; regno++)
12787 {
12788 reg_stat_type *rsp = &reg_stat[regno];
12789 if (rsp->last_set
12790 && rsp->last_set_label == label_tick
12791 && DF_INSN_LUID (rsp->last_set) > from_luid)
12792 return 1;
12793 }
12794 return 0;
12795 }
12796
12797 if (code == MEM && mem_last_set > from_luid)
12798 return 1;
12799
12800 fmt = GET_RTX_FORMAT (code);
12801
12802 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12803 {
12804 if (fmt[i] == 'E')
12805 {
12806 int j;
12807 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12808 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12809 return 1;
12810 }
12811 else if (fmt[i] == 'e'
12812 && use_crosses_set_p (XEXP (x, i), from_luid))
12813 return 1;
12814 }
12815 return 0;
12816 }
12817 \f
12818 /* Define three variables used for communication between the following
12819 routines. */
12820
12821 static unsigned int reg_dead_regno, reg_dead_endregno;
12822 static int reg_dead_flag;
12823
12824 /* Function called via note_stores from reg_dead_at_p.
12825
12826 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12827 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12828
12829 static void
12830 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12831 {
12832 unsigned int regno, endregno;
12833
12834 if (!REG_P (dest))
12835 return;
12836
12837 regno = REGNO (dest);
12838 endregno = END_REGNO (dest);
12839 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12840 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12841 }
12842
12843 /* Return nonzero if REG is known to be dead at INSN.
12844
12845 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12846 referencing REG, it is dead. If we hit a SET referencing REG, it is
12847 live. Otherwise, see if it is live or dead at the start of the basic
12848 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12849 must be assumed to be always live. */
12850
12851 static int
12852 reg_dead_at_p (rtx reg, rtx_insn *insn)
12853 {
12854 basic_block block;
12855 unsigned int i;
12856
12857 /* Set variables for reg_dead_at_p_1. */
12858 reg_dead_regno = REGNO (reg);
12859 reg_dead_endregno = END_REGNO (reg);
12860
12861 reg_dead_flag = 0;
12862
12863 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12864 we allow the machine description to decide whether use-and-clobber
12865 patterns are OK. */
12866 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12867 {
12868 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12869 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12870 return 0;
12871 }
12872
12873 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12874 beginning of basic block. */
12875 block = BLOCK_FOR_INSN (insn);
12876 for (;;)
12877 {
12878 if (INSN_P (insn))
12879 {
12880 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12881 if (reg_dead_flag)
12882 return reg_dead_flag == 1 ? 1 : 0;
12883
12884 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12885 return 1;
12886 }
12887
12888 if (insn == BB_HEAD (block))
12889 break;
12890
12891 insn = PREV_INSN (insn);
12892 }
12893
12894 /* Look at live-in sets for the basic block that we were in. */
12895 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12896 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12897 return 0;
12898
12899 return 1;
12900 }
12901 \f
12902 /* Note hard registers in X that are used. */
12903
12904 static void
12905 mark_used_regs_combine (rtx x)
12906 {
12907 RTX_CODE code = GET_CODE (x);
12908 unsigned int regno;
12909 int i;
12910
12911 switch (code)
12912 {
12913 case LABEL_REF:
12914 case SYMBOL_REF:
12915 case CONST:
12916 CASE_CONST_ANY:
12917 case PC:
12918 case ADDR_VEC:
12919 case ADDR_DIFF_VEC:
12920 case ASM_INPUT:
12921 #ifdef HAVE_cc0
12922 /* CC0 must die in the insn after it is set, so we don't need to take
12923 special note of it here. */
12924 case CC0:
12925 #endif
12926 return;
12927
12928 case CLOBBER:
12929 /* If we are clobbering a MEM, mark any hard registers inside the
12930 address as used. */
12931 if (MEM_P (XEXP (x, 0)))
12932 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12933 return;
12934
12935 case REG:
12936 regno = REGNO (x);
12937 /* A hard reg in a wide mode may really be multiple registers.
12938 If so, mark all of them just like the first. */
12939 if (regno < FIRST_PSEUDO_REGISTER)
12940 {
12941 /* None of this applies to the stack, frame or arg pointers. */
12942 if (regno == STACK_POINTER_REGNUM
12943 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12944 || regno == HARD_FRAME_POINTER_REGNUM
12945 #endif
12946 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12947 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12948 #endif
12949 || regno == FRAME_POINTER_REGNUM)
12950 return;
12951
12952 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12953 }
12954 return;
12955
12956 case SET:
12957 {
12958 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12959 the address. */
12960 rtx testreg = SET_DEST (x);
12961
12962 while (GET_CODE (testreg) == SUBREG
12963 || GET_CODE (testreg) == ZERO_EXTRACT
12964 || GET_CODE (testreg) == STRICT_LOW_PART)
12965 testreg = XEXP (testreg, 0);
12966
12967 if (MEM_P (testreg))
12968 mark_used_regs_combine (XEXP (testreg, 0));
12969
12970 mark_used_regs_combine (SET_SRC (x));
12971 }
12972 return;
12973
12974 default:
12975 break;
12976 }
12977
12978 /* Recursively scan the operands of this expression. */
12979
12980 {
12981 const char *fmt = GET_RTX_FORMAT (code);
12982
12983 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12984 {
12985 if (fmt[i] == 'e')
12986 mark_used_regs_combine (XEXP (x, i));
12987 else if (fmt[i] == 'E')
12988 {
12989 int j;
12990
12991 for (j = 0; j < XVECLEN (x, i); j++)
12992 mark_used_regs_combine (XVECEXP (x, i, j));
12993 }
12994 }
12995 }
12996 }
12997 \f
12998 /* Remove register number REGNO from the dead registers list of INSN.
12999
13000 Return the note used to record the death, if there was one. */
13001
13002 rtx
13003 remove_death (unsigned int regno, rtx_insn *insn)
13004 {
13005 rtx note = find_regno_note (insn, REG_DEAD, regno);
13006
13007 if (note)
13008 remove_note (insn, note);
13009
13010 return note;
13011 }
13012
13013 /* For each register (hardware or pseudo) used within expression X, if its
13014 death is in an instruction with luid between FROM_LUID (inclusive) and
13015 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13016 list headed by PNOTES.
13017
13018 That said, don't move registers killed by maybe_kill_insn.
13019
13020 This is done when X is being merged by combination into TO_INSN. These
13021 notes will then be distributed as needed. */
13022
13023 static void
13024 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13025 rtx *pnotes)
13026 {
13027 const char *fmt;
13028 int len, i;
13029 enum rtx_code code = GET_CODE (x);
13030
13031 if (code == REG)
13032 {
13033 unsigned int regno = REGNO (x);
13034 rtx_insn *where_dead = reg_stat[regno].last_death;
13035
13036 /* Don't move the register if it gets killed in between from and to. */
13037 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13038 && ! reg_referenced_p (x, maybe_kill_insn))
13039 return;
13040
13041 if (where_dead
13042 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13043 && DF_INSN_LUID (where_dead) >= from_luid
13044 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13045 {
13046 rtx note = remove_death (regno, where_dead);
13047
13048 /* It is possible for the call above to return 0. This can occur
13049 when last_death points to I2 or I1 that we combined with.
13050 In that case make a new note.
13051
13052 We must also check for the case where X is a hard register
13053 and NOTE is a death note for a range of hard registers
13054 including X. In that case, we must put REG_DEAD notes for
13055 the remaining registers in place of NOTE. */
13056
13057 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13058 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13059 > GET_MODE_SIZE (GET_MODE (x))))
13060 {
13061 unsigned int deadregno = REGNO (XEXP (note, 0));
13062 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13063 unsigned int ourend = END_HARD_REGNO (x);
13064 unsigned int i;
13065
13066 for (i = deadregno; i < deadend; i++)
13067 if (i < regno || i >= ourend)
13068 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13069 }
13070
13071 /* If we didn't find any note, or if we found a REG_DEAD note that
13072 covers only part of the given reg, and we have a multi-reg hard
13073 register, then to be safe we must check for REG_DEAD notes
13074 for each register other than the first. They could have
13075 their own REG_DEAD notes lying around. */
13076 else if ((note == 0
13077 || (note != 0
13078 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13079 < GET_MODE_SIZE (GET_MODE (x)))))
13080 && regno < FIRST_PSEUDO_REGISTER
13081 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13082 {
13083 unsigned int ourend = END_HARD_REGNO (x);
13084 unsigned int i, offset;
13085 rtx oldnotes = 0;
13086
13087 if (note)
13088 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13089 else
13090 offset = 1;
13091
13092 for (i = regno + offset; i < ourend; i++)
13093 move_deaths (regno_reg_rtx[i],
13094 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13095 }
13096
13097 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13098 {
13099 XEXP (note, 1) = *pnotes;
13100 *pnotes = note;
13101 }
13102 else
13103 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13104 }
13105
13106 return;
13107 }
13108
13109 else if (GET_CODE (x) == SET)
13110 {
13111 rtx dest = SET_DEST (x);
13112
13113 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13114
13115 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13116 that accesses one word of a multi-word item, some
13117 piece of everything register in the expression is used by
13118 this insn, so remove any old death. */
13119 /* ??? So why do we test for equality of the sizes? */
13120
13121 if (GET_CODE (dest) == ZERO_EXTRACT
13122 || GET_CODE (dest) == STRICT_LOW_PART
13123 || (GET_CODE (dest) == SUBREG
13124 && (((GET_MODE_SIZE (GET_MODE (dest))
13125 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13126 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13127 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13128 {
13129 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13130 return;
13131 }
13132
13133 /* If this is some other SUBREG, we know it replaces the entire
13134 value, so use that as the destination. */
13135 if (GET_CODE (dest) == SUBREG)
13136 dest = SUBREG_REG (dest);
13137
13138 /* If this is a MEM, adjust deaths of anything used in the address.
13139 For a REG (the only other possibility), the entire value is
13140 being replaced so the old value is not used in this insn. */
13141
13142 if (MEM_P (dest))
13143 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13144 to_insn, pnotes);
13145 return;
13146 }
13147
13148 else if (GET_CODE (x) == CLOBBER)
13149 return;
13150
13151 len = GET_RTX_LENGTH (code);
13152 fmt = GET_RTX_FORMAT (code);
13153
13154 for (i = 0; i < len; i++)
13155 {
13156 if (fmt[i] == 'E')
13157 {
13158 int j;
13159 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13160 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13161 to_insn, pnotes);
13162 }
13163 else if (fmt[i] == 'e')
13164 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13165 }
13166 }
13167 \f
13168 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13169 pattern of an insn. X must be a REG. */
13170
13171 static int
13172 reg_bitfield_target_p (rtx x, rtx body)
13173 {
13174 int i;
13175
13176 if (GET_CODE (body) == SET)
13177 {
13178 rtx dest = SET_DEST (body);
13179 rtx target;
13180 unsigned int regno, tregno, endregno, endtregno;
13181
13182 if (GET_CODE (dest) == ZERO_EXTRACT)
13183 target = XEXP (dest, 0);
13184 else if (GET_CODE (dest) == STRICT_LOW_PART)
13185 target = SUBREG_REG (XEXP (dest, 0));
13186 else
13187 return 0;
13188
13189 if (GET_CODE (target) == SUBREG)
13190 target = SUBREG_REG (target);
13191
13192 if (!REG_P (target))
13193 return 0;
13194
13195 tregno = REGNO (target), regno = REGNO (x);
13196 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13197 return target == x;
13198
13199 endtregno = end_hard_regno (GET_MODE (target), tregno);
13200 endregno = end_hard_regno (GET_MODE (x), regno);
13201
13202 return endregno > tregno && regno < endtregno;
13203 }
13204
13205 else if (GET_CODE (body) == PARALLEL)
13206 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13207 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13208 return 1;
13209
13210 return 0;
13211 }
13212 \f
13213 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13214 as appropriate. I3 and I2 are the insns resulting from the combination
13215 insns including FROM (I2 may be zero).
13216
13217 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13218 not need REG_DEAD notes because they are being substituted for. This
13219 saves searching in the most common cases.
13220
13221 Each note in the list is either ignored or placed on some insns, depending
13222 on the type of note. */
13223
13224 static void
13225 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13226 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13227 {
13228 rtx note, next_note;
13229 rtx tem_note;
13230 rtx_insn *tem_insn;
13231
13232 for (note = notes; note; note = next_note)
13233 {
13234 rtx_insn *place = 0, *place2 = 0;
13235
13236 next_note = XEXP (note, 1);
13237 switch (REG_NOTE_KIND (note))
13238 {
13239 case REG_BR_PROB:
13240 case REG_BR_PRED:
13241 /* Doesn't matter much where we put this, as long as it's somewhere.
13242 It is preferable to keep these notes on branches, which is most
13243 likely to be i3. */
13244 place = i3;
13245 break;
13246
13247 case REG_NON_LOCAL_GOTO:
13248 if (JUMP_P (i3))
13249 place = i3;
13250 else
13251 {
13252 gcc_assert (i2 && JUMP_P (i2));
13253 place = i2;
13254 }
13255 break;
13256
13257 case REG_EH_REGION:
13258 /* These notes must remain with the call or trapping instruction. */
13259 if (CALL_P (i3))
13260 place = i3;
13261 else if (i2 && CALL_P (i2))
13262 place = i2;
13263 else
13264 {
13265 gcc_assert (cfun->can_throw_non_call_exceptions);
13266 if (may_trap_p (i3))
13267 place = i3;
13268 else if (i2 && may_trap_p (i2))
13269 place = i2;
13270 /* ??? Otherwise assume we've combined things such that we
13271 can now prove that the instructions can't trap. Drop the
13272 note in this case. */
13273 }
13274 break;
13275
13276 case REG_ARGS_SIZE:
13277 /* ??? How to distribute between i3-i1. Assume i3 contains the
13278 entire adjustment. Assert i3 contains at least some adjust. */
13279 if (!noop_move_p (i3))
13280 {
13281 int old_size, args_size = INTVAL (XEXP (note, 0));
13282 /* fixup_args_size_notes looks at REG_NORETURN note,
13283 so ensure the note is placed there first. */
13284 if (CALL_P (i3))
13285 {
13286 rtx *np;
13287 for (np = &next_note; *np; np = &XEXP (*np, 1))
13288 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13289 {
13290 rtx n = *np;
13291 *np = XEXP (n, 1);
13292 XEXP (n, 1) = REG_NOTES (i3);
13293 REG_NOTES (i3) = n;
13294 break;
13295 }
13296 }
13297 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13298 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13299 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13300 gcc_assert (old_size != args_size
13301 || (CALL_P (i3)
13302 && !ACCUMULATE_OUTGOING_ARGS
13303 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13304 }
13305 break;
13306
13307 case REG_NORETURN:
13308 case REG_SETJMP:
13309 case REG_TM:
13310 case REG_CALL_DECL:
13311 /* These notes must remain with the call. It should not be
13312 possible for both I2 and I3 to be a call. */
13313 if (CALL_P (i3))
13314 place = i3;
13315 else
13316 {
13317 gcc_assert (i2 && CALL_P (i2));
13318 place = i2;
13319 }
13320 break;
13321
13322 case REG_UNUSED:
13323 /* Any clobbers for i3 may still exist, and so we must process
13324 REG_UNUSED notes from that insn.
13325
13326 Any clobbers from i2 or i1 can only exist if they were added by
13327 recog_for_combine. In that case, recog_for_combine created the
13328 necessary REG_UNUSED notes. Trying to keep any original
13329 REG_UNUSED notes from these insns can cause incorrect output
13330 if it is for the same register as the original i3 dest.
13331 In that case, we will notice that the register is set in i3,
13332 and then add a REG_UNUSED note for the destination of i3, which
13333 is wrong. However, it is possible to have REG_UNUSED notes from
13334 i2 or i1 for register which were both used and clobbered, so
13335 we keep notes from i2 or i1 if they will turn into REG_DEAD
13336 notes. */
13337
13338 /* If this register is set or clobbered in I3, put the note there
13339 unless there is one already. */
13340 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13341 {
13342 if (from_insn != i3)
13343 break;
13344
13345 if (! (REG_P (XEXP (note, 0))
13346 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13347 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13348 place = i3;
13349 }
13350 /* Otherwise, if this register is used by I3, then this register
13351 now dies here, so we must put a REG_DEAD note here unless there
13352 is one already. */
13353 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13354 && ! (REG_P (XEXP (note, 0))
13355 ? find_regno_note (i3, REG_DEAD,
13356 REGNO (XEXP (note, 0)))
13357 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13358 {
13359 PUT_REG_NOTE_KIND (note, REG_DEAD);
13360 place = i3;
13361 }
13362 break;
13363
13364 case REG_EQUAL:
13365 case REG_EQUIV:
13366 case REG_NOALIAS:
13367 /* These notes say something about results of an insn. We can
13368 only support them if they used to be on I3 in which case they
13369 remain on I3. Otherwise they are ignored.
13370
13371 If the note refers to an expression that is not a constant, we
13372 must also ignore the note since we cannot tell whether the
13373 equivalence is still true. It might be possible to do
13374 slightly better than this (we only have a problem if I2DEST
13375 or I1DEST is present in the expression), but it doesn't
13376 seem worth the trouble. */
13377
13378 if (from_insn == i3
13379 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13380 place = i3;
13381 break;
13382
13383 case REG_INC:
13384 /* These notes say something about how a register is used. They must
13385 be present on any use of the register in I2 or I3. */
13386 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13387 place = i3;
13388
13389 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13390 {
13391 if (place)
13392 place2 = i2;
13393 else
13394 place = i2;
13395 }
13396 break;
13397
13398 case REG_LABEL_TARGET:
13399 case REG_LABEL_OPERAND:
13400 /* This can show up in several ways -- either directly in the
13401 pattern, or hidden off in the constant pool with (or without?)
13402 a REG_EQUAL note. */
13403 /* ??? Ignore the without-reg_equal-note problem for now. */
13404 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13405 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13406 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13407 && XEXP (XEXP (tem_note, 0), 0) == XEXP (note, 0)))
13408 place = i3;
13409
13410 if (i2
13411 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13412 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13413 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13414 && XEXP (XEXP (tem_note, 0), 0) == XEXP (note, 0))))
13415 {
13416 if (place)
13417 place2 = i2;
13418 else
13419 place = i2;
13420 }
13421
13422 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13423 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13424 there. */
13425 if (place && JUMP_P (place)
13426 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13427 && (JUMP_LABEL (place) == NULL
13428 || JUMP_LABEL (place) == XEXP (note, 0)))
13429 {
13430 rtx label = JUMP_LABEL (place);
13431
13432 if (!label)
13433 JUMP_LABEL (place) = XEXP (note, 0);
13434 else if (LABEL_P (label))
13435 LABEL_NUSES (label)--;
13436 }
13437
13438 if (place2 && JUMP_P (place2)
13439 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13440 && (JUMP_LABEL (place2) == NULL
13441 || JUMP_LABEL (place2) == XEXP (note, 0)))
13442 {
13443 rtx label = JUMP_LABEL (place2);
13444
13445 if (!label)
13446 JUMP_LABEL (place2) = XEXP (note, 0);
13447 else if (LABEL_P (label))
13448 LABEL_NUSES (label)--;
13449 place2 = 0;
13450 }
13451 break;
13452
13453 case REG_NONNEG:
13454 /* This note says something about the value of a register prior
13455 to the execution of an insn. It is too much trouble to see
13456 if the note is still correct in all situations. It is better
13457 to simply delete it. */
13458 break;
13459
13460 case REG_DEAD:
13461 /* If we replaced the right hand side of FROM_INSN with a
13462 REG_EQUAL note, the original use of the dying register
13463 will not have been combined into I3 and I2. In such cases,
13464 FROM_INSN is guaranteed to be the first of the combined
13465 instructions, so we simply need to search back before
13466 FROM_INSN for the previous use or set of this register,
13467 then alter the notes there appropriately.
13468
13469 If the register is used as an input in I3, it dies there.
13470 Similarly for I2, if it is nonzero and adjacent to I3.
13471
13472 If the register is not used as an input in either I3 or I2
13473 and it is not one of the registers we were supposed to eliminate,
13474 there are two possibilities. We might have a non-adjacent I2
13475 or we might have somehow eliminated an additional register
13476 from a computation. For example, we might have had A & B where
13477 we discover that B will always be zero. In this case we will
13478 eliminate the reference to A.
13479
13480 In both cases, we must search to see if we can find a previous
13481 use of A and put the death note there. */
13482
13483 if (from_insn
13484 && from_insn == i2mod
13485 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13486 tem_insn = from_insn;
13487 else
13488 {
13489 if (from_insn
13490 && CALL_P (from_insn)
13491 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13492 place = from_insn;
13493 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13494 place = i3;
13495 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13496 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13497 place = i2;
13498 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13499 && !(i2mod
13500 && reg_overlap_mentioned_p (XEXP (note, 0),
13501 i2mod_old_rhs)))
13502 || rtx_equal_p (XEXP (note, 0), elim_i1)
13503 || rtx_equal_p (XEXP (note, 0), elim_i0))
13504 break;
13505 tem_insn = i3;
13506 }
13507
13508 if (place == 0)
13509 {
13510 basic_block bb = this_basic_block;
13511
13512 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13513 {
13514 if (!NONDEBUG_INSN_P (tem_insn))
13515 {
13516 if (tem_insn == BB_HEAD (bb))
13517 break;
13518 continue;
13519 }
13520
13521 /* If the register is being set at TEM_INSN, see if that is all
13522 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13523 into a REG_UNUSED note instead. Don't delete sets to
13524 global register vars. */
13525 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13526 || !global_regs[REGNO (XEXP (note, 0))])
13527 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13528 {
13529 rtx set = single_set (tem_insn);
13530 rtx inner_dest = 0;
13531 #ifdef HAVE_cc0
13532 rtx_insn *cc0_setter = NULL;
13533 #endif
13534
13535 if (set != 0)
13536 for (inner_dest = SET_DEST (set);
13537 (GET_CODE (inner_dest) == STRICT_LOW_PART
13538 || GET_CODE (inner_dest) == SUBREG
13539 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13540 inner_dest = XEXP (inner_dest, 0))
13541 ;
13542
13543 /* Verify that it was the set, and not a clobber that
13544 modified the register.
13545
13546 CC0 targets must be careful to maintain setter/user
13547 pairs. If we cannot delete the setter due to side
13548 effects, mark the user with an UNUSED note instead
13549 of deleting it. */
13550
13551 if (set != 0 && ! side_effects_p (SET_SRC (set))
13552 && rtx_equal_p (XEXP (note, 0), inner_dest)
13553 #ifdef HAVE_cc0
13554 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13555 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13556 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13557 #endif
13558 )
13559 {
13560 /* Move the notes and links of TEM_INSN elsewhere.
13561 This might delete other dead insns recursively.
13562 First set the pattern to something that won't use
13563 any register. */
13564 rtx old_notes = REG_NOTES (tem_insn);
13565
13566 PATTERN (tem_insn) = pc_rtx;
13567 REG_NOTES (tem_insn) = NULL;
13568
13569 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13570 NULL_RTX, NULL_RTX, NULL_RTX);
13571 distribute_links (LOG_LINKS (tem_insn));
13572
13573 SET_INSN_DELETED (tem_insn);
13574 if (tem_insn == i2)
13575 i2 = NULL;
13576
13577 #ifdef HAVE_cc0
13578 /* Delete the setter too. */
13579 if (cc0_setter)
13580 {
13581 PATTERN (cc0_setter) = pc_rtx;
13582 old_notes = REG_NOTES (cc0_setter);
13583 REG_NOTES (cc0_setter) = NULL;
13584
13585 distribute_notes (old_notes, cc0_setter,
13586 cc0_setter, NULL,
13587 NULL_RTX, NULL_RTX, NULL_RTX);
13588 distribute_links (LOG_LINKS (cc0_setter));
13589
13590 SET_INSN_DELETED (cc0_setter);
13591 if (cc0_setter == i2)
13592 i2 = NULL;
13593 }
13594 #endif
13595 }
13596 else
13597 {
13598 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13599
13600 /* If there isn't already a REG_UNUSED note, put one
13601 here. Do not place a REG_DEAD note, even if
13602 the register is also used here; that would not
13603 match the algorithm used in lifetime analysis
13604 and can cause the consistency check in the
13605 scheduler to fail. */
13606 if (! find_regno_note (tem_insn, REG_UNUSED,
13607 REGNO (XEXP (note, 0))))
13608 place = tem_insn;
13609 break;
13610 }
13611 }
13612 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13613 || (CALL_P (tem_insn)
13614 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13615 {
13616 place = tem_insn;
13617
13618 /* If we are doing a 3->2 combination, and we have a
13619 register which formerly died in i3 and was not used
13620 by i2, which now no longer dies in i3 and is used in
13621 i2 but does not die in i2, and place is between i2
13622 and i3, then we may need to move a link from place to
13623 i2. */
13624 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13625 && from_insn
13626 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13627 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13628 {
13629 struct insn_link *links = LOG_LINKS (place);
13630 LOG_LINKS (place) = NULL;
13631 distribute_links (links);
13632 }
13633 break;
13634 }
13635
13636 if (tem_insn == BB_HEAD (bb))
13637 break;
13638 }
13639
13640 }
13641
13642 /* If the register is set or already dead at PLACE, we needn't do
13643 anything with this note if it is still a REG_DEAD note.
13644 We check here if it is set at all, not if is it totally replaced,
13645 which is what `dead_or_set_p' checks, so also check for it being
13646 set partially. */
13647
13648 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13649 {
13650 unsigned int regno = REGNO (XEXP (note, 0));
13651 reg_stat_type *rsp = &reg_stat[regno];
13652
13653 if (dead_or_set_p (place, XEXP (note, 0))
13654 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13655 {
13656 /* Unless the register previously died in PLACE, clear
13657 last_death. [I no longer understand why this is
13658 being done.] */
13659 if (rsp->last_death != place)
13660 rsp->last_death = 0;
13661 place = 0;
13662 }
13663 else
13664 rsp->last_death = place;
13665
13666 /* If this is a death note for a hard reg that is occupying
13667 multiple registers, ensure that we are still using all
13668 parts of the object. If we find a piece of the object
13669 that is unused, we must arrange for an appropriate REG_DEAD
13670 note to be added for it. However, we can't just emit a USE
13671 and tag the note to it, since the register might actually
13672 be dead; so we recourse, and the recursive call then finds
13673 the previous insn that used this register. */
13674
13675 if (place && regno < FIRST_PSEUDO_REGISTER
13676 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13677 {
13678 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13679 bool all_used = true;
13680 unsigned int i;
13681
13682 for (i = regno; i < endregno; i++)
13683 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13684 && ! find_regno_fusage (place, USE, i))
13685 || dead_or_set_regno_p (place, i))
13686 {
13687 all_used = false;
13688 break;
13689 }
13690
13691 if (! all_used)
13692 {
13693 /* Put only REG_DEAD notes for pieces that are
13694 not already dead or set. */
13695
13696 for (i = regno; i < endregno;
13697 i += hard_regno_nregs[i][reg_raw_mode[i]])
13698 {
13699 rtx piece = regno_reg_rtx[i];
13700 basic_block bb = this_basic_block;
13701
13702 if (! dead_or_set_p (place, piece)
13703 && ! reg_bitfield_target_p (piece,
13704 PATTERN (place)))
13705 {
13706 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13707 NULL_RTX);
13708
13709 distribute_notes (new_note, place, place,
13710 NULL, NULL_RTX, NULL_RTX,
13711 NULL_RTX);
13712 }
13713 else if (! refers_to_regno_p (i, i + 1,
13714 PATTERN (place), 0)
13715 && ! find_regno_fusage (place, USE, i))
13716 for (tem_insn = PREV_INSN (place); ;
13717 tem_insn = PREV_INSN (tem_insn))
13718 {
13719 if (!NONDEBUG_INSN_P (tem_insn))
13720 {
13721 if (tem_insn == BB_HEAD (bb))
13722 break;
13723 continue;
13724 }
13725 if (dead_or_set_p (tem_insn, piece)
13726 || reg_bitfield_target_p (piece,
13727 PATTERN (tem_insn)))
13728 {
13729 add_reg_note (tem_insn, REG_UNUSED, piece);
13730 break;
13731 }
13732 }
13733 }
13734
13735 place = 0;
13736 }
13737 }
13738 }
13739 break;
13740
13741 default:
13742 /* Any other notes should not be present at this point in the
13743 compilation. */
13744 gcc_unreachable ();
13745 }
13746
13747 if (place)
13748 {
13749 XEXP (note, 1) = REG_NOTES (place);
13750 REG_NOTES (place) = note;
13751 }
13752
13753 if (place2)
13754 add_shallow_copy_of_reg_note (place2, note);
13755 }
13756 }
13757 \f
13758 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13759 I3, I2, and I1 to new locations. This is also called to add a link
13760 pointing at I3 when I3's destination is changed. */
13761
13762 static void
13763 distribute_links (struct insn_link *links)
13764 {
13765 struct insn_link *link, *next_link;
13766
13767 for (link = links; link; link = next_link)
13768 {
13769 rtx_insn *place = 0;
13770 rtx_insn *insn;
13771 rtx set, reg;
13772
13773 next_link = link->next;
13774
13775 /* If the insn that this link points to is a NOTE or isn't a single
13776 set, ignore it. In the latter case, it isn't clear what we
13777 can do other than ignore the link, since we can't tell which
13778 register it was for. Such links wouldn't be used by combine
13779 anyway.
13780
13781 It is not possible for the destination of the target of the link to
13782 have been changed by combine. The only potential of this is if we
13783 replace I3, I2, and I1 by I3 and I2. But in that case the
13784 destination of I2 also remains unchanged. */
13785
13786 if (NOTE_P (link->insn)
13787 || (set = single_set (link->insn)) == 0)
13788 continue;
13789
13790 reg = SET_DEST (set);
13791 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13792 || GET_CODE (reg) == STRICT_LOW_PART)
13793 reg = XEXP (reg, 0);
13794
13795 /* A LOG_LINK is defined as being placed on the first insn that uses
13796 a register and points to the insn that sets the register. Start
13797 searching at the next insn after the target of the link and stop
13798 when we reach a set of the register or the end of the basic block.
13799
13800 Note that this correctly handles the link that used to point from
13801 I3 to I2. Also note that not much searching is typically done here
13802 since most links don't point very far away. */
13803
13804 for (insn = NEXT_INSN (link->insn);
13805 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13806 || BB_HEAD (this_basic_block->next_bb) != insn));
13807 insn = NEXT_INSN (insn))
13808 if (DEBUG_INSN_P (insn))
13809 continue;
13810 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13811 {
13812 if (reg_referenced_p (reg, PATTERN (insn)))
13813 place = insn;
13814 break;
13815 }
13816 else if (CALL_P (insn)
13817 && find_reg_fusage (insn, USE, reg))
13818 {
13819 place = insn;
13820 break;
13821 }
13822 else if (INSN_P (insn) && reg_set_p (reg, insn))
13823 break;
13824
13825 /* If we found a place to put the link, place it there unless there
13826 is already a link to the same insn as LINK at that point. */
13827
13828 if (place)
13829 {
13830 struct insn_link *link2;
13831
13832 FOR_EACH_LOG_LINK (link2, place)
13833 if (link2->insn == link->insn)
13834 break;
13835
13836 if (link2 == NULL)
13837 {
13838 link->next = LOG_LINKS (place);
13839 LOG_LINKS (place) = link;
13840
13841 /* Set added_links_insn to the earliest insn we added a
13842 link to. */
13843 if (added_links_insn == 0
13844 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13845 added_links_insn = place;
13846 }
13847 }
13848 }
13849 }
13850 \f
13851 /* Check for any register or memory mentioned in EQUIV that is not
13852 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13853 of EXPR where some registers may have been replaced by constants. */
13854
13855 static bool
13856 unmentioned_reg_p (rtx equiv, rtx expr)
13857 {
13858 subrtx_iterator::array_type array;
13859 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
13860 {
13861 const_rtx x = *iter;
13862 if ((REG_P (x) || MEM_P (x))
13863 && !reg_mentioned_p (x, expr))
13864 return true;
13865 }
13866 return false;
13867 }
13868 \f
13869 DEBUG_FUNCTION void
13870 dump_combine_stats (FILE *file)
13871 {
13872 fprintf
13873 (file,
13874 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13875 combine_attempts, combine_merges, combine_extras, combine_successes);
13876 }
13877
13878 void
13879 dump_combine_total_stats (FILE *file)
13880 {
13881 fprintf
13882 (file,
13883 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13884 total_attempts, total_merges, total_extras, total_successes);
13885 }
13886 \f
13887 /* Try combining insns through substitution. */
13888 static unsigned int
13889 rest_of_handle_combine (void)
13890 {
13891 int rebuild_jump_labels_after_combine;
13892
13893 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13894 df_note_add_problem ();
13895 df_analyze ();
13896
13897 regstat_init_n_sets_and_refs ();
13898
13899 rebuild_jump_labels_after_combine
13900 = combine_instructions (get_insns (), max_reg_num ());
13901
13902 /* Combining insns may have turned an indirect jump into a
13903 direct jump. Rebuild the JUMP_LABEL fields of jumping
13904 instructions. */
13905 if (rebuild_jump_labels_after_combine)
13906 {
13907 timevar_push (TV_JUMP);
13908 rebuild_jump_labels (get_insns ());
13909 cleanup_cfg (0);
13910 timevar_pop (TV_JUMP);
13911 }
13912
13913 regstat_free_n_sets_and_refs ();
13914 return 0;
13915 }
13916
13917 namespace {
13918
13919 const pass_data pass_data_combine =
13920 {
13921 RTL_PASS, /* type */
13922 "combine", /* name */
13923 OPTGROUP_NONE, /* optinfo_flags */
13924 TV_COMBINE, /* tv_id */
13925 PROP_cfglayout, /* properties_required */
13926 0, /* properties_provided */
13927 0, /* properties_destroyed */
13928 0, /* todo_flags_start */
13929 TODO_df_finish, /* todo_flags_finish */
13930 };
13931
13932 class pass_combine : public rtl_opt_pass
13933 {
13934 public:
13935 pass_combine (gcc::context *ctxt)
13936 : rtl_opt_pass (pass_data_combine, ctxt)
13937 {}
13938
13939 /* opt_pass methods: */
13940 virtual bool gate (function *) { return (optimize > 0); }
13941 virtual unsigned int execute (function *)
13942 {
13943 return rest_of_handle_combine ();
13944 }
13945
13946 }; // class pass_combine
13947
13948 } // anon namespace
13949
13950 rtl_opt_pass *
13951 make_pass_combine (gcc::context *ctxt)
13952 {
13953 return new pass_combine (ctxt);
13954 }