Index: include/llvm/Analysis/MustExecute.h =================================================================== --- include/llvm/Analysis/MustExecute.h +++ include/llvm/Analysis/MustExecute.h @@ -48,6 +48,8 @@ bool MayThrow = false; // The current loop contains an instruction which // may throw. bool HeaderMayThrow = false; // Same as previous, but specific to loop header + // The current loop for which this info is calculated. + Loop *CurLoop = nullptr; public: // Used to update funclet bundle operands. @@ -70,6 +72,10 @@ void computeLoopSafetyInfo(Loop *); LoopSafetyInfo() = default; + + LoopSafetyInfo(Loop *L) { + computeLoopSafetyInfo(L); + } }; /// Returns true if the instruction in a loop is guaranteed to execute at least Index: lib/Analysis/MustExecute.cpp =================================================================== --- lib/Analysis/MustExecute.cpp +++ lib/Analysis/MustExecute.cpp @@ -23,14 +23,17 @@ using namespace llvm; bool LoopSafetyInfo::headerMayThrow() const { + assert(CurLoop && "Should calculate the info first!"); return HeaderMayThrow; } bool LoopSafetyInfo::anyBlockMayThrow() const { + assert(CurLoop && "Should calculate the info first!"); return MayThrow; } -void LoopSafetyInfo::computeLoopSafetyInfo(Loop *CurLoop) { +void LoopSafetyInfo::computeLoopSafetyInfo(Loop *L) { + CurLoop = L; assert(CurLoop != nullptr && "CurLoop can't be null"); BasicBlock *Header = CurLoop->getHeader(); // Iterate over header and compute safety info. Index: lib/Transforms/Scalar/LoopIdiomRecognize.cpp =================================================================== --- lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -319,8 +319,7 @@ // The following transforms hoist stores/memsets into the loop pre-header. // Give up if the loop has instructions may throw. - LoopSafetyInfo SafetyInfo; - SafetyInfo.computeLoopSafetyInfo(CurLoop); + LoopSafetyInfo SafetyInfo(CurLoop); if (SafetyInfo.anyBlockMayThrow()) return MadeChange; Index: lib/Transforms/Utils/LoopUnrollAndJam.cpp =================================================================== --- lib/Transforms/Utils/LoopUnrollAndJam.cpp +++ lib/Transforms/Utils/LoopUnrollAndJam.cpp @@ -761,8 +761,7 @@ } // Check the loop safety info for exceptions. - LoopSafetyInfo LSI; - LSI.computeLoopSafetyInfo(L); + LoopSafetyInfo LSI(L); if (LSI.anyBlockMayThrow()) { LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Something may throw\n"); return false;