diff --git a/llvm/lib/CodeGen/MachineLoopInfo.cpp b/llvm/lib/CodeGen/MachineLoopInfo.cpp --- a/llvm/lib/CodeGen/MachineLoopInfo.cpp +++ b/llvm/lib/CodeGen/MachineLoopInfo.cpp @@ -80,7 +80,7 @@ MachineBasicBlock *NextMBB = &*std::next(BotMBB->getIterator()); while (contains(NextMBB)) { BotMBB = NextMBB; - if (BotMBB == &*std::next(BotMBB->getIterator())) + if (BotMBB->getIterator() == std::prev(End)) break; NextMBB = &*std::next(BotMBB->getIterator()); } diff --git a/llvm/unittests/CodeGen/CMakeLists.txt b/llvm/unittests/CodeGen/CMakeLists.txt --- a/llvm/unittests/CodeGen/CMakeLists.txt +++ b/llvm/unittests/CodeGen/CMakeLists.txt @@ -27,6 +27,7 @@ LexicalScopesTest.cpp MachineInstrBundleIteratorTest.cpp MachineInstrTest.cpp + MachineLoopInfoTest.cpp MachineOperandTest.cpp RegAllocScoreTest.cpp PassManagerTest.cpp diff --git a/llvm/unittests/CodeGen/MachineLoopInfoTest.cpp b/llvm/unittests/CodeGen/MachineLoopInfoTest.cpp new file mode 100644 --- /dev/null +++ b/llvm/unittests/CodeGen/MachineLoopInfoTest.cpp @@ -0,0 +1,45 @@ +//===- MachineLoopInfoTest.cpp --------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/CodeGen/MachineLoopInfo.h" +#include "llvm/CodeGen/MachineBasicBlock.h" +#include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineModuleInfo.h" +#include "llvm/CodeGen/TargetFrameLowering.h" +#include "llvm/CodeGen/TargetInstrInfo.h" +#include "llvm/CodeGen/TargetLowering.h" +#include "llvm/CodeGen/TargetSubtargetInfo.h" +#include "llvm/MC/TargetRegistry.h" +#include "llvm/Target/TargetMachine.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { +// Include helper functions to ease the manipulation of MachineFunctions. +#include "MFCommon.inc" + +TEST(MachineLoopInfoTest, TopBottomBlocks) { + LLVMContext Ctx; + Module Mod("Module", Ctx); + auto MF = createMachineFunction(Ctx, Mod); + auto *TopBlock = MF->CreateMachineBasicBlock(); + auto *BottomBlock = MF->CreateMachineBasicBlock(); + + MF->push_back(TopBlock); + MF->push_back(BottomBlock); + + LoopInfoBase LI; + MachineLoop *Loop = LI.AllocateLoop(); + Loop->addBasicBlockToLoop(TopBlock, LI); + Loop->addBasicBlockToLoop(BottomBlock, LI); + + ASSERT_EQ(TopBlock, Loop->getTopBlock()); + ASSERT_EQ(BottomBlock, Loop->getBottomBlock()); +} +} // end namespace