Index: lib/CodeGen/PeepholeOptimizer.cpp =================================================================== --- lib/CodeGen/PeepholeOptimizer.cpp +++ lib/CodeGen/PeepholeOptimizer.cpp @@ -1542,8 +1542,20 @@ // If we run into an instruction we can't fold across, discard // the load candidates. - if (MI->isLoadFoldBarrier()) - FoldAsLoadDefCandidates.clear(); + // Better to clear array a bit later - before next loop iteration. + // Then load may be folded into store or call + // or instruction with side effects. + bool IsLoadFoldBarrier = MI->isLoadFoldBarrier(); + struct ScopedFoldLoadsCleaner { + bool Enabled; + SmallSet &Array; + ScopedFoldLoadsCleaner(bool CleanArr, SmallSet &Arr) + : Enabled(CleanArr), Array(Arr) {} + ~ScopedFoldLoadsCleaner() { + if (Enabled) + Array.clear(); + } + } FoldLoadsArrayCleaner(IsLoadFoldBarrier, FoldAsLoadDefCandidates); if (MI->isPosition() || MI->isPHI()) continue;