@@ -514,41 +514,38 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
514
514
SmallVector<std::pair<unsigned , unsigned >, 8 > CSEPairs;
515
515
SmallVector<unsigned , 2 > ImplicitDefsToUpdate;
516
516
SmallVector<unsigned , 2 > ImplicitDefs;
517
- for (MachineBasicBlock::iterator I = MBB->begin (), E = MBB->end (); I != E; ) {
518
- MachineInstr *MI = &*I;
519
- ++I;
520
-
521
- if (!isCSECandidate (MI))
517
+ for (MachineInstr &MI : llvm::make_early_inc_range (*MBB)) {
518
+ if (!isCSECandidate (&MI))
522
519
continue ;
523
520
524
- bool FoundCSE = VNT.count (MI);
521
+ bool FoundCSE = VNT.count (& MI);
525
522
if (!FoundCSE) {
526
523
// Using trivial copy propagation to find more CSE opportunities.
527
- if (PerformTrivialCopyPropagation (MI, MBB)) {
524
+ if (PerformTrivialCopyPropagation (& MI, MBB)) {
528
525
Changed = true ;
529
526
530
527
// After coalescing MI itself may become a copy.
531
- if (MI-> isCopyLike ())
528
+ if (MI. isCopyLike ())
532
529
continue ;
533
530
534
531
// Try again to see if CSE is possible.
535
- FoundCSE = VNT.count (MI);
532
+ FoundCSE = VNT.count (& MI);
536
533
}
537
534
}
538
535
539
536
// Commute commutable instructions.
540
537
bool Commuted = false ;
541
- if (!FoundCSE && MI-> isCommutable ()) {
542
- if (MachineInstr *NewMI = TII->commuteInstruction (* MI)) {
538
+ if (!FoundCSE && MI. isCommutable ()) {
539
+ if (MachineInstr *NewMI = TII->commuteInstruction (MI)) {
543
540
Commuted = true ;
544
541
FoundCSE = VNT.count (NewMI);
545
- if (NewMI != MI) {
542
+ if (NewMI != & MI) {
546
543
// New instruction. It doesn't need to be kept.
547
544
NewMI->eraseFromParent ();
548
545
Changed = true ;
549
546
} else if (!FoundCSE)
550
547
// MI was changed but it didn't help, commute it back!
551
- (void )TII->commuteInstruction (* MI);
548
+ (void )TII->commuteInstruction (MI);
552
549
}
553
550
}
554
551
@@ -559,8 +556,8 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
559
556
SmallSet<MCRegister, 8 > PhysRefs;
560
557
PhysDefVector PhysDefs;
561
558
bool PhysUseDef = false ;
562
- if (FoundCSE && hasLivePhysRegDefUses (MI, MBB, PhysRefs,
563
- PhysDefs, PhysUseDef)) {
559
+ if (FoundCSE &&
560
+ hasLivePhysRegDefUses (&MI, MBB, PhysRefs, PhysDefs, PhysUseDef)) {
564
561
FoundCSE = false ;
565
562
566
563
// ... Unless the CS is local or is in the sole predecessor block
@@ -569,23 +566,23 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
569
566
// This can never be the case if the instruction both uses and
570
567
// defines the same physical register, which was detected above.
571
568
if (!PhysUseDef) {
572
- unsigned CSVN = VNT.lookup (MI);
569
+ unsigned CSVN = VNT.lookup (& MI);
573
570
MachineInstr *CSMI = Exps[CSVN];
574
- if (PhysRegDefsReach (CSMI, MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
571
+ if (PhysRegDefsReach (CSMI, & MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
575
572
FoundCSE = true ;
576
573
}
577
574
}
578
575
579
576
if (!FoundCSE) {
580
- VNT.insert (MI, CurrVN++);
581
- Exps.push_back (MI);
577
+ VNT.insert (& MI, CurrVN++);
578
+ Exps.push_back (& MI);
582
579
continue ;
583
580
}
584
581
585
582
// Found a common subexpression, eliminate it.
586
- unsigned CSVN = VNT.lookup (MI);
583
+ unsigned CSVN = VNT.lookup (& MI);
587
584
MachineInstr *CSMI = Exps[CSVN];
588
- LLVM_DEBUG (dbgs () << " Examining: " << * MI);
585
+ LLVM_DEBUG (dbgs () << " Examining: " << MI);
589
586
LLVM_DEBUG (dbgs () << " *** Found a common subexpression: " << *CSMI);
590
587
591
588
// Prevent CSE-ing non-local convergent instructions.
@@ -597,20 +594,20 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
597
594
// definition, so it's necessary to use `isConvergent` to prevent illegally
598
595
// CSE-ing the subset of `isConvergent` instructions which do fall into this
599
596
// extended definition.
600
- if (MI-> isConvergent () && MI-> getParent () != CSMI->getParent ()) {
597
+ if (MI. isConvergent () && MI. getParent () != CSMI->getParent ()) {
601
598
LLVM_DEBUG (dbgs () << " *** Convergent MI and subexpression exist in "
602
599
" different BBs, avoid CSE!\n " );
603
- VNT.insert (MI, CurrVN++);
604
- Exps.push_back (MI);
600
+ VNT.insert (& MI, CurrVN++);
601
+ Exps.push_back (& MI);
605
602
continue ;
606
603
}
607
604
608
605
// Check if it's profitable to perform this CSE.
609
606
bool DoCSE = true ;
610
- unsigned NumDefs = MI-> getNumDefs ();
607
+ unsigned NumDefs = MI. getNumDefs ();
611
608
612
- for (unsigned i = 0 , e = MI-> getNumOperands (); NumDefs && i != e; ++i) {
613
- MachineOperand &MO = MI-> getOperand (i);
609
+ for (unsigned i = 0 , e = MI. getNumOperands (); NumDefs && i != e; ++i) {
610
+ MachineOperand &MO = MI. getOperand (i);
614
611
if (!MO.isReg () || !MO.isDef ())
615
612
continue ;
616
613
Register OldReg = MO.getReg ();
@@ -635,7 +632,7 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
635
632
Register::isVirtualRegister (NewReg) &&
636
633
" Do not CSE physical register defs!" );
637
634
638
- if (!isProfitableToCSE (NewReg, OldReg, CSMI->getParent (), MI)) {
635
+ if (!isProfitableToCSE (NewReg, OldReg, CSMI->getParent (), & MI)) {
639
636
LLVM_DEBUG (dbgs () << " *** Not profitable, avoid CSE!\n " );
640
637
DoCSE = false ;
641
638
break ;
@@ -674,7 +671,7 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
674
671
for (unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
675
672
CSMI->getOperand (ImplicitDefToUpdate).setIsDead (false );
676
673
for (const auto &PhysDef : PhysDefs)
677
- if (!MI-> getOperand (PhysDef.first ).isDead ())
674
+ if (!MI. getOperand (PhysDef.first ).isDead ())
678
675
CSMI->getOperand (PhysDef.first ).setIsDead (false );
679
676
680
677
// Go through implicit defs of CSMI and MI, and clear the kill flags on
@@ -687,8 +684,8 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
687
684
// Since we eliminated MI, and reused a register imp-def'd by CSMI
688
685
// (here %nzcv), that register, if it was killed before MI, should have
689
686
// that kill flag removed, because it's lifetime was extended.
690
- if (CSMI->getParent () == MI-> getParent ()) {
691
- for (MachineBasicBlock::iterator II = CSMI, IE = MI; II != IE; ++II)
687
+ if (CSMI->getParent () == MI. getParent ()) {
688
+ for (MachineBasicBlock::iterator II = CSMI, IE = & MI; II != IE; ++II)
692
689
for (auto ImplicitDef : ImplicitDefs)
693
690
if (MachineOperand *MO = II->findRegisterUseOperand (
694
691
ImplicitDef, /* isKill=*/ true , TRI))
@@ -711,16 +708,16 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
711
708
++NumCrossBBCSEs;
712
709
}
713
710
714
- MI-> eraseFromParent ();
711
+ MI. eraseFromParent ();
715
712
++NumCSEs;
716
713
if (!PhysRefs.empty ())
717
714
++NumPhysCSEs;
718
715
if (Commuted)
719
716
++NumCommutes;
720
717
Changed = true ;
721
718
} else {
722
- VNT.insert (MI, CurrVN++);
723
- Exps.push_back (MI);
719
+ VNT.insert (& MI, CurrVN++);
720
+ Exps.push_back (& MI);
724
721
}
725
722
CSEPairs.clear ();
726
723
ImplicitDefsToUpdate.clear ();
@@ -807,19 +804,16 @@ bool MachineCSE::isPRECandidate(MachineInstr *MI) {
807
804
bool MachineCSE::ProcessBlockPRE (MachineDominatorTree *DT,
808
805
MachineBasicBlock *MBB) {
809
806
bool Changed = false ;
810
- for (MachineBasicBlock::iterator I = MBB->begin (), E = MBB->end (); I != E;) {
811
- MachineInstr *MI = &*I;
812
- ++I;
813
-
814
- if (!isPRECandidate (MI))
807
+ for (MachineInstr &MI : llvm::make_early_inc_range (*MBB)) {
808
+ if (!isPRECandidate (&MI))
815
809
continue ;
816
810
817
- if (!PREMap.count (MI)) {
818
- PREMap[MI] = MBB;
811
+ if (!PREMap.count (& MI)) {
812
+ PREMap[& MI] = MBB;
819
813
continue ;
820
814
}
821
815
822
- auto MBB1 = PREMap[MI];
816
+ auto MBB1 = PREMap[& MI];
823
817
assert (
824
818
!DT->properlyDominates (MBB, MBB1) &&
825
819
" MBB cannot properly dominate MBB1 while DFS through dominators tree!" );
@@ -844,17 +838,17 @@ bool MachineCSE::ProcessBlockPRE(MachineDominatorTree *DT,
844
838
// it's necessary to use `isConvergent` to prevent illegally PRE-ing the
845
839
// subset of `isConvergent` instructions which do fall into this
846
840
// extended definition.
847
- if (MI-> isConvergent () && CMBB != MBB)
841
+ if (MI. isConvergent () && CMBB != MBB)
848
842
continue ;
849
843
850
- assert (MI-> getOperand (0 ).isDef () &&
844
+ assert (MI. getOperand (0 ).isDef () &&
851
845
" First operand of instr with one explicit def must be this def" );
852
- Register VReg = MI-> getOperand (0 ).getReg ();
846
+ Register VReg = MI. getOperand (0 ).getReg ();
853
847
Register NewReg = MRI->cloneVirtualRegister (VReg);
854
- if (!isProfitableToCSE (NewReg, VReg, CMBB, MI))
848
+ if (!isProfitableToCSE (NewReg, VReg, CMBB, & MI))
855
849
continue ;
856
850
MachineInstr &NewMI =
857
- TII->duplicate (*CMBB, CMBB->getFirstTerminator (), * MI);
851
+ TII->duplicate (*CMBB, CMBB->getFirstTerminator (), MI);
858
852
859
853
// When hoisting, make sure we don't carry the debug location of
860
854
// the original instruction, as that's not correct and can cause
@@ -864,7 +858,7 @@ bool MachineCSE::ProcessBlockPRE(MachineDominatorTree *DT,
864
858
865
859
NewMI.getOperand (0 ).setReg (NewReg);
866
860
867
- PREMap[MI] = CMBB;
861
+ PREMap[& MI] = CMBB;
868
862
++NumPREs;
869
863
Changed = true ;
870
864
}
0 commit comments