1 /* Move registers around to reduce number of move instructions needed.
2    Copyright (C) 1987-2013 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 
21 /* This module makes some simple RTL code transformations which
22    improve the subsequent register allocation.  */
23 
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "insn-config.h"
31 #include "recog.h"
32 #include "target.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "expr.h"
38 #include "basic-block.h"
39 #include "except.h"
40 #include "diagnostic-core.h"
41 #include "reload.h"
42 #include "tree-pass.h"
43 #include "df.h"
44 #include "ira.h"
45 
46 static int optimize_reg_copy_1 (rtx, rtx, rtx);
47 static void optimize_reg_copy_2 (rtx, rtx, rtx);
48 static void optimize_reg_copy_3 (rtx, rtx, rtx);
49 static void copy_src_to_dest (rtx, rtx, rtx);
50 
51 enum match_use
52 {
53   READ,
54   WRITE,
55   READWRITE
56 };
57 
58 struct match {
59   int with[MAX_RECOG_OPERANDS];
60   enum match_use use[MAX_RECOG_OPERANDS];
61   int commutative[MAX_RECOG_OPERANDS];
62   int early_clobber[MAX_RECOG_OPERANDS];
63 };
64 
65 static int find_matches (rtx, struct match *);
66 static int fixup_match_2 (rtx, rtx, rtx, rtx);
67 
68 /* Return nonzero if registers with CLASS1 and CLASS2 can be merged without
69    causing too much register allocation problems.  */
70 static int
regclass_compatible_p(reg_class_t class0,reg_class_t class1)71 regclass_compatible_p (reg_class_t class0, reg_class_t class1)
72 {
73   return (class0 == class1
74 	  || (reg_class_subset_p (class0, class1)
75 	      && ! targetm.class_likely_spilled_p (class0))
76 	  || (reg_class_subset_p (class1, class0)
77 	      && ! targetm.class_likely_spilled_p (class1)));
78 }
79 
80 
81 #ifdef AUTO_INC_DEC
82 
83 /* Find the place in the rtx X where REG is used as a memory address.
84    Return the MEM rtx that so uses it.
85    If PLUSCONST is nonzero, search instead for a memory address equivalent to
86    (plus REG (const_int PLUSCONST)).
87 
88    If such an address does not appear, return 0.
89    If REG appears more than once, or is used other than in such an address,
90    return (rtx) 1.  */
91 
92 static rtx
find_use_as_address(rtx x,rtx reg,HOST_WIDE_INT plusconst)93 find_use_as_address (rtx x, rtx reg, HOST_WIDE_INT plusconst)
94 {
95   enum rtx_code code = GET_CODE (x);
96   const char * const fmt = GET_RTX_FORMAT (code);
97   int i;
98   rtx value = 0;
99   rtx tem;
100 
101   if (code == MEM && XEXP (x, 0) == reg && plusconst == 0)
102     return x;
103 
104   if (code == MEM && GET_CODE (XEXP (x, 0)) == PLUS
105       && XEXP (XEXP (x, 0), 0) == reg
106       && CONST_INT_P (XEXP (XEXP (x, 0), 1))
107       && INTVAL (XEXP (XEXP (x, 0), 1)) == plusconst)
108     return x;
109 
110   if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
111     {
112       /* If REG occurs inside a MEM used in a bit-field reference,
113 	 that is unacceptable.  */
114       if (find_use_as_address (XEXP (x, 0), reg, 0) != 0)
115 	return (rtx) (size_t) 1;
116     }
117 
118   if (x == reg)
119     return (rtx) (size_t) 1;
120 
121   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
122     {
123       if (fmt[i] == 'e')
124 	{
125 	  tem = find_use_as_address (XEXP (x, i), reg, plusconst);
126 	  if (value == 0)
127 	    value = tem;
128 	  else if (tem != 0)
129 	    return (rtx) (size_t) 1;
130 	}
131       else if (fmt[i] == 'E')
132 	{
133 	  int j;
134 	  for (j = XVECLEN (x, i) - 1; j >= 0; j--)
135 	    {
136 	      tem = find_use_as_address (XVECEXP (x, i, j), reg, plusconst);
137 	      if (value == 0)
138 		value = tem;
139 	      else if (tem != 0)
140 		return (rtx) (size_t) 1;
141 	    }
142 	}
143     }
144 
145   return value;
146 }
147 
148 
149 /* INC_INSN is an instruction that adds INCREMENT to REG.
150    Try to fold INC_INSN as a post/pre in/decrement into INSN.
151    Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
152    Return nonzero for success.  */
153 static int
try_auto_increment(rtx insn,rtx inc_insn,rtx inc_insn_set,rtx reg,HOST_WIDE_INT increment,int pre)154 try_auto_increment (rtx insn, rtx inc_insn, rtx inc_insn_set, rtx reg,
155 		    HOST_WIDE_INT increment, int pre)
156 {
157   enum rtx_code inc_code;
158 
159   rtx pset = single_set (insn);
160   if (pset)
161     {
162       /* Can't use the size of SET_SRC, we might have something like
163 	 (sign_extend:SI (mem:QI ...  */
164       rtx use = find_use_as_address (pset, reg, 0);
165       if (use != 0 && use != (rtx) (size_t) 1)
166 	{
167 	  int size = GET_MODE_SIZE (GET_MODE (use));
168 	  if (0
169 	      || (HAVE_POST_INCREMENT
170 		  && pre == 0 && (inc_code = POST_INC, increment == size))
171 	      || (HAVE_PRE_INCREMENT
172 		  && pre == 1 && (inc_code = PRE_INC, increment == size))
173 	      || (HAVE_POST_DECREMENT
174 		  && pre == 0 && (inc_code = POST_DEC, increment == -size))
175 	      || (HAVE_PRE_DECREMENT
176 		  && pre == 1 && (inc_code = PRE_DEC, increment == -size))
177 	  )
178 	    {
179 	      if (inc_insn_set)
180 		validate_change
181 		  (inc_insn,
182 		   &SET_SRC (inc_insn_set),
183 		   XEXP (SET_SRC (inc_insn_set), 0), 1);
184 	      validate_change (insn, &XEXP (use, 0),
185 			       gen_rtx_fmt_e (inc_code,
186 					      GET_MODE (XEXP (use, 0)), reg),
187 			       1);
188 	      if (apply_change_group ())
189 		{
190 		  /* If there is a REG_DEAD note on this insn, we must
191 		     change this not to REG_UNUSED meaning that the register
192 		     is set, but the value is dead.  Failure to do so will
193 		     result in sched1 dying -- when it recomputes lifetime
194 		     information, the number of REG_DEAD notes will have
195 		     changed.  */
196 		  rtx note = find_reg_note (insn, REG_DEAD, reg);
197 		  if (note)
198 		    PUT_REG_NOTE_KIND (note, REG_UNUSED);
199 
200 		  add_reg_note (insn, REG_INC, reg);
201 
202 		  if (! inc_insn_set)
203 		    delete_insn (inc_insn);
204 		  return 1;
205 		}
206 	    }
207 	}
208     }
209   return 0;
210 }
211 #endif
212 
213 
214 static int *regno_src_regno;
215 
216 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
217    in INSN.
218 
219    Search forward to see if SRC dies before either it or DEST is modified,
220    but don't scan past the end of a basic block.  If so, we can replace SRC
221    with DEST and let SRC die in INSN.
222 
223    This will reduce the number of registers live in that range and may enable
224    DEST to be tied to SRC, thus often saving one register in addition to a
225    register-register copy.  */
226 
227 static int
optimize_reg_copy_1(rtx insn,rtx dest,rtx src)228 optimize_reg_copy_1 (rtx insn, rtx dest, rtx src)
229 {
230   rtx p, q;
231   rtx note;
232   rtx dest_death = 0;
233   int sregno = REGNO (src);
234   int dregno = REGNO (dest);
235   basic_block bb = BLOCK_FOR_INSN (insn);
236 
237   /* We don't want to mess with hard regs if register classes are small.  */
238   if (sregno == dregno
239       || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
240 	  && (sregno < FIRST_PSEUDO_REGISTER
241 	      || dregno < FIRST_PSEUDO_REGISTER))
242       /* We don't see all updates to SP if they are in an auto-inc memory
243 	 reference, so we must disallow this optimization on them.  */
244       || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
245     return 0;
246 
247   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
248     {
249       if (! INSN_P (p))
250 	continue;
251       if (BLOCK_FOR_INSN (p) != bb)
252 	break;
253 
254       if (reg_set_p (src, p) || reg_set_p (dest, p)
255 	  /* If SRC is an asm-declared register, it must not be replaced
256 	     in any asm.  Unfortunately, the REG_EXPR tree for the asm
257 	     variable may be absent in the SRC rtx, so we can't check the
258 	     actual register declaration easily (the asm operand will have
259 	     it, though).  To avoid complicating the test for a rare case,
260 	     we just don't perform register replacement for a hard reg
261 	     mentioned in an asm.  */
262 	  || (sregno < FIRST_PSEUDO_REGISTER
263 	      && asm_noperands (PATTERN (p)) >= 0
264 	      && reg_overlap_mentioned_p (src, PATTERN (p)))
265 	  /* Don't change hard registers used by a call.  */
266 	  || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
267 	      && find_reg_fusage (p, USE, src))
268 	  /* Don't change a USE of a register.  */
269 	  || (GET_CODE (PATTERN (p)) == USE
270 	      && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
271 	break;
272 
273       /* See if all of SRC dies in P.  This test is slightly more
274 	 conservative than it needs to be.  */
275       if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
276 	  && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
277 	{
278 	  int failed = 0;
279 	  int d_length = 0;
280 	  int s_length = 0;
281 	  int d_n_calls = 0;
282 	  int s_n_calls = 0;
283 	  int s_freq_calls = 0;
284 	  int d_freq_calls = 0;
285 
286 	  /* We can do the optimization.  Scan forward from INSN again,
287 	     replacing regs as we go.  Set FAILED if a replacement can't
288 	     be done.  In that case, we can't move the death note for SRC.
289 	     This should be rare.  */
290 
291 	  /* Set to stop at next insn.  */
292 	  for (q = next_real_insn (insn);
293 	       q != next_real_insn (p);
294 	       q = next_real_insn (q))
295 	    {
296 	      if (reg_overlap_mentioned_p (src, PATTERN (q)))
297 		{
298 		  /* If SRC is a hard register, we might miss some
299 		     overlapping registers with validate_replace_rtx,
300 		     so we would have to undo it.  We can't if DEST is
301 		     present in the insn, so fail in that combination
302 		     of cases.  */
303 		  if (sregno < FIRST_PSEUDO_REGISTER
304 		      && reg_mentioned_p (dest, PATTERN (q)))
305 		    failed = 1;
306 
307 		  /* Attempt to replace all uses.  */
308 		  else if (!validate_replace_rtx (src, dest, q))
309 		    failed = 1;
310 
311 		  /* If this succeeded, but some part of the register
312 		     is still present, undo the replacement.  */
313 		  else if (sregno < FIRST_PSEUDO_REGISTER
314 			   && reg_overlap_mentioned_p (src, PATTERN (q)))
315 		    {
316 		      validate_replace_rtx (dest, src, q);
317 		      failed = 1;
318 		    }
319 		}
320 
321 	      /* For SREGNO, count the total number of insns scanned.
322 		 For DREGNO, count the total number of insns scanned after
323 		 passing the death note for DREGNO.  */
324 	      if (!DEBUG_INSN_P (p))
325 		{
326 		  s_length++;
327 		  if (dest_death)
328 		    d_length++;
329 		}
330 
331 	      /* If the insn in which SRC dies is a CALL_INSN, don't count it
332 		 as a call that has been crossed.  Otherwise, count it.  */
333 	      if (q != p && CALL_P (q))
334 		{
335 		  /* Similarly, total calls for SREGNO, total calls beyond
336 		     the death note for DREGNO.  */
337 		  s_n_calls++;
338 		  s_freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (q));
339 		  if (dest_death)
340 		    {
341 		      d_n_calls++;
342 		      d_freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (q));
343 		    }
344 		}
345 
346 	      /* If DEST dies here, remove the death note and save it for
347 		 later.  Make sure ALL of DEST dies here; again, this is
348 		 overly conservative.  */
349 	      if (dest_death == 0
350 		  && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
351 		{
352 		  if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
353 		    failed = 1, dest_death = 0;
354 		  else
355 		    remove_note (q, dest_death);
356 		}
357 	    }
358 
359 	  if (! failed)
360 	    {
361 	      /* These counters need to be updated if and only if we are
362 		 going to move the REG_DEAD note.  */
363 	      if (sregno >= FIRST_PSEUDO_REGISTER)
364 		{
365 		  if (REG_LIVE_LENGTH (sregno) >= 0)
366 		    {
367 		      REG_LIVE_LENGTH (sregno) -= s_length;
368 		      /* REG_LIVE_LENGTH is only an approximation after
369 			 combine if sched is not run, so make sure that we
370 			 still have a reasonable value.  */
371 		      if (REG_LIVE_LENGTH (sregno) < 2)
372 			REG_LIVE_LENGTH (sregno) = 2;
373 		    }
374 
375 		  REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
376 		  REG_FREQ_CALLS_CROSSED (sregno) -= s_freq_calls;
377 		}
378 
379 	      /* Move death note of SRC from P to INSN.  */
380 	      remove_note (p, note);
381 	      XEXP (note, 1) = REG_NOTES (insn);
382 	      REG_NOTES (insn) = note;
383 	    }
384 
385 	  /* DEST is also dead if INSN has a REG_UNUSED note for DEST.  */
386 	  if (! dest_death
387 	      && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
388 	    {
389 	      PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
390 	      remove_note (insn, dest_death);
391 	    }
392 
393 	  /* Put death note of DEST on P if we saw it die.  */
394 	  if (dest_death)
395 	    {
396 	      XEXP (dest_death, 1) = REG_NOTES (p);
397 	      REG_NOTES (p) = dest_death;
398 
399 	      if (dregno >= FIRST_PSEUDO_REGISTER)
400 		{
401 		  /* If and only if we are moving the death note for DREGNO,
402 		     then we need to update its counters.  */
403 		  if (REG_LIVE_LENGTH (dregno) >= 0)
404 		    REG_LIVE_LENGTH (dregno) += d_length;
405 		  REG_N_CALLS_CROSSED (dregno) += d_n_calls;
406 		  REG_FREQ_CALLS_CROSSED (dregno) += d_freq_calls;
407 		}
408 	    }
409 
410 	  return ! failed;
411 	}
412 
413       /* If SRC is a hard register which is set or killed in some other
414 	 way, we can't do this optimization.  */
415       else if (sregno < FIRST_PSEUDO_REGISTER
416 	       && dead_or_set_p (p, src))
417 	break;
418     }
419   return 0;
420 }
421 
422 /* INSN is a copy of SRC to DEST, in which SRC dies.  See if we now have
423    a sequence of insns that modify DEST followed by an insn that sets
424    SRC to DEST in which DEST dies, with no prior modification of DEST.
425    (There is no need to check if the insns in between actually modify
426    DEST.  We should not have cases where DEST is not modified, but
427    the optimization is safe if no such modification is detected.)
428    In that case, we can replace all uses of DEST, starting with INSN and
429    ending with the set of SRC to DEST, with SRC.  We do not do this
430    optimization if a CALL_INSN is crossed unless SRC already crosses a
431    call or if DEST dies before the copy back to SRC.
432 
433    It is assumed that DEST and SRC are pseudos; it is too complicated to do
434    this for hard registers since the substitutions we may make might fail.  */
435 
436 static void
optimize_reg_copy_2(rtx insn,rtx dest,rtx src)437 optimize_reg_copy_2 (rtx insn, rtx dest, rtx src)
438 {
439   rtx p, q;
440   rtx set;
441   int sregno = REGNO (src);
442   int dregno = REGNO (dest);
443   basic_block bb = BLOCK_FOR_INSN (insn);
444 
445   for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
446     {
447       if (! INSN_P (p))
448 	continue;
449       if (BLOCK_FOR_INSN (p) != bb)
450 	break;
451 
452       set = single_set (p);
453       if (set && SET_SRC (set) == dest && SET_DEST (set) == src
454 	  && find_reg_note (p, REG_DEAD, dest))
455 	{
456 	  /* We can do the optimization.  Scan forward from INSN again,
457 	     replacing regs as we go.  */
458 
459 	  /* Set to stop at next insn.  */
460 	  for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
461 	    if (INSN_P (q))
462 	      {
463 		if (reg_mentioned_p (dest, PATTERN (q)))
464 		  {
465 		    rtx note;
466 
467 		    PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
468 		    note = FIND_REG_INC_NOTE (q, dest);
469 		    if (note)
470 		      {
471 			remove_note (q, note);
472 			add_reg_note (q, REG_INC, src);
473 		      }
474 		    df_insn_rescan (q);
475 		  }
476 
477 		if (CALL_P (q))
478 		  {
479 		    int freq = REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (q));
480 		    REG_N_CALLS_CROSSED (dregno)--;
481 		    REG_N_CALLS_CROSSED (sregno)++;
482 		    REG_FREQ_CALLS_CROSSED (dregno) -= freq;
483 		    REG_FREQ_CALLS_CROSSED (sregno) += freq;
484 		  }
485 	      }
486 
487 	  remove_note (p, find_reg_note (p, REG_DEAD, dest));
488 	  REG_N_DEATHS (dregno)--;
489 	  remove_note (insn, find_reg_note (insn, REG_DEAD, src));
490 	  REG_N_DEATHS (sregno)--;
491 	  return;
492 	}
493 
494       if (reg_set_p (src, p)
495 	  || find_reg_note (p, REG_DEAD, dest)
496 	  || (CALL_P (p) && REG_N_CALLS_CROSSED (sregno) == 0))
497 	break;
498     }
499 }
500 
501 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
502    Look if SRC dies there, and if it is only set once, by loading
503    it from memory.  If so, try to incorporate the zero/sign extension
504    into the memory read, change SRC to the mode of DEST, and alter
505    the remaining accesses to use the appropriate SUBREG.  This allows
506    SRC and DEST to be tied later.  */
507 static void
optimize_reg_copy_3(rtx insn,rtx dest,rtx src)508 optimize_reg_copy_3 (rtx insn, rtx dest, rtx src)
509 {
510   rtx src_reg = XEXP (src, 0);
511   int src_no = REGNO (src_reg);
512   int dst_no = REGNO (dest);
513   rtx p, set, set_insn;
514   enum machine_mode old_mode;
515   basic_block bb = BLOCK_FOR_INSN (insn);
516 
517   if (src_no < FIRST_PSEUDO_REGISTER
518       || dst_no < FIRST_PSEUDO_REGISTER
519       || ! find_reg_note (insn, REG_DEAD, src_reg)
520       || REG_N_DEATHS (src_no) != 1
521       || REG_N_SETS (src_no) != 1)
522     return;
523 
524   for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
525     if (INSN_P (p) && BLOCK_FOR_INSN (p) != bb)
526       break;
527 
528   if (! p || BLOCK_FOR_INSN (p) != bb)
529     return;
530 
531   if (! (set = single_set (p))
532       || !MEM_P (SET_SRC (set))
533       /* If there's a REG_EQUIV note, this must be an insn that loads an
534 	 argument.  Prefer keeping the note over doing this optimization.  */
535       || find_reg_note (p, REG_EQUIV, NULL_RTX)
536       || SET_DEST (set) != src_reg)
537     return;
538 
539   /* Be conservative: although this optimization is also valid for
540      volatile memory references, that could cause trouble in later passes.  */
541   if (MEM_VOLATILE_P (SET_SRC (set)))
542     return;
543 
544   /* Do not use a SUBREG to truncate from one mode to another if truncation
545      is not a nop.  */
546   if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
547       && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (src), GET_MODE (src_reg)))
548     return;
549 
550   set_insn = p;
551   old_mode = GET_MODE (src_reg);
552   PUT_MODE (src_reg, GET_MODE (src));
553   XEXP (src, 0) = SET_SRC (set);
554 
555   /* Include this change in the group so that it's easily undone if
556      one of the changes in the group is invalid.  */
557   validate_change (p, &SET_SRC (set), src, 1);
558 
559   /* Now walk forward making additional replacements.  We want to be able
560      to undo all the changes if a later substitution fails.  */
561   while (p = NEXT_INSN (p), p != insn)
562     {
563       if (! INSN_P (p))
564 	continue;
565 
566       /* Make a tentative change.  */
567       validate_replace_rtx_group (src_reg,
568 				  gen_lowpart_SUBREG (old_mode, src_reg),
569 				  p);
570     }
571 
572   validate_replace_rtx_group (src, src_reg, insn);
573 
574   /* Now see if all the changes are valid.  */
575   if (! apply_change_group ())
576     {
577       /* One or more changes were no good.  Back out everything.  */
578       PUT_MODE (src_reg, old_mode);
579       XEXP (src, 0) = src_reg;
580     }
581   else
582     {
583       rtx note = find_reg_note (set_insn, REG_EQUAL, NULL_RTX);
584       if (note)
585 	{
586 	  if (rtx_equal_p (XEXP (note, 0), XEXP (src, 0)))
587 	    {
588 	      XEXP (note, 0)
589 		= gen_rtx_fmt_e (GET_CODE (src), GET_MODE (src),
590 				 XEXP (note, 0));
591 	      df_notes_rescan (set_insn);
592 	    }
593 	  else
594 	    remove_note (set_insn, note);
595 	}
596     }
597 }
598 
599 
600 /* If we were not able to update the users of src to use dest directly, try
601    instead moving the value to dest directly before the operation.  */
602 
603 static void
copy_src_to_dest(rtx insn,rtx src,rtx dest)604 copy_src_to_dest (rtx insn, rtx src, rtx dest)
605 {
606   rtx seq;
607   rtx link;
608   rtx next;
609   rtx set;
610   rtx move_insn;
611   rtx *p_insn_notes;
612   rtx *p_move_notes;
613   int src_regno;
614   int dest_regno;
615 
616   /* A REG_LIVE_LENGTH of -1 indicates the register must not go into
617      a hard register, e.g. because it crosses as setjmp.  See the
618      comment in regstat.c:regstat_bb_compute_ri.  Don't try to apply
619      any transformations to such regs.  */
620 
621   if (REG_P (src)
622       && REG_LIVE_LENGTH (REGNO (src)) > 0
623       && REG_P (dest)
624       && REG_LIVE_LENGTH (REGNO (dest)) > 0
625       && (set = single_set (insn)) != NULL_RTX
626       && !reg_mentioned_p (dest, SET_SRC (set))
627       && GET_MODE (src) == GET_MODE (dest))
628     {
629       int old_num_regs = reg_rtx_no;
630 
631       /* Generate the src->dest move.  */
632       start_sequence ();
633       emit_move_insn (dest, src);
634       seq = get_insns ();
635       end_sequence ();
636       /* If this sequence uses new registers, we may not use it.  */
637       if (old_num_regs != reg_rtx_no
638 	  || ! validate_replace_rtx (src, dest, insn))
639 	{
640 	  /* We have to restore reg_rtx_no to its old value, lest
641 	     recompute_reg_usage will try to compute the usage of the
642 	     new regs, yet reg_n_info is not valid for them.  */
643 	  reg_rtx_no = old_num_regs;
644 	  return;
645 	}
646       emit_insn_before (seq, insn);
647       move_insn = PREV_INSN (insn);
648       p_move_notes = &REG_NOTES (move_insn);
649       p_insn_notes = &REG_NOTES (insn);
650 
651       /* Move any notes mentioning src to the move instruction.  */
652       for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
653 	{
654 	  next = XEXP (link, 1);
655 	  if (XEXP (link, 0) == src)
656 	    {
657 	      *p_move_notes = link;
658 	      p_move_notes = &XEXP (link, 1);
659 	    }
660 	  else
661 	    {
662 	      *p_insn_notes = link;
663 	      p_insn_notes = &XEXP (link, 1);
664 	    }
665 	}
666 
667       *p_move_notes = NULL_RTX;
668       *p_insn_notes = NULL_RTX;
669 
670       /* Update the various register tables.  */
671       dest_regno = REGNO (dest);
672       INC_REG_N_SETS (dest_regno, 1);
673       REG_LIVE_LENGTH (dest_regno)++;
674       src_regno = REGNO (src);
675       if (! find_reg_note (move_insn, REG_DEAD, src))
676 	REG_LIVE_LENGTH (src_regno)++;
677     }
678 }
679 
680 /* reg_set_in_bb[REGNO] points to basic block iff the register is set
681    only once in the given block and has REG_EQUAL note.  */
682 
683 static basic_block *reg_set_in_bb;
684 
685 /* Size of reg_set_in_bb array.  */
686 static unsigned int max_reg_computed;
687 
688 
689 /* Return whether REG is set in only one location, and is set to a
690    constant, but is set in a different basic block from INSN (an
691    instructions which uses REG).  In this case REG is equivalent to a
692    constant, and we don't want to break that equivalence, because that
693    may increase register pressure and make reload harder.  If REG is
694    set in the same basic block as INSN, we don't worry about it,
695    because we'll probably need a register anyhow (??? but what if REG
696    is used in a different basic block as well as this one?).  */
697 
698 static bool
reg_is_remote_constant_p(rtx reg,rtx insn)699 reg_is_remote_constant_p (rtx reg, rtx insn)
700 {
701   basic_block bb;
702   rtx p;
703   int max;
704 
705   if (!reg_set_in_bb)
706     {
707       max_reg_computed = max = max_reg_num ();
708       reg_set_in_bb = XCNEWVEC (basic_block, max);
709 
710       FOR_EACH_BB (bb)
711 	FOR_BB_INSNS (bb, p)
712 	  {
713 	    rtx s;
714 
715 	    if (!INSN_P (p))
716 	      continue;
717 	    s = single_set (p);
718 	    /* This is the instruction which sets REG.  If there is a
719 	       REG_EQUAL note, then REG is equivalent to a constant.  */
720 	    if (s != 0
721 	        && REG_P (SET_DEST (s))
722 	        && REG_N_SETS (REGNO (SET_DEST (s))) == 1
723 	        && find_reg_note (p, REG_EQUAL, NULL_RTX))
724 	      reg_set_in_bb[REGNO (SET_DEST (s))] = bb;
725 	  }
726     }
727 
728   gcc_assert (REGNO (reg) < max_reg_computed);
729   if (reg_set_in_bb[REGNO (reg)] == NULL)
730     return false;
731   return (reg_set_in_bb[REGNO (reg)] != BLOCK_FOR_INSN (insn));
732 }
733 
734 /* INSN is adding a CONST_INT to a REG.  We search backwards looking for
735    another add immediate instruction with the same source and dest registers,
736    and if we find one, we change INSN to an increment, and return 1.  If
737    no changes are made, we return 0.
738 
739    This changes
740      (set (reg100) (plus reg1 offset1))
741      ...
742      (set (reg100) (plus reg1 offset2))
743    to
744      (set (reg100) (plus reg1 offset1))
745      ...
746      (set (reg100) (plus reg100 offset2-offset1))  */
747 
748 /* ??? What does this comment mean?  */
749 /* cse disrupts preincrement / postdecrement sequences when it finds a
750    hard register as ultimate source, like the frame pointer.  */
751 
752 static int
fixup_match_2(rtx insn,rtx dst,rtx src,rtx offset)753 fixup_match_2 (rtx insn, rtx dst, rtx src, rtx offset)
754 {
755   rtx p, dst_death = 0;
756   int length, num_calls = 0, freq_calls = 0;
757   basic_block bb = BLOCK_FOR_INSN (insn);
758 
759   /* If SRC dies in INSN, we'd have to move the death note.  This is
760      considered to be very unlikely, so we just skip the optimization
761      in this case.  */
762   if (find_regno_note (insn, REG_DEAD, REGNO (src)))
763     return 0;
764 
765   /* Scan backward to find the first instruction that sets DST.  */
766 
767   for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
768     {
769       rtx pset;
770 
771       if (! INSN_P (p))
772 	continue;
773       if (BLOCK_FOR_INSN (p) != bb)
774 	break;
775 
776       if (find_regno_note (p, REG_DEAD, REGNO (dst)))
777 	dst_death = p;
778       if (! dst_death && !DEBUG_INSN_P (p))
779 	length++;
780 
781       pset = single_set (p);
782       if (pset && SET_DEST (pset) == dst
783 	  && GET_CODE (SET_SRC (pset)) == PLUS
784 	  && XEXP (SET_SRC (pset), 0) == src
785 	  && CONST_INT_P (XEXP (SET_SRC (pset), 1)))
786 	{
787 	  HOST_WIDE_INT newconst
788 	    = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
789 	  rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
790 
791 	  if (add && validate_change (insn, &PATTERN (insn), add, 0))
792 	    {
793 	      /* Remove the death note for DST from DST_DEATH.  */
794 	      if (dst_death)
795 		{
796 		  remove_death (REGNO (dst), dst_death);
797 		  REG_LIVE_LENGTH (REGNO (dst)) += length;
798 		  REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
799 		  REG_FREQ_CALLS_CROSSED (REGNO (dst)) += freq_calls;
800 		}
801 
802 	      if (dump_file)
803 		fprintf (dump_file,
804 			 "Fixed operand of insn %d.\n",
805 			  INSN_UID (insn));
806 
807 #ifdef AUTO_INC_DEC
808 	      for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
809 		{
810 		  if (! INSN_P (p))
811 		    continue;
812 		  if (BLOCK_FOR_INSN (p) != bb)
813 		    break;
814 		  if (reg_overlap_mentioned_p (dst, PATTERN (p)))
815 		    {
816 		      if (try_auto_increment (p, insn, 0, dst, newconst, 0))
817 			return 1;
818 		      break;
819 		    }
820 		}
821 	      for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
822 		{
823 		  if (! INSN_P (p))
824 		    continue;
825 		  if (BLOCK_FOR_INSN (p) != bb)
826 		    break;
827 		  if (reg_overlap_mentioned_p (dst, PATTERN (p)))
828 		    {
829 		      try_auto_increment (p, insn, 0, dst, newconst, 1);
830 		      break;
831 		    }
832 		}
833 #endif
834 	      return 1;
835 	    }
836 	}
837 
838       if (reg_set_p (dst, PATTERN (p)))
839 	break;
840 
841       /* If we have passed a call instruction, and the
842          pseudo-reg SRC is not already live across a call,
843          then don't perform the optimization.  */
844       /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
845 	 hard regs are clobbered.  Thus, we only use it for src for
846 	 non-call insns.  */
847       if (CALL_P (p))
848 	{
849 	  if (! dst_death)
850 	    {
851 	      num_calls++;
852 	      freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (p));
853 	    }
854 
855 	  if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
856 	    break;
857 
858 	  if ((HARD_REGISTER_P (dst) && call_used_regs [REGNO (dst)])
859 	      || find_reg_fusage (p, CLOBBER, dst))
860 	    break;
861 	}
862       else if (reg_set_p (src, PATTERN (p)))
863 	break;
864     }
865 
866   return 0;
867 }
868 
869 /* A forward pass.  Replace output operands with input operands.  */
870 
871 static void
regmove_forward_pass(void)872 regmove_forward_pass (void)
873 {
874   basic_block bb;
875   rtx insn;
876 
877   if (! flag_expensive_optimizations)
878     return;
879 
880   if (dump_file)
881     fprintf (dump_file, "Starting forward pass...\n");
882 
883   FOR_EACH_BB (bb)
884     {
885       FOR_BB_INSNS (bb, insn)
886 	{
887 	  rtx set = single_set (insn);
888 	  if (! set)
889 	    continue;
890 
891 	  if ((GET_CODE (SET_SRC (set)) == SIGN_EXTEND
892 	       || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
893 	      && REG_P (XEXP (SET_SRC (set), 0))
894 	      && REG_P (SET_DEST (set)))
895 	    optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
896 
897 	  if (REG_P (SET_SRC (set))
898 	      && REG_P (SET_DEST (set)))
899 	    {
900 	      /* If this is a register-register copy where SRC is not dead,
901 		 see if we can optimize it.  If this optimization succeeds,
902 		 it will become a copy where SRC is dead.  */
903 	      if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
904 		   || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
905 		  && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
906 		{
907 		  /* Similarly for a pseudo-pseudo copy when SRC is dead.  */
908 		  if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
909 		    optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
910 		  if (regno_src_regno[REGNO (SET_DEST (set))] < 0
911 		      && SET_SRC (set) != SET_DEST (set))
912 		    {
913 		      int srcregno = REGNO (SET_SRC (set));
914 		      if (regno_src_regno[srcregno] >= 0)
915 			srcregno = regno_src_regno[srcregno];
916 		      regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
917 		    }
918 		}
919 	    }
920 	}
921     }
922 }
923 
924 /* A backward pass.  Replace input operands with output operands.  */
925 
926 static void
regmove_backward_pass(void)927 regmove_backward_pass (void)
928 {
929   basic_block bb;
930   rtx insn, prev;
931 
932   if (dump_file)
933     fprintf (dump_file, "Starting backward pass...\n");
934 
935   FOR_EACH_BB_REVERSE (bb)
936     {
937       /* ??? Use the safe iterator because fixup_match_2 can remove
938 	     insns via try_auto_increment.  */
939       FOR_BB_INSNS_REVERSE_SAFE (bb, insn, prev)
940 	{
941 	  struct match match;
942 	  rtx copy_src, copy_dst;
943 	  int op_no, match_no;
944 	  int success = 0;
945 
946 	  if (! INSN_P (insn))
947 	    continue;
948 
949 	  if (! find_matches (insn, &match))
950 	    continue;
951 
952 	  /* Now scan through the operands looking for a destination operand
953 	     which is supposed to match a source operand.
954 	     Then scan backward for an instruction which sets the source
955 	     operand.  If safe, then replace the source operand with the
956 	     dest operand in both instructions.  */
957 
958 	  copy_src = NULL_RTX;
959 	  copy_dst = NULL_RTX;
960 	  for (op_no = 0; op_no < recog_data.n_operands; op_no++)
961 	    {
962 	      rtx set, p, src, dst;
963 	      rtx src_note, dst_note;
964 	      int num_calls = 0, freq_calls = 0;
965 	      enum reg_class src_class, dst_class;
966 	      int length;
967 
968 	      match_no = match.with[op_no];
969 
970 	      /* Nothing to do if the two operands aren't supposed to match.  */
971 	      if (match_no < 0)
972 		continue;
973 
974 	      dst = recog_data.operand[match_no];
975 	      src = recog_data.operand[op_no];
976 
977 	      if (!REG_P (src))
978 		continue;
979 
980 	      if (!REG_P (dst)
981 		  || REGNO (dst) < FIRST_PSEUDO_REGISTER
982 		  || REG_LIVE_LENGTH (REGNO (dst)) < 0
983 		  || GET_MODE (src) != GET_MODE (dst))
984 		continue;
985 
986 	      /* If the operands already match, then there is nothing to do.  */
987 	      if (operands_match_p (src, dst))
988 		continue;
989 
990 	      if (match.commutative[op_no] >= 0)
991 		{
992 		  rtx comm = recog_data.operand[match.commutative[op_no]];
993 		  if (operands_match_p (comm, dst))
994 		    continue;
995 		}
996 
997 	      set = single_set (insn);
998 	      if (! set)
999 		continue;
1000 
1001 	      /* Note that single_set ignores parts of a parallel set for
1002 		 which one of the destinations is REG_UNUSED.  We can't
1003 		 handle that here, since we can wind up rewriting things
1004 		 such that a single register is set twice within a single
1005 		 parallel.  */
1006 	      if (reg_set_p (src, insn))
1007 		continue;
1008 
1009 	      /* match_no/dst must be a write-only operand, and
1010 		 operand_operand/src must be a read-only operand.  */
1011 	      if (match.use[op_no] != READ
1012 		  || match.use[match_no] != WRITE)
1013 		continue;
1014 
1015 	      if (match.early_clobber[match_no]
1016 		  && count_occurrences (PATTERN (insn), src, 0) > 1)
1017 		continue;
1018 
1019 	      /* Make sure match_no is the destination.  */
1020 	      if (recog_data.operand[match_no] != SET_DEST (set))
1021 		continue;
1022 
1023 	      if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1024 		{
1025 		  if (GET_CODE (SET_SRC (set)) == PLUS
1026 		      && CONST_INT_P (XEXP (SET_SRC (set), 1))
1027 		      && XEXP (SET_SRC (set), 0) == src
1028 		      && fixup_match_2 (insn, dst, src,
1029 					XEXP (SET_SRC (set), 1)))
1030 		    break;
1031 		  continue;
1032 		}
1033 	      src_class = reg_preferred_class (REGNO (src));
1034 	      dst_class = reg_preferred_class (REGNO (dst));
1035 
1036 	      if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1037 		{
1038 		  /* We used to force the copy here like in other cases, but
1039 		     it produces worse code, as it eliminates no copy
1040 		     instructions and the copy emitted will be produced by
1041 		     reload anyway.  On patterns with multiple alternatives,
1042 		     there may be better solution available.
1043 
1044 		     In particular this change produced slower code for numeric
1045 		     i387 programs.  */
1046 
1047 		  continue;
1048 		}
1049 
1050 	      if (! regclass_compatible_p (src_class, dst_class))
1051 		{
1052 		  if (!copy_src)
1053 		    {
1054 		      copy_src = src;
1055 		      copy_dst = dst;
1056 		    }
1057 		  continue;
1058 		}
1059 
1060 	      /* Can not modify an earlier insn to set dst if this insn
1061 		 uses an old value in the source.  */
1062 	      if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1063 		{
1064 		  if (!copy_src)
1065 		    {
1066 		      copy_src = src;
1067 		      copy_dst = dst;
1068 		    }
1069 		  continue;
1070 		}
1071 
1072 	      /* If src is set once in a different basic block,
1073 		 and is set equal to a constant, then do not use
1074 		 it for this optimization, as this would make it
1075 		 no longer equivalent to a constant.  */
1076 
1077 	      if (reg_is_remote_constant_p (src, insn))
1078 		{
1079 		  if (!copy_src)
1080 		    {
1081 		      copy_src = src;
1082 		      copy_dst = dst;
1083 		    }
1084 		  continue;
1085 		}
1086 
1087 
1088 	      if (dump_file)
1089 		fprintf (dump_file,
1090 			 "Could fix operand %d of insn %d matching operand %d.\n",
1091 			 op_no, INSN_UID (insn), match_no);
1092 
1093 	      /* Scan backward to find the first instruction that uses
1094 		 the input operand.  If the operand is set here, then
1095 		 replace it in both instructions with match_no.  */
1096 
1097 	      for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1098 		{
1099 		  rtx pset;
1100 
1101 		  if (! INSN_P (p))
1102 		    continue;
1103 		  if (BLOCK_FOR_INSN (p) != bb)
1104 		    break;
1105 
1106 		  if (!DEBUG_INSN_P (p))
1107 		    length++;
1108 
1109 		  /* ??? See if all of SRC is set in P.  This test is much
1110 		     more conservative than it needs to be.  */
1111 		  pset = single_set (p);
1112 		  if (pset && SET_DEST (pset) == src)
1113 		    {
1114 		      /* We use validate_replace_rtx, in case there
1115 			 are multiple identical source operands.  All
1116 			 of them have to be changed at the same time:
1117 			 when validate_replace_rtx() calls
1118 			 apply_change_group().  */
1119 		      validate_change (p, &SET_DEST (pset), dst, 1);
1120 		      if (validate_replace_rtx (src, dst, insn))
1121 			success = 1;
1122 		      break;
1123 		    }
1124 
1125 		  /* We can't make this change if DST is mentioned at
1126 		     all in P, since we are going to change its value.
1127 		     We can't make this change if SRC is read or
1128 		     partially written in P, since we are going to
1129 		     eliminate SRC.  However, if it's a debug insn, we
1130 		     can't refrain from making the change, for this
1131 		     would cause codegen differences, so instead we
1132 		     invalidate debug expressions that reference DST,
1133 		     and adjust references to SRC in them so that they
1134 		     become references to DST.  */
1135 		  if (reg_mentioned_p (dst, PATTERN (p)))
1136 		    {
1137 		      if (DEBUG_INSN_P (p))
1138 			validate_change (p, &INSN_VAR_LOCATION_LOC (p),
1139 					 gen_rtx_UNKNOWN_VAR_LOC (), 1);
1140 		      else
1141 			break;
1142 		    }
1143 		  if (reg_overlap_mentioned_p (src, PATTERN (p)))
1144 		    {
1145 		      if (DEBUG_INSN_P (p))
1146 			validate_replace_rtx_group (src, dst, p);
1147 		      else
1148 			break;
1149 		    }
1150 
1151 		  /* If we have passed a call instruction, and the
1152 		     pseudo-reg DST is not already live across a call,
1153 		     then don't perform the optimization.  */
1154 		  if (CALL_P (p))
1155 		    {
1156 		      num_calls++;
1157 		      freq_calls += REG_FREQ_FROM_BB  (BLOCK_FOR_INSN (p));
1158 
1159 		      if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1160 			break;
1161 		    }
1162 		}
1163 
1164 	      if (success)
1165 		{
1166 		  int dstno, srcno;
1167 
1168 		  /* Remove the death note for SRC from INSN.  */
1169 		  remove_note (insn, src_note);
1170 		  /* Move the death note for SRC to P if it is used
1171 		     there.  */
1172 		  if (reg_overlap_mentioned_p (src, PATTERN (p)))
1173 		    {
1174 		      XEXP (src_note, 1) = REG_NOTES (p);
1175 		      REG_NOTES (p) = src_note;
1176 		    }
1177 		  /* If there is a REG_DEAD note for DST on P, then remove
1178 		     it, because DST is now set there.  */
1179 		  if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1180 		    remove_note (p, dst_note);
1181 
1182 		  dstno = REGNO (dst);
1183 		  srcno = REGNO (src);
1184 
1185 		  INC_REG_N_SETS (dstno, 1);
1186 		  INC_REG_N_SETS (srcno, -1);
1187 
1188 		  REG_N_CALLS_CROSSED (dstno) += num_calls;
1189 		  REG_N_CALLS_CROSSED (srcno) -= num_calls;
1190 		  REG_FREQ_CALLS_CROSSED (dstno) += freq_calls;
1191 		  REG_FREQ_CALLS_CROSSED (srcno) -= freq_calls;
1192 
1193 		  REG_LIVE_LENGTH (dstno) += length;
1194 		  if (REG_LIVE_LENGTH (srcno) >= 0)
1195 		    {
1196 		      REG_LIVE_LENGTH (srcno) -= length;
1197 		      /* REG_LIVE_LENGTH is only an approximation after
1198 			 combine if sched is not run, so make sure that we
1199 			 still have a reasonable value.  */
1200 		      if (REG_LIVE_LENGTH (srcno) < 2)
1201 			REG_LIVE_LENGTH (srcno) = 2;
1202 		    }
1203 
1204 		  if (dump_file)
1205 		    fprintf (dump_file,
1206 			     "Fixed operand %d of insn %d matching operand %d.\n",
1207 			     op_no, INSN_UID (insn), match_no);
1208 
1209 		  break;
1210 		}
1211 	      else if (num_changes_pending () > 0)
1212 		cancel_changes (0);
1213 	    }
1214 
1215 	  /* If we weren't able to replace any of the alternatives, try an
1216 	     alternative approach of copying the source to the destination.  */
1217 	  if (!success && copy_src != NULL_RTX)
1218 	    copy_src_to_dest (insn, copy_src, copy_dst);
1219 	}
1220     }
1221 }
1222 
1223 /* Main entry for the register move optimization.  */
1224 
1225 static unsigned int
regmove_optimize(void)1226 regmove_optimize (void)
1227 {
1228   int i;
1229   int nregs = max_reg_num ();
1230 
1231   df_note_add_problem ();
1232   df_analyze ();
1233 
1234   regstat_init_n_sets_and_refs ();
1235   regstat_compute_ri ();
1236 
1237   if (flag_ira_loop_pressure)
1238     ira_set_pseudo_classes (true, dump_file);
1239 
1240   regno_src_regno = XNEWVEC (int, nregs);
1241   for (i = nregs; --i >= 0; )
1242     regno_src_regno[i] = -1;
1243 
1244   /* A forward pass.  Replace output operands with input operands.  */
1245   regmove_forward_pass ();
1246 
1247   /* A backward pass.  Replace input operands with output operands.  */
1248   regmove_backward_pass ();
1249 
1250   /* Clean up.  */
1251   free (regno_src_regno);
1252   if (reg_set_in_bb)
1253     {
1254       free (reg_set_in_bb);
1255       reg_set_in_bb = NULL;
1256     }
1257   regstat_free_n_sets_and_refs ();
1258   regstat_free_ri ();
1259   if (flag_ira_loop_pressure)
1260     free_reg_info ();
1261   return 0;
1262 }
1263 
1264 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1265    Returns 0 if INSN can't be recognized, or if the alternative can't be
1266    determined.
1267 
1268    Initialize the info in MATCHP based on the constraints.  */
1269 
1270 static int
find_matches(rtx insn,struct match * matchp)1271 find_matches (rtx insn, struct match *matchp)
1272 {
1273   int likely_spilled[MAX_RECOG_OPERANDS];
1274   int op_no;
1275   int any_matches = 0;
1276 
1277   extract_insn (insn);
1278   if (! constrain_operands (0))
1279     return 0;
1280 
1281   /* Must initialize this before main loop, because the code for
1282      the commutative case may set matches for operands other than
1283      the current one.  */
1284   for (op_no = recog_data.n_operands; --op_no >= 0; )
1285     matchp->with[op_no] = matchp->commutative[op_no] = -1;
1286 
1287   for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1288     {
1289       const char *p;
1290       char c;
1291       int i = 0;
1292 
1293       p = recog_data.constraints[op_no];
1294 
1295       likely_spilled[op_no] = 0;
1296       matchp->use[op_no] = READ;
1297       matchp->early_clobber[op_no] = 0;
1298       if (*p == '=')
1299 	matchp->use[op_no] = WRITE;
1300       else if (*p == '+')
1301 	matchp->use[op_no] = READWRITE;
1302 
1303       for (;*p && i < which_alternative; p++)
1304 	if (*p == ',')
1305 	  i++;
1306 
1307       while ((c = *p) != '\0' && c != ',')
1308 	{
1309 	  switch (c)
1310 	    {
1311 	    case '=':
1312 	      break;
1313 	    case '+':
1314 	      break;
1315 	    case '&':
1316 	      matchp->early_clobber[op_no] = 1;
1317 	      break;
1318 	    case '%':
1319 	      matchp->commutative[op_no] = op_no + 1;
1320 	      matchp->commutative[op_no + 1] = op_no;
1321 	      break;
1322 
1323 	    case '0': case '1': case '2': case '3': case '4':
1324 	    case '5': case '6': case '7': case '8': case '9':
1325 	      {
1326 		char *end;
1327 		unsigned long match_ul = strtoul (p, &end, 10);
1328 		int match = match_ul;
1329 
1330 		p = end;
1331 
1332 		if (match < op_no && likely_spilled[match])
1333 		  continue;
1334 		matchp->with[op_no] = match;
1335 		any_matches = 1;
1336 		if (matchp->commutative[op_no] >= 0)
1337 		  matchp->with[matchp->commutative[op_no]] = match;
1338 	      }
1339 	    continue;
1340 
1341 	  case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1342 	  case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1343 	  case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1344 	  case 'C': case 'D': case 'W': case 'Y': case 'Z':
1345 	    if (targetm.class_likely_spilled_p (REG_CLASS_FROM_CONSTRAINT ((unsigned char) c, p)))
1346 	      likely_spilled[op_no] = 1;
1347 	    break;
1348 	  }
1349 	  p += CONSTRAINT_LEN (c, p);
1350 	}
1351     }
1352   return any_matches;
1353 }
1354 
1355 
1356 
1357 static bool
gate_handle_regmove(void)1358 gate_handle_regmove (void)
1359 {
1360   return (optimize > 0 && flag_regmove);
1361 }
1362 
1363 
1364 struct rtl_opt_pass pass_regmove =
1365 {
1366  {
1367   RTL_PASS,
1368   "regmove",                            /* name */
1369   OPTGROUP_NONE,                        /* optinfo_flags */
1370   gate_handle_regmove,                  /* gate */
1371   regmove_optimize,			/* execute */
1372   NULL,                                 /* sub */
1373   NULL,                                 /* next */
1374   0,                                    /* static_pass_number */
1375   TV_REGMOVE,                           /* tv_id */
1376   0,                                    /* properties_required */
1377   0,                                    /* properties_provided */
1378   0,                                    /* properties_destroyed */
1379   0,                                    /* todo_flags_start */
1380   TODO_df_finish | TODO_verify_rtl_sharing |
1381   TODO_ggc_collect                      /* todo_flags_finish */
1382  }
1383 };
1384