Skip to content

Commit 8472eb1

Browse files
authored
[AArch64LoadStoreOpt] BaseReg update is searched also in CF successor (#145583)
Look for reg update instruction (to merge w/ mem instruction into pre/post-increment form) not only inside a single MBB but also along a CF path going downward w/o side enters such that BaseReg is alive along it but not at its exits. Regression test is updated accordingly.
1 parent 5cd56c9 commit 8472eb1

File tree

3 files changed

+442
-24
lines changed

3 files changed

+442
-24
lines changed

llvm/lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,31 +2529,64 @@ MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
25292529
return E;
25302530
}
25312531

2532-
for (unsigned Count = 0; MBBI != E && Count < Limit;
2533-
MBBI = next_nodbg(MBBI, E)) {
2534-
MachineInstr &MI = *MBBI;
2535-
2536-
// Don't count transient instructions towards the search limit since there
2537-
// may be different numbers of them if e.g. debug information is present.
2538-
if (!MI.isTransient())
2539-
++Count;
2540-
2541-
// If we found a match, return it.
2542-
if (isMatchingUpdateInsn(*I, MI, BaseReg, UnscaledOffset))
2543-
return MBBI;
2532+
unsigned Count = 0;
2533+
MachineBasicBlock *CurMBB = I->getParent();
2534+
// choice of next block to visit is liveins-based
2535+
bool VisitSucc = CurMBB->getParent()->getRegInfo().tracksLiveness();
2536+
2537+
while (true) {
2538+
for (MachineBasicBlock::iterator CurEnd = CurMBB->end();
2539+
MBBI != CurEnd && Count < Limit; MBBI = next_nodbg(MBBI, CurEnd)) {
2540+
MachineInstr &MI = *MBBI;
2541+
2542+
// Don't count transient instructions towards the search limit since there
2543+
// may be different numbers of them if e.g. debug information is present.
2544+
if (!MI.isTransient())
2545+
++Count;
2546+
2547+
// If we found a match, return it.
2548+
if (isMatchingUpdateInsn(*I, MI, BaseReg, UnscaledOffset))
2549+
return MBBI;
2550+
2551+
// Update the status of what the instruction clobbered and used.
2552+
LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits,
2553+
TRI);
2554+
2555+
// Otherwise, if the base register is used or modified, we have no match,
2556+
// so return early. If we are optimizing SP, do not allow instructions
2557+
// that may load or store in between the load and the optimized value
2558+
// update.
2559+
if (!ModifiedRegUnits.available(BaseReg) ||
2560+
!UsedRegUnits.available(BaseReg) ||
2561+
(BaseRegSP && MBBI->mayLoadOrStore()))
2562+
return E;
2563+
}
25442564

2545-
// Update the status of what the instruction clobbered and used.
2546-
LiveRegUnits::accumulateUsedDefed(MI, ModifiedRegUnits, UsedRegUnits, TRI);
2565+
if (!VisitSucc || Limit <= Count)
2566+
break;
25472567

2548-
// Otherwise, if the base register is used or modified, we have no match, so
2549-
// return early.
2550-
// If we are optimizing SP, do not allow instructions that may load or store
2551-
// in between the load and the optimized value update.
2552-
if (!ModifiedRegUnits.available(BaseReg) ||
2553-
!UsedRegUnits.available(BaseReg) ||
2554-
(BaseRegSP && MBBI->mayLoadOrStore()))
2555-
return E;
2568+
// Try to go downward to successors along a CF path w/o side enters
2569+
// such that BaseReg is alive along it but not at its exits
2570+
MachineBasicBlock *SuccToVisit = nullptr;
2571+
unsigned LiveSuccCount = 0;
2572+
MCRegister RegNoBAse = BaseReg;
2573+
for (MachineBasicBlock *Succ : CurMBB->successors()) {
2574+
for (MCRegAliasIterator AI(BaseReg, TRI, true); AI.isValid(); ++AI) {
2575+
if (Succ->isLiveIn(*AI)) {
2576+
if (LiveSuccCount++)
2577+
return E;
2578+
if (Succ->pred_size() == 1)
2579+
SuccToVisit = Succ;
2580+
break;
2581+
}
2582+
}
2583+
}
2584+
if (!SuccToVisit)
2585+
break;
2586+
CurMBB = SuccToVisit;
2587+
MBBI = CurMBB->begin();
25562588
}
2589+
25572590
return E;
25582591
}
25592592

0 commit comments

Comments
 (0)