[llvm] 63c9fe2 - [CodeGen] Fix for MachineBasicBlock::rfindDebugLoc(instr_rend())

Bjorn Pettersson via llvm-commits llvm-commits at lists.llvm.org
Thu May 25 05:50:41 PDT 2023


Author: Bjorn Pettersson
Date: 2023-05-25T14:48:52+02:00
New Revision: 63c9fe2db5cbaa7e068d24472d1aabed084789ce

URL: https://github.com/llvm/llvm-project/commit/63c9fe2db5cbaa7e068d24472d1aabed084789ce
DIFF: https://github.com/llvm/llvm-project/commit/63c9fe2db5cbaa7e068d24472d1aabed084789ce.diff

LOG: [CodeGen] Fix for MachineBasicBlock::rfindDebugLoc(instr_rend())

Make sure we do not crash in rfindDebugLoc when starting at
instr_rend(). Solution is to see it as we start one MI before the
first MI, so we can start searching forward at instr_begin()
instead.

This behavior is similar to how findPrevDebugLoc(instr_end()) works.

Differential Revision: https://reviews.llvm.org/D150577

Added: 
    

Modified: 
    llvm/lib/CodeGen/MachineBasicBlock.cpp
    llvm/unittests/CodeGen/MachineBasicBlockTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp
index 1e923185de2e..6a1d5eea5134 100644
--- a/llvm/lib/CodeGen/MachineBasicBlock.cpp
+++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp
@@ -1474,6 +1474,8 @@ MachineBasicBlock::findDebugLoc(instr_iterator MBBI) {
 }
 
 DebugLoc MachineBasicBlock::rfindDebugLoc(reverse_instr_iterator MBBI) {
+  if (MBBI == instr_rend())
+    return findDebugLoc(instr_begin());
   // Skip debug declarations, we don't want a DebugLoc from them.
   MBBI = skipDebugInstructionsBackward(MBBI, instr_rbegin());
   if (!MBBI->isDebugInstr())

diff  --git a/llvm/unittests/CodeGen/MachineBasicBlockTest.cpp b/llvm/unittests/CodeGen/MachineBasicBlockTest.cpp
index e88b21c2d679..3dd22353100c 100644
--- a/llvm/unittests/CodeGen/MachineBasicBlockTest.cpp
+++ b/llvm/unittests/CodeGen/MachineBasicBlockTest.cpp
@@ -56,9 +56,8 @@ TEST(FindDebugLocTest, DifferentIterators) {
   EXPECT_EQ(DL0, MBB.findDebugLoc(MBB.instr_begin()));
   EXPECT_EQ(DL0, MBB.findDebugLoc(MBB.instr_end()));
 
-  // FIXME: This would crash (see https://reviews.llvm.org/D150577).
-  //EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rbegin()));
-  //EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rend()));
+  EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rbegin()));
+  EXPECT_EQ(DL0, MBB.rfindDebugLoc(MBB.instr_rend()));
 
   EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_begin()));
   EXPECT_EQ(DL0, MBB.findPrevDebugLoc(MBB.instr_end()));
@@ -84,8 +83,7 @@ TEST(FindDebugLocTest, DifferentIterators) {
   EXPECT_EQ(DL3, MBB.findDebugLoc(MI3));
   EXPECT_EQ(DL0, MBB.findDebugLoc(MBB.instr_end()));
 
-  // FIXME: This would crash (see https://reviews.llvm.org/D150577).
-  //EXPECT_EQ(DL1, MBB.rfindDebugLoc(MBB.instr_rend()));
+  EXPECT_EQ(DL1, MBB.rfindDebugLoc(MBB.instr_rend()));
   EXPECT_EQ(DL1, MBB.rfindDebugLoc(MI1));
   EXPECT_EQ(DL3, MBB.rfindDebugLoc(MI2));
   EXPECT_EQ(DL3, MBB.rfindDebugLoc(MI3));


        


More information about the llvm-commits mailing list