-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign.cc
More file actions
1674 lines (1531 loc) · 50.1 KB
/
Copy pathassign.cc
File metadata and controls
1674 lines (1531 loc) · 50.1 KB
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
/* contains code used for assigning machine registers to live ranges.
* if the live range has not been allocated a register then it is
* given a temporary register. all of the details of managing
* temporary registers are in this file.
*
* if you want to peek into the bowles of hell then read on...
*/
/*--------------------------INCLUDES---------------------------*/
#include <SSA.h>
#include <list>
#include <vector>
#include <map>
#include <list>
#include <algorithm>
#include <functional>
#include <utility>
#include "assign.h"
#include "chow.h"
#include "live_range.h"
#include "live_unit.h"
#include "rc.h"
#include "spill.h"
#include "stats.h"
#include "color.h"
#include "mapping.h"
#include "cfg_tools.h"
#include "params.h"
#include "chow_extensions.h"
/*------------------MODULE LOCAL DECLARATIONS------------------*/
namespace {
/* local types */
//this struct is used to keep track of the contents of any temporary
//registers as well as any registers that are evicted in FRAME/JSR
struct AssignedReg
{
Register machineReg;
Inst* forInst;
RegPurpose forPurpose;
LRID forLRID;
Boolean free;
int index;
bool dirty;
int next_use;
bool local;
std::vector<AssignedReg*>* regpool;
};
//handy typedefs to save typing
typedef std::map<LRID,AssignedReg*> AssignedRegMap;
typedef std::map<LRID, AssignedReg*>::iterator RegMapIterator;
typedef std::vector<std::pair<LRID, AssignedReg*> > EvictedList;
typedef std::vector<AssignedReg*> AssignedRegList;
//this struct is used to keep track of which lrids are currently in
//the temporary registers as well as live ranges that may have been
//evicted. these contents are also used for choosing which registers
//can be evicted
struct RegisterContents
{
EvictedList* evicted; //currently evicted registers
AssignedRegList* assignable;//all non-reserved registers
AssignedRegList* reserved; //all reserved registers
RegisterClass::RC rc; //register class for these contents
};
/* local variables */
std::vector<RegisterContents> reg_contents;
std::map<Inst*, int> inst_order;
/* local functions */
std::pair<Register,bool>
GetFreeTmpReg(LRID lrid,
Block* blk,
Inst* origInst,
Inst* updatedInst,
Operation* op,
RegPurpose purpose,
const RegisterList& instUses,
const RegisterList& instDefs);
Register MarkRegisterUsed(AssignedReg* tmpReg,
Inst* inst,
RegPurpose purpose,
LRID lrid,
AssignedRegList* reglist,
unsigned int rwidth);
RegisterContents* RegContentsForLRID(LRID lrid);
bool InsertEvictedStore(LRID evictedLRID,
RegisterContents* regContents,
AssignedReg* evictedReg,
Inst* origInst,
Inst* updatedInst,
Block* blk);
AssignedReg*
FindSutiableTmpReg(RegisterContents* regContents,
Block* blk,
Inst* origInst,
Inst* updatedInst,
RegPurpose purpose,
const RegisterList& instUses,
unsigned int reg_width);
AssignedReg*
Belady(const AssignedRegList& choices, Block* blk, Inst* origInst, uint);
uint ResetForRegWidth(AssignedRegList::const_iterator begin);
uint ResetForRegWidth(AssignedReg* tmpReg);
int
UpdateDistances(
std::map<LRID, int>& distances,
Block* blk,
int startdist,
Inst* start_inst = NULL
);
void AssignRegister(
AssignedReg* tmpReg,
AssignedRegList* reglist,
LRID lrid,
Inst* origInst,
RegPurpose purpose,
unsigned int rwidth,
bool is_dirty
);
void StoreIfNeeded(AssignedReg* tmpReg, Inst* origInst, Block* blk);
AssignedReg* FindInRegPool(LRID lrid, AssignedRegList* regpool);
void ResetRegSpan(AssignedReg* startingReg, uint width);
void StoreAndResetRegSpan( AssignedReg* , Inst* , Block* , uint );
bool LiveIn(LRID orig_lrid, Block* blk);
//for local allocation
bool recompute_dist_map = true;
std::map<Inst*, std::map<LRID, int> >distance_map;
void BuildInstOrderingMap();
void ComputeDistanceMap(Block* start_blk);
void RecordDistance(
Register vreg,
Inst* inst,
std::map<LRID, int>& next_uses);
void AnnotateBlockWithDistances(Block* blk, std::map<LRID, int>& next_uses);
/* inline functions */
inline unsigned int UB(unsigned int size, unsigned int width)
{
return size - width + 1;
}
unsigned int RegWidth(LRID lrid);
inline unsigned int RegWidth(AssignedReg* tmpReg)
{
return RegWidth(tmpReg->forLRID);
}
inline unsigned int RegWidth(LRID lrid)
{
int rwidth = 1;
if(lrid != NO_LRID){rwidth = Chow::live_ranges[lrid]->RegWidth();}
return rwidth;
}
inline void ResetAssignedReg(AssignedReg* ar)
{
ar->free = TRUE;
ar->forLRID = NO_LRID;
ar->forInst = NULL;
ar->dirty = false;
ar->next_use = -1;
ar->local = false;
}
/* used as predicates for seaching reg lists */
template<class Predicate>
const AssignedRegList&
FindCandidateRegs(const AssignedRegList* possibles,
unsigned int reg_width,
Predicate pred);
class AssignedReg_Usable;
/* for debugging */
void dump_assignedlist_contents(const AssignedRegList& arList);
}
/*--------------------BEGIN IMPLEMENTATION---------------------*/
namespace Assign {
/* variables */
const Register REG_UNALLOCATED = 666;
/*
*=========
* Init()
*=========
* Initialize structures need for assignment
**/
void Init(Arena arena)
{
using RegisterClass::all_classes;
//make space for the number of register classes we are using
//reg_contents.resize(all_classes.size());
//reg_contents.reserve(all_classes.size());
RegisterContents regc;
reg_contents.insert(reg_contents.end(), all_classes.size(), regc);
//allocate register_contents structs per register class. these are
//used during register assignment to find temporary registers when
//we need to evict a register if we don't have enough reserved
//registers to satisfy the needs of the instruction
for(unsigned int i = 0; i < all_classes.size(); i++)
{
RegisterClass::RC rc = all_classes[i];
reg_contents[rc].evicted = new EvictedList;
reg_contents[rc].assignable= new AssignedRegList;
reg_contents[rc].reserved= new AssignedRegList;
reg_contents[rc].rc = rc;
//first add the reserved registers
//actual reserved must be calculated in this way since we reserve
//an extra register for the frame pointer, but it is not available
//to be used as a temporary register so we must remember this when
//building the reserved regs list
RegisterClass::ReservedRegsInfo rri = RegisterClass::GetReservedRegInfo(rc);
for(int i = 0; i < rri.cReserved; i++)
{
AssignedReg* rr = (AssignedReg*)
Arena_GetMemClear(arena, sizeof(AssignedReg));
rr->machineReg = rri.regs[i];
rr->free = TRUE;
rr->index = i;
rr->regpool = reg_contents[rc].reserved;
rr->regpool->push_back(rr);
}
//now build the list of all remaining machine registers for this
//register class.
Register base = FirstRegister(rc) + rri.cHidden;
int num_assignable = RegisterClass::NumMachineReg(rc);
for(int i = 0; i < num_assignable; i++)
{
AssignedReg* rr = (AssignedReg*)
Arena_GetMemClear(arena, sizeof(AssignedReg));
rr->machineReg = base+i;
rr->free = TRUE;
rr->index = i;
rr->regpool = reg_contents[rc].assignable;
rr->regpool->push_back(rr);
}
}
//get an ordering of instructions for local allocation decisions
BuildInstOrderingMap();
}
/*
*===================
* InitLocalAllocation()
*===================
* initializes the data structures needed to do local allocation. this
* is only done for blocks that are the head of a SingleSuccessorPath,
* otherwise the block is part of such a path and the information does
* not need to be recomputed.
**/
void InitLocalAllocation(Block* blk)
{
if(recompute_dist_map)
{
ComputeDistanceMap(blk);
recompute_dist_map = false;
}
}
/*
*===================
* EnsureReg()
*===================
* Makes sure that the given live range is in a machin register.
* If the live range is not allocated to a register then it will be
* given an available temporary register.
*
* reg - this will be updated with the machine reg for the live range
**/
void EnsureReg(Register* reg,
Block* blk,
Inst* origInst,
Inst** updatedInst,
Operation* op,
RegPurpose purpose,
const RegisterList& instUses,
const RegisterList& instDefs)
{
using Spill::InsertLoad;
using Spill::InsertStore;
using Chow::live_ranges;
debug("ensuring r%d for inst %d (0x%p): \n %s",
*reg, inst_order[origInst], origInst, Debug::StringOfInst(origInst));
LRID orig_lrid = Mapping::SSAName2OrigLRID(*reg);
*reg = GetMachineRegAssignment(blk, orig_lrid);
//this live range is spilled. find a temporary register
if(*reg == REG_UNALLOCATED)
{
std::pair<Register,bool> regXneedMem;
Register tmpReg;
bool needMemAccess;
//find a temporary register for this value
regXneedMem = GetFreeTmpReg(orig_lrid, blk, origInst, *updatedInst,
op, purpose, instUses, instDefs);
//get return values
tmpReg = regXneedMem.first;
needMemAccess = regXneedMem.second;
debug("got a free temporary register: %d, needsMemAccess: %c",
tmpReg, needMemAccess ? 't' : 'f');
//generate a load or store if the value was not already in the
//register
if(needMemAccess)
{
/* must grab the actual live range here to make sure that loads
* and stores respect the rematerialization settings */
LiveRange* lr = (*live_ranges[orig_lrid]->blockmap)[bid(blk)];
if(purpose == FOR_USE)
{
InsertLoad(lr, *updatedInst, tmpReg, Spill::REG_FP);
}
else //FOR_DEF
{
*updatedInst =
InsertStore(lr,*updatedInst, tmpReg, Spill::REG_FP, AFTER_INST);
}
}
*reg = tmpReg;
}
else
{
debug("reg: %d for orig_lrid: %d is allocated", *reg, orig_lrid);
}
}
/*
*===================
* HandleCopy()
*===================
* Assigns registers for a copy instruction.
*
* Copies are handled separately because we want to catch the case
* where the source of the copy is not allocated a register and would
* require a load.
**/
void HandleCopy(Block* blk,
Inst* origInst,
Inst** updatedInst,
Operation** op,
const RegisterList& instUses,
const RegisterList& instDefs)
{
debug("handling copy for inst %d (0x%p): \n %s",
inst_order[origInst], origInst, Debug::StringOfInst(origInst));
bool delete_copy = false;
Register* srcp = &((*op)->arguments[0]);
Register* destp = &((*op)->arguments[1]);
LRID src_lrid = Mapping::SSAName2OrigLRID(*srcp);
*srcp = GetMachineRegAssignment(blk, src_lrid);
if(*srcp == REG_UNALLOCATED)
{
//check to see if its already in a tmp reg
if(FindInRegPool(src_lrid, RegContentsForLRID(src_lrid)->reserved))
{
//and call GetFreeTmpReg so that the data structures get updated
//as necessary for belady
std::pair<Register,bool> regXneedMem =
GetFreeTmpReg(src_lrid, blk, origInst, *updatedInst,
*op, FOR_USE, instUses, instDefs);
assert(regXneedMem.second == false);
*srcp = regXneedMem.first;
}
else
{
delete_copy = true;
}
}
//we always need a register for the dest of the copy
EnsureReg(destp, blk, origInst, updatedInst,
*op, FOR_DEF, instUses, instDefs);
if(delete_copy)
{
//insert load from src to dest reg
LiveRange* lr = (*Chow::live_ranges[src_lrid]->blockmap)[bid(blk)];
Spill::InsertLoad(lr, *updatedInst, *destp, Spill::REG_FP);
//delete copy
*op = NULL;
}
}
//TODO: properly commend these function when you know they should be kept
inline LiveRange* RealLR(LRID orig_lrid, Block* blk);
void InsertCopy(AssignedReg* tmpReg, Edge* succ_edge);
void ResetAllocatedTmpRegs(AssignedRegList* reserved, Block* blk);
void ResetAllTmpRegs(AssignedRegList* reserved, Block* blk);
/*
*===================
* ResetFreeTmpRegs()
*===================
* used to reset the count of which temporary registers are in use for
* an instruction
**/
void ResetFreeTmpRegs(Block* blk)
{
//need to reset all tmps if there are multiple paths from this block
//or multiple paths to the successor
bool reset_all = !SingleSuccessorPath(blk);
if(reset_all)
{
//make sure that we update our distance map for the next block
recompute_dist_map = true;
//take care of business for each register class
for(unsigned int i = 0; i < reg_contents.size(); i++)
{
//set all reserved registers to be FREE so they can be used in
//the next block
ResetAllTmpRegs(reg_contents[i].reserved, blk);
}
}
else
{
//take care of business for each register class
debug("resetting tmp regs allocated in succesor to be free");
for(unsigned int i = 0; i < reg_contents.size(); i++)
{
//reset registers for those containing live ranges allocated in
//the successor block
ResetAllocatedTmpRegs(reg_contents[i].reserved, blk);
}
}
}
/* reset all temp regs. if a global lr is in a tmp reg and live out on
* an edge then insert a store on that edge */
void ResetAllTmpRegs(AssignedRegList* reserved, Block* blk)
{
debug("resetting all tmp regs to be free");
//if we are not moving loads and stores then just store in the block
if(!Params::Algorithm::enhanced_register_promotion)
{
StoreAndResetRegSpan(
reserved->front(), Block_LastInst(blk), blk, reserved->size()
);
}
else
{
//first collect all globals as they are the only ones who might
//need a store
AssignedRegList globals;
AssignedRegList::const_iterator resIT;
for(resIT = reserved->begin(); resIT != reserved->end();)
{
AssignedReg* tmpReg = *resIT;
if(!tmpReg->local) globals.push_back(tmpReg);
//skip over other regs used by this live range
resIT = resIT+RegWidth(*resIT);
}
//now either store the global or generate a copy if it is
//allocated in a successor block.
for(resIT = globals.begin(); resIT != globals.end(); resIT++)
{
Edge* e;
AssignedReg* tmpReg = *resIT;
debug("resetting global tmp reg: r%d for lrid: %d",
tmpReg->machineReg, tmpReg->forLRID);
Block_ForAllSuccs(e,blk)
{
//if it is allocated in the successor then insert a copy
if(IsAllocated(tmpReg->forLRID, e->succ) &&
LiveIn(tmpReg->forLRID, e->succ))
{
debug("lrid allocated in and live in at successor: %s, from: %s",
bname(e->succ), bname(e->pred));
InsertCopy(tmpReg, e);
}
//if it is dirty and live in insert a store
else if(tmpReg->dirty && LiveIn(tmpReg->forLRID, e->succ))
{
debug("tmpreg dirty and live in at successor: %s",
bname(e->succ));
MovedSpillDescription msd = {0};
//msd.lr = RealLR(tmpReg->forLRID, blk);
msd.lr = Chow::live_ranges[tmpReg->forLRID];
msd.spill_type = STORE_SPILL;
msd.orig_blk = blk;
msd.mreg = tmpReg->machineReg;
(void)Chow::Extensions::AddEdgeExtensionNode(e, msd);
}
}
}
//reset all tmp regs since we are at end of local allocation
ResetRegSpan(reserved->front(), reserved->size());
}
}
/* only reset regs which are allocated in a successor block */
void ResetAllocatedTmpRegs(AssignedRegList* reserved, Block* blk)
{
using Chow::Extensions::AddEdgeExtensionNode;
AssignedRegList::const_iterator resIT;
for(resIT = reserved->begin(); resIT != reserved->end();)
{
//reset the reg if this tmp reg holds a live range that has a
//real register in the next block
if(IsAllocated((*resIT)->forLRID, blk->succ->succ))
{
debug("resetting tmp reg: r%d for lrid: %d",
(*resIT)->machineReg, (*resIT)->forLRID);
//insert copy must be done on the edge
if(!Params::Algorithm::enhanced_register_promotion)
{
StoreAndResetRegSpan(
*resIT, Block_LastInst(blk), blk, RegWidth(*resIT)
);
}
else
{
AssignedReg* tmpReg = *resIT;
//only need to insert a copy if the live range is live in.
//this is ok here because we are only dealing with single
//successor blocks
if(LiveIn(tmpReg->forLRID, blk->succ->succ))
InsertCopy(tmpReg, blk->succ);
ResetForRegWidth(tmpReg);
}
}
//skip over other regs used by this live range
resIT = resIT+RegWidth(*resIT);
}
}
/* insert a copy onto the edge. if the live range is dirty then insert
* a copy-def so that we treat the copy as a def in case it should
* reach anyone outside the live range */
void InsertCopy(AssignedReg* tmpReg, Edge* succ_edge)
{
using Chow::Extensions::AddEdgeExtensionNode;
debug("lr: %d is allocated reg: %d in blk: %s",
tmpReg->forLRID,
GetMachineRegAssignment(succ_edge->succ, tmpReg->forLRID),
bname(succ_edge->succ)
);
Block* pred_blk = succ_edge->pred;
Block* succ_blk = succ_edge->succ;
Register dest_reg = GetMachineRegAssignment(succ_blk, tmpReg->forLRID);
assert(dest_reg != REG_UNALLOCATED);
//if the register has been written to then we need to insert a copy
//def so that the def will be stored if needed
MovedSpillDescription msd = {0};
//msd.lr = RealLR(tmpReg->forLRID, pred_blk);
//msd.lr_dest = RealLR(tmpReg->forLRID, succ_blk);
msd.lr = Chow::live_ranges[tmpReg->forLRID];
msd.lr_dest = RealLR(tmpReg->forLRID, succ_blk); //dest needs real lr
msd.cp_src = tmpReg->machineReg;
msd.cp_dest = dest_reg;
msd.orig_blk = pred_blk;
if(tmpReg->dirty) {
msd.spill_type = COPYDEF_SPILL;
}
else {
msd.spill_type = COPY_SPILL;
}
//add the copy to the edge
Edge_Extension* ee = AddEdgeExtensionNode(succ_edge, msd);
//remove load for this register from the edge list
typedef std::list<MovedSpillDescription> L;
L* spill_list = ee->spill_list;
bool found_load = false;
for(L::iterator it = spill_list->begin(); it != spill_list->end(); it++)
{
debug("match: %d_%d to load_for: %d_%d", (*it).lr->id,
(*it).lr->orig_lrid, msd.lr->id, msd.lr->orig_lrid);
debug("st: %d, st: %d", (*it).spill_type, LOAD_SPILL);
if((*it).lr->orig_lrid == msd.lr->orig_lrid &&
(*it).spill_type == LOAD_SPILL)
{
found_load = true;
spill_list->erase(it); //delete the load
break;
}
}
//may not find the load on SSB with no enhanced motion
assert(found_load);
}
/* get a handle to the the real live range for a block given the
* orignal lrid. (it may have chaged due to splitting) */
inline LiveRange* RealLR(LRID orig_lrid, Block* blk)
{
typedef std::map<unsigned int, LiveRange*> M;
M* blockmap = Chow::live_ranges[orig_lrid]->blockmap;
M::iterator it = blockmap->find(bid(blk));
assert(it != blockmap->end());
assert((*it).second != NULL);
return (*it).second;
}
/*
*==========================
* IsAllocated()
*==========================
* returns true if the live range is allocated a register in the
* block. the live range does not have to have originally contained
* the block.
**/
inline bool InMap(const std::map<uint, LiveRange*>* m, uint key)
{return m->find(key) != m->end();}
inline bool IsAllocated(LRID lrid, Block* blk)
{
if(lrid == NO_LRID || lrid == Spill::frame.lrid) return false;
//have to check to see if the block was ever part of the live range
//since checking machine reg assignement requires that the block
//given was once part of the live range. so bail out here if not
if(!InMap(Chow::live_ranges[lrid]->blockmap, bid(blk))) return false;
return (GetMachineRegAssignment(blk, lrid) != REG_UNALLOCATED);
}
/*
*==========================
* GetMachineRegAssignment()
*==========================
* Gets the machine register assignment for a lrid in a given block
* the block must have originally been part of the live range.
**/
Register GetMachineRegAssignment(Block* b, LRID lrid)
{
using Coloring::GetColor;
using Coloring::NO_COLOR;
if(lrid == Spill::frame.lrid)
return Spill::REG_FP;
/* return REG_UNALLOCATED; to spill everything */
Register color = GetColor(b, lrid);
if(color == NO_COLOR)
return REG_UNALLOCATED;
RegisterClass::RC rc = Chow::live_ranges[lrid]->rc;
return RegisterClass::MachineRegForColor(rc, color);
}
/*
*===================
* UnEvict()
*===================
* restores any allocted live ranges to their registers if they were
* evicted to make room for an unallocted live range involved in a
* function call.
**/
void UnEvict(Inst** updatedInst)
{
debug("checking for registers needing unevicting");
//look at each register classes evicted list
for(unsigned int i = 0; i < reg_contents.size(); i++)
{
EvictedList* evicted = reg_contents[i].evicted;
if(evicted->size() > 0)
{
debug("some registers need unevicting");
//1) anyone that was evicted needs to be loaded back in
//TODO: this is bullshit!! (says tim).
//we can do better than just reloading the register because it is
//the end of the block. we could look down the path that leads
//from this block and see where the next use is and put the load
//right before it, but for now we just put the load here
EvictedList::iterator evIT;
for(evIT = evicted->begin(); evIT != evicted->end(); evIT++)
{
LRID evictedLRID = (*evIT).first;
AssignedReg* kicked = (*evIT).second;
//load the live range back into its register if we kicked one
//out when commendeering the machine register
if(evictedLRID != NO_LRID)
{
debug("unevicting lrid: %d to reg: %d", evictedLRID,
kicked->machineReg);
*updatedInst =
Spill::InsertLoad(Chow::live_ranges[evictedLRID],
*updatedInst, kicked->machineReg,
Spill::REG_FP, AFTER_INST);
}
}
//reset values on assigned regs. this is needed in case a
//register gets chosen for eviction again we don't want the old
//values hanging around in the AssignedReg*
for(AssignedRegList::iterator it=reg_contents[i].assignable->begin();
it != reg_contents[i].assignable->end();
it++)
{
ResetAssignedReg(*it);
}
//2) remove all evicted registers from evicted list
evicted->clear();
}
}
}
}//end Assign namespace
/*-------------------BEGIN LOCAL DEFINITIONS-------------------*/
namespace {
/* debug routines */
void dump_reglist_contents(const RegisterList& regList, Block* blk)
{
debug("-- reglist contents --");
RegisterList::const_iterator rmIt;
for(rmIt = regList.begin(); rmIt != regList.end(); rmIt++)
debug(" %d --> r%d", (*rmIt),
Assign::GetMachineRegAssignment(blk, *rmIt));
debug("-- end reglist contents --");
}
void dump_assignedlist_contents(const AssignedRegList& arList)
{
debug("-- assigned reglist contents --");
AssignedRegList::const_iterator rmIt;
for(rmIt = arList.begin(); rmIt != arList.end(); rmIt++)
debug(" r%d (%d lrid %s for inst %p(%d) nxU: %d)",
(*rmIt)->machineReg, (*rmIt)->forLRID,
((*rmIt)->dirty ? "dirty" : "clean"),
(*rmIt)->forInst, inst_order[(*rmIt)->forInst],(*rmIt)->next_use);
debug("-- end assigned reglist contents --");
}
/*
*=======================
* AssignedReg_Usable()
*=======================
* says whether we can use the temporary register or not
**/
class AssignedReg_Usable : public std::unary_function<AssignedReg*, bool>
{
const Inst* inst;
RegPurpose purpose;
const RegisterList& instUses;
public:
AssignedReg_Usable(const Inst* inst_, RegPurpose purpose_,
const RegisterList& instUses_) :
inst(inst_), purpose(purpose_), instUses(instUses_) {}
/* we can use a previously used reserved reg if it is for a
* different instruction or it was used in the instruction to hold
* a USE and we now need it for a DEF. we also check the list of
* uses in the inst to make sure we don't use this temp register if
* the lrid it stores is needed for this instruction (even if it was
* put in for the previous instruction for example) */
bool operator() (const AssignedReg* reserved) const
{
if(reserved->forInst != inst)
{
//make sure this reg is not used in the instruction under
//question since it has to occupy a register in that case
//and is not available to be evicted
if(purpose == FOR_USE)
{
if(find(instUses.begin(), instUses.end(), reserved->forLRID)
!= instUses.end())
{
return false;
}
}
return true;
}
else //needed in the same inst
{
return reserved->forPurpose != purpose;
}
}
};
/*
*=======================
* AssignedReg_IsFree()
*=======================
* predicate for whether a temporary register is free or not
**/
class AssignedReg_IsFree : public std::unary_function<AssignedReg*, bool>
{
public:
bool operator() (const AssignedReg* rReg) const
{
return rReg->free;
}
};
/*
*=======================
* AssignedReg_LridEq()
*=======================
* predicate for whether a temporary register has a matching LRID
**/
class AssignedReg_LridEq : public std::unary_function<AssignedReg*, bool>
{
LRID lrid;
public:
AssignedReg_LridEq(LRID _lrid) : lrid(_lrid){};
bool operator() (const AssignedReg* rReg) const
{
return rReg->forLRID == lrid;
}
};
/*
*=======================
* AssignedReg_Evictable()
*=======================
* predicate for whether a register can be evicted if needed in a
* JSR/FRAME instruction
**/
class AssignedReg_Evictable :public std::unary_function<AssignedReg*, bool>
{
Inst* inst;
const RegisterList& forbidden_regs;
Block* blk;
public:
AssignedReg_Evictable(Inst* inst_, const RegisterList& rList, Block* b):
inst(inst_), forbidden_regs(rList), blk(b) {};
bool operator() (const AssignedReg* aReg) const
{
//can not use if already assigned for the same inst
if(aReg->forInst == inst) return false;
//can not use if will be assigned in the future
for(RegisterList::const_iterator it = forbidden_regs.begin();
it != forbidden_regs.end();
it++)
{
Register mReg = Assign::GetMachineRegAssignment(blk, *it);
if(mReg != Assign::REG_UNALLOCATED && mReg == aReg->machineReg)
return false;
}
//othwise...
return true;
}
};
/*
*===================
* GetFreeTmpReg()
*===================
* gets the next available free tmp register from the pool of
* available free temp registers
**/
std::pair<Register,bool>
GetFreeTmpReg(LRID lrid,
Block* blk,
Inst* origInst,
Inst* updatedInst,
Operation* op,
RegPurpose purpose,
const RegisterList& instUses,
const RegisterList& instDefs)
{
using Assign::GetMachineRegAssignment;
debug("looking for temporary reg for %s of lrid: %d",
(purpose == FOR_USE ? "FOR_USE" : "FOR_DEF"), lrid);
std::pair<Register,bool> regXneedMem;
//a memory access is only needed if we are bringing a value from
//memory into a register, or we are evicting a register that is used
//for a def
regXneedMem.second = false;
//get the register contents struct for this lrid register class
RegisterContents* regContents = RegContentsForLRID(lrid);
unsigned int rwidth = RegWidth(lrid);
dump_assignedlist_contents(*regContents->reserved);
//1) check to see if this lrid is already stored in one of our
//temporary registers.
{
AssignedReg* tmpReg = FindInRegPool(lrid, regContents->reserved);
if(tmpReg != NULL)
{
debug("found the live range already in a temporary register");
AssignRegister(tmpReg, tmpReg->regpool, lrid, origInst, purpose,
rwidth, tmpReg->dirty || (purpose == FOR_DEF));
regXneedMem.first = tmpReg->machineReg;
return regXneedMem;
}
}
//find a sutiable temporary register to use, either
//2) a reserved register that is still available, or
//3) kick someone out of an occupied reserved register not needed
//for this instruction
{//private scope for tmpReg
AssignedReg* tmpReg =
FindSutiableTmpReg(regContents, blk, origInst, updatedInst,
purpose, instUses, rwidth);
if(tmpReg != NULL)
{
regXneedMem.first =
MarkRegisterUsed(tmpReg, origInst, purpose, lrid,
regContents->reserved, rwidth);
if(purpose == FOR_USE) regXneedMem.second = true;
return regXneedMem;
}
}
//4) at this point we need to evict a register that we determined
//should really be allocated. This is because we have an operation
//with too many uses. make sure this only happens for FRAME and JSR
//operations
debug("no more reserved regs available must evict an allocted reg");
assert(op->opcode == FRAME ||
op->opcode == JSRr ||
op->opcode == iJSRr ||
op->opcode == fJSRr ||
op->opcode == dJSRr ||
op->opcode == cJSRr ||
op->opcode == qJSRr ||
op->opcode == JSRl ||
op->opcode == iJSRl ||
op->opcode == fJSRl ||
op->opcode == dJSRl ||
op->opcode == cJSRl ||
op->opcode == qJSRl);
//DROP >>>>
// static Register bullshitReg = 1000;
// regXneedMem.first = bullshitReg++;
// return regXneedMem;
//DROP <<<<
//first we have to seach through the assignable registers since it
//may be the case that we have already evicted a register for the
//lrid and it appears twice in the JSR call
{
AssignedReg* tmpReg = FindInRegPool(lrid, regContents->assignable);
if(tmpReg != NULL)
{
regXneedMem.first = tmpReg->machineReg;
return regXneedMem;
}
}
//evict a live range from a register and record the fact that the
//register has been commandeered
//to find a suitable register to evict we start with the list of all
//machine registers. we go through the uses (or defs) in the op and
//remove any machine reg that is in use in the current operation
//from the list of potential register we can commandeer
const AssignedRegList& potentials =
purpose == FOR_USE ?
FindCandidateRegs(regContents->assignable,
rwidth,
AssignedReg_Evictable(origInst,instUses,blk))
:
FindCandidateRegs(regContents->assignable,
rwidth,
AssignedReg_Evictable(origInst,instDefs,blk));
//there should be at least one register left to choose from. evict
//it and use it now.
//dump_reglist_contents(instUses, blk);
//dump_assignedlist_contents(*regContents->assignable);
assert(potentials.size() > 0);
AssignedReg* tmpReg = potentials.front();
debug("evicting machine register: %d", tmpReg->machineReg);
//find the lrid assigned to this machine register so that we know
//which live range is about to be evicted
RegisterClass::RC rc = Chow::live_ranges[lrid]->rc;