[llvm] 75790dd - [RemoveDIs] Fix nullptr dereference in getFirstNonPHIIt() (#84595)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 11 16:05:32 PDT 2024


Author: Daniel Sanders
Date: 2024-03-11T16:05:29-07:00
New Revision: 75790dd2d0cff5b0c3e543e256f6c8f0fb5d0689

URL: https://github.com/llvm/llvm-project/commit/75790dd2d0cff5b0c3e543e256f6c8f0fb5d0689
DIFF: https://github.com/llvm/llvm-project/commit/75790dd2d0cff5b0c3e543e256f6c8f0fb5d0689.diff

LOG: [RemoveDIs] Fix nullptr dereference in getFirstNonPHIIt() (#84595)

getFirstNonPHI() returns nullptr for blocks that lack a non-phi
(including a terminator) but getFirstNonPHIIt() may dereference its
result unconditionally. Return end() instead.

This came up for us downstream while correcting our getFirstNonPHI()
calls that intended to return the position after the phi's but before
the debug info to getFirstNonPHIIt(). The pass in question is populating
new BB's and hasn't added terminators yet.

Added: 
    

Modified: 
    llvm/lib/IR/BasicBlock.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp
index 25aa3261164513..c188d2f912d16b 100644
--- a/llvm/lib/IR/BasicBlock.cpp
+++ b/llvm/lib/IR/BasicBlock.cpp
@@ -348,6 +348,8 @@ const Instruction* BasicBlock::getFirstNonPHI() const {
 
 BasicBlock::const_iterator BasicBlock::getFirstNonPHIIt() const {
   const Instruction *I = getFirstNonPHI();
+  if (!I)
+    return end();
   BasicBlock::const_iterator It = I->getIterator();
   // Set the head-inclusive bit to indicate that this iterator includes
   // any debug-info at the start of the block. This is a no-op unless the


        


More information about the llvm-commits mailing list