18
18
#include " llvm/CodeGen/MachineInstr.h"
19
19
#include " llvm/CodeGen/MachineFrameInfo.h"
20
20
#include " llvm/CodeGen/MachineRegisterInfo.h"
21
+ #include " llvm/CodeGen/LiveVariables.h"
21
22
#include " llvm/CodeGen/Passes.h"
22
23
#include " llvm/CodeGen/RegAllocRegistry.h"
23
24
#include " llvm/Target/TargetInstrInfo.h"
@@ -123,6 +124,7 @@ namespace {
123
124
124
125
virtual void getAnalysisUsage (AnalysisUsage &AU) const {
125
126
AU.setPreservesCFG ();
127
+ AU.addRequired <LiveVariables>();
126
128
AU.addRequiredID (PHIEliminationID);
127
129
AU.addRequiredID (TwoAddressInstructionPassID);
128
130
MachineFunctionPass::getAnalysisUsage (AU);
@@ -218,10 +220,6 @@ namespace {
218
220
unsigned OpNum, SmallSet<unsigned , 4 > &RRegs,
219
221
unsigned PhysReg);
220
222
221
- // / ComputeLocalLiveness - Computes liveness of registers within a basic
222
- // / block, setting the killed/dead flags as appropriate.
223
- void ComputeLocalLiveness (MachineBasicBlock& MBB);
224
-
225
223
void reloadPhysReg (MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
226
224
unsigned PhysReg);
227
225
};
@@ -538,177 +536,6 @@ static bool isReadModWriteImplicitDef(MachineInstr *MI, unsigned Reg) {
538
536
return false ;
539
537
}
540
538
541
- // precedes - Helper function to determine with MachineInstr A
542
- // precedes MachineInstr B within the same MBB.
543
- static bool precedes (MachineBasicBlock::iterator A,
544
- MachineBasicBlock::iterator B) {
545
- if (A == B)
546
- return false ;
547
-
548
- MachineBasicBlock::iterator I = A->getParent ()->begin ();
549
- while (I != A->getParent ()->end ()) {
550
- if (I == A)
551
- return true ;
552
- else if (I == B)
553
- return false ;
554
-
555
- ++I;
556
- }
557
-
558
- return false ;
559
- }
560
-
561
- // / ComputeLocalLiveness - Computes liveness of registers within a basic
562
- // / block, setting the killed/dead flags as appropriate.
563
- void RAFast::ComputeLocalLiveness (MachineBasicBlock& MBB) {
564
- MachineRegisterInfo &MRI = MBB.getParent ()->getRegInfo ();
565
- // Keep track of the most recently seen previous use or def of each reg,
566
- // so that we can update them with dead/kill markers.
567
- DenseMap<unsigned , std::pair<MachineInstr*, unsigned > > LastUseDef;
568
- for (MachineBasicBlock::iterator I = MBB.begin (), E = MBB.end ();
569
- I != E; ++I) {
570
- if (I->isDebugValue ())
571
- continue ;
572
-
573
- for (unsigned i = 0 , e = I->getNumOperands (); i != e; ++i) {
574
- MachineOperand &MO = I->getOperand (i);
575
- // Uses don't trigger any flags, but we need to save
576
- // them for later. Also, we have to process these
577
- // _before_ processing the defs, since an instr
578
- // uses regs before it defs them.
579
- if (!MO.isReg () || !MO.getReg () || !MO.isUse ())
580
- continue ;
581
-
582
- LastUseDef[MO.getReg ()] = std::make_pair (I, i);
583
-
584
- if (TargetRegisterInfo::isVirtualRegister (MO.getReg ())) continue ;
585
-
586
- const unsigned *Aliases = TRI->getAliasSet (MO.getReg ());
587
- if (Aliases == 0 )
588
- continue ;
589
-
590
- while (*Aliases) {
591
- DenseMap<unsigned , std::pair<MachineInstr*, unsigned > >::iterator
592
- alias = LastUseDef.find (*Aliases);
593
-
594
- if (alias != LastUseDef.end () && alias->second .first != I)
595
- LastUseDef[*Aliases] = std::make_pair (I, i);
596
-
597
- ++Aliases;
598
- }
599
- }
600
-
601
- for (unsigned i = 0 , e = I->getNumOperands (); i != e; ++i) {
602
- MachineOperand &MO = I->getOperand (i);
603
- // Defs others than 2-addr redefs _do_ trigger flag changes:
604
- // - A def followed by a def is dead
605
- // - A use followed by a def is a kill
606
- if (!MO.isReg () || !MO.getReg () || !MO.isDef ()) continue ;
607
-
608
- DenseMap<unsigned , std::pair<MachineInstr*, unsigned > >::iterator
609
- last = LastUseDef.find (MO.getReg ());
610
- if (last != LastUseDef.end ()) {
611
- // Check if this is a two address instruction. If so, then
612
- // the def does not kill the use.
613
- if (last->second .first == I &&
614
- I->isRegTiedToUseOperand (i))
615
- continue ;
616
-
617
- MachineOperand &lastUD =
618
- last->second .first ->getOperand (last->second .second );
619
- if (lastUD.isDef ())
620
- lastUD.setIsDead (true );
621
- else
622
- lastUD.setIsKill (true );
623
- }
624
-
625
- LastUseDef[MO.getReg ()] = std::make_pair (I, i);
626
- }
627
- }
628
-
629
- // Live-out (of the function) registers contain return values of the function,
630
- // so we need to make sure they are alive at return time.
631
- MachineBasicBlock::iterator Ret = MBB.getFirstTerminator ();
632
- bool BBEndsInReturn = (Ret != MBB.end () && Ret->getDesc ().isReturn ());
633
-
634
- if (BBEndsInReturn)
635
- for (MachineRegisterInfo::liveout_iterator
636
- I = MF->getRegInfo ().liveout_begin (),
637
- E = MF->getRegInfo ().liveout_end (); I != E; ++I)
638
- if (!Ret->readsRegister (*I)) {
639
- Ret->addOperand (MachineOperand::CreateReg (*I, false , true ));
640
- LastUseDef[*I] = std::make_pair (Ret, Ret->getNumOperands ()-1 );
641
- }
642
-
643
- // Finally, loop over the final use/def of each reg
644
- // in the block and determine if it is dead.
645
- for (DenseMap<unsigned , std::pair<MachineInstr*, unsigned > >::iterator
646
- I = LastUseDef.begin (), E = LastUseDef.end (); I != E; ++I) {
647
- MachineInstr *MI = I->second .first ;
648
- unsigned idx = I->second .second ;
649
- MachineOperand &MO = MI->getOperand (idx);
650
-
651
- bool isPhysReg = TargetRegisterInfo::isPhysicalRegister (MO.getReg ());
652
-
653
- // A crude approximation of "live-out" calculation
654
- bool usedOutsideBlock = isPhysReg ? false :
655
- UsedInMultipleBlocks.test (MO.getReg () -
656
- TargetRegisterInfo::FirstVirtualRegister);
657
-
658
- // If the machine BB ends in a return instruction, then the value isn't used
659
- // outside of the BB.
660
- if (!isPhysReg && (!usedOutsideBlock || BBEndsInReturn)) {
661
- // DBG_VALUE complicates this: if the only refs of a register outside
662
- // this block are DBG_VALUE, we can't keep the reg live just for that,
663
- // as it will cause the reg to be spilled at the end of this block when
664
- // it wouldn't have been otherwise. Nullify the DBG_VALUEs when that
665
- // happens.
666
- bool UsedByDebugValueOnly = false ;
667
- for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin (MO.getReg ()),
668
- UE = MRI.reg_end (); UI != UE; ++UI) {
669
- // Two cases:
670
- // - used in another block
671
- // - used in the same block before it is defined (loop)
672
- if (UI->getParent () == &MBB &&
673
- !(MO.isDef () && UI.getOperand ().isUse () && precedes (&*UI, MI)))
674
- continue ;
675
-
676
- if (UI->isDebugValue ()) {
677
- UsedByDebugValueOnly = true ;
678
- continue ;
679
- }
680
-
681
- // A non-DBG_VALUE use means we can leave DBG_VALUE uses alone.
682
- UsedInMultipleBlocks.set (MO.getReg () -
683
- TargetRegisterInfo::FirstVirtualRegister);
684
- usedOutsideBlock = true ;
685
- UsedByDebugValueOnly = false ;
686
- break ;
687
- }
688
-
689
- if (UsedByDebugValueOnly)
690
- for (MachineRegisterInfo::reg_iterator UI = MRI.reg_begin (MO.getReg ()),
691
- UE = MRI.reg_end (); UI != UE; ++UI)
692
- if (UI->isDebugValue () &&
693
- (UI->getParent () != &MBB ||
694
- (MO.isDef () && precedes (&*UI, MI))))
695
- UI.getOperand ().setReg (0U );
696
- }
697
-
698
- // Physical registers and those that are not live-out of the block are
699
- // killed/dead at their last use/def within this block.
700
- if (isPhysReg || !usedOutsideBlock || BBEndsInReturn) {
701
- if (MO.isUse ()) {
702
- // Don't mark uses that are tied to defs as kills.
703
- if (!MI->isRegTiedToDefOperand (idx))
704
- MO.setIsKill (true );
705
- } else {
706
- MO.setIsDead (true );
707
- }
708
- }
709
- }
710
- }
711
-
712
539
void RAFast::AllocateBasicBlock (MachineBasicBlock &MBB) {
713
540
// loop over each instruction
714
541
MachineBasicBlock::iterator MII = MBB.begin ();
@@ -733,8 +560,6 @@ void RAFast::AllocateBasicBlock(MachineBasicBlock &MBB) {
733
560
}
734
561
}
735
562
736
- ComputeLocalLiveness (MBB);
737
-
738
563
// Otherwise, sequentially allocate each instruction in the MBB.
739
564
while (MII != MBB.end ()) {
740
565
MachineInstr *MI = MII++;
0 commit comments