@@ -318,8 +318,13 @@ void LiveVars::HandleVirtRegUse(Value *VL, BasicBlock *MBB,
318
318
// scan all the kills in order to replace the right one.
319
319
if (ScanAllKills) {
320
320
for (unsigned i = 0 , e = VRInfo.Kills .size (); i != e; ++i) {
321
- if (VRInfo.Kills [i]->getParent () == MBB){
322
- VRInfo.Kills [i] = MI;
321
+ if (VRInfo.Kills [i]->getParent () == MBB) {
322
+ Instruction* killInst = VRInfo.Kills [i];
323
+ assert (DistanceMap.count (killInst) && DistanceMap.count (MI) &&
324
+ " DistanceMap not set up yet." );
325
+ if (DistanceMap[killInst] < DistanceMap[MI]) {
326
+ VRInfo.Kills [i] = MI;
327
+ }
323
328
return ;
324
329
}
325
330
}
@@ -413,6 +418,20 @@ void LiveVars::HandleVirtRegDef(Instruction* MI)
413
418
VRInfo.Kills .push_back (MI);
414
419
}
415
420
421
+ void LiveVars::initDistance (Function& F)
422
+ {
423
+ DistanceMap.clear ();
424
+
425
+ for (auto &BB : F)
426
+ {
427
+ unsigned Dist = 0 ;
428
+ for (auto &II : BB) {
429
+ Instruction *MI = &II;
430
+ DistanceMap.insert (std::make_pair (MI, Dist++));
431
+ }
432
+ }
433
+ }
434
+
416
435
void LiveVars::ComputeLiveness (Function* mf, WIAnalysis* wia)
417
436
{
418
437
releaseMemory ();
@@ -421,6 +440,10 @@ void LiveVars::ComputeLiveness(Function* mf, WIAnalysis* wia)
421
440
422
441
preAllocMemory (*MF);
423
442
443
+ // First, set up DistanceMap
444
+ // save distance map
445
+ initDistance (*MF);
446
+
424
447
analyzePHINodes (*mf);
425
448
BasicBlock *Entry = &(*MF->begin ());
426
449
typedef df_iterator_default_set<BasicBlock*,16 > VisitedTy;
@@ -447,14 +470,6 @@ void LiveVars::ComputeLiveness(Function* mf, WIAnalysis* wia)
447
470
MarkVirtRegAliveInBlock (getLVInfo (DefV), DefBlk, MBB);
448
471
}
449
472
}
450
-
451
- // save distance map
452
- unsigned Dist = 0 ;
453
- for (BasicBlock::iterator I = MBB->begin (), E = MBB->end ();
454
- I != E; ++I) {
455
- Instruction *MI = &(*I);
456
- DistanceMap.insert (std::make_pair (MI, Dist++));
457
- }
458
473
}
459
474
}
460
475
@@ -582,6 +597,8 @@ void LiveVars::Calculate(Function* mf, WIAnalysis* wia)
582
597
583
598
preAllocMemory (*MF);
584
599
600
+ initDistance (*MF);
601
+
585
602
analyzePHINodes (*mf);
586
603
587
604
BasicBlock *Entry = &(*MF->begin ());
@@ -594,12 +611,10 @@ void LiveVars::Calculate(Function* mf, WIAnalysis* wia)
594
611
BasicBlock *MBB = *DFI;
595
612
596
613
// Loop over all of the instructions, processing them.
597
- unsigned Dist = 0 ;
598
614
for (BasicBlock::iterator I = MBB->begin (), E = MBB->end ();
599
615
I != E; ++I)
600
616
{
601
617
Instruction *MI = &(*I);
602
- DistanceMap.insert (std::make_pair (MI, Dist++));
603
618
604
619
// Unless it is a PHI node. In this case, ONLY process the DEF, not any
605
620
// of the uses. They will be handled in other basic blocks.
0 commit comments