BasicAA depending on PhiValues analysis

Hi all,

I encountered a problem where an optimization that uses BasicAA is very fragile due to BasicAA’s “weak” reliance on the PhiValues analysis.

The whole story is this: our team is in the process of upgrading to the newest version of LLVM, and I discovered that on the newest LLVM, Global Value Numbering doesn’t apply an important optimization anymore.

A little bit of background: GVN depends on Basic Alias Analysis, and BasicAA depends on PhiValues. However, the dependence between GVN and AA is “strong”:


Whereas between BasicAA and PhiValues it is “weak” (note “IfAvailable”):

auto *PVWP = getAnalysisIfAvailable();

So, in the old LLVM, BasicAA that runs before GVN, happens to find cached PhiValues analysis, gives good results to GVN, and GVN applies the optimization. But on the new LLVM, BasicAA doesn’t have PhiValues, gives poor results to GVN, and GVN gives up. It looks to me that whether BasicAA gets its hands on PhiValues is almost entirely up to chance, which makes the clients of BasicAA very fragile, meaning that it’s hard to be sure if an optimization you need will get applied.

I see the discussion in but I’m pretty much out of my depth there and I’m not sure if a solution to the problem above has been mentioned.

Maybe the new pass manager has this problem solved?

Anyhow, I would appreciate any input, thank you in advance.


From what I remember, and from looking at the code, the way things are supposed to work is:

  • GVN uses MemoryDependenceAnalysis
  • MemoryDependenceAnalysis has PhiValuesWrapperPass as a required dependency
  • The alias analysis used by MemoryDependenceAnalysis therefore makes use of PhiValuesWrapperPass

I don’t remember if the alias analysis used by MemoryDependenceAnalysis also getting and
using the result of PhiValuesWrapperPass was an intended consequence of this or not.
Possibly it would make sense for GVN to directly require PhiValuesWrapperPass, which may
fix the problem.



Below is some information for posterity, we have a workaround for our OOT target and, as I’ve been told today in the Alias Analysis monthly meeting, the new pass manager doesn’t have this problem.

Thank you for your suggestion but unfortunately, adding PhiValues as dependency to either GVN or MemDep won’t solve the problem. Analysis passes such as BasicAA have two stages of living:

  1. During the runOnFunction method, and

  2. Querying time.

The decision of whether PhiValues available for BasicAA is made during the first phase:

bool BasicAAWrapperPass::runOnFunction(Function &F) {
auto *PV = getAnalysisIfAvailable();

During querying time, BasicAA checks if PhiValues is available:

bool BasicAAResult::alias(Function &F) {
if (PV)
// Use PhiValues
// Fall back to simpler heuristics

Given the following pass order:

Basic Alias Analysis
Function Alias Analysis
Phi Values Analysis
Function Alias Analysis
Memory Dependence Analysis

Global Value Numbering

BasicAA is run as a prerequisite to MergedLoadStoreMotion and it happens not to have PhiValues available. MergedLoadStoreMotion preserves BasicAA, so runOnFunction is not run again before GVN, so when GVN queries BasicAA, BasicAA doesn’t have access to PhiValues.

A workaround suggested at the Alias Analysis meeting, is to add a dummy pass before GVN whose sole purpose is to invalidate BasicAA.

Thanks everybody for their help,