[PATCH] D136434: [NFC] Fix invalid iterator dereferencing in MachineLoop::getBottomBlock

Sergei Kachkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 21 03:45:10 PDT 2022


kachkov98 updated this revision to Diff 469536.
kachkov98 added a comment.

Add unit test


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136434/new/

https://reviews.llvm.org/D136434

Files:
  llvm/lib/CodeGen/MachineLoopInfo.cpp
  llvm/unittests/CodeGen/CMakeLists.txt
  llvm/unittests/CodeGen/MachineLoopInfoTest.cpp


Index: llvm/unittests/CodeGen/MachineLoopInfoTest.cpp
===================================================================
--- /dev/null
+++ 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<MachineBasicBlock, MachineLoop> 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
Index: llvm/unittests/CodeGen/CMakeLists.txt
===================================================================
--- llvm/unittests/CodeGen/CMakeLists.txt
+++ llvm/unittests/CodeGen/CMakeLists.txt
@@ -27,6 +27,7 @@
   LexicalScopesTest.cpp
   MachineInstrBundleIteratorTest.cpp
   MachineInstrTest.cpp
+  MachineLoopInfoTest.cpp
   MachineOperandTest.cpp
   RegAllocScoreTest.cpp
   PassManagerTest.cpp
Index: llvm/lib/CodeGen/MachineLoopInfo.cpp
===================================================================
--- llvm/lib/CodeGen/MachineLoopInfo.cpp
+++ 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());
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136434.469536.patch
Type: text/x-patch
Size: 2726 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221021/cccd2003/attachment.bin>


More information about the llvm-commits mailing list