From 339daae806b208cad060ce93f63e40f24a311812 Mon Sep 17 00:00:00 2001 From: Mikael Holmen Date: Fri, 15 Mar 2019 13:51:05 +0000 Subject: [CodeGenPrepare] avoid crashing from replacing a phi twice Summary: This is a fix to bug 41052: https://bugs.llvm.org/show_bug.cgi?id=41052 While trying to optimize a memory instruction in a dead basic block, we end up registering the same phi for replacement twice. This patch avoids registering more than the first replacement candidate for a phi. Patch by: JesperAntonsson Reviewers: skatkov, aprantl Reviewed By: aprantl Subscribers: jdoerfert, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D59358 llvm-svn: 356260 --- llvm/lib/CodeGen/CodeGenPrepare.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'llvm/lib/CodeGen') diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index e8498eb375e..44f1709b6e9 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -3282,6 +3282,8 @@ private: PhiNodeSet &PhiNodesToMatch) { SmallVector WorkList; Matcher.insert({ PHI, Candidate }); + SmallSet MatchedPHIs; + MatchedPHIs.insert(PHI); WorkList.push_back({ PHI, Candidate }); SmallSet Visited; while (!WorkList.empty()) { @@ -3314,8 +3316,10 @@ private: if (Matcher.count({ FirstPhi, SecondPhi })) continue; // So the values are different and does not match. So we need them to - // match. - Matcher.insert({ FirstPhi, SecondPhi }); + // match. (But we register no more than one match per PHI node, so that + // we won't later try to replace them twice.) + if (!MatchedPHIs.insert(FirstPhi).second) + Matcher.insert({ FirstPhi, SecondPhi }); // But me must check it. WorkList.push_back({ FirstPhi, SecondPhi }); } -- cgit v1.2.3