OrenIshShalom
OrenIshShalom

Reputation: 7112

Using the results of LLVM alias analysis from an LLVM loop pass

I have an LLVM loop pass, and I need to check whether two values may alias to one another. If I first run an alias analysis pass, and then the loop pass, how can I query the results of the AA pass? It should probably be something similar to:

virtual bool runOnLoop(Loop *loop, LPPassManager &LPM)
{
    Value *v1 = getValueSomehow();
    Value *v2 = getValueSomeOtherWay();

    // EDITED with compor's answer
    AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();

    if (AA->isNoAlias(v1,v2))
    {
        errs() << "OK";
    }
}

However when I run it with this opt command (I put the aa pass first)

opt            \
-basicaa       \
-loop-simplify \
-instnamer     \
-indvars       \
-simplifycfg   \
-view-cfg      \
-o input.ready.bc input.bc

I get this runtime error:

opt: PassAnalysisSupport.h:236: [...]:
Assertion `ResultPass && "getAnalysis*() called on an analysis that was not " "'required' by pass!"' failed.
...
Aborted (core dumped)

EDIT:

It does not suffice to simply put the -basicaa pass first to opt. Indeed, like compor said, one has to override the getAnalysisUsage, and explicitly say which passes are required.

struct StaticAnalyzer : public LoopPass
{
    static char ID;
    StaticAnalyzer():LoopPass(ID){}

    void getAnalysisUsage(AnalysisUsage &AU) const override
    {
        AU.addRequired<AAResultsWrapperPass>();
    }
    // ...
};

Upvotes: 0

Views: 1202

Answers (1)

compor
compor

Reputation: 2329

When in doubt do as LLVM does; for example, in this case have a look in llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp.

Using the llvm::legacy::PassManager this is done by adding the Alias Analysis as a requirement of your pass:

void getAnalysisUsage(AnalysisUsage &AU) const override {
    AU.addRequired<AAResultsWrapperPass>();
    [...]
}

and then accessing the analysis results in the run() method:

AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();

Also, note that you can "plug" and combine various alias analyses (plural) via the commandline (mentioned here), e.g.

opt -basicaa [...]

Upvotes: 1

Related Questions