Skip to content

Commit 4cc7c47

Browse files
[MachineCSE] Use make_early_inc_range (NFC)
1 parent c8b1ed5 commit 4cc7c47

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

llvm/lib/CodeGen/MachineCSE.cpp

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -514,41 +514,38 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
514514
SmallVector<std::pair<unsigned, unsigned>, 8> CSEPairs;
515515
SmallVector<unsigned, 2> ImplicitDefsToUpdate;
516516
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))
522519
continue;
523520

524-
bool FoundCSE = VNT.count(MI);
521+
bool FoundCSE = VNT.count(&MI);
525522
if (!FoundCSE) {
526523
// Using trivial copy propagation to find more CSE opportunities.
527-
if (PerformTrivialCopyPropagation(MI, MBB)) {
524+
if (PerformTrivialCopyPropagation(&MI, MBB)) {
528525
Changed = true;
529526

530527
// After coalescing MI itself may become a copy.
531-
if (MI->isCopyLike())
528+
if (MI.isCopyLike())
532529
continue;
533530

534531
// Try again to see if CSE is possible.
535-
FoundCSE = VNT.count(MI);
532+
FoundCSE = VNT.count(&MI);
536533
}
537534
}
538535

539536
// Commute commutable instructions.
540537
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)) {
543540
Commuted = true;
544541
FoundCSE = VNT.count(NewMI);
545-
if (NewMI != MI) {
542+
if (NewMI != &MI) {
546543
// New instruction. It doesn't need to be kept.
547544
NewMI->eraseFromParent();
548545
Changed = true;
549546
} else if (!FoundCSE)
550547
// MI was changed but it didn't help, commute it back!
551-
(void)TII->commuteInstruction(*MI);
548+
(void)TII->commuteInstruction(MI);
552549
}
553550
}
554551

@@ -559,8 +556,8 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
559556
SmallSet<MCRegister, 8> PhysRefs;
560557
PhysDefVector PhysDefs;
561558
bool PhysUseDef = false;
562-
if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs,
563-
PhysDefs, PhysUseDef)) {
559+
if (FoundCSE &&
560+
hasLivePhysRegDefUses(&MI, MBB, PhysRefs, PhysDefs, PhysUseDef)) {
564561
FoundCSE = false;
565562

566563
// ... Unless the CS is local or is in the sole predecessor block
@@ -569,23 +566,23 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
569566
// This can never be the case if the instruction both uses and
570567
// defines the same physical register, which was detected above.
571568
if (!PhysUseDef) {
572-
unsigned CSVN = VNT.lookup(MI);
569+
unsigned CSVN = VNT.lookup(&MI);
573570
MachineInstr *CSMI = Exps[CSVN];
574-
if (PhysRegDefsReach(CSMI, MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
571+
if (PhysRegDefsReach(CSMI, &MI, PhysRefs, PhysDefs, CrossMBBPhysDef))
575572
FoundCSE = true;
576573
}
577574
}
578575

579576
if (!FoundCSE) {
580-
VNT.insert(MI, CurrVN++);
581-
Exps.push_back(MI);
577+
VNT.insert(&MI, CurrVN++);
578+
Exps.push_back(&MI);
582579
continue;
583580
}
584581

585582
// Found a common subexpression, eliminate it.
586-
unsigned CSVN = VNT.lookup(MI);
583+
unsigned CSVN = VNT.lookup(&MI);
587584
MachineInstr *CSMI = Exps[CSVN];
588-
LLVM_DEBUG(dbgs() << "Examining: " << *MI);
585+
LLVM_DEBUG(dbgs() << "Examining: " << MI);
589586
LLVM_DEBUG(dbgs() << "*** Found a common subexpression: " << *CSMI);
590587

591588
// Prevent CSE-ing non-local convergent instructions.
@@ -597,20 +594,20 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
597594
// definition, so it's necessary to use `isConvergent` to prevent illegally
598595
// CSE-ing the subset of `isConvergent` instructions which do fall into this
599596
// extended definition.
600-
if (MI->isConvergent() && MI->getParent() != CSMI->getParent()) {
597+
if (MI.isConvergent() && MI.getParent() != CSMI->getParent()) {
601598
LLVM_DEBUG(dbgs() << "*** Convergent MI and subexpression exist in "
602599
"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);
605602
continue;
606603
}
607604

608605
// Check if it's profitable to perform this CSE.
609606
bool DoCSE = true;
610-
unsigned NumDefs = MI->getNumDefs();
607+
unsigned NumDefs = MI.getNumDefs();
611608

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);
614611
if (!MO.isReg() || !MO.isDef())
615612
continue;
616613
Register OldReg = MO.getReg();
@@ -635,7 +632,7 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
635632
Register::isVirtualRegister(NewReg) &&
636633
"Do not CSE physical register defs!");
637634

638-
if (!isProfitableToCSE(NewReg, OldReg, CSMI->getParent(), MI)) {
635+
if (!isProfitableToCSE(NewReg, OldReg, CSMI->getParent(), &MI)) {
639636
LLVM_DEBUG(dbgs() << "*** Not profitable, avoid CSE!\n");
640637
DoCSE = false;
641638
break;
@@ -674,7 +671,7 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
674671
for (unsigned ImplicitDefToUpdate : ImplicitDefsToUpdate)
675672
CSMI->getOperand(ImplicitDefToUpdate).setIsDead(false);
676673
for (const auto &PhysDef : PhysDefs)
677-
if (!MI->getOperand(PhysDef.first).isDead())
674+
if (!MI.getOperand(PhysDef.first).isDead())
678675
CSMI->getOperand(PhysDef.first).setIsDead(false);
679676

680677
// Go through implicit defs of CSMI and MI, and clear the kill flags on
@@ -687,8 +684,8 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
687684
// Since we eliminated MI, and reused a register imp-def'd by CSMI
688685
// (here %nzcv), that register, if it was killed before MI, should have
689686
// 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)
692689
for (auto ImplicitDef : ImplicitDefs)
693690
if (MachineOperand *MO = II->findRegisterUseOperand(
694691
ImplicitDef, /*isKill=*/true, TRI))
@@ -711,16 +708,16 @@ bool MachineCSE::ProcessBlockCSE(MachineBasicBlock *MBB) {
711708
++NumCrossBBCSEs;
712709
}
713710

714-
MI->eraseFromParent();
711+
MI.eraseFromParent();
715712
++NumCSEs;
716713
if (!PhysRefs.empty())
717714
++NumPhysCSEs;
718715
if (Commuted)
719716
++NumCommutes;
720717
Changed = true;
721718
} else {
722-
VNT.insert(MI, CurrVN++);
723-
Exps.push_back(MI);
719+
VNT.insert(&MI, CurrVN++);
720+
Exps.push_back(&MI);
724721
}
725722
CSEPairs.clear();
726723
ImplicitDefsToUpdate.clear();
@@ -807,19 +804,16 @@ bool MachineCSE::isPRECandidate(MachineInstr *MI) {
807804
bool MachineCSE::ProcessBlockPRE(MachineDominatorTree *DT,
808805
MachineBasicBlock *MBB) {
809806
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))
815809
continue;
816810

817-
if (!PREMap.count(MI)) {
818-
PREMap[MI] = MBB;
811+
if (!PREMap.count(&MI)) {
812+
PREMap[&MI] = MBB;
819813
continue;
820814
}
821815

822-
auto MBB1 = PREMap[MI];
816+
auto MBB1 = PREMap[&MI];
823817
assert(
824818
!DT->properlyDominates(MBB, MBB1) &&
825819
"MBB cannot properly dominate MBB1 while DFS through dominators tree!");
@@ -844,17 +838,17 @@ bool MachineCSE::ProcessBlockPRE(MachineDominatorTree *DT,
844838
// it's necessary to use `isConvergent` to prevent illegally PRE-ing the
845839
// subset of `isConvergent` instructions which do fall into this
846840
// extended definition.
847-
if (MI->isConvergent() && CMBB != MBB)
841+
if (MI.isConvergent() && CMBB != MBB)
848842
continue;
849843

850-
assert(MI->getOperand(0).isDef() &&
844+
assert(MI.getOperand(0).isDef() &&
851845
"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();
853847
Register NewReg = MRI->cloneVirtualRegister(VReg);
854-
if (!isProfitableToCSE(NewReg, VReg, CMBB, MI))
848+
if (!isProfitableToCSE(NewReg, VReg, CMBB, &MI))
855849
continue;
856850
MachineInstr &NewMI =
857-
TII->duplicate(*CMBB, CMBB->getFirstTerminator(), *MI);
851+
TII->duplicate(*CMBB, CMBB->getFirstTerminator(), MI);
858852

859853
// When hoisting, make sure we don't carry the debug location of
860854
// the original instruction, as that's not correct and can cause
@@ -864,7 +858,7 @@ bool MachineCSE::ProcessBlockPRE(MachineDominatorTree *DT,
864858

865859
NewMI.getOperand(0).setReg(NewReg);
866860

867-
PREMap[MI] = CMBB;
861+
PREMap[&MI] = CMBB;
868862
++NumPREs;
869863
Changed = true;
870864
}

0 commit comments

Comments
 (0)