Skip to content

Commit

Permalink
[Xtensa] Fixup hwloop pass
Browse files Browse the repository at this point in the history
Create block and insert it before loop end address as
target for jump/branch instruction to avoid premature
exit from loop.
  • Loading branch information
andreisfr committed Mar 29, 2023
1 parent a07cafd commit f389074
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
44 changes: 39 additions & 5 deletions llvm/lib/Target/Xtensa/XtensaFixupHWLoops.cpp
Expand Up @@ -290,7 +290,6 @@ bool XtensaFixupHwLoops::fixupLoopInstrs(MachineLoop *L) {
Changed = true;
break;
}

if (PII != PIB) {
LoopEnd = MF->CreateMachineBasicBlock();
MF->insert(++(PMBB->getIterator()), LoopEnd);
Expand All @@ -302,10 +301,45 @@ bool XtensaFixupHwLoops::fixupLoopInstrs(MachineLoop *L) {
.addMBB(LoopEnd);
LoopEnd->addSuccessor(LoopEnd);
} else {
BuildMI(*PMBB, PII, DL, TII->get(Xtensa::LOOPEND)).addMBB(PMBB);
PMBB->addSuccessor(PMBB);
BuildMI(*PMBB, PII, DL, TII->get(Xtensa::NOP));
LoopEnd = PMBB;
bool NeedBlockForJump = false;
// Check for branches to the loop end basic block from
// predecessors
for (auto I = PMBB->pred_begin(), E = PMBB->pred_end(); I != E;
++I) {
MachineBasicBlock *PLEMBB = *I;
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
SmallVector<MachineOperand, 4> Cond;
if (!TII->analyzeBranch(*PLEMBB, TBB, FBB, Cond)) {
if (TBB == PMBB) {
NeedBlockForJump = true;
break;
}
} else {
NeedBlockForJump = true;
break;
}
}
// Create block and insert it before loop end address as
// target for jump/branch instruction to avoid premature exit from
// loop
if (NeedBlockForJump) {
LoopEnd = MF->CreateMachineBasicBlock();
MF->insert(++(PMBB->getIterator()), LoopEnd);
LoopEnd->transferSuccessors(PMBB);
LoopEnd->splice(LoopEnd->end(), PMBB, PII, PMBB->end());
PMBB->addSuccessor(LoopEnd);
BuildMI(*PMBB, PMBB->end(), DL, TII->get(Xtensa::NOP));

BuildMI(*LoopEnd, LoopEnd->begin(), DL,
TII->get(Xtensa::LOOPEND))
.addMBB(LoopEnd);
LoopEnd->addSuccessor(LoopEnd);
} else {
BuildMI(*PMBB, PII, DL, TII->get(Xtensa::LOOPEND)).addMBB(PMBB);
PMBB->addSuccessor(PMBB);
BuildMI(*PMBB, PII, DL, TII->get(Xtensa::NOP));
LoopEnd = PMBB;
}
}

Changed = true;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/Xtensa/XtensaHardwareLoops.cpp
Expand Up @@ -360,7 +360,7 @@ bool XtensaHardwareLoops::processLoop(MachineLoop *L) {
}

bool XtensaHardwareLoops::checkLoopSize(MachineLoop *L) {
uint64_t LoopSize = 0;
uint64_t LoopSize = 3; //Reserve space for possible NOP

for (auto *MBB : L->getBlocks()) {
uint64_t BlockSize = 0;
Expand Down

0 comments on commit f389074

Please sign in to comment.