diff --git a/include/llvm/ADT/IntervalMap.h b/include/llvm/ADT/IntervalMap.h index b128f31a8d2..dd4eec3c63c 100644 --- a/include/llvm/ADT/IntervalMap.h +++ b/include/llvm/ADT/IntervalMap.h @@ -1235,7 +1235,7 @@ public: if (!branched()) { assert(rootOffset && "Cannot decrement begin()"); --rootOffset; - } else if (treeLeafOffset()) + } else if (valid() && treeLeafOffset()) --treeLeafOffset(); else treeDecrement(); diff --git a/unittests/ADT/IntervalMapTest.cpp b/unittests/ADT/IntervalMapTest.cpp index 5c8b61f278d..c7def84340a 100644 --- a/unittests/ADT/IntervalMapTest.cpp +++ b/unittests/ADT/IntervalMapTest.cpp @@ -352,6 +352,16 @@ TEST(IntervalMapTest, Branched) { EXPECT_FALSE(I.valid()); EXPECT_TRUE(I == map.end()); + // Backwards iteration. + for (unsigned i = 99; i; --i) { + --I; + ASSERT_TRUE(I.valid()); + EXPECT_EQ(10*i, I.start()); + EXPECT_EQ(10*i+5, I.stop()); + EXPECT_EQ(i, *I); + } + EXPECT_TRUE(I == map.begin()); + } } // namespace