From d99f3bacb4b92847e8fae3be4cab4acbe107df52 Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Wed, 23 May 2018 10:09:53 +0000 Subject: [LoopUnswitch] Fix SCEV invalidation in unswitching Loop unswitching makes substantial changes to a loop that can also affect cached SCEV info in its outer loops as well, but it only cares to invalidate SCEV cache for the innermost loop in case of full unswitching and does not invalidate anything at all in case of trivial unswitching. As result, we may end up with incorrect data in cache. Differential Revision: https://reviews.llvm.org/D46045 Reviewed By: mzolotukhin llvm-svn: 333072 --- llvm/lib/Transforms/Scalar/LoopUnswitch.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Transforms/Scalar/LoopUnswitch.cpp') diff --git a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp index b530f7c5dbd..4aae2bfc89c 100644 --- a/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/LoopUnswitch.cpp @@ -975,6 +975,10 @@ void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, << " blocks] in Function " << L->getHeader()->getParent()->getName() << " on cond: " << *Val << " == " << *Cond << "\n"); + // We are going to make essential changes to CFG. This may invalidate cached + // information for L or one of its parent loops in SCEV. + if (auto *SEWP = getAnalysisIfAvailable()) + SEWP->getSE().forgetTopmostLoop(L); // First step, split the preheader, so that we know that there is a safe place // to insert the conditional branch. We will change loopPreheader to have a @@ -1201,8 +1205,10 @@ void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val, << " blocks] in Function " << F->getName() << " when '" << *Val << "' == " << *LIC << "\n"); + // We are going to make essential changes to CFG. This may invalidate cached + // information for L or one of its parent loops in SCEV. if (auto *SEWP = getAnalysisIfAvailable()) - SEWP->getSE().forgetLoop(L); + SEWP->getSE().forgetTopmostLoop(L); LoopBlocks.clear(); NewBlocks.clear(); -- cgit v1.2.3