Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/* Subroutines used for calculate rtx costs of Andes NDS32 cpu for GNU compiler
   Copyright (C) 2012-2020 Free Software Foundation, Inc.
   Contributed by Andes Technology Corporation.

   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/>.  */

/* ------------------------------------------------------------------------ */

#define IN_TARGET_CODE 1

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "rtl.h"
#include "tree.h"
#include "memmodel.h"
#include "tm_p.h"
#include "optabs.h"		/* For GEN_FCN.  */
#include "recog.h"
#include "tm-constrs.h"
#include "tree-pass.h"

/* ------------------------------------------------------------------------ */

typedef bool (*rtx_cost_func) (rtx, int, int, int, int*);

struct rtx_cost_model_t {
  rtx_cost_func speed_prefer;
  rtx_cost_func size_prefer;
};

static rtx_cost_model_t rtx_cost_model;

static int insn_size_16bit; /* Initial at nds32_init_rtx_costs.  */
static const int insn_size_32bit = 4;

static bool
nds32_rtx_costs_speed_prefer (rtx x ATTRIBUTE_UNUSED,
			      int code,
			      int outer_code ATTRIBUTE_UNUSED,
			      int opno ATTRIBUTE_UNUSED,
			      int *total)
{
  rtx op0;
  rtx op1;
  machine_mode mode = GET_MODE (x);
  /* Scale cost by mode size.  */
  int cost = COSTS_N_INSNS (GET_MODE_SIZE (mode) / GET_MODE_SIZE (SImode));

  switch (code)
    {
    case USE:
      /* Used in combine.c as a marker.  */
      *total = 0;
      return true;

    case CONST_INT:
      /* When not optimizing for size, we care more about the cost
	 of hot code, and hot code is often in a loop.  If a constant
	 operand needs to be forced into a register, we will often be
	 able to hoist the constant load out of the loop, so the load
	 should not contribute to the cost.  */
      if (outer_code == SET || outer_code == PLUS)
	*total = satisfies_constraint_Is20 (x) ? 0 : 4;
      else if (outer_code == AND || outer_code == IOR || outer_code == XOR
	       || outer_code == MINUS)
	*total = satisfies_constraint_Iu15 (x) ? 0 : 4;
      else if (outer_code == ASHIFT || outer_code == ASHIFTRT
	       || outer_code == LSHIFTRT)
	*total = satisfies_constraint_Iu05 (x) ? 0 : 4;
      else if (GET_RTX_CLASS (outer_code) == RTX_COMPARE
	       || GET_RTX_CLASS (outer_code) == RTX_COMM_COMPARE)
	*total = satisfies_constraint_Is16 (x) ? 0 : 4;
      else
	*total = COSTS_N_INSNS (1);
      return true;

    case CONST:
    case LO_SUM:
    case HIGH:
    case SYMBOL_REF:
      *total = COSTS_N_INSNS (1);
      return true;

    case MEM:
      *total = COSTS_N_INSNS (1);
      return true;

    case SET:
      op0 = SET_DEST (x);
      op1 = SET_SRC (x);
      mode = GET_MODE (op0);
      /* Scale cost by mode size.  */
      cost = COSTS_N_INSNS (GET_MODE_SIZE (mode) / GET_MODE_SIZE (SImode));

      switch (GET_CODE (op1))
	{
	case REG:
	case SUBREG:
	  /* Register move and Store instructions.  */
	  if ((REG_P (op0) || MEM_P (op0))
	      && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (DImode))
	    *total = COSTS_N_INSNS (1);
	  else
	    *total = cost;
	  return true;

	case MEM:
	  /* Load instructions.  */
	  if (REG_P (op0) && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (DImode))
	    *total = COSTS_N_INSNS (1);
	  else
	    *total = cost;
	  return true;

	case CONST_INT:
	  /* movi instruction.  */
	  if (REG_P (op0) && GET_MODE_SIZE (mode) < GET_MODE_SIZE (DImode))
	    {
	      if (satisfies_constraint_Is20 (op1))
		*total = COSTS_N_INSNS (1) - 1;
	      else
		*total = COSTS_N_INSNS (2);
	    }
	  else
	    *total = cost;
	  return true;

	case CONST:
	case SYMBOL_REF:
	case LABEL_REF:
	  /* la instruction.  */
	  if (REG_P (op0) && GET_MODE_SIZE (mode) < GET_MODE_SIZE (DImode))
	    *total = COSTS_N_INSNS (1) - 1;
	  else
	    *total = cost;
	  return true;
	case VEC_SELECT:
	  *total = cost;
	  return true;

	default:
	  *total = cost;
	  return true;
	}

    case PLUS:
      op0 = XEXP (x, 0);
      op1 = XEXP (x, 1);

      if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (DImode))
	*total = cost;
      else if (GET_CODE (op0) == MULT || GET_CODE (op0) == LSHIFTRT
	       || GET_CODE (op1) == MULT || GET_CODE (op1) == LSHIFTRT)
	/* ALU_SHIFT */
	*total = COSTS_N_INSNS (2);

      else if ((GET_CODE (op1) == CONST_INT
		&& satisfies_constraint_Is15 (op1))
		|| REG_P (op1))
	/* ADD instructions */
	*total = COSTS_N_INSNS (1);
      else
	/* ADD instructions: IMM out of range.  */
	*total = COSTS_N_INSNS (2);
      return true;

    case MINUS:
      op0 = XEXP (x, 0);
      op1 = XEXP (x, 1);

      if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (DImode))
	*total = cost;
      else if (GET_CODE (op0) == MULT || GET_CODE (op0) == LSHIFTRT
	       || GET_CODE (op1) == MULT || GET_CODE (op1) == LSHIFTRT)
	/* ALU_SHIFT */
	*total = COSTS_N_INSNS (2);
      else if ((GET_CODE (op0) == CONST_INT
		&& satisfies_constraint_Is15 (op0))
		|| REG_P (op0))
	/* SUB instructions */
	*total = COSTS_N_INSNS (1);
      else
	/* SUB instructions: IMM out of range.  */
	*total = COSTS_N_INSNS (2);
      return true;

    case TRUNCATE:
      /* TRUNCATE and AND behavior is same. */
      *total = COSTS_N_INSNS (1);
      return true;

    case AND:
    case IOR:
    case XOR:
      op0 = XEXP (x, 0);
      op1 = XEXP (x, 1);

      if (NDS32_EXT_DSP_P ())
	{
	  /* We prefer (and (ior) (ior)) than (ior (and) (and)) for
	     synthetize pk** and insb instruction.  */
	  if (code == AND && GET_CODE (op0) == IOR && GET_CODE (op1) == IOR)
	    return COSTS_N_INSNS (1);

	  if (code == IOR && GET_CODE (op0) == AND && GET_CODE (op1) == AND)
	    return COSTS_N_INSNS (10);
	}

      if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (DImode))
	*total = cost;
      else if (GET_CODE (op0) == ASHIFT || GET_CODE (op0) == LSHIFTRT)
	*total = COSTS_N_INSNS (2);
      else if ((GET_CODE (op1) == CONST_INT
	       && satisfies_constraint_Iu15 (op1))
	       || REG_P (op1))
	/* AND, OR, XOR instructions */
	*total = COSTS_N_INSNS (1);
      else if (code == AND || GET_CODE (op0) == NOT)
	/* BITC instruction */
	*total = COSTS_N_INSNS (1);
      else
	/* AND, OR, XOR instructions: IMM out of range.  */
	*total = COSTS_N_INSNS (2);
      return true;

    case MULT:
      if (GET_MODE (x) == DImode
	  || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND
	  || GET_CODE (XEXP (x, 1)) == ZERO_EXTEND)
	/* MUL instructions */
	*total = COSTS_N_INSNS (1);
      else if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (DImode))
	*total = cost;
      else if (outer_code == PLUS || outer_code == MINUS)
	*total = COSTS_N_INSNS (2);
      else if ((GET_CODE (XEXP (x, 1)) == CONST_INT
	       && satisfies_constraint_Iu05 (XEXP (x, 1)))
	       || REG_P (XEXP (x, 1)))
	/* MUL instructions */
	*total = COSTS_N_INSNS (1);
      else
	/* MUL instructions: IMM out of range.  */
	*total = COSTS_N_INSNS (2);

      if (TARGET_MUL_SLOW)
	*total += COSTS_N_INSNS (4);

      return true;

    case LSHIFTRT:
      if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (DImode))
	*total = cost;
      else if (outer_code == PLUS || outer_code == MINUS
	       || outer_code == AND || outer_code == IOR
	       || outer_code == XOR)
	*total = COSTS_N_INSNS (2);
      else if ((GET_CODE (XEXP (x, 1)) == CONST_INT
	       && satisfies_constraint_Iu05 (XEXP (x, 1)))
	       || REG_P (XEXP (x, 1)))
	/* SRL instructions */
	*total = COSTS_N_INSNS (1);
      else
	/* SRL instructions: IMM out of range.  */
	*total = COSTS_N_INSNS (2);
      return true;

    case ASHIFT:
      if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (DImode))
	*total = cost;
      else if (outer_code == AND || outer_code == IOR
	       || outer_code == XOR)
	*total = COSTS_N_INSNS (2);
      else if ((GET_CODE (XEXP (x, 1)) == CONST_INT
	       && satisfies_constraint_Iu05 (XEXP (x, 1)))
	       || REG_P (XEXP (x, 1)))
	/* SLL instructions */
	*total = COSTS_N_INSNS (1);
      else
	/* SLL instructions: IMM out of range.  */
	*total = COSTS_N_INSNS (2);
      return true;

    case ASHIFTRT:
    case ROTATERT:
      if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (DImode))
	*total = cost;
      else if ((GET_CODE (XEXP (x, 1)) == CONST_INT
	       && satisfies_constraint_Iu05 (XEXP (x, 1)))
	       || REG_P (XEXP (x, 1)))
	/* ROTR, SLL instructions */
	*total = COSTS_N_INSNS (1);
      else
	/* ROTR, SLL instructions: IMM out of range.  */
	*total = COSTS_N_INSNS (2);
      return true;

    case LT:
    case LTU:
      if (outer_code == SET)
	{
	  if ((GET_CODE (XEXP (x, 1)) == CONST_INT
	      && satisfies_constraint_Iu15 (XEXP (x, 1)))
	      || REG_P (XEXP (x, 1)))
	    /* SLT, SLTI instructions */
	    *total = COSTS_N_INSNS (1);
	  else
	    /* SLT, SLT instructions: IMM out of range.  */
	    *total = COSTS_N_INSNS (2);
	}
      else
	/* branch */
	*total = COSTS_N_INSNS (2);
      return true;

    case EQ:
    case NE:
    case GE:
    case LE:
    case GT:
      /* branch */
      *total = COSTS_N_INSNS (2);
      return true;

    case IF_THEN_ELSE:
      if (GET_CODE (XEXP (x, 1)) == LABEL_REF)
	/* branch */
	*total = COSTS_N_INSNS (2);
      else
	/* cmovz, cmovn instructions */
	*total = COSTS_N_INSNS (1);
      return true;

    case LABEL_REF:
      if (outer_code == IF_THEN_ELSE)
	/* branch */
	*total = COSTS_N_INSNS (2);
      else
	*total = COSTS_N_INSNS (1);
      return true;

    case ZERO_EXTEND:
    case SIGN_EXTEND:
      if (MEM_P (XEXP (x, 0)))
	/* Using memory access. */
	*total = COSTS_N_INSNS (1);
      else
	/* Zero extend and sign extend instructions.  */
	*total = COSTS_N_INSNS (1);
      return true;

    case NEG:
    case NOT:
      *total = COSTS_N_INSNS (1);
      return true;

    case DIV:
    case UDIV:
    case MOD:
    case UMOD:
      *total = COSTS_N_INSNS (20);
      return true;

    case CALL:
      *total = COSTS_N_INSNS (2);
      return true;

    case CLZ:
    case SMIN:
    case SMAX:
    case ZERO_EXTRACT:
      if (TARGET_EXT_PERF)
	*total = COSTS_N_INSNS (1);
      else
	*total = COSTS_N_INSNS (3);
      return true;
    case VEC_SELECT:
      *total = COSTS_N_INSNS (1);
      return true;

    default:
      *total = COSTS_N_INSNS (3);
      return true;
    }
}

static bool
nds32_rtx_costs_size_prefer (rtx x,
			     int code,
			     int outer_code,
			     int opno ATTRIBUTE_UNUSED,
			     int *total)
{
  /* In gcc/rtl.h, the default value of COSTS_N_INSNS(N) is N*4.
     We treat it as 4-byte cost for each instruction
     under code size consideration.  */
  switch (code)
    {
    case SET:
      /* For 'SET' rtx, we need to return false
	 so that it can recursively calculate costs.  */
      return false;

    case USE:
      /* Used in combine.c as a marker.  */
      *total = 0;
      break;

    case CONST_INT:
      /* All instructions involving constant operation
	 need to be considered for cost evaluation.  */
      if (outer_code == SET)
	{
	  /* (set X imm5s), use movi55, 2-byte cost.
	     (set X imm20s), use movi, 4-byte cost.
	     (set X BIG_INT), use sethi/ori, 8-byte cost.  */
	  if (satisfies_constraint_Is05 (x))
	    *total = insn_size_16bit;
	  else if (satisfies_constraint_Is20 (x))
	    *total = insn_size_32bit;
	  else
	    *total = insn_size_32bit * 2;
	}
      else if (outer_code == PLUS || outer_code == MINUS)
	{
	  /* Possible addi333/subi333 or subi45/addi45, 2-byte cost.
	     General case, cost 1 instruction with 4-byte.  */
	  if (satisfies_constraint_Iu05 (x))
	    *total = insn_size_16bit;
	  else
	    *total = insn_size_32bit;
	}
      else if (outer_code == ASHIFT)
	{
	  /* Possible slli333, 2-byte cost.
	     General case, cost 1 instruction with 4-byte.  */
	  if (satisfies_constraint_Iu03 (x))
	    *total = insn_size_16bit;
	  else
	    *total = insn_size_32bit;
	}
      else if (outer_code == ASHIFTRT || outer_code == LSHIFTRT)
	{
	  /* Possible srai45 or srli45, 2-byte cost.
	     General case, cost 1 instruction with 4-byte.  */
	  if (satisfies_constraint_Iu05 (x))
	    *total = insn_size_16bit;
	  else
	    *total = insn_size_32bit;
	}
      else
	{
	  /* For other cases, simply set it 4-byte cost.  */
	  *total = insn_size_32bit;
	}
      break;

    case CONST_DOUBLE:
      /* It requires high part and low part processing, set it 8-byte cost.  */
      *total = insn_size_32bit * 2;
      break;

    case CONST:
    case SYMBOL_REF:
      *total = insn_size_32bit * 2;
      break;

    default:
      /* For other cases, generally we set it 4-byte cost
	 and stop resurively traversing.  */
      *total = insn_size_32bit;
      break;
    }

  return true;
}

void
nds32_init_rtx_costs (void)
{
  rtx_cost_model.speed_prefer = nds32_rtx_costs_speed_prefer;
  rtx_cost_model.size_prefer  = nds32_rtx_costs_size_prefer;

  if (TARGET_16_BIT)
    insn_size_16bit = 2;
  else
    insn_size_16bit = 4;
}

/* This target hook describes the relative costs of RTL expressions.
   Return 'true' when all subexpressions of x have been processed.
   Return 'false' to sum the costs of sub-rtx, plus cost of this operation.
   Refer to gcc/rtlanal.c for more information.  */
bool
nds32_rtx_costs_impl (rtx x,
		      machine_mode mode ATTRIBUTE_UNUSED,
		      int outer_code,
		      int opno,
		      int *total,
		      bool speed)
{
  int code = GET_CODE (x);

  /* According to 'speed', use suitable cost model section.  */
  if (speed)
    return rtx_cost_model.speed_prefer(x, code, outer_code, opno, total);
  else
    return rtx_cost_model.size_prefer(x, code, outer_code, opno, total);
}


int nds32_address_cost_speed_prefer (rtx address)
{
  rtx plus0, plus1;
  enum rtx_code code;

  code = GET_CODE (address);

  switch (code)
    {
    case POST_MODIFY:
    case POST_INC:
    case POST_DEC:
      /* We encourage that rtx contains
	 POST_MODIFY/POST_INC/POST_DEC behavior.  */
      return COSTS_N_INSNS (1) - 2;

    case SYMBOL_REF:
      /* We can have gp-relative load/store for symbol_ref.
	Have it 4-byte cost.  */
      return COSTS_N_INSNS (2);

    case CONST:
      /* It is supposed to be the pattern (const (plus symbol_ref const_int)).
	 Have it 4-byte cost.  */
      return COSTS_N_INSNS (2);

    case REG:
      /* Simply return 4-byte costs.  */
      return COSTS_N_INSNS (1) - 2;

    case PLUS:
      /* We do not need to check if the address is a legitimate address,
	 because this hook is never called with an invalid address.
	 But we better check the range of
	 const_int value for cost, if it exists.  */
      plus0 = XEXP (address, 0);
      plus1 = XEXP (address, 1);

      if (REG_P (plus0) && CONST_INT_P (plus1))
	return COSTS_N_INSNS (1) - 2;
      else if (ARITHMETIC_P (plus0) || ARITHMETIC_P (plus1))
	return COSTS_N_INSNS (1) - 1;
      else if (REG_P (plus0) && REG_P (plus1))
	return COSTS_N_INSNS (1);

      /* For other 'plus' situation, make it cost 4-byte.  */
      return COSTS_N_INSNS (1);

    default:
      break;
    }

  return COSTS_N_INSNS (4);

}

int nds32_address_cost_speed_fwprop (rtx address)
{
  rtx plus0, plus1;
  enum rtx_code code;

  code = GET_CODE (address);

  switch (code)
    {
    case POST_MODIFY:
    case POST_INC:
    case POST_DEC:
      /* We encourage that rtx contains
	 POST_MODIFY/POST_INC/POST_DEC behavior.  */
      return 0;

    case SYMBOL_REF:
      /* We can have gp-relative load/store for symbol_ref.
	 Have it 4-byte cost.  */
      return COSTS_N_INSNS (2);

    case CONST:
      /* It is supposed to be the pattern (const (plus symbol_ref const_int)).
	 Have it 4-byte cost.  */
      return COSTS_N_INSNS (2);

    case REG:
      /* Simply return 4-byte costs.  */
      return COSTS_N_INSNS (1);

    case PLUS:
      /* We do not need to check if the address is a legitimate address,
	 because this hook is never called with an invalid address.
	 But we better check the range of
	 const_int value for cost, if it exists.  */
      plus0 = XEXP (address, 0);
      plus1 = XEXP (address, 1);

      if (REG_P (plus0) && CONST_INT_P (plus1))
	{
	  /* If it is possible to be lwi333/swi333 form,
	     make it 2-byte cost.  */
	  if (satisfies_constraint_Iu03 (plus1))
	    return (COSTS_N_INSNS (1) - 2);
	  else
	    return COSTS_N_INSNS (1);
	}
      if (ARITHMETIC_P (plus0) || ARITHMETIC_P (plus1))
	return COSTS_N_INSNS (1) - 2;
      else if (REG_P (plus0) && REG_P (plus1))
	return COSTS_N_INSNS (1);

      /* For other 'plus' situation, make it cost 4-byte.  */
      return COSTS_N_INSNS (1);

    default:
      break;
    }

  return COSTS_N_INSNS (4);
}


int nds32_address_cost_size_prefer (rtx address)
{
  rtx plus0, plus1;
  enum rtx_code code;

  code = GET_CODE (address);

  switch (code)
    {
    case POST_MODIFY:
    case POST_INC:
    case POST_DEC:
      /* We encourage that rtx contains
	 POST_MODIFY/POST_INC/POST_DEC behavior.  */
      return 0;

    case SYMBOL_REF:
      /* We can have gp-relative load/store for symbol_ref.
	 Have it 4-byte cost.  */
      return COSTS_N_INSNS (2);

    case CONST:
      /* It is supposed to be the pattern (const (plus symbol_ref const_int)).
	 Have it 4-byte cost.  */
      return COSTS_N_INSNS (2);

    case REG:
      /* Simply return 4-byte costs.  */
      return COSTS_N_INSNS (1) - 1;

    case PLUS:
      /* We do not need to check if the address is a legitimate address,
	 because this hook is never called with an invalid address.
	 But we better check the range of
	 const_int value for cost, if it exists.  */
      plus0 = XEXP (address, 0);
      plus1 = XEXP (address, 1);

      if (REG_P (plus0) && CONST_INT_P (plus1))
	{
	  /* If it is possible to be lwi333/swi333 form,
	     make it 2-byte cost.  */
	  if (satisfies_constraint_Iu03 (plus1))
	    return (COSTS_N_INSNS (1) - 2);
	  else
	    return COSTS_N_INSNS (1) - 1;
	}

      /* (plus (reg) (mult (reg) (const))) */
      if (ARITHMETIC_P (plus0) || ARITHMETIC_P (plus1))
	return (COSTS_N_INSNS (1) - 1);

      /* For other 'plus' situation, make it cost 4-byte.  */
      return COSTS_N_INSNS (1);

    default:
      break;
    }

  return COSTS_N_INSNS (4);

}

int nds32_address_cost_impl (rtx address,
			     machine_mode mode ATTRIBUTE_UNUSED,
			     addr_space_t as ATTRIBUTE_UNUSED,
			     bool speed_p)
{
  if (speed_p)
    {
      if (current_pass->tv_id == TV_FWPROP)
	return nds32_address_cost_speed_fwprop (address);
      else
	return nds32_address_cost_speed_prefer (address);
    }
  else
    return nds32_address_cost_size_prefer (address);
}

/* ------------------------------------------------------------------------ */