Skip to content

Commit c0d1161

Browse files
committed
[CodeGen][MachineLoop] Fix getLoopID
Mirror the getLoopID implementation of LoopInfo in MachineLoopInfo. getLoopID used findLoopControlBlock to detect the special case where there is a single latch. However, findLoopControlBlock returns the exiting block if the latch is not an exiting block. The middle end places the LoopID metadata on the latch regardless of if it's an exiting block or not.
1 parent 3d47bc9 commit c0d1161

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

llvm/lib/CodeGen/MachineLoopInfo.cpp

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -181,48 +181,32 @@ MachineLoopInfo::findLoopPreheader(MachineLoop *L, bool SpeculativePreheader,
181181

182182
MDNode *MachineLoop::getLoopID() const {
183183
MDNode *LoopID = nullptr;
184-
if (const auto *MBB = findLoopControlBlock()) {
185-
// If there is a single latch block, then the metadata
186-
// node is attached to its terminating instruction.
184+
185+
// Go through the latch blocks and check the terminator for the metadata
186+
SmallVector<MachineBasicBlock *, 4> LatchesBlocks;
187+
getLoopLatches(LatchesBlocks);
188+
for (const auto *MBB : LatchesBlocks) {
187189
const auto *BB = MBB->getBasicBlock();
188190
if (!BB)
189191
return nullptr;
190-
if (const auto *TI = BB->getTerminator())
191-
LoopID = TI->getMetadata(LLVMContext::MD_loop);
192-
} else if (const auto *MBB = getHeader()) {
193-
// There seem to be multiple latch blocks, so we have to
194-
// visit all predecessors of the loop header and check
195-
// their terminating instructions for the metadata.
196-
if (const auto *Header = MBB->getBasicBlock()) {
197-
// Walk over all blocks in the loop.
198-
for (const auto *MBB : this->blocks()) {
199-
const auto *BB = MBB->getBasicBlock();
200-
if (!BB)
201-
return nullptr;
202-
const auto *TI = BB->getTerminator();
203-
if (!TI)
204-
return nullptr;
205-
MDNode *MD = nullptr;
206-
// Check if this terminating instruction jumps to the loop header.
207-
for (const auto *Succ : successors(TI)) {
208-
if (Succ == Header) {
209-
// This is a jump to the header - gather the metadata from it.
210-
MD = TI->getMetadata(LLVMContext::MD_loop);
211-
break;
212-
}
213-
}
214-
if (!MD)
215-
continue;
216-
if (!LoopID)
217-
LoopID = MD;
218-
else if (MD != LoopID)
219-
return nullptr;
220-
}
221-
}
192+
const auto *TI = BB->getTerminator();
193+
if (!TI)
194+
return nullptr;
195+
MDNode *MD = TI->getMetadata(LLVMContext::MD_loop);
196+
197+
if (!MD)
198+
return nullptr;
199+
200+
if (!LoopID)
201+
LoopID = MD;
202+
else if (MD != LoopID)
203+
return nullptr;
222204
}
223-
if (LoopID &&
224-
(LoopID->getNumOperands() == 0 || LoopID->getOperand(0) != LoopID))
225-
LoopID = nullptr;
205+
206+
if (!LoopID || LoopID->getNumOperands() == 0 ||
207+
LoopID->getOperand(0) != LoopID)
208+
return nullptr;
209+
226210
return LoopID;
227211
}
228212

0 commit comments

Comments
 (0)