Memory leaks in LegacyPassManager depending on order of addRequired passes

Hello,

I notice some strange behavior with the LegacyPassManager. I’ve added a new pass requirement of BlockFrequencyInfoWrapperPass to lib/Transforms/IPO/GlobalOpt.cpp.
However, depending on the order of where I add it, I see memory leak errors with LeakSanitizer.
Having the following order shows the leak:
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired();
AU.addRequired();
AU.addRequired();
AU.addRequired();
}

and changing it to add BlockFrequencyInfoWrapperPass after DominatorTreeWrapperPass fixes the issue.
The leak comes from line 1032 of lib/IR/LegacyPassManager.cpp: Pass *AnalysisPass = PI->createPass();
AnalysisPass is then passed to addLowerLevelRequiredPass, which does not make use of it. It seems that I’m exposing some problem with the LegacyPassManager.

Thanks,
Zaara