diff options
author | Matt Arsenault <Matthew.Arsenault@amd.com> | 2017-12-29 19:25:53 +0000 |
---|---|---|
committer | Matt Arsenault <Matthew.Arsenault@amd.com> | 2017-12-29 19:25:53 +0000 |
commit | 0598347e4db4dfa506b9c7d15c17892db4a87cc0 (patch) | |
tree | 5c7cba3d824b84a1ab1a1a59bc23b5c111fb9a7c | |
parent | 6908779027330569d973493b476a2d37c1d6efeb (diff) |
IR: Fix BasicBlock::phis for empty blocks
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321567 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/IR/BasicBlock.cpp | 3 | ||||
-rw-r--r-- | unittests/IR/BasicBlockTest.cpp | 6 |
2 files changed, 8 insertions, 1 deletions
diff --git a/lib/IR/BasicBlock.cpp b/lib/IR/BasicBlock.cpp index 22513924a96..938c40182b9 100644 --- a/lib/IR/BasicBlock.cpp +++ b/lib/IR/BasicBlock.cpp @@ -264,7 +264,8 @@ const BasicBlock *BasicBlock::getUniqueSuccessor() const { } iterator_range<BasicBlock::phi_iterator> BasicBlock::phis() { - return make_range<phi_iterator>(dyn_cast<PHINode>(&front()), nullptr); + PHINode *P = empty() ? nullptr : dyn_cast<PHINode>(&*begin()); + return make_range<phi_iterator>(P, nullptr); } /// This method is used to notify a BasicBlock that the diff --git a/unittests/IR/BasicBlockTest.cpp b/unittests/IR/BasicBlockTest.cpp index f1777e35b82..08a41ff3693 100644 --- a/unittests/IR/BasicBlockTest.cpp +++ b/unittests/IR/BasicBlockTest.cpp @@ -33,6 +33,12 @@ TEST(BasicBlockTest, PhiRange) { std::unique_ptr<BasicBlock> BB2(BasicBlock::Create(Context)); BranchInst::Create(BB.get(), BB2.get()); + // Make sure this doesn't crash if there are no phis. + for (auto &PN : BB->phis()) { + (void)PN; + EXPECT_TRUE(false) << "empty block should have no phis"; + } + // Make it a cycle. auto *BI = BranchInst::Create(BB.get(), BB.get()); |