[llvm] r339773 - [SimplifyCFG] Remove pointer from SmallPtrSet before deletion

Chijun Sima via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 15 06:56:21 PDT 2018


Author: sima
Date: Wed Aug 15 06:56:21 2018
New Revision: 339773

URL: http://llvm.org/viewvc/llvm-project?rev=339773&view=rev
Log:
[SimplifyCFG] Remove pointer from SmallPtrSet before deletion

Summary:
Previously, `eraseFromParent()` calls `delete` which invalidates the value of the pointer. Copying the value of the pointer later is undefined behavior in C++11 and implementation-defined (which may cause a segfault on implementations having strict pointer safety) in C++14.

This patch removes the BasicBlock pointer from related SmallPtrSet before `delete` invalidates it in the SimplifyCFG pass.

Reviewers: kuhar, dmgreen, davide, trentxintong

Reviewed By: kuhar, dmgreen

Subscribers: llvm-commits

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

Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=339773&r1=339772&r2=339773&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Wed Aug 15 06:56:21 2018
@@ -3861,9 +3861,9 @@ bool SimplifyCFGOpt::SimplifySingleResum
   }
 
   // The landingpad is now unreachable.  Zap it.
-  BB->eraseFromParent();
   if (LoopHeaders)
     LoopHeaders->erase(BB);
+  BB->eraseFromParent();
   return true;
 }
 
@@ -4083,9 +4083,9 @@ bool SimplifyCFGOpt::SimplifyReturn(Retu
     // If we eliminated all predecessors of the block, delete the block now.
     if (pred_empty(BB)) {
       // We know there are no successors, so just nuke the block.
-      BB->eraseFromParent();
       if (LoopHeaders)
         LoopHeaders->erase(BB);
+      BB->eraseFromParent();
     }
 
     return true;
@@ -4245,9 +4245,9 @@ bool SimplifyCFGOpt::SimplifyUnreachable
   // If this block is now dead, remove it.
   if (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()) {
     // We know there are no successors, so just nuke the block.
-    BB->eraseFromParent();
     if (LoopHeaders)
       LoopHeaders->erase(BB);
+    BB->eraseFromParent();
     return true;
   }
 




More information about the llvm-commits mailing list