diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -668,7 +668,7 @@ // the logic of the original code, but operates on "shadow" values. It // can benefit from re-running some general purpose optimization // passes. - MPM.addPass(RecomputeGlobalsAAPass()); + MPM.addPass(RequireAnalysisPass()); FunctionPassManager FPM; FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */)); FPM.addPass(InstCombinePass()); @@ -723,10 +723,21 @@ MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles)); } }; - if (ClSanitizeOnOptimizerEarlyEP) - PB.registerOptimizerEarlyEPCallback(SanitizersCallback); - else + if (ClSanitizeOnOptimizerEarlyEP) { + PB.registerOptimizerEarlyEPCallback( + [SanitizersCallback](ModulePassManager &MPM, OptimizationLevel Level) { + ModulePassManager NewMPM; + SanitizersCallback(NewMPM, Level); + if (!NewMPM.isEmpty()) { + // Sanitizers can abandon. + NewMPM.addPass(RequireAnalysisPass()); + MPM.addPass(std::move(NewMPM)); + } + }); + } else { + // LastEP does not need GlobalsAA. PB.registerOptimizerLastEPCallback(SanitizersCallback); + } } void EmitAssemblyHelper::RunOptimizationPipeline(