forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathira-costs.c
2435 lines (2208 loc) · 75.4 KB
/
ira-costs.c
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* IRA hard register and memory cost calculation for allocnos or pseudos.
Copyright (C) 2006-2020 Free Software Foundation, Inc.
Contributed by Vladimir Makarov <[email protected]>.
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 "backend.h"
#include "target.h"
#include "rtl.h"
#include "tree.h"
#include "predict.h"
#include "memmodel.h"
#include "tm_p.h"
#include "insn-config.h"
#include "regs.h"
#include "ira.h"
#include "ira-int.h"
#include "addresses.h"
#include "reload.h"
/* The flags is set up every time when we calculate pseudo register
classes through function ira_set_pseudo_classes. */
static bool pseudo_classes_defined_p = false;
/* TRUE if we work with allocnos. Otherwise we work with pseudos. */
static bool allocno_p;
/* Number of elements in array `costs'. */
static int cost_elements_num;
/* The `costs' struct records the cost of using hard registers of each
class considered for the calculation and of using memory for each
allocno or pseudo. */
struct costs
{
int mem_cost;
/* Costs for register classes start here. We process only some
allocno classes. */
int cost[1];
};
#define max_struct_costs_size \
(this_target_ira_int->x_max_struct_costs_size)
#define init_cost \
(this_target_ira_int->x_init_cost)
#define temp_costs \
(this_target_ira_int->x_temp_costs)
#define op_costs \
(this_target_ira_int->x_op_costs)
#define this_op_costs \
(this_target_ira_int->x_this_op_costs)
/* Costs of each class for each allocno or pseudo. */
static struct costs *costs;
/* Accumulated costs of each class for each allocno. */
static struct costs *total_allocno_costs;
/* It is the current size of struct costs. */
static size_t struct_costs_size;
/* Return pointer to structure containing costs of allocno or pseudo
with given NUM in array ARR. */
#define COSTS(arr, num) \
((struct costs *) ((char *) (arr) + (num) * struct_costs_size))
/* Return index in COSTS when processing reg with REGNO. */
#define COST_INDEX(regno) (allocno_p \
? ALLOCNO_NUM (ira_curr_regno_allocno_map[regno]) \
: (int) regno)
/* Record register class preferences of each allocno or pseudo. Null
value means no preferences. It happens on the 1st iteration of the
cost calculation. */
static enum reg_class *pref;
/* Allocated buffers for pref. */
static enum reg_class *pref_buffer;
/* Record allocno class of each allocno with the same regno. */
static enum reg_class *regno_aclass;
/* Record cost gains for not allocating a register with an invariant
equivalence. */
static int *regno_equiv_gains;
/* Execution frequency of the current insn. */
static int frequency;
/* Info about reg classes whose costs are calculated for a pseudo. */
struct cost_classes
{
/* Number of the cost classes in the subsequent array. */
int num;
/* Container of the cost classes. */
enum reg_class classes[N_REG_CLASSES];
/* Map reg class -> index of the reg class in the previous array.
-1 if it is not a cost class. */
int index[N_REG_CLASSES];
/* Map hard regno index of first class in array CLASSES containing
the hard regno, -1 otherwise. */
int hard_regno_index[FIRST_PSEUDO_REGISTER];
};
/* Types of pointers to the structure above. */
typedef struct cost_classes *cost_classes_t;
typedef const struct cost_classes *const_cost_classes_t;
/* Info about cost classes for each pseudo. */
static cost_classes_t *regno_cost_classes;
/* Helper for cost_classes hashing. */
struct cost_classes_hasher : pointer_hash <cost_classes>
{
static inline hashval_t hash (const cost_classes *);
static inline bool equal (const cost_classes *, const cost_classes *);
static inline void remove (cost_classes *);
};
/* Returns hash value for cost classes info HV. */
inline hashval_t
cost_classes_hasher::hash (const cost_classes *hv)
{
return iterative_hash (&hv->classes, sizeof (enum reg_class) * hv->num, 0);
}
/* Compares cost classes info HV1 and HV2. */
inline bool
cost_classes_hasher::equal (const cost_classes *hv1, const cost_classes *hv2)
{
return (hv1->num == hv2->num
&& memcmp (hv1->classes, hv2->classes,
sizeof (enum reg_class) * hv1->num) == 0);
}
/* Delete cost classes info V from the hash table. */
inline void
cost_classes_hasher::remove (cost_classes *v)
{
ira_free (v);
}
/* Hash table of unique cost classes. */
static hash_table<cost_classes_hasher> *cost_classes_htab;
/* Map allocno class -> cost classes for pseudo of given allocno
class. */
static cost_classes_t cost_classes_aclass_cache[N_REG_CLASSES];
/* Map mode -> cost classes for pseudo of give mode. */
static cost_classes_t cost_classes_mode_cache[MAX_MACHINE_MODE];
/* Cost classes that include all classes in ira_important_classes. */
static cost_classes all_cost_classes;
/* Use the array of classes in CLASSES_PTR to fill out the rest of
the structure. */
static void
complete_cost_classes (cost_classes_t classes_ptr)
{
for (int i = 0; i < N_REG_CLASSES; i++)
classes_ptr->index[i] = -1;
for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
classes_ptr->hard_regno_index[i] = -1;
for (int i = 0; i < classes_ptr->num; i++)
{
enum reg_class cl = classes_ptr->classes[i];
classes_ptr->index[cl] = i;
for (int j = ira_class_hard_regs_num[cl] - 1; j >= 0; j--)
{
unsigned int hard_regno = ira_class_hard_regs[cl][j];
if (classes_ptr->hard_regno_index[hard_regno] < 0)
classes_ptr->hard_regno_index[hard_regno] = i;
}
}
}
/* Initialize info about the cost classes for each pseudo. */
static void
initiate_regno_cost_classes (void)
{
int size = sizeof (cost_classes_t) * max_reg_num ();
regno_cost_classes = (cost_classes_t *) ira_allocate (size);
memset (regno_cost_classes, 0, size);
memset (cost_classes_aclass_cache, 0,
sizeof (cost_classes_t) * N_REG_CLASSES);
memset (cost_classes_mode_cache, 0,
sizeof (cost_classes_t) * MAX_MACHINE_MODE);
cost_classes_htab = new hash_table<cost_classes_hasher> (200);
all_cost_classes.num = ira_important_classes_num;
for (int i = 0; i < ira_important_classes_num; i++)
all_cost_classes.classes[i] = ira_important_classes[i];
complete_cost_classes (&all_cost_classes);
}
/* Create new cost classes from cost classes FROM and set up members
index and hard_regno_index. Return the new classes. The function
implements some common code of two functions
setup_regno_cost_classes_by_aclass and
setup_regno_cost_classes_by_mode. */
static cost_classes_t
setup_cost_classes (cost_classes_t from)
{
cost_classes_t classes_ptr;
classes_ptr = (cost_classes_t) ira_allocate (sizeof (struct cost_classes));
classes_ptr->num = from->num;
for (int i = 0; i < from->num; i++)
classes_ptr->classes[i] = from->classes[i];
complete_cost_classes (classes_ptr);
return classes_ptr;
}
/* Return a version of FULL that only considers registers in REGS that are
valid for mode MODE. Both FULL and the returned class are globally
allocated. */
static cost_classes_t
restrict_cost_classes (cost_classes_t full, machine_mode mode,
const_hard_reg_set regs)
{
static struct cost_classes narrow;
int map[N_REG_CLASSES];
narrow.num = 0;
for (int i = 0; i < full->num; i++)
{
/* Assume that we'll drop the class. */
map[i] = -1;
/* Ignore classes that are too small for the mode. */
enum reg_class cl = full->classes[i];
if (!contains_reg_of_mode[cl][mode])
continue;
/* Calculate the set of registers in CL that belong to REGS and
are valid for MODE. */
HARD_REG_SET valid_for_cl = reg_class_contents[cl] & regs;
valid_for_cl &= ~(ira_prohibited_class_mode_regs[cl][mode]
| ira_no_alloc_regs);
if (hard_reg_set_empty_p (valid_for_cl))
continue;
/* Don't use this class if the set of valid registers is a subset
of an existing class. For example, suppose we have two classes
GR_REGS and FR_REGS and a union class GR_AND_FR_REGS. Suppose
that the mode changes allowed by FR_REGS are not as general as
the mode changes allowed by GR_REGS.
In this situation, the mode changes for GR_AND_FR_REGS could
either be seen as the union or the intersection of the mode
changes allowed by the two subclasses. The justification for
the union-based definition would be that, if you want a mode
change that's only allowed by GR_REGS, you can pick a register
from the GR_REGS subclass. The justification for the
intersection-based definition would be that every register
from the class would allow the mode change.
However, if we have a register that needs to be in GR_REGS,
using GR_AND_FR_REGS with the intersection-based definition
would be too pessimistic, since it would bring in restrictions
that only apply to FR_REGS. Conversely, if we have a register
that needs to be in FR_REGS, using GR_AND_FR_REGS with the
union-based definition would lose the extra restrictions
placed on FR_REGS. GR_AND_FR_REGS is therefore only useful
for cases where GR_REGS and FP_REGS are both valid. */
int pos;
for (pos = 0; pos < narrow.num; ++pos)
{
enum reg_class cl2 = narrow.classes[pos];
if (hard_reg_set_subset_p (valid_for_cl, reg_class_contents[cl2]))
break;
}
map[i] = pos;
if (pos == narrow.num)
{
/* If several classes are equivalent, prefer to use the one
that was chosen as the allocno class. */
enum reg_class cl2 = ira_allocno_class_translate[cl];
if (ira_class_hard_regs_num[cl] == ira_class_hard_regs_num[cl2])
cl = cl2;
narrow.classes[narrow.num++] = cl;
}
}
if (narrow.num == full->num)
return full;
cost_classes **slot = cost_classes_htab->find_slot (&narrow, INSERT);
if (*slot == NULL)
{
cost_classes_t classes = setup_cost_classes (&narrow);
/* Map equivalent classes to the representative that we chose above. */
for (int i = 0; i < ira_important_classes_num; i++)
{
enum reg_class cl = ira_important_classes[i];
int index = full->index[cl];
if (index >= 0)
classes->index[cl] = map[index];
}
*slot = classes;
}
return *slot;
}
/* Setup cost classes for pseudo REGNO whose allocno class is ACLASS.
This function is used when we know an initial approximation of
allocno class of the pseudo already, e.g. on the second iteration
of class cost calculation or after class cost calculation in
register-pressure sensitive insn scheduling or register-pressure
sensitive loop-invariant motion. */
static void
setup_regno_cost_classes_by_aclass (int regno, enum reg_class aclass)
{
static struct cost_classes classes;
cost_classes_t classes_ptr;
enum reg_class cl;
int i;
cost_classes **slot;
HARD_REG_SET temp, temp2;
bool exclude_p;
if ((classes_ptr = cost_classes_aclass_cache[aclass]) == NULL)
{
temp = reg_class_contents[aclass] & ~ira_no_alloc_regs;
/* We exclude classes from consideration which are subsets of
ACLASS only if ACLASS is an uniform class. */
exclude_p = ira_uniform_class_p[aclass];
classes.num = 0;
for (i = 0; i < ira_important_classes_num; i++)
{
cl = ira_important_classes[i];
if (exclude_p)
{
/* Exclude non-uniform classes which are subsets of
ACLASS. */
temp2 = reg_class_contents[cl] & ~ira_no_alloc_regs;
if (hard_reg_set_subset_p (temp2, temp) && cl != aclass)
continue;
}
classes.classes[classes.num++] = cl;
}
slot = cost_classes_htab->find_slot (&classes, INSERT);
if (*slot == NULL)
{
classes_ptr = setup_cost_classes (&classes);
*slot = classes_ptr;
}
classes_ptr = cost_classes_aclass_cache[aclass] = (cost_classes_t) *slot;
}
if (regno_reg_rtx[regno] != NULL_RTX)
{
/* Restrict the classes to those that are valid for REGNO's mode
(which might for example exclude singleton classes if the mode
requires two registers). Also restrict the classes to those that
are valid for subregs of REGNO. */
const HARD_REG_SET *valid_regs = valid_mode_changes_for_regno (regno);
if (!valid_regs)
valid_regs = ®_class_contents[ALL_REGS];
classes_ptr = restrict_cost_classes (classes_ptr,
PSEUDO_REGNO_MODE (regno),
*valid_regs);
}
regno_cost_classes[regno] = classes_ptr;
}
/* Setup cost classes for pseudo REGNO with MODE. Usage of MODE can
decrease number of cost classes for the pseudo, if hard registers
of some important classes cannot hold a value of MODE. So the
pseudo cannot get hard register of some important classes and cost
calculation for such important classes is only wasting CPU
time. */
static void
setup_regno_cost_classes_by_mode (int regno, machine_mode mode)
{
if (const HARD_REG_SET *valid_regs = valid_mode_changes_for_regno (regno))
regno_cost_classes[regno] = restrict_cost_classes (&all_cost_classes,
mode, *valid_regs);
else
{
if (cost_classes_mode_cache[mode] == NULL)
cost_classes_mode_cache[mode]
= restrict_cost_classes (&all_cost_classes, mode,
reg_class_contents[ALL_REGS]);
regno_cost_classes[regno] = cost_classes_mode_cache[mode];
}
}
/* Finalize info about the cost classes for each pseudo. */
static void
finish_regno_cost_classes (void)
{
ira_free (regno_cost_classes);
delete cost_classes_htab;
cost_classes_htab = NULL;
}
/* Compute the cost of loading X into (if TO_P is TRUE) or from (if
TO_P is FALSE) a register of class RCLASS in mode MODE. X must not
be a pseudo register. */
static int
copy_cost (rtx x, machine_mode mode, reg_class_t rclass, bool to_p,
secondary_reload_info *prev_sri)
{
secondary_reload_info sri;
reg_class_t secondary_class = NO_REGS;
/* If X is a SCRATCH, there is actually nothing to move since we are
assuming optimal allocation. */
if (GET_CODE (x) == SCRATCH)
return 0;
/* Get the class we will actually use for a reload. */
rclass = targetm.preferred_reload_class (x, rclass);
/* If we need a secondary reload for an intermediate, the cost is
that to load the input into the intermediate register, then to
copy it. */
sri.prev_sri = prev_sri;
sri.extra_cost = 0;
/* PR 68770: Secondary reload might examine the t_icode field. */
sri.t_icode = CODE_FOR_nothing;
secondary_class = targetm.secondary_reload (to_p, x, rclass, mode, &sri);
if (secondary_class != NO_REGS)
{
ira_init_register_move_cost_if_necessary (mode);
return (ira_register_move_cost[mode][(int) secondary_class][(int) rclass]
+ sri.extra_cost
+ copy_cost (x, mode, secondary_class, to_p, &sri));
}
/* For memory, use the memory move cost, for (hard) registers, use
the cost to move between the register classes, and use 2 for
everything else (constants). */
if (MEM_P (x) || rclass == NO_REGS)
return sri.extra_cost
+ ira_memory_move_cost[mode][(int) rclass][to_p != 0];
else if (REG_P (x))
{
reg_class_t x_class = REGNO_REG_CLASS (REGNO (x));
ira_init_register_move_cost_if_necessary (mode);
return (sri.extra_cost
+ ira_register_move_cost[mode][(int) x_class][(int) rclass]);
}
else
/* If this is a constant, we may eventually want to call rtx_cost
here. */
return sri.extra_cost + COSTS_N_INSNS (1);
}
/* Record the cost of using memory or hard registers of various
classes for the operands in INSN.
N_ALTS is the number of alternatives.
N_OPS is the number of operands.
OPS is an array of the operands.
MODES are the modes of the operands, in case any are VOIDmode.
CONSTRAINTS are the constraints to use for the operands. This array
is modified by this procedure.
This procedure works alternative by alternative. For each
alternative we assume that we will be able to allocate all allocnos
to their ideal register class and calculate the cost of using that
alternative. Then we compute, for each operand that is a
pseudo-register, the cost of having the allocno allocated to each
register class and using it in that alternative. To this cost is
added the cost of the alternative.
The cost of each class for this insn is its lowest cost among all
the alternatives. */
static void
record_reg_classes (int n_alts, int n_ops, rtx *ops,
machine_mode *modes, const char **constraints,
rtx_insn *insn, enum reg_class *pref)
{
int alt;
int i, j, k;
int insn_allows_mem[MAX_RECOG_OPERANDS];
move_table *move_in_cost, *move_out_cost;
short (*mem_cost)[2];
for (i = 0; i < n_ops; i++)
insn_allows_mem[i] = 0;
/* Process each alternative, each time minimizing an operand's cost
with the cost for each operand in that alternative. */
alternative_mask preferred = get_preferred_alternatives (insn);
for (alt = 0; alt < n_alts; alt++)
{
enum reg_class classes[MAX_RECOG_OPERANDS];
int allows_mem[MAX_RECOG_OPERANDS];
enum reg_class rclass;
int alt_fail = 0;
int alt_cost = 0, op_cost_add;
if (!TEST_BIT (preferred, alt))
{
for (i = 0; i < recog_data.n_operands; i++)
constraints[i] = skip_alternative (constraints[i]);
continue;
}
for (i = 0; i < n_ops; i++)
{
unsigned char c;
const char *p = constraints[i];
rtx op = ops[i];
machine_mode mode = modes[i];
int allows_addr = 0;
int win = 0;
/* Initially show we know nothing about the register class. */
classes[i] = NO_REGS;
allows_mem[i] = 0;
/* If this operand has no constraints at all, we can
conclude nothing about it since anything is valid. */
if (*p == 0)
{
if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
memset (this_op_costs[i], 0, struct_costs_size);
continue;
}
/* If this alternative is only relevant when this operand
matches a previous operand, we do different things
depending on whether this operand is a allocno-reg or not.
We must process any modifiers for the operand before we
can make this test. */
while (*p == '%' || *p == '=' || *p == '+' || *p == '&')
p++;
if (p[0] >= '0' && p[0] <= '0' + i)
{
/* Copy class and whether memory is allowed from the
matching alternative. Then perform any needed cost
computations and/or adjustments. */
j = p[0] - '0';
classes[i] = classes[j];
allows_mem[i] = allows_mem[j];
if (allows_mem[i])
insn_allows_mem[i] = 1;
if (! REG_P (op) || REGNO (op) < FIRST_PSEUDO_REGISTER)
{
/* If this matches the other operand, we have no
added cost and we win. */
if (rtx_equal_p (ops[j], op))
win = 1;
/* If we can put the other operand into a register,
add to the cost of this alternative the cost to
copy this operand to the register used for the
other operand. */
else if (classes[j] != NO_REGS)
{
alt_cost += copy_cost (op, mode, classes[j], 1, NULL);
win = 1;
}
}
else if (! REG_P (ops[j])
|| REGNO (ops[j]) < FIRST_PSEUDO_REGISTER)
{
/* This op is an allocno but the one it matches is
not. */
/* If we can't put the other operand into a
register, this alternative can't be used. */
if (classes[j] == NO_REGS)
alt_fail = 1;
/* Otherwise, add to the cost of this alternative
the cost to copy the other operand to the hard
register used for this operand. */
else
alt_cost += copy_cost (ops[j], mode, classes[j], 1, NULL);
}
else
{
/* The costs of this operand are not the same as the
other operand since move costs are not symmetric.
Moreover, if we cannot tie them, this alternative
needs to do a copy, which is one insn. */
struct costs *pp = this_op_costs[i];
int *pp_costs = pp->cost;
cost_classes_t cost_classes_ptr
= regno_cost_classes[REGNO (op)];
enum reg_class *cost_classes = cost_classes_ptr->classes;
bool in_p = recog_data.operand_type[i] != OP_OUT;
bool out_p = recog_data.operand_type[i] != OP_IN;
enum reg_class op_class = classes[i];
ira_init_register_move_cost_if_necessary (mode);
if (! in_p)
{
ira_assert (out_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][0] * frequency;
}
}
else
{
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_out_cost[op_class][rclass] * frequency;
}
}
}
else if (! out_p)
{
ira_assert (in_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][1] * frequency;
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_in_cost[rclass][op_class] * frequency;
}
}
}
else
{
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((mem_cost[rclass][0]
+ mem_cost[rclass][1])
* frequency);
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((move_in_cost[rclass][op_class]
+ move_out_cost[op_class][rclass])
* frequency);
}
}
}
/* If the alternative actually allows memory, make
things a bit cheaper since we won't need an extra
insn to load it. */
pp->mem_cost
= ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
+ (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
- allows_mem[i]) * frequency;
/* If we have assigned a class to this allocno in
our first pass, add a cost to this alternative
corresponding to what we would add if this
allocno were not in the appropriate class. */
if (pref)
{
enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
if (pref_class == NO_REGS)
alt_cost
+= ((out_p
? ira_memory_move_cost[mode][op_class][0] : 0)
+ (in_p
? ira_memory_move_cost[mode][op_class][1]
: 0));
else if (ira_reg_class_intersect
[pref_class][op_class] == NO_REGS)
alt_cost
+= ira_register_move_cost[mode][pref_class][op_class];
}
if (REGNO (ops[i]) != REGNO (ops[j])
&& ! find_reg_note (insn, REG_DEAD, op))
alt_cost += 2;
p++;
}
}
/* Scan all the constraint letters. See if the operand
matches any of the constraints. Collect the valid
register classes and see if this operand accepts
memory. */
while ((c = *p))
{
switch (c)
{
case '*':
/* Ignore the next letter for this pass. */
c = *++p;
break;
case '^':
alt_cost += 2;
break;
case '?':
alt_cost += 2;
break;
case 'g':
if (MEM_P (op)
|| (CONSTANT_P (op)
&& (! flag_pic || LEGITIMATE_PIC_OPERAND_P (op))))
win = 1;
insn_allows_mem[i] = allows_mem[i] = 1;
classes[i] = ira_reg_class_subunion[classes[i]][GENERAL_REGS];
break;
default:
enum constraint_num cn = lookup_constraint (p);
enum reg_class cl;
switch (get_constraint_type (cn))
{
case CT_REGISTER:
cl = reg_class_for_constraint (cn);
if (cl != NO_REGS)
classes[i] = ira_reg_class_subunion[classes[i]][cl];
break;
case CT_CONST_INT:
if (CONST_INT_P (op)
&& insn_const_int_ok_for_constraint (INTVAL (op), cn))
win = 1;
break;
case CT_MEMORY:
/* Every MEM can be reloaded to fit. */
insn_allows_mem[i] = allows_mem[i] = 1;
if (MEM_P (op))
win = 1;
break;
case CT_SPECIAL_MEMORY:
insn_allows_mem[i] = allows_mem[i] = 1;
if (MEM_P (op) && constraint_satisfied_p (op, cn))
win = 1;
break;
case CT_ADDRESS:
/* Every address can be reloaded to fit. */
allows_addr = 1;
if (address_operand (op, GET_MODE (op))
|| constraint_satisfied_p (op, cn))
win = 1;
/* We know this operand is an address, so we
want it to be allocated to a hard register
that can be the base of an address,
i.e. BASE_REG_CLASS. */
classes[i]
= ira_reg_class_subunion[classes[i]]
[base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
ADDRESS, SCRATCH)];
break;
case CT_FIXED_FORM:
if (constraint_satisfied_p (op, cn))
win = 1;
break;
}
break;
}
p += CONSTRAINT_LEN (c, p);
if (c == ',')
break;
}
constraints[i] = p;
if (alt_fail)
break;
/* How we account for this operand now depends on whether it
is a pseudo register or not. If it is, we first check if
any register classes are valid. If not, we ignore this
alternative, since we want to assume that all allocnos get
allocated for register preferencing. If some register
class is valid, compute the costs of moving the allocno
into that class. */
if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
{
if (classes[i] == NO_REGS && ! allows_mem[i])
{
/* We must always fail if the operand is a REG, but
we did not find a suitable class and memory is
not allowed.
Otherwise we may perform an uninitialized read
from this_op_costs after the `continue' statement
below. */
alt_fail = 1;
}
else
{
unsigned int regno = REGNO (op);
struct costs *pp = this_op_costs[i];
int *pp_costs = pp->cost;
cost_classes_t cost_classes_ptr = regno_cost_classes[regno];
enum reg_class *cost_classes = cost_classes_ptr->classes;
bool in_p = recog_data.operand_type[i] != OP_OUT;
bool out_p = recog_data.operand_type[i] != OP_IN;
enum reg_class op_class = classes[i];
ira_init_register_move_cost_if_necessary (mode);
if (! in_p)
{
ira_assert (out_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][0] * frequency;
}
}
else
{
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_out_cost[op_class][rclass] * frequency;
}
}
}
else if (! out_p)
{
ira_assert (in_p);
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = mem_cost[rclass][1] * frequency;
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k]
= move_in_cost[rclass][op_class] * frequency;
}
}
}
else
{
if (op_class == NO_REGS)
{
mem_cost = ira_memory_move_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((mem_cost[rclass][0]
+ mem_cost[rclass][1])
* frequency);
}
}
else
{
move_in_cost = ira_may_move_in_cost[mode];
move_out_cost = ira_may_move_out_cost[mode];
for (k = cost_classes_ptr->num - 1; k >= 0; k--)
{
rclass = cost_classes[k];
pp_costs[k] = ((move_in_cost[rclass][op_class]
+ move_out_cost[op_class][rclass])
* frequency);
}
}
}
if (op_class == NO_REGS)
/* Although we don't need insn to reload from
memory, still accessing memory is usually more
expensive than a register. */
pp->mem_cost = frequency;
else
/* If the alternative actually allows memory, make
things a bit cheaper since we won't need an
extra insn to load it. */
pp->mem_cost
= ((out_p ? ira_memory_move_cost[mode][op_class][0] : 0)
+ (in_p ? ira_memory_move_cost[mode][op_class][1] : 0)
- allows_mem[i]) * frequency;
/* If we have assigned a class to this allocno in
our first pass, add a cost to this alternative
corresponding to what we would add if this
allocno were not in the appropriate class. */
if (pref)
{
enum reg_class pref_class = pref[COST_INDEX (REGNO (op))];
if (pref_class == NO_REGS)
{
if (op_class != NO_REGS)
alt_cost
+= ((out_p
? ira_memory_move_cost[mode][op_class][0]
: 0)
+ (in_p
? ira_memory_move_cost[mode][op_class][1]
: 0));
}
else if (op_class == NO_REGS)
alt_cost
+= ((out_p
? ira_memory_move_cost[mode][pref_class][1]
: 0)
+ (in_p
? ira_memory_move_cost[mode][pref_class][0]
: 0));
else if (ira_reg_class_intersect[pref_class][op_class]
== NO_REGS)
alt_cost += (ira_register_move_cost
[mode][pref_class][op_class]);
}
}
}
/* Otherwise, if this alternative wins, either because we
have already determined that or if we have a hard
register of the proper class, there is no cost for this
alternative. */
else if (win || (REG_P (op)
&& reg_fits_class_p (op, classes[i],
0, GET_MODE (op))))
;
/* If registers are valid, the cost of this alternative
includes copying the object to and/or from a
register. */
else if (classes[i] != NO_REGS)
{
if (recog_data.operand_type[i] != OP_OUT)
alt_cost += copy_cost (op, mode, classes[i], 1, NULL);
if (recog_data.operand_type[i] != OP_IN)
alt_cost += copy_cost (op, mode, classes[i], 0, NULL);
}
/* The only other way this alternative can be used is if
this is a constant that could be placed into memory. */
else if (CONSTANT_P (op) && (allows_addr || allows_mem[i]))
alt_cost += ira_memory_move_cost[mode][classes[i]][1];
else
alt_fail = 1;