summaryrefslogtreecommitdiff
path: root/gcc/config/sh/sh-mem.cc
blob: 1b84f9b63b7766d304b6d14612a9238f8367f7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/* Helper routines for memory move and comparison insns.
   Copyright (C) 2013-2014 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "machmode.h"
#include "rtl.h"
#include "tree.h"
#include "expr.h"
#include "tm_p.h"
#include "basic-block.h"

/* Like force_operand, but guarantees that VALUE ends up in TARGET.  */
static void
force_into (rtx value, rtx target)
{
  value = force_operand (value, target);
  if (! rtx_equal_p (value, target))
    emit_insn (gen_move_insn (target, value));
}

/* Emit code to perform a block move.  Choose the best method.

   OPERANDS[0] is the destination.
   OPERANDS[1] is the source.
   OPERANDS[2] is the size.
   OPERANDS[3] is the alignment safe to use.  */
bool
expand_block_move (rtx *operands)
{
  int align = INTVAL (operands[3]);
  int constp = (CONST_INT_P (operands[2]));
  int bytes = (constp ? INTVAL (operands[2]) : 0);

  if (! constp)
    return false;

  /* If we could use mov.l to move words and dest is word-aligned, we
     can use movua.l for loads and still generate a relatively short
     and efficient sequence.  */
  if (TARGET_SH4A_ARCH && align < 4
      && MEM_ALIGN (operands[0]) >= 32
      && can_move_by_pieces (bytes, 32))
    {
      rtx dest = copy_rtx (operands[0]);
      rtx src = copy_rtx (operands[1]);
      /* We could use different pseudos for each copied word, but
	 since movua can only load into r0, it's kind of
	 pointless.  */
      rtx temp = gen_reg_rtx (SImode);
      rtx src_addr = copy_addr_to_reg (XEXP (src, 0));
      int copied = 0;

      while (copied + 4 <= bytes)
	{
	  rtx to = adjust_address (dest, SImode, copied);
	  rtx from = adjust_automodify_address (src, BLKmode,
						src_addr, copied);

	  set_mem_size (from, 4);
	  emit_insn (gen_movua (temp, from));
	  emit_move_insn (src_addr, plus_constant (Pmode, src_addr, 4));
	  emit_move_insn (to, temp);
	  copied += 4;
	}

      if (copied < bytes)
	move_by_pieces (adjust_address (dest, BLKmode, copied),
			adjust_automodify_address (src, BLKmode,
						   src_addr, copied),
			bytes - copied, align, 0);

      return true;
    }

  /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
     alignment, or if it isn't a multiple of 4 bytes, then fail.  */
  if (align < 4 || (bytes % 4 != 0))
    return false;

  if (TARGET_HARD_SH4)
    {
      if (bytes < 12)
	return false;
      else if (bytes == 12)
	{
	  rtx func_addr_rtx = gen_reg_rtx (Pmode);
	  rtx r4 = gen_rtx_REG (SImode, 4);
	  rtx r5 = gen_rtx_REG (SImode, 5);

	  function_symbol (func_addr_rtx, "__movmemSI12_i4", SFUNC_STATIC);
	  force_into (XEXP (operands[0], 0), r4);
	  force_into (XEXP (operands[1], 0), r5);
	  emit_insn (gen_block_move_real_i4 (func_addr_rtx));
	  return true;
	}
      else if (! optimize_size)
	{
	  const char *entry_name;
	  rtx func_addr_rtx = gen_reg_rtx (Pmode);
	  int dwords;
	  rtx r4 = gen_rtx_REG (SImode, 4);
	  rtx r5 = gen_rtx_REG (SImode, 5);
	  rtx r6 = gen_rtx_REG (SImode, 6);

	  entry_name = (bytes & 4 ? "__movmem_i4_odd" : "__movmem_i4_even");
	  function_symbol (func_addr_rtx, entry_name, SFUNC_STATIC);
	  force_into (XEXP (operands[0], 0), r4);
	  force_into (XEXP (operands[1], 0), r5);

	  dwords = bytes >> 3;
	  emit_insn (gen_move_insn (r6, GEN_INT (dwords - 1)));
	  emit_insn (gen_block_lump_real_i4 (func_addr_rtx));
	  return true;
	}
      else
	return false;
    }
  if (bytes < 64)
    {
      char entry[30];
      rtx func_addr_rtx = gen_reg_rtx (Pmode);
      rtx r4 = gen_rtx_REG (SImode, 4);
      rtx r5 = gen_rtx_REG (SImode, 5);

      sprintf (entry, "__movmemSI%d", bytes);
      function_symbol (func_addr_rtx, entry, SFUNC_STATIC);
      force_into (XEXP (operands[0], 0), r4);
      force_into (XEXP (operands[1], 0), r5);
      emit_insn (gen_block_move_real (func_addr_rtx));
      return true;
    }

  /* This is the same number of bytes as a memcpy call, but to a different
     less common function name, so this will occasionally use more space.  */
  if (! optimize_size)
    {
      rtx func_addr_rtx = gen_reg_rtx (Pmode);
      int final_switch, while_loop;
      rtx r4 = gen_rtx_REG (SImode, 4);
      rtx r5 = gen_rtx_REG (SImode, 5);
      rtx r6 = gen_rtx_REG (SImode, 6);

      function_symbol (func_addr_rtx, "__movmem", SFUNC_STATIC);
      force_into (XEXP (operands[0], 0), r4);
      force_into (XEXP (operands[1], 0), r5);

      /* r6 controls the size of the move.  16 is decremented from it
	 for each 64 bytes moved.  Then the negative bit left over is used
	 as an index into a list of move instructions.  e.g., a 72 byte move
	 would be set up with size(r6) = 14, for one iteration through the
	 big while loop, and a switch of -2 for the last part.  */

      final_switch = 16 - ((bytes / 4) % 16);
      while_loop = ((bytes / 4) / 16 - 1) * 16;
      emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
      emit_insn (gen_block_lump_real (func_addr_rtx));
      return true;
    }

  return false;
}

static int prob_unlikely = REG_BR_PROB_BASE / 10;
static int prob_likely = REG_BR_PROB_BASE / 4;

/* Emit code to perform a strcmp.

   OPERANDS[0] is the destination.
   OPERANDS[1] is the first string.
   OPERANDS[2] is the second string.
   OPERANDS[3] is the known alignment.  */
bool
sh_expand_cmpstr (rtx *operands)
{
  rtx addr1 = operands[1];
  rtx addr2 = operands[2];
  rtx s1_addr = copy_addr_to_reg (XEXP (addr1, 0));
  rtx s2_addr = copy_addr_to_reg (XEXP (addr2, 0));
  rtx tmp0 = gen_reg_rtx (SImode);
  rtx tmp1 = gen_reg_rtx (SImode);
  rtx tmp2 = gen_reg_rtx (SImode);
  rtx tmp3 = gen_reg_rtx (SImode);

  rtx jump;
  rtx L_return = gen_label_rtx ();
  rtx L_loop_byte = gen_label_rtx ();
  rtx L_end_loop_byte = gen_label_rtx ();
  rtx L_loop_long = gen_label_rtx ();
  rtx L_end_loop_long = gen_label_rtx ();

  int align = INTVAL (operands[3]);

  emit_move_insn (tmp0, const0_rtx);

  if (align < 4)
    {
      emit_insn (gen_iorsi3 (tmp1, s1_addr, s2_addr));
      emit_insn (gen_tstsi_t (GEN_INT (3), tmp1));
      jump = emit_jump_insn (gen_branch_false (L_loop_byte));
      add_int_reg_note (jump, REG_BR_PROB, prob_likely);
    }

  addr1 = adjust_automodify_address (addr1, SImode, s1_addr, 0);
  addr2 = adjust_automodify_address (addr2, SImode, s2_addr, 0);

  /* tmp2 is aligned, OK to load.  */
  emit_move_insn (tmp3, addr2);
  emit_move_insn (s2_addr, plus_constant (Pmode, s2_addr, 4));

  /*start long loop.  */
  emit_label (L_loop_long);

  emit_move_insn (tmp2, tmp3);

  /* tmp1 is aligned, OK to load.  */
  emit_move_insn (tmp1, addr1);
  emit_move_insn (s1_addr, plus_constant (Pmode, s1_addr, 4));

  /* Is there a 0 byte ?  */
  emit_insn (gen_andsi3 (tmp3, tmp3, tmp1));

  emit_insn (gen_cmpstr_t (tmp0, tmp3));
  jump = emit_jump_insn (gen_branch_true (L_end_loop_long));
  add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

  emit_insn (gen_cmpeqsi_t (tmp1, tmp2));

  /* tmp2 is aligned, OK to load.  */
  emit_move_insn (tmp3, addr2);
  emit_move_insn (s2_addr, plus_constant (Pmode, s2_addr, 4));

  jump = emit_jump_insn (gen_branch_true (L_loop_long));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);
  /* end loop.  */

  /* Fallthu, substract words.  */
  if (TARGET_LITTLE_ENDIAN)
    {
      rtx low_1 = gen_lowpart (HImode, tmp1);
      rtx low_2 = gen_lowpart (HImode, tmp2);

      emit_insn (gen_rotlhi3_8 (low_1, low_1));
      emit_insn (gen_rotlhi3_8 (low_2, low_2));
      emit_insn (gen_rotlsi3_16 (tmp1, tmp1));
      emit_insn (gen_rotlsi3_16 (tmp2, tmp2));
      emit_insn (gen_rotlhi3_8 (low_1, low_1));
      emit_insn (gen_rotlhi3_8 (low_2, low_2));
    }

  jump = emit_jump_insn (gen_jump_compact (L_return));
  emit_barrier_after (jump);

  emit_label (L_end_loop_long);

  emit_move_insn (s1_addr, plus_constant (Pmode, s1_addr, -4));
  emit_move_insn (s2_addr, plus_constant (Pmode, s2_addr, -4));

  /* start byte loop.  */
  addr1 = adjust_address (addr1, QImode, 0);
  addr2 = adjust_address (addr2, QImode, 0);

  emit_label (L_loop_byte);

  emit_insn (gen_extendqisi2 (tmp2, addr2));
  emit_move_insn (s2_addr, plus_constant (Pmode, s2_addr, 1));

  emit_insn (gen_extendqisi2 (tmp1, addr1));
  emit_move_insn (s1_addr, plus_constant (Pmode, s1_addr, 1));

  emit_insn (gen_cmpeqsi_t (tmp2, const0_rtx));
  jump = emit_jump_insn (gen_branch_true (L_end_loop_byte));
  add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

  emit_insn (gen_cmpeqsi_t (tmp1, tmp2));
  if (flag_delayed_branch)
    emit_insn (gen_zero_extendqisi2 (tmp2, gen_lowpart (QImode, tmp2)));
  jump = emit_jump_insn (gen_branch_true (L_loop_byte));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);
  /* end loop.  */

  emit_label (L_end_loop_byte);

  if (! flag_delayed_branch)
    emit_insn (gen_zero_extendqisi2 (tmp2, gen_lowpart (QImode, tmp2)));
  emit_insn (gen_zero_extendqisi2 (tmp1, gen_lowpart (QImode, tmp1)));

  emit_label (L_return);

  emit_insn (gen_subsi3 (operands[0], tmp1, tmp2));

  return true;
}

/* Emit code to perform a strncmp.

   OPERANDS[0] is the destination.
   OPERANDS[1] is the first string.
   OPERANDS[2] is the second string.
   OPERANDS[3] is the length.
   OPERANDS[4] is the known alignment.  */
bool
sh_expand_cmpnstr (rtx *operands)
{
  rtx addr1 = operands[1];
  rtx addr2 = operands[2];
  rtx s1_addr = copy_addr_to_reg (XEXP (addr1, 0));
  rtx s2_addr = copy_addr_to_reg (XEXP (addr2, 0));
  rtx tmp1 = gen_reg_rtx (SImode);
  rtx tmp2 = gen_reg_rtx (SImode);

  rtx jump;
  rtx L_return = gen_label_rtx ();
  rtx L_loop_byte = gen_label_rtx ();
  rtx L_end_loop_byte = gen_label_rtx ();

  rtx len = force_reg (SImode, operands[3]);
  int constp = CONST_INT_P (operands[3]);

  /* Loop on a register count. */
  if (constp)
    {
      rtx tmp0 = gen_reg_rtx (SImode);
      rtx tmp3 = gen_reg_rtx (SImode);
      rtx lenw = gen_reg_rtx (SImode);

      rtx L_loop_long = gen_label_rtx ();
      rtx L_end_loop_long = gen_label_rtx ();

      int align = INTVAL (operands[4]);
      int bytes = INTVAL (operands[3]);
      int witers = bytes / 4;

      if (witers > 1)
        {
          addr1 = adjust_automodify_address (addr1, SImode, s1_addr, 0);
          addr2 = adjust_automodify_address (addr2, SImode, s2_addr, 0);

          emit_move_insn (tmp0, const0_rtx);

          if (align < 4)
            {
              emit_insn (gen_iorsi3 (tmp1, s1_addr, s2_addr));
              emit_insn (gen_tstsi_t (GEN_INT (3), tmp1));
              jump = emit_jump_insn (gen_branch_false (L_loop_byte));
              add_int_reg_note (jump, REG_BR_PROB, prob_likely);
            }

          /* word count. Do we have iterations ? */
          emit_insn (gen_lshrsi3 (lenw, len, GEN_INT (2)));

          /*start long loop.  */
          emit_label (L_loop_long);

          /* tmp2 is aligned, OK to load.  */
          emit_move_insn (tmp2, addr2);
          emit_move_insn (s2_addr, plus_constant (Pmode, s2_addr,
                                                  GET_MODE_SIZE (SImode)));

          /* tmp1 is aligned, OK to load.  */
          emit_move_insn (tmp1, addr1);
          emit_move_insn (s1_addr, plus_constant (Pmode, s1_addr,
                                                  GET_MODE_SIZE (SImode)));

          /* Is there a 0 byte ?  */
          emit_insn (gen_andsi3 (tmp3, tmp2, tmp1));

          emit_insn (gen_cmpstr_t (tmp0, tmp3));
          jump = emit_jump_insn (gen_branch_true (L_end_loop_long));
          add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

          emit_insn (gen_cmpeqsi_t (tmp1, tmp2));
          jump = emit_jump_insn (gen_branch_false (L_end_loop_long));
          add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

          if (TARGET_SH2)
            emit_insn (gen_dect (lenw, lenw));
          else
            {
              emit_insn (gen_addsi3 (lenw, lenw, GEN_INT (-1)));
              emit_insn (gen_tstsi_t (lenw, lenw));
            }

          jump = emit_jump_insn (gen_branch_false (L_loop_long));
          add_int_reg_note (jump, REG_BR_PROB, prob_likely);

         int sbytes = bytes % 4;

          /* end loop.  Reached max iterations.  */
          if (! sbytes)
            {
              jump = emit_jump_insn (gen_jump_compact (L_return));
              emit_barrier_after (jump);
            }
          else
            {
              /* Remaining bytes to check.  */

              addr1 = adjust_automodify_address (addr1, QImode, s1_addr, 0);
              addr2 = adjust_automodify_address (addr2, QImode, s2_addr, 0);

              while (sbytes--)
                {
                  emit_insn (gen_extendqisi2 (tmp1, addr1));
                  emit_insn (gen_extendqisi2 (tmp2, addr2));

                  emit_insn (gen_cmpeqsi_t (tmp2, const0_rtx));
                  jump = emit_jump_insn (gen_branch_true (L_end_loop_byte));
                  add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

                  emit_insn (gen_cmpeqsi_t (tmp1, tmp2));
                  if (flag_delayed_branch)
                    emit_insn (gen_zero_extendqisi2 (tmp2,
                                                     gen_lowpart (QImode,
                                                                  tmp2)));
                  jump = emit_jump_insn (gen_branch_false (L_end_loop_byte));
                  add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

                  addr1 = adjust_address (addr1, QImode,
                                          GET_MODE_SIZE (QImode));
                  addr2 = adjust_address (addr2, QImode,
                                          GET_MODE_SIZE (QImode));
                }

              jump = emit_jump_insn (gen_jump_compact( L_end_loop_byte));
              emit_barrier_after (jump);
            }

          emit_label (L_end_loop_long);

          /* Found last word.  Restart it byte per byte. */

          emit_move_insn (s1_addr, plus_constant (Pmode, s1_addr,
                                                  -GET_MODE_SIZE (SImode)));
          emit_move_insn (s2_addr, plus_constant (Pmode, s2_addr,
                                                  -GET_MODE_SIZE (SImode)));

          /* fall thru.  */
        }

      addr1 = adjust_automodify_address (addr1, QImode, s1_addr, 0);
      addr2 = adjust_automodify_address (addr2, QImode, s2_addr, 0);

      while (bytes--)
        {
          emit_insn (gen_extendqisi2 (tmp1, addr1));
          emit_insn (gen_extendqisi2 (tmp2, addr2));

          emit_insn (gen_cmpeqsi_t (tmp2, const0_rtx));
          jump = emit_jump_insn (gen_branch_true (L_end_loop_byte));
          add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

          emit_insn (gen_cmpeqsi_t (tmp1, tmp2));
          if (flag_delayed_branch)
            emit_insn (gen_zero_extendqisi2 (tmp2,
                                             gen_lowpart (QImode, tmp2)));
          jump = emit_jump_insn (gen_branch_false (L_end_loop_byte));
          add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

          addr1 = adjust_address (addr1, QImode, GET_MODE_SIZE (QImode));
          addr2 = adjust_address (addr2, QImode, GET_MODE_SIZE (QImode));
        }

      jump = emit_jump_insn (gen_jump_compact( L_end_loop_byte));
      emit_barrier_after (jump);
    }

  addr1 = adjust_automodify_address (addr1, QImode, s1_addr, 0);
  addr2 = adjust_automodify_address (addr2, QImode, s2_addr, 0);

  emit_label (L_loop_byte);

  emit_insn (gen_extendqisi2 (tmp2, addr2));
  emit_move_insn (s2_addr, plus_constant (Pmode, s2_addr, 1));

  emit_insn (gen_extendqisi2 (tmp1, addr1));
  emit_move_insn (s1_addr, plus_constant (Pmode, s1_addr, 1));

  emit_insn (gen_cmpeqsi_t (tmp2, const0_rtx));
  jump = emit_jump_insn (gen_branch_true (L_end_loop_byte));
  add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

  emit_insn (gen_cmpeqsi_t (tmp1, tmp2));
  if (flag_delayed_branch)
    emit_insn (gen_zero_extendqisi2 (tmp2, gen_lowpart (QImode, tmp2)));
  jump = emit_jump_insn (gen_branch_false (L_end_loop_byte));
  add_int_reg_note (jump, REG_BR_PROB, prob_unlikely);

  if (TARGET_SH2)
    emit_insn (gen_dect (len, len));
  else
    {
      emit_insn (gen_addsi3 (len, len, GEN_INT (-1)));
      emit_insn (gen_tstsi_t (len, len));
    }

  jump = emit_jump_insn (gen_branch_false (L_loop_byte));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);
  /* end byte loop.  */

  emit_label (L_end_loop_byte);

  if (! flag_delayed_branch)
    emit_insn (gen_zero_extendqisi2 (tmp2, gen_lowpart (QImode, tmp2)));
  emit_insn (gen_zero_extendqisi2 (tmp1, gen_lowpart (QImode, tmp1)));

  emit_label (L_return);

  emit_insn (gen_subsi3 (operands[0], tmp1, tmp2));

  return true;
}

/* Emit code to perform a strlen

   OPERANDS[0] is the destination.
   OPERANDS[1] is the string.
   OPERANDS[2] is the char to search.
   OPERANDS[3] is the alignment.  */
bool
sh_expand_strlen (rtx *operands)
{
  rtx addr1 = operands[1];
  rtx current_addr = copy_addr_to_reg (XEXP (addr1, 0));
  rtx start_addr = gen_reg_rtx (Pmode);
  rtx tmp0 = gen_reg_rtx (SImode);
  rtx tmp1 = gen_reg_rtx (SImode);
  rtx L_return = gen_label_rtx ();
  rtx L_loop_byte = gen_label_rtx ();

  rtx jump;
  rtx L_loop_long = gen_label_rtx ();
  rtx L_end_loop_long = gen_label_rtx ();

  int align = INTVAL (operands[3]);

  emit_move_insn (operands[0], GEN_INT (-1));

  /* remember start of string.  */
  emit_move_insn (start_addr, current_addr);

  if (align < 4)
    {
      emit_insn (gen_tstsi_t (GEN_INT (3), current_addr));
      jump = emit_jump_insn (gen_branch_false (L_loop_byte));
      add_int_reg_note (jump, REG_BR_PROB, prob_likely);
    }

  emit_move_insn (tmp0, operands[2]);

  addr1 = adjust_automodify_address (addr1, SImode, current_addr, 0);

  /*start long loop.  */
  emit_label (L_loop_long);

  /* tmp1 is aligned, OK to load.  */
  emit_move_insn (tmp1, addr1);
  emit_move_insn (current_addr, plus_constant (Pmode, current_addr, 4));

  /* Is there a 0 byte ?  */
  emit_insn (gen_cmpstr_t (tmp0, tmp1));

  jump = emit_jump_insn (gen_branch_false (L_loop_long));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);
  /* end loop.  */

  emit_label (L_end_loop_long);

  emit_move_insn (current_addr, plus_constant (Pmode, current_addr, -4));

  addr1 = adjust_address (addr1, QImode, 0);

  /* unroll remaining bytes.  */
  emit_insn (gen_extendqisi2 (tmp1, addr1));
  emit_insn (gen_cmpeqsi_t (tmp1, const0_rtx));
  jump = emit_jump_insn (gen_branch_true (L_return));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);

  emit_move_insn (current_addr, plus_constant (Pmode, current_addr, 1));

  emit_insn (gen_extendqisi2 (tmp1, addr1));
  emit_insn (gen_cmpeqsi_t (tmp1, const0_rtx));
  jump = emit_jump_insn (gen_branch_true (L_return));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);

  emit_move_insn (current_addr, plus_constant (Pmode, current_addr, 1));

  emit_insn (gen_extendqisi2 (tmp1, addr1));
  emit_insn (gen_cmpeqsi_t (tmp1, const0_rtx));
  jump = emit_jump_insn (gen_branch_true (L_return));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);

  emit_move_insn (current_addr, plus_constant (Pmode, current_addr, 1));

  emit_insn (gen_extendqisi2 (tmp1, addr1));
  jump = emit_jump_insn (gen_jump_compact (L_return));
  emit_barrier_after (jump);

  /* start byte loop.  */
  emit_label (L_loop_byte);

  emit_insn (gen_extendqisi2 (tmp1, addr1));
  emit_move_insn (current_addr, plus_constant (Pmode, current_addr, 1));

  emit_insn (gen_cmpeqsi_t (tmp1, const0_rtx));
  jump = emit_jump_insn (gen_branch_false (L_loop_byte));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);

  /* end loop.  */

  emit_insn (gen_addsi3 (start_addr, start_addr, GEN_INT (1)));

  emit_label (L_return);

  emit_insn (gen_subsi3 (operands[0], current_addr, start_addr));

  return true;
}

/* Emit code to perform a memset

   OPERANDS[0] is the destination.
   OPERANDS[1] is the size;
   OPERANDS[2] is the char to search.
   OPERANDS[3] is the alignment.  */
void
sh_expand_setmem (rtx *operands)
{
  rtx L_loop_byte = gen_label_rtx ();
  rtx L_loop_word = gen_label_rtx ();
  rtx L_return = gen_label_rtx ();
  rtx jump;
  rtx dest = copy_rtx (operands[0]);
  rtx dest_addr = copy_addr_to_reg (XEXP (dest, 0));
  rtx val = force_reg (SImode, operands[2]);
  int align = INTVAL (operands[3]);
  int count = 0;
  rtx len = force_reg (SImode, operands[1]);

  if (! CONST_INT_P (operands[1]))
    return;

  count = INTVAL (operands[1]);

  if (CONST_INT_P (operands[2])
      && (INTVAL (operands[2]) == 0 || INTVAL (operands[2]) == -1) && count > 8)
    {
      rtx lenw = gen_reg_rtx (SImode);

      if (align < 4)
        {
          emit_insn (gen_tstsi_t (GEN_INT (3), dest_addr));
          jump = emit_jump_insn (gen_branch_false (L_loop_byte));
          add_int_reg_note (jump, REG_BR_PROB, prob_likely);
        }

      /* word count. Do we have iterations ? */
      emit_insn (gen_lshrsi3 (lenw, len, GEN_INT (2)));

      dest = adjust_automodify_address (dest, SImode, dest_addr, 0);

      /* start loop.  */
      emit_label (L_loop_word);

      if (TARGET_SH2)
        emit_insn (gen_dect (lenw, lenw));
      else
        {
          emit_insn (gen_addsi3 (lenw, lenw, GEN_INT (-1)));
          emit_insn (gen_tstsi_t (lenw, lenw));
        }

      emit_move_insn (dest, val);
      emit_move_insn (dest_addr, plus_constant (Pmode, dest_addr,
                                                GET_MODE_SIZE (SImode)));


      jump = emit_jump_insn (gen_branch_false (L_loop_word));
      add_int_reg_note (jump, REG_BR_PROB, prob_likely);
      count = count % 4;

      dest = adjust_address (dest, QImode, 0);

      val = gen_lowpart (QImode, val);

      while (count--)
        {
          emit_move_insn (dest, val);
          emit_move_insn (dest_addr, plus_constant (Pmode, dest_addr,
                                                    GET_MODE_SIZE (QImode)));
        }

      jump = emit_jump_insn (gen_jump_compact (L_return));
      emit_barrier_after (jump);
    }

  dest = adjust_automodify_address (dest, QImode, dest_addr, 0);

  /* start loop.  */
  emit_label (L_loop_byte);

  if (TARGET_SH2)
    emit_insn (gen_dect (len, len));
  else
    {
      emit_insn (gen_addsi3 (len, len, GEN_INT (-1)));
      emit_insn (gen_tstsi_t (len, len));
    }

  val = gen_lowpart (QImode, val);
  emit_move_insn (dest, val);
  emit_move_insn (dest_addr, plus_constant (Pmode, dest_addr,
                                            GET_MODE_SIZE (QImode)));

  jump = emit_jump_insn (gen_branch_false (L_loop_byte));
  add_int_reg_note (jump, REG_BR_PROB, prob_likely);

  emit_label (L_return);

  return;
}