From e8263f33d93c811bfc26c7d2d2eaaee4196d06be Mon Sep 17 00:00:00 2001 From: Chijun Sima Date: Wed, 15 Aug 2018 13:56:21 +0000 Subject: [PATCH] [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 llvm-svn: 339773 --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index c87b5c1..d88a908 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3861,9 +3861,9 @@ bool SimplifyCFGOpt::SimplifySingleResume(ResumeInst *RI) { } // 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(ReturnInst *RI, IRBuilder<> &Builder) { // 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(UnreachableInst *UI) { // 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; } -- 2.7.4