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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This file contains register alloction support and is intended to be
* included by:
*
* Codegen-$(TARGET_ARCH_VARIANT).c
*
*/
#include "../CompilerUtility.h"
#include "../CompilerIR.h"
#include "../Dataflow.h"
#include "Ralloc.h"
#define SREG(c, s) ((c)->regLocation[(s)].sRegLow)
/*
* Get the "real" sreg number associated with an sReg slot. In general,
* sReg values passed through codegen are the SSA names created by
* dataflow analysis and refer to slot numbers in the cUnit->regLocation
* array. However, renaming is accomplished by simply replacing RegLocation
* entries in the cUnit->reglocation[] array. Therefore, when location
* records for operands are first created, we need to ask the locRecord
* identified by the dataflow pass what it's new name is.
*/
/*
* Free all allocated temps in the temp pools. Note that this does
* not affect the "liveness" of a temp register, which will stay
* live until it is either explicitly killed or reallocated.
*/
extern void oatResetRegPool(CompilationUnit* cUnit)
{
int i;
for (i=0; i < cUnit->regPool->numCoreRegs; i++) {
if (cUnit->regPool->coreRegs[i].isTemp)
cUnit->regPool->coreRegs[i].inUse = false;
}
for (i=0; i < cUnit->regPool->numFPRegs; i++) {
if (cUnit->regPool->FPRegs[i].isTemp)
cUnit->regPool->FPRegs[i].inUse = false;
}
}
/* Set up temp & preserved register pools specialized by target */
extern void oatInitPool(RegisterInfo* regs, int* regNums, int num)
{
int i;
for (i=0; i < num; i++) {
regs[i].reg = regNums[i];
regs[i].inUse = false;
regs[i].isTemp = false;
regs[i].pair = false;
regs[i].live = false;
regs[i].dirty = false;
regs[i].sReg = INVALID_SREG;
}
}
STATIC void dumpRegPool(RegisterInfo* p, int numRegs)
{
int i;
LOG(INFO) << "================================================";
for (i=0; i < numRegs; i++ ){
char buf[100];
snprintf(buf, 100,
"R[%d]: T:%d, U:%d, P:%d, p:%d, LV:%d, D:%d, SR:%d, ST:%x, EN:%x",
p[i].reg, p[i].isTemp, p[i].inUse, p[i].pair, p[i].partner,
p[i].live, p[i].dirty, p[i].sReg,(int)p[i].defStart,
(int)p[i].defEnd);
LOG(INFO) << buf;
}
LOG(INFO) << "================================================";
}
void oatDumpCoreRegPool(CompilationUnit* cUnit)
{
dumpRegPool(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs);
}
void oatDumpFpRegPool(CompilationUnit* cUnit)
{
dumpRegPool(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs);
}
void oatFlushRegWide(CompilationUnit* cUnit, int reg1, int reg2)
{
RegisterInfo* info1 = oatGetRegInfo(cUnit, reg1);
RegisterInfo* info2 = oatGetRegInfo(cUnit, reg2);
DCHECK(info1 && info2 && info1->pair && info2->pair &&
(info1->partner == info2->reg) &&
(info2->partner == info1->reg));
if ((info1->live && info1->dirty) || (info2->live && info2->dirty)) {
if (!(info1->isTemp && info2->isTemp)) {
/* Should not happen. If it does, there's a problem in evalLoc */
LOG(FATAL) << "Long half-temp, half-promoted";
}
info1->dirty = false;
info2->dirty = false;
if (oatS2VReg(cUnit, info2->sReg) <
oatS2VReg(cUnit, info1->sReg))
info1 = info2;
int vReg = oatS2VReg(cUnit, info1->sReg);
oatFlushRegWideImpl(cUnit, rSP,
oatVRegOffset(cUnit, vReg),
info1->reg, info1->partner);
}
}
void oatFlushReg(CompilationUnit* cUnit, int reg)
{
RegisterInfo* info = oatGetRegInfo(cUnit, reg);
if (info->live && info->dirty) {
info->dirty = false;
int vReg = oatS2VReg(cUnit, info->sReg);
oatFlushRegImpl(cUnit, rSP,
oatVRegOffset(cUnit, vReg),
reg, kWord);
}
}
/* Mark a temp register as dead. Does not affect allocation state. */
void oatClobber(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = oatGetRegInfo(cUnit, reg);
if (p->isTemp) {
DCHECK(!(p->live && p->dirty)) << "Live & dirty temp in clobber";
p->live = false;
p->sReg = INVALID_SREG;
p->defStart = NULL;
p->defEnd = NULL;
if (p->pair) {
p->pair = false;
oatClobber(cUnit, p->partner);
}
}
}
STATIC void clobberSRegBody(RegisterInfo* p, int numRegs, int sReg)
{
int i;
for (i=0; i< numRegs; i++) {
if (p[i].sReg == sReg) {
if (p[i].isTemp) {
p[i].live = false;
}
p[i].defStart = NULL;
p[i].defEnd = NULL;
}
}
}
/* Clobber any temp associated with an sReg. Could be in either class */
extern void oatClobberSReg(CompilationUnit* cUnit, int sReg)
{
clobberSRegBody(cUnit->regPool->coreRegs, cUnit->regPool->numCoreRegs,
sReg);
clobberSRegBody(cUnit->regPool->FPRegs, cUnit->regPool->numFPRegs,
sReg);
}
/* Reserve a callee-save register. Return -1 if none available */
extern int oatAllocPreservedCoreReg(CompilationUnit* cUnit, int sReg)
{
int res = -1;
RegisterInfo* coreRegs = cUnit->regPool->coreRegs;
for (int i = 0; i < cUnit->regPool->numCoreRegs; i++) {
if (!coreRegs[i].isTemp && !coreRegs[i].inUse) {
res = coreRegs[i].reg;
coreRegs[i].inUse = true;
cUnit->coreSpillMask |= (1 << res);
cUnit->coreVmapTable.push_back(sReg);
cUnit->numCoreSpills++;
// Should be promoting based on initial sReg set
DCHECK_EQ(sReg, oatS2VReg(cUnit, sReg));
cUnit->promotionMap[sReg].coreLocation = kLocPhysReg;
cUnit->promotionMap[sReg].coreReg = res;
break;
}
}
return res;
}
/*
* Mark a callee-save fp register as promoted. Note that
* vpush/vpop uses contiguous register lists so we must
* include any holes in the mask. Associate holes with
* Dalvik register INVALID_VREG (0xFFFFU).
*/
STATIC void markPreservedSingle(CompilationUnit* cUnit, int sReg, int reg)
{
DCHECK_GE(reg, FP_REG_MASK + FP_CALLEE_SAVE_BASE);
reg = (reg & FP_REG_MASK) - FP_CALLEE_SAVE_BASE;
// Ensure fpVmapTable is large enough
int tableSize = cUnit->fpVmapTable.size();
for (int i = tableSize; i < (reg + 1); i++) {
cUnit->fpVmapTable.push_back(INVALID_VREG);
}
// Add the current mapping
cUnit->fpVmapTable[reg] = sReg;
// Size of fpVmapTable is high-water mark, use to set mask
cUnit->numFPSpills = cUnit->fpVmapTable.size();
cUnit->fpSpillMask = ((1 << cUnit->numFPSpills) - 1) << FP_CALLEE_SAVE_BASE;
}
/*
* Reserve a callee-save fp single register. Try to fullfill request for
* even/odd allocation, but go ahead and allocate anything if not
* available. If nothing's available, return -1.
*/
STATIC int allocPreservedSingle(CompilationUnit* cUnit, int sReg, bool even)
{
int res = -1;
RegisterInfo* FPRegs = cUnit->regPool->FPRegs;
for (int i = 0; i < cUnit->regPool->numFPRegs; i++) {
if (!FPRegs[i].isTemp && !FPRegs[i].inUse &&
((FPRegs[i].reg & 0x1) == 0) == even) {
res = FPRegs[i].reg;
FPRegs[i].inUse = true;
// Should be promoting based on initial sReg set
DCHECK_EQ(sReg, oatS2VReg(cUnit, sReg));
markPreservedSingle(cUnit, sReg, res);
cUnit->promotionMap[sReg].fpLocation = kLocPhysReg;
cUnit->promotionMap[sReg].fpReg = res;
break;
}
}
return res;
}
/*
* Somewhat messy code here. We want to allocate a pair of contiguous
* physical single-precision floating point registers starting with
* an even numbered reg. It is possible that the paired sReg (sReg+1)
* has already been allocated - try to fit if possible. Fail to
* allocate if we can't meet the requirements for the pair of
* sReg<=sX[even] & (sReg+1)<= sX+1.
*/
STATIC int allocPreservedDouble(CompilationUnit* cUnit, int sReg)
{
int res = -1; // Assume failure
// Should be promoting based on initial sReg set
DCHECK_EQ(sReg, oatS2VReg(cUnit, sReg));
if (cUnit->promotionMap[sReg+1].fpLocation == kLocPhysReg) {
// Upper reg is already allocated. Can we fit?
int highReg = cUnit->promotionMap[sReg+1].fpReg;
if ((highReg & 1) == 0) {
// High reg is even - fail.
return res;
}
// Is the low reg of the pair free?
RegisterInfo* p = oatGetRegInfo(cUnit, highReg-1);
if (p->inUse || p->isTemp) {
// Already allocated or not preserved - fail.
return res;
}
// OK - good to go.
res = p->reg;
p->inUse = true;
DCHECK_EQ((res & 1), 0);
markPreservedSingle(cUnit, sReg, res);
} else {
RegisterInfo* FPRegs = cUnit->regPool->FPRegs;
for (int i = 0; i < cUnit->regPool->numFPRegs; i++) {
if (!FPRegs[i].isTemp && !FPRegs[i].inUse &&
((FPRegs[i].reg & 0x1) == 0x0) &&
!FPRegs[i+1].isTemp && !FPRegs[i+1].inUse &&
((FPRegs[i+1].reg & 0x1) == 0x1) &&
(FPRegs[i].reg + 1) == FPRegs[i+1].reg) {
res = FPRegs[i].reg;
FPRegs[i].inUse = true;
markPreservedSingle(cUnit, sReg, res);
FPRegs[i+1].inUse = true;
DCHECK_EQ(res + 1, FPRegs[i+1].reg);
markPreservedSingle(cUnit, sReg+1, res+1);
break;
}
}
}
if (res != -1) {
cUnit->promotionMap[sReg].fpLocation = kLocPhysReg;
cUnit->promotionMap[sReg].fpReg = res;
cUnit->promotionMap[sReg+1].fpLocation = kLocPhysReg;
cUnit->promotionMap[sReg+1].fpReg = res + 1;
}
return res;
}
/*
* Reserve a callee-save fp register. If this register can be used
* as the first of a double, attempt to allocate an even pair of fp
* single regs (but if can't still attempt to allocate a single, preferring
* first to allocate an odd register.
*/
extern int oatAllocPreservedFPReg(CompilationUnit* cUnit, int sReg,
bool doubleStart)
{
int res = -1;
if (doubleStart) {
res = allocPreservedDouble(cUnit, sReg);
}
if (res == -1) {
res = allocPreservedSingle(cUnit, sReg, false /* try odd # */);
}
if (res == -1)
res = allocPreservedSingle(cUnit, sReg, true /* try even # */);
return res;
}
STATIC int allocTempBody(CompilationUnit* cUnit, RegisterInfo* p, int numRegs,
int* nextTemp, bool required)
{
int i;
int next = *nextTemp;
for (i=0; i< numRegs; i++) {
if (next >= numRegs)
next = 0;
if (p[next].isTemp && !p[next].inUse && !p[next].live) {
oatClobber(cUnit, p[next].reg);
p[next].inUse = true;
p[next].pair = false;
*nextTemp = next + 1;
return p[next].reg;
}
next++;
}
next = *nextTemp;
for (i=0; i< numRegs; i++) {
if (next >= numRegs)
next = 0;
if (p[next].isTemp && !p[next].inUse) {
oatClobber(cUnit, p[next].reg);
p[next].inUse = true;
p[next].pair = false;
*nextTemp = next + 1;
return p[next].reg;
}
next++;
}
if (required) {
oatCodegenDump(cUnit);
dumpRegPool(cUnit->regPool->coreRegs,
cUnit->regPool->numCoreRegs);
LOG(FATAL) << "No free temp registers";
}
return -1; // No register available
}
//REDO: too many assumptions.
extern int oatAllocTempDouble(CompilationUnit* cUnit)
{
RegisterInfo* p = cUnit->regPool->FPRegs;
int numRegs = cUnit->regPool->numFPRegs;
/* Start looking at an even reg */
int next = cUnit->regPool->nextFPReg & ~0x1;
// First try to avoid allocating live registers
for (int i=0; i < numRegs; i+=2) {
if (next >= numRegs)
next = 0;
if ((p[next].isTemp && !p[next].inUse && !p[next].live) &&
(p[next+1].isTemp && !p[next+1].inUse && !p[next+1].live)) {
oatClobber(cUnit, p[next].reg);
oatClobber(cUnit, p[next+1].reg);
p[next].inUse = true;
p[next+1].inUse = true;
DCHECK_EQ((p[next].reg+1), p[next+1].reg);
DCHECK_EQ((p[next].reg & 0x1), 0);
cUnit->regPool->nextFPReg = next + 2;
if (cUnit->regPool->nextFPReg >= numRegs) {
cUnit->regPool->nextFPReg = 0;
}
return p[next].reg;
}
next += 2;
}
next = cUnit->regPool->nextFPReg & ~0x1;
// No choice - find a pair and kill it.
for (int i=0; i < numRegs; i+=2) {
if (next >= numRegs)
next = 0;
if (p[next].isTemp && !p[next].inUse && p[next+1].isTemp &&
!p[next+1].inUse) {
oatClobber(cUnit, p[next].reg);
oatClobber(cUnit, p[next+1].reg);
p[next].inUse = true;
p[next+1].inUse = true;
DCHECK_EQ((p[next].reg+1), p[next+1].reg);
DCHECK_EQ((p[next].reg & 0x1), 0);
cUnit->regPool->nextFPReg = next + 2;
if (cUnit->regPool->nextFPReg >= numRegs) {
cUnit->regPool->nextFPReg = 0;
}
return p[next].reg;
}
next += 2;
}
LOG(FATAL) << "No free temp registers (pair)";
return -1;
}
/* Return a temp if one is available, -1 otherwise */
extern int oatAllocFreeTemp(CompilationUnit* cUnit)
{
return allocTempBody(cUnit, cUnit->regPool->coreRegs,
cUnit->regPool->numCoreRegs,
&cUnit->regPool->nextCoreReg, true);
}
extern int oatAllocTemp(CompilationUnit* cUnit)
{
return allocTempBody(cUnit, cUnit->regPool->coreRegs,
cUnit->regPool->numCoreRegs,
&cUnit->regPool->nextCoreReg, true);
}
extern int oatAllocTempFloat(CompilationUnit* cUnit)
{
return allocTempBody(cUnit, cUnit->regPool->FPRegs,
cUnit->regPool->numFPRegs,
&cUnit->regPool->nextFPReg, true);
}
STATIC RegisterInfo* allocLiveBody(RegisterInfo* p, int numRegs, int sReg)
{
int i;
if (sReg == -1)
return NULL;
for (i=0; i < numRegs; i++) {
if (p[i].live && (p[i].sReg == sReg)) {
if (p[i].isTemp)
p[i].inUse = true;
return &p[i];
}
}
return NULL;
}
STATIC RegisterInfo* allocLive(CompilationUnit* cUnit, int sReg,
int regClass)
{
RegisterInfo* res = NULL;
switch(regClass) {
case kAnyReg:
res = allocLiveBody(cUnit->regPool->FPRegs,
cUnit->regPool->numFPRegs, sReg);
if (res)
break;
/* Intentional fallthrough */
case kCoreReg:
res = allocLiveBody(cUnit->regPool->coreRegs,
cUnit->regPool->numCoreRegs, sReg);
break;
case kFPReg:
res = allocLiveBody(cUnit->regPool->FPRegs,
cUnit->regPool->numFPRegs, sReg);
break;
default:
LOG(FATAL) << "Invalid register type";
}
return res;
}
extern void oatFreeTemp(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = cUnit->regPool->coreRegs;
int numRegs = cUnit->regPool->numCoreRegs;
int i;
for (i=0; i< numRegs; i++) {
if (p[i].reg == reg) {
if (p[i].isTemp) {
p[i].inUse = false;
}
p[i].pair = false;
return;
}
}
p = cUnit->regPool->FPRegs;
numRegs = cUnit->regPool->numFPRegs;
for (i=0; i< numRegs; i++) {
if (p[i].reg == reg) {
if (p[i].isTemp) {
p[i].inUse = false;
}
p[i].pair = false;
return;
}
}
LOG(FATAL) << "Tried to free a non-existant temp: r" << reg;
}
extern RegisterInfo* oatIsLive(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = cUnit->regPool->coreRegs;
int numRegs = cUnit->regPool->numCoreRegs;
int i;
for (i=0; i< numRegs; i++) {
if (p[i].reg == reg) {
return p[i].live ? &p[i] : NULL;
}
}
p = cUnit->regPool->FPRegs;
numRegs = cUnit->regPool->numFPRegs;
for (i=0; i< numRegs; i++) {
if (p[i].reg == reg) {
return p[i].live ? &p[i] : NULL;
}
}
return NULL;
}
extern RegisterInfo* oatIsTemp(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = oatGetRegInfo(cUnit, reg);
return (p->isTemp) ? p : NULL;
}
extern RegisterInfo* oatIsPromoted(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = oatGetRegInfo(cUnit, reg);
return (p->isTemp) ? NULL : p;
}
extern bool oatIsDirty(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = oatGetRegInfo(cUnit, reg);
return p->dirty;
}
/*
* Similar to oatAllocTemp(), but forces the allocation of a specific
* register. No check is made to see if the register was previously
* allocated. Use with caution.
*/
extern void oatLockTemp(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = cUnit->regPool->coreRegs;
int numRegs = cUnit->regPool->numCoreRegs;
int i;
for (i=0; i< numRegs; i++) {
if (p[i].reg == reg) {
DCHECK(p[i].isTemp);
p[i].inUse = true;
p[i].live = false;
return;
}
}
p = cUnit->regPool->FPRegs;
numRegs = cUnit->regPool->numFPRegs;
for (i=0; i< numRegs; i++) {
if (p[i].reg == reg) {
DCHECK(p[i].isTemp);
p[i].inUse = true;
p[i].live = false;
return;
}
}
LOG(FATAL) << "Tried to lock a non-existant temp: r" << reg;
}
extern void oatResetDef(CompilationUnit* cUnit, int reg)
{
RegisterInfo* p = oatGetRegInfo(cUnit, reg);
p->defStart = NULL;
p->defEnd = NULL;
}
STATIC void nullifyRange(CompilationUnit* cUnit, LIR *start, LIR *finish,
int sReg1, int sReg2)
{
if (start && finish) {
LIR *p;
DCHECK_EQ(sReg1, sReg2);
for (p = start; ;p = p->next) {
((ArmLIR *)p)->flags.isNop = true;
((ArmLIR *)p)->flags.squashed = true;
if (p == finish)
break;
}
}
}
/*
* Mark the beginning and end LIR of a def sequence. Note that
* on entry start points to the LIR prior to the beginning of the
* sequence.
*/
extern void oatMarkDef(CompilationUnit* cUnit, RegLocation rl,
LIR *start, LIR *finish)
{
DCHECK(!rl.wide);
DCHECK(start && start->next);
DCHECK(finish);
RegisterInfo* p = oatGetRegInfo(cUnit, rl.lowReg);
p->defStart = start->next;
p->defEnd = finish;
}
/*
* Mark the beginning and end LIR of a def sequence. Note that
* on entry start points to the LIR prior to the beginning of the
* sequence.
*/
extern void oatMarkDefWide(CompilationUnit* cUnit, RegLocation rl,
LIR *start, LIR *finish)
{
DCHECK(rl.wide);
DCHECK(start && start->next);
DCHECK(finish);
RegisterInfo* p = oatGetRegInfo(cUnit, rl.lowReg);
oatResetDef(cUnit, rl.highReg); // Only track low of pair
p->defStart = start->next;
p->defEnd = finish;
}
extern RegLocation oatWideToNarrow(CompilationUnit* cUnit,
RegLocation rl)
{
DCHECK(rl.wide);
if (rl.location == kLocPhysReg) {
RegisterInfo* infoLo = oatGetRegInfo(cUnit, rl.lowReg);
RegisterInfo* infoHi = oatGetRegInfo(cUnit, rl.highReg);
if (infoLo->isTemp) {
infoLo->pair = false;
infoLo->defStart = NULL;
infoLo->defEnd = NULL;
}
if (infoHi->isTemp) {
infoHi->pair = false;
infoHi->defStart = NULL;
infoHi->defEnd = NULL;
}
}
rl.wide = false;
return rl;
}
extern void oatResetDefLoc(CompilationUnit* cUnit, RegLocation rl)
{
DCHECK(!rl.wide);
RegisterInfo* p = oatIsTemp(cUnit, rl.lowReg);
if (p && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
DCHECK(!p->pair);
nullifyRange(cUnit, p->defStart, p->defEnd,
p->sReg, rl.sRegLow);
}
oatResetDef(cUnit, rl.lowReg);
}
extern void oatResetDefLocWide(CompilationUnit* cUnit, RegLocation rl)
{
DCHECK(rl.wide);
RegisterInfo* pLow = oatIsTemp(cUnit, rl.lowReg);
RegisterInfo* pHigh = oatIsTemp(cUnit, rl.highReg);
if (pLow && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
DCHECK(pLow->pair);
nullifyRange(cUnit, pLow->defStart, pLow->defEnd,
pLow->sReg, rl.sRegLow);
}
if (pHigh && !(cUnit->disableOpt & (1 << kSuppressLoads))) {
DCHECK(pHigh->pair);
}
oatResetDef(cUnit, rl.lowReg);
oatResetDef(cUnit, rl.highReg);
}
extern void oatResetDefTracking(CompilationUnit* cUnit)
{
int i;
for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
oatResetDef(cUnit, cUnit->regPool->coreRegs[i].reg);
}
for (i=0; i< cUnit->regPool->numFPRegs; i++) {
oatResetDef(cUnit, cUnit->regPool->FPRegs[i].reg);
}
}
extern void oatClobberAllRegs(CompilationUnit* cUnit)
{
int i;
for (i=0; i< cUnit->regPool->numCoreRegs; i++) {
oatClobber(cUnit, cUnit->regPool->coreRegs[i].reg);
}
for (i=0; i< cUnit->regPool->numFPRegs; i++) {
oatClobber(cUnit, cUnit->regPool->FPRegs[i].reg);
}
}
/* To be used when explicitly managing register use */
extern void oatLockCallTemps(CompilationUnit* cUnit)
{
//TODO: Arm specific - move to target dependent code
oatLockTemp(cUnit, r0);
oatLockTemp(cUnit, r1);
oatLockTemp(cUnit, r2);
oatLockTemp(cUnit, r3);
}
/* To be used when explicitly managing register use */
extern void oatFreeCallTemps(CompilationUnit* cUnit)
{
//TODO: Arm specific - move to target dependent code
oatFreeTemp(cUnit, r0);
oatFreeTemp(cUnit, r1);
oatFreeTemp(cUnit, r2);
oatFreeTemp(cUnit, r3);
}
// Make sure nothing is live and dirty
STATIC void flushAllRegsBody(CompilationUnit* cUnit, RegisterInfo* info,
int numRegs)
{
int i;
for (i=0; i < numRegs; i++) {
if (info[i].live && info[i].dirty) {
if (info[i].pair) {
oatFlushRegWide(cUnit, info[i].reg, info[i].partner);
} else {
oatFlushReg(cUnit, info[i].reg);
}
}
}
}
extern void oatFlushAllRegs(CompilationUnit* cUnit)
{
flushAllRegsBody(cUnit, cUnit->regPool->coreRegs,
cUnit->regPool->numCoreRegs);
flushAllRegsBody(cUnit, cUnit->regPool->FPRegs,
cUnit->regPool->numFPRegs);
oatClobberAllRegs(cUnit);
}
//TUNING: rewrite all of this reg stuff. Probably use an attribute table
STATIC bool regClassMatches(int regClass, int reg)
{
if (regClass == kAnyReg) {
return true;
} else if (regClass == kCoreReg) {
return !FPREG(reg);
} else {
return FPREG(reg);
}
}
extern void oatMarkLive(CompilationUnit* cUnit, int reg, int sReg)
{
RegisterInfo* info = oatGetRegInfo(cUnit, reg);
if ((info->reg == reg) && (info->sReg == sReg) && info->live) {
return; /* already live */
} else if (sReg != INVALID_SREG) {
oatClobberSReg(cUnit, sReg);
if (info->isTemp) {
info->live = true;
}
} else {
/* Can't be live if no associated sReg */
DCHECK(info->isTemp);
info->live = false;
}
info->sReg = sReg;
}
extern void oatMarkTemp(CompilationUnit* cUnit, int reg)
{
RegisterInfo* info = oatGetRegInfo(cUnit, reg);
info->isTemp = true;
}
extern void oatUnmarkTemp(CompilationUnit* cUnit, int reg)
{
RegisterInfo* info = oatGetRegInfo(cUnit, reg);
info->isTemp = false;
}
extern void oatMarkPair(CompilationUnit* cUnit, int lowReg, int highReg)
{
RegisterInfo* infoLo = oatGetRegInfo(cUnit, lowReg);
RegisterInfo* infoHi = oatGetRegInfo(cUnit, highReg);
infoLo->pair = infoHi->pair = true;
infoLo->partner = highReg;
infoHi->partner = lowReg;
}
extern void oatMarkClean(CompilationUnit* cUnit, RegLocation loc)
{
RegisterInfo* info = oatGetRegInfo(cUnit, loc.lowReg);
info->dirty = false;
if (loc.wide) {
info = oatGetRegInfo(cUnit, loc.highReg);
info->dirty = false;
}
}
extern void oatMarkDirty(CompilationUnit* cUnit, RegLocation loc)
{
if (loc.home) {
// If already home, can't be dirty
return;
}
RegisterInfo* info = oatGetRegInfo(cUnit, loc.lowReg);
info->dirty = true;
if (loc.wide) {
info = oatGetRegInfo(cUnit, loc.highReg);
info->dirty = true;
}
}
extern void oatMarkInUse(CompilationUnit* cUnit, int reg)
{
RegisterInfo* info = oatGetRegInfo(cUnit, reg);
info->inUse = true;
}
STATIC void copyRegInfo(CompilationUnit* cUnit, int newReg, int oldReg)
{
RegisterInfo* newInfo = oatGetRegInfo(cUnit, newReg);
RegisterInfo* oldInfo = oatGetRegInfo(cUnit, oldReg);
// Target temp status must not change
bool isTemp = newInfo->isTemp;
*newInfo = *oldInfo;
// Restore target's temp status
newInfo->isTemp = isTemp;
newInfo->reg = newReg;
}
/*
* Return an updated location record with current in-register status.
* If the value lives in live temps, reflect that fact. No code
* is generated. If the live value is part of an older pair,
* clobber both low and high.
* TUNING: clobbering both is a bit heavy-handed, but the alternative
* is a bit complex when dealing with FP regs. Examine code to see
* if it's worthwhile trying to be more clever here.
*/
extern RegLocation oatUpdateLoc(CompilationUnit* cUnit, RegLocation loc)
{
DCHECK(!loc.wide);
DCHECK(oatCheckCorePoolSanity(cUnit));
if (loc.location == kLocDalvikFrame) {
RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg);
if (infoLo) {
if (infoLo->pair) {
oatClobber(cUnit, infoLo->reg);
oatClobber(cUnit, infoLo->partner);
oatFreeTemp(cUnit, infoLo->reg);
} else {
loc.lowReg = infoLo->reg;
loc.location = kLocPhysReg;
}
}
}
return loc;
}
bool oatCheckCorePoolSanity(CompilationUnit* cUnit)
{
for (static int i = 0; i < cUnit->regPool->numCoreRegs; i++) {
if (cUnit->regPool->coreRegs[i].pair) {
static int myReg = cUnit->regPool->coreRegs[i].reg;
static int mySreg = cUnit->regPool->coreRegs[i].sReg;
static int partnerReg = cUnit->regPool->coreRegs[i].partner;
static RegisterInfo* partner = oatGetRegInfo(cUnit, partnerReg);
DCHECK(partner != NULL);
DCHECK(partner->pair);
DCHECK_EQ(myReg, partner->partner);
static int partnerSreg = partner->sReg;
if (mySreg == INVALID_SREG) {
DCHECK_EQ(partnerSreg, INVALID_SREG);
} else {
int diff = mySreg - partnerSreg;
DCHECK((diff == -1) || (diff == 1));
}
}
if (!cUnit->regPool->coreRegs[i].live) {
DCHECK(cUnit->regPool->coreRegs[i].defStart == NULL);
DCHECK(cUnit->regPool->coreRegs[i].defEnd == NULL);
}
}
return true;
}
/* see comments for updateLoc */
extern RegLocation oatUpdateLocWide(CompilationUnit* cUnit,
RegLocation loc)
{
DCHECK(loc.wide);
DCHECK(oatCheckCorePoolSanity(cUnit));
if (loc.location == kLocDalvikFrame) {
// Are the dalvik regs already live in physical registers?
RegisterInfo* infoLo = allocLive(cUnit, loc.sRegLow, kAnyReg);
RegisterInfo* infoHi = allocLive(cUnit,
oatSRegHi(loc.sRegLow), kAnyReg);
bool match = true;
match = match && (infoLo != NULL);
match = match && (infoHi != NULL);
// Are they both core or both FP?
match = match && (FPREG(infoLo->reg) == FPREG(infoHi->reg));
// If a pair of floating point singles, are they properly aligned?
if (match && FPREG(infoLo->reg)) {
match &= ((infoLo->reg & 0x1) == 0);
match &= ((infoHi->reg - infoLo->reg) == 1);
}
// If previously used as a pair, it is the same pair?
if (match && (infoLo->pair || infoHi->pair)) {
match = (infoLo->pair == infoHi->pair);
match &= ((infoLo->reg == infoHi->partner) &&
(infoHi->reg == infoLo->partner));
}
if (match) {
// Can reuse - update the register usage info
loc.lowReg = infoLo->reg;
loc.highReg = infoHi->reg;
loc.location = kLocPhysReg;
oatMarkPair(cUnit, loc.lowReg, loc.highReg);
DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
return loc;
}
// Can't easily reuse - clobber and free any overlaps
if (infoLo) {
oatClobber(cUnit, infoLo->reg);
oatFreeTemp(cUnit, infoLo->reg);
if (infoLo->pair)
oatClobber(cUnit, infoLo->partner);
}
if (infoHi) {
oatClobber(cUnit, infoHi->reg);
oatFreeTemp(cUnit, infoHi->reg);
if (infoHi->pair)
oatClobber(cUnit, infoHi->partner);
}
}
return loc;
}
/* For use in cases we don't know (or care) width */
extern RegLocation oatUpdateRawLoc(CompilationUnit* cUnit,
RegLocation loc)
{
if (loc.wide)
return oatUpdateLocWide(cUnit, loc);
else
return oatUpdateLoc(cUnit, loc);
}
STATIC RegLocation evalLocWide(CompilationUnit* cUnit, RegLocation loc,
int regClass, bool update)
{
DCHECK(loc.wide);
int newRegs;
int lowReg;
int highReg;
loc = oatUpdateLocWide(cUnit, loc);
/* If already in registers, we can assume proper form. Right reg class? */
if (loc.location == kLocPhysReg) {
DCHECK_EQ(FPREG(loc.lowReg), FPREG(loc.highReg));
DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
if (!regClassMatches(regClass, loc.lowReg)) {
/* Wrong register class. Reallocate and copy */
newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass);
lowReg = newRegs & 0xff;
highReg = (newRegs >> 8) & 0xff;
oatRegCopyWide(cUnit, lowReg, highReg, loc.lowReg,
loc.highReg);
copyRegInfo(cUnit, lowReg, loc.lowReg);
copyRegInfo(cUnit, highReg, loc.highReg);
oatClobber(cUnit, loc.lowReg);
oatClobber(cUnit, loc.highReg);
loc.lowReg = lowReg;
loc.highReg = highReg;
oatMarkPair(cUnit, loc.lowReg, loc.highReg);
DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
}
return loc;
}
DCHECK_NE(loc.sRegLow, INVALID_SREG);
DCHECK_NE(oatSRegHi(loc.sRegLow), INVALID_SREG);
newRegs = oatAllocTypedTempPair(cUnit, loc.fp, regClass);
loc.lowReg = newRegs & 0xff;
loc.highReg = (newRegs >> 8) & 0xff;
oatMarkPair(cUnit, loc.lowReg, loc.highReg);
if (update) {
loc.location = kLocPhysReg;
oatMarkLive(cUnit, loc.lowReg, loc.sRegLow);
oatMarkLive(cUnit, loc.highReg, oatSRegHi(loc.sRegLow));
}
DCHECK(!FPREG(loc.lowReg) || ((loc.lowReg & 0x1) == 0));
return loc;
}
extern RegLocation oatEvalLoc(CompilationUnit* cUnit, RegLocation loc,
int regClass, bool update)
{
int newReg;
if (loc.wide)
return evalLocWide(cUnit, loc, regClass, update);
loc = oatUpdateLoc(cUnit, loc);
if (loc.location == kLocPhysReg) {
if (!regClassMatches(regClass, loc.lowReg)) {
/* Wrong register class. Realloc, copy and transfer ownership */
newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass);
oatRegCopy(cUnit, newReg, loc.lowReg);
copyRegInfo(cUnit, newReg, loc.lowReg);
oatClobber(cUnit, loc.lowReg);
loc.lowReg = newReg;
}
return loc;
}
DCHECK_NE(loc.sRegLow, INVALID_SREG);
newReg = oatAllocTypedTemp(cUnit, loc.fp, regClass);
loc.lowReg = newReg;
if (update) {
loc.location = kLocPhysReg;
oatMarkLive(cUnit, loc.lowReg, loc.sRegLow);
}
return loc;
}
extern RegLocation oatGetDest(CompilationUnit* cUnit, MIR* mir, int num)
{
RegLocation res = cUnit->regLocation[mir->ssaRep->defs[num]];
DCHECK(!res.wide);
return res;
}
extern RegLocation oatGetSrc(CompilationUnit* cUnit, MIR* mir, int num)
{
RegLocation res = cUnit->regLocation[mir->ssaRep->uses[num]];
DCHECK(!res.wide);
return res;
}
extern RegLocation oatGetRawSrc(CompilationUnit* cUnit, MIR* mir, int num)
{
RegLocation res = cUnit->regLocation[mir->ssaRep->uses[num]];
return res;
}
extern RegLocation oatGetDestWide(CompilationUnit* cUnit, MIR* mir,
int low, int high)
{
RegLocation res = cUnit->regLocation[mir->ssaRep->defs[low]];
DCHECK(res.wide);
return res;
}
extern RegLocation oatGetSrcWide(CompilationUnit* cUnit, MIR* mir,
int low, int high)
{
RegLocation res = cUnit->regLocation[mir->ssaRep->uses[low]];
DCHECK(res.wide);
return res;
}
|