@@ -106,13 +106,8 @@ namespace {
106
106
// / that it is alive across blocks.
107
107
BitVector MayLiveAcrossBlocks;
108
108
109
- // / State of a physical register.
110
- enum RegState {
111
- // / A disabled register is not available for allocation, but an alias may
112
- // / be in use. A register can only be moved out of the disabled state if
113
- // / all aliases are disabled.
114
- regDisabled,
115
-
109
+ // / State of a register unit.
110
+ enum RegUnitState {
116
111
// / A free register is not currently in use and can be allocated
117
112
// / immediately without checking aliases.
118
113
regFree,
@@ -126,8 +121,8 @@ namespace {
126
121
// / register. In that case, LiveVirtRegs contains the inverse mapping.
127
122
};
128
123
129
- // / Maps each physical register to a RegState enum or a virtual register.
130
- std::vector<unsigned > PhysRegState ;
124
+ // / Maps each physical register to a RegUnitState enum or virtual register.
125
+ std::vector<unsigned > RegUnitStates ;
131
126
132
127
SmallVector<Register, 16 > VirtDead;
133
128
SmallVector<MachineInstr *, 32 > Coalesced;
@@ -138,6 +133,7 @@ namespace {
138
133
RegUnitSet UsedInInstr;
139
134
140
135
void setPhysRegState (MCPhysReg PhysReg, unsigned NewState);
136
+ bool isPhysRegFree (MCPhysReg PhysReg) const ;
141
137
142
138
// / Mark a physreg as used in this instruction.
143
139
void markRegUsedInInstr (MCPhysReg PhysReg) {
@@ -189,14 +185,15 @@ namespace {
189
185
bool isLastUseOfLocalReg (const MachineOperand &MO) const ;
190
186
191
187
void addKillFlag (const LiveReg &LRI);
188
+ bool verifyRegStateMapping (const LiveReg &LR) const ;
192
189
void killVirtReg (LiveReg &LR);
193
190
void killVirtReg (Register VirtReg);
194
191
void spillVirtReg (MachineBasicBlock::iterator MI, LiveReg &LR);
195
192
void spillVirtReg (MachineBasicBlock::iterator MI, Register VirtReg);
196
193
197
194
void usePhysReg (MachineOperand &MO);
198
195
void definePhysReg (MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
199
- RegState NewState);
196
+ unsigned NewState);
200
197
unsigned calcSpillCost (MCPhysReg PhysReg) const ;
201
198
void assignVirtToPhysReg (LiveReg &, MCPhysReg PhysReg);
202
199
@@ -229,7 +226,8 @@ namespace {
229
226
bool mayLiveOut (Register VirtReg);
230
227
bool mayLiveIn (Register VirtReg);
231
228
232
- void dumpState ();
229
+ void printRegUnitState (unsigned State) const ;
230
+ void dumpState () const ;
233
231
};
234
232
235
233
} // end anonymous namespace
@@ -240,7 +238,16 @@ INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
240
238
false )
241
239
242
240
void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
243
- PhysRegState[PhysReg] = NewState;
241
+ for (MCRegUnitIterator UI (PhysReg, TRI); UI.isValid (); ++UI)
242
+ RegUnitStates[*UI] = NewState;
243
+ }
244
+
245
+ bool RegAllocFast::isPhysRegFree (MCPhysReg PhysReg) const {
246
+ for (MCRegUnitIterator UI (PhysReg, TRI); UI.isValid (); ++UI) {
247
+ if (RegUnitStates[*UI] != regFree)
248
+ return false ;
249
+ }
250
+ return true ;
244
251
}
245
252
246
253
// / This allocates space for the specified virtual register to be held on the
@@ -384,12 +391,21 @@ void RegAllocFast::addKillFlag(const LiveReg &LR) {
384
391
}
385
392
}
386
393
394
+ bool RegAllocFast::verifyRegStateMapping (const LiveReg &LR) const {
395
+ for (MCRegUnitIterator UI (LR.PhysReg , TRI); UI.isValid (); ++UI) {
396
+ if (RegUnitStates[*UI] != LR.VirtReg )
397
+ return false ;
398
+ }
399
+
400
+ return true ;
401
+ }
402
+
387
403
// / Mark virtreg as no longer available.
388
404
void RegAllocFast::killVirtReg (LiveReg &LR) {
405
+ assert (verifyRegStateMapping (LR) && " Broken RegState mapping" );
389
406
addKillFlag (LR);
390
- assert (PhysRegState[LR.PhysReg ] == LR.VirtReg &&
391
- " Broken RegState mapping" );
392
- setPhysRegState (LR.PhysReg , regFree);
407
+ MCPhysReg PhysReg = LR.PhysReg ;
408
+ setPhysRegState (PhysReg, regFree);
393
409
LR.PhysReg = 0 ;
394
410
}
395
411
@@ -416,15 +432,17 @@ void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
416
432
417
433
// / Do the actual work of spilling.
418
434
void RegAllocFast::spillVirtReg (MachineBasicBlock::iterator MI, LiveReg &LR) {
419
- assert (PhysRegState[LR.PhysReg ] == LR.VirtReg && " Broken RegState mapping" );
435
+ assert (verifyRegStateMapping (LR) && " Broken RegState mapping" );
436
+
437
+ MCPhysReg PhysReg = LR.PhysReg ;
420
438
421
439
if (LR.Dirty ) {
422
440
// If this physreg is used by the instruction, we want to kill it on the
423
441
// instruction, not on the spill.
424
442
bool SpillKill = MachineBasicBlock::iterator (LR.LastUse ) != MI;
425
443
LR.Dirty = false ;
426
444
427
- spill (MI, LR.VirtReg , LR. PhysReg , SpillKill);
445
+ spill (MI, LR.VirtReg , PhysReg, SpillKill);
428
446
429
447
if (SpillKill)
430
448
LR.LastUse = nullptr ; // Don't kill register again
@@ -460,53 +478,16 @@ void RegAllocFast::usePhysReg(MachineOperand &MO) {
460
478
assert (PhysReg.isPhysical () && " Bad usePhysReg operand" );
461
479
462
480
markRegUsedInInstr (PhysReg);
463
- switch (PhysRegState[PhysReg]) {
464
- case regDisabled:
465
- break ;
466
- case regReserved:
467
- PhysRegState[PhysReg] = regFree;
468
- LLVM_FALLTHROUGH;
469
- case regFree:
470
- MO.setIsKill ();
471
- return ;
472
- default :
473
- // The physreg was allocated to a virtual register. That means the value we
474
- // wanted has been clobbered.
475
- llvm_unreachable (" Instruction uses an allocated register" );
476
- }
477
481
478
- // Maybe a superregister is reserved?
479
- for (MCRegAliasIterator AI (PhysReg, TRI, false ); AI.isValid (); ++AI) {
480
- MCPhysReg Alias = *AI;
481
- switch (PhysRegState[Alias]) {
482
- case regDisabled:
483
- break ;
482
+ for (MCRegUnitIterator UI (PhysReg, TRI); UI.isValid (); ++UI) {
483
+ switch (RegUnitStates[*UI]) {
484
484
case regReserved:
485
- // Either PhysReg is a subregister of Alias and we mark the
486
- // whole register as free, or PhysReg is the superregister of
487
- // Alias and we mark all the aliases as disabled before freeing
488
- // PhysReg.
489
- // In the latter case, since PhysReg was disabled, this means that
490
- // its value is defined only by physical sub-registers. This check
491
- // is performed by the assert of the default case in this loop.
492
- // Note: The value of the superregister may only be partial
493
- // defined, that is why regDisabled is a valid state for aliases.
494
- assert ((TRI->isSuperRegister (PhysReg, Alias) ||
495
- TRI->isSuperRegister (Alias, PhysReg)) &&
496
- " Instruction is not using a subregister of a reserved register" );
485
+ RegUnitStates[*UI] = regFree;
497
486
LLVM_FALLTHROUGH;
498
487
case regFree:
499
- if (TRI->isSuperRegister (PhysReg, Alias)) {
500
- // Leave the superregister in the working set.
501
- setPhysRegState (Alias, regFree);
502
- MO.getParent ()->addRegisterKilled (Alias, TRI, true );
503
- return ;
504
- }
505
- // Some other alias was in the working set - clear it.
506
- setPhysRegState (Alias, regDisabled);
507
488
break ;
508
489
default :
509
- llvm_unreachable (" Instruction uses an alias of an allocated register " );
490
+ llvm_unreachable (" Unexpected reg unit state " );
510
491
}
511
492
}
512
493
@@ -519,38 +500,20 @@ void RegAllocFast::usePhysReg(MachineOperand &MO) {
519
500
// / similar to defineVirtReg except the physreg is reserved instead of
520
501
// / allocated.
521
502
void RegAllocFast::definePhysReg (MachineBasicBlock::iterator MI,
522
- MCPhysReg PhysReg, RegState NewState) {
523
- markRegUsedInInstr (PhysReg);
524
- switch (Register VirtReg = PhysRegState[PhysReg]) {
525
- case regDisabled:
526
- break ;
527
- default :
528
- spillVirtReg (MI, VirtReg);
529
- LLVM_FALLTHROUGH;
530
- case regFree:
531
- case regReserved:
532
- setPhysRegState (PhysReg, NewState);
533
- return ;
534
- }
535
-
536
- // This is a disabled register, disable all aliases.
537
- setPhysRegState (PhysReg, NewState);
538
- for (MCRegAliasIterator AI (PhysReg, TRI, false ); AI.isValid (); ++AI) {
539
- MCPhysReg Alias = *AI;
540
- switch (Register VirtReg = PhysRegState[Alias]) {
541
- case regDisabled:
542
- break ;
503
+ MCPhysReg PhysReg, unsigned NewState) {
504
+ for (MCRegUnitIterator UI (PhysReg, TRI); UI.isValid (); ++UI) {
505
+ switch (unsigned VirtReg = RegUnitStates[*UI]) {
543
506
default :
544
507
spillVirtReg (MI, VirtReg);
545
- LLVM_FALLTHROUGH ;
508
+ break ;
546
509
case regFree:
547
510
case regReserved:
548
- setPhysRegState (Alias, regDisabled);
549
- if (TRI->isSuperRegister (PhysReg, Alias))
550
- return ;
551
511
break ;
552
512
}
553
513
}
514
+
515
+ markRegUsedInInstr (PhysReg);
516
+ setPhysRegState (PhysReg, NewState);
554
517
}
555
518
556
519
// / Return the cost of spilling clearing out PhysReg and aliases so it is free
@@ -563,46 +526,24 @@ unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
563
526
<< " is already used in instr.\n " );
564
527
return spillImpossible;
565
528
}
566
- switch (Register VirtReg = PhysRegState[PhysReg]) {
567
- case regDisabled:
568
- break ;
569
- case regFree:
570
- return 0 ;
571
- case regReserved:
572
- LLVM_DEBUG (dbgs () << printReg (VirtReg, TRI) << " corresponding "
573
- << printReg (PhysReg, TRI) << " is reserved already.\n " );
574
- return spillImpossible;
575
- default : {
576
- LiveRegMap::const_iterator LRI = findLiveVirtReg (VirtReg);
577
- assert (LRI != LiveVirtRegs.end () && LRI->PhysReg &&
578
- " Missing VirtReg entry" );
579
- return LRI->Dirty ? spillDirty : spillClean;
580
- }
581
- }
582
529
583
- // This is a disabled register, add up cost of aliases.
584
- LLVM_DEBUG (dbgs () << printReg (PhysReg, TRI) << " is disabled.\n " );
585
- unsigned Cost = 0 ;
586
- for (MCRegAliasIterator AI (PhysReg, TRI, false ); AI.isValid (); ++AI) {
587
- MCPhysReg Alias = *AI;
588
- switch (Register VirtReg = PhysRegState[Alias]) {
589
- case regDisabled:
590
- break ;
530
+ for (MCRegUnitIterator UI (PhysReg, TRI); UI.isValid (); ++UI) {
531
+ switch (unsigned VirtReg = RegUnitStates[*UI]) {
591
532
case regFree:
592
- ++Cost;
593
533
break ;
594
534
case regReserved:
535
+ LLVM_DEBUG (dbgs () << printReg (VirtReg, TRI) << " corresponding "
536
+ << printReg (PhysReg, TRI) << " is reserved already.\n " );
595
537
return spillImpossible;
596
538
default : {
597
539
LiveRegMap::const_iterator LRI = findLiveVirtReg (VirtReg);
598
540
assert (LRI != LiveVirtRegs.end () && LRI->PhysReg &&
599
541
" Missing VirtReg entry" );
600
- Cost += LRI->Dirty ? spillDirty : spillClean;
601
- break ;
542
+ return LRI->Dirty ? spillDirty : spillClean;
602
543
}
603
544
}
604
545
}
605
- return Cost ;
546
+ return 0 ;
606
547
}
607
548
608
549
// / This method updates local state so that we know that PhysReg is the
@@ -909,9 +850,17 @@ void RegAllocFast::handleThroughOperands(MachineInstr &MI,
909
850
if (!Reg || !Reg.isPhysical ())
910
851
continue ;
911
852
markRegUsedInInstr (Reg);
912
- for (MCRegAliasIterator AI (Reg, TRI, true ); AI.isValid (); ++AI) {
913
- if (ThroughRegs.count (PhysRegState[*AI]))
914
- definePhysReg (MI, *AI, regFree);
853
+
854
+ for (MCRegUnitIterator UI (Reg, TRI); UI.isValid (); ++UI) {
855
+ if (!ThroughRegs.count (RegUnitStates[*UI]))
856
+ continue ;
857
+
858
+ // Need to spill any aliasing registers.
859
+ for (MCRegUnitRootIterator RI (*UI, TRI); RI.isValid (); ++RI) {
860
+ for (MCSuperRegIterator SI (*RI, TRI, true ); SI.isValid (); ++SI) {
861
+ definePhysReg (MI, *SI, regFree);
862
+ }
863
+ }
915
864
}
916
865
}
917
866
@@ -975,37 +924,40 @@ void RegAllocFast::handleThroughOperands(MachineInstr &MI,
975
924
}
976
925
977
926
#ifndef NDEBUG
978
- void RegAllocFast::dumpState () {
979
- for ( unsigned Reg = 1 , E = TRI-> getNumRegs (); Reg != E; ++Reg) {
980
- if (PhysRegState[Reg] == regDisabled) continue ;
981
- dbgs () << " " << printReg (Reg, TRI);
982
- switch (PhysRegState[Reg ]) {
927
+
928
+ void RegAllocFast::dumpState () const {
929
+ for ( unsigned Unit = 1 , UnitE = TRI-> getNumRegUnits (); Unit != UnitE ;
930
+ ++Unit) {
931
+ switch ( unsigned VirtReg = RegUnitStates[Unit ]) {
983
932
case regFree:
984
933
break ;
985
934
case regReserved:
986
- dbgs () << " * " ;
935
+ dbgs () << " " << printRegUnit (Unit, TRI) << " [P] " ;
987
936
break ;
988
937
default : {
989
- dbgs () << ' =' << printReg (PhysRegState[Reg]);
990
- LiveRegMap::iterator LRI = findLiveVirtReg (PhysRegState[Reg]);
991
- assert (LRI != LiveVirtRegs.end () && LRI->PhysReg &&
992
- " Missing VirtReg entry" );
993
- if (LRI->Dirty )
994
- dbgs () << " *" ;
995
- assert (LRI->PhysReg == Reg && " Bad inverse map" );
938
+ dbgs () << ' ' << printRegUnit (Unit, TRI) << ' =' << printReg (VirtReg);
939
+ LiveRegMap::const_iterator I = findLiveVirtReg (VirtReg);
940
+ assert (I != LiveVirtRegs.end () && " have LiveVirtRegs entry" );
941
+ if (I->Dirty )
942
+ dbgs () << " [D]" ;
943
+ assert (TRI->hasRegUnit (I->PhysReg , Unit) && " inverse mapping present" );
996
944
break ;
997
945
}
998
946
}
999
947
}
1000
948
dbgs () << ' \n ' ;
1001
949
// Check that LiveVirtRegs is the inverse.
1002
- for (LiveRegMap::iterator i = LiveVirtRegs.begin (),
1003
- e = LiveVirtRegs.end (); i != e; ++i) {
1004
- if (!i->PhysReg )
1005
- continue ;
1006
- assert (i->VirtReg .isVirtual () && " Bad map key" );
1007
- assert (Register::isPhysicalRegister (i->PhysReg ) && " Bad map value" );
1008
- assert (PhysRegState[i->PhysReg ] == i->VirtReg && " Bad inverse map" );
950
+ for (const LiveReg &LR : LiveVirtRegs) {
951
+ Register VirtReg = LR.VirtReg ;
952
+ assert (VirtReg.isVirtual () && " Bad map key" );
953
+ MCPhysReg PhysReg = LR.PhysReg ;
954
+ if (PhysReg != 0 ) {
955
+ assert (Register::isPhysicalRegister (PhysReg) &&
956
+ " mapped to physreg" );
957
+ for (MCRegUnitIterator UI (PhysReg, TRI); UI.isValid (); ++UI) {
958
+ assert (RegUnitStates[*UI] == VirtReg && " inverse map valid" );
959
+ }
960
+ }
1009
961
}
1010
962
}
1011
963
#endif
@@ -1247,7 +1199,7 @@ void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
1247
1199
this ->MBB = &MBB;
1248
1200
LLVM_DEBUG (dbgs () << " \n Allocating " << MBB);
1249
1201
1250
- PhysRegState .assign (TRI->getNumRegs (), regDisabled );
1202
+ RegUnitStates .assign (TRI->getNumRegUnits (), regFree );
1251
1203
assert (LiveVirtRegs.empty () && " Mapping not cleared from last block?" );
1252
1204
1253
1205
MachineBasicBlock::iterator MII = MBB.begin ();
0 commit comments