Alias analysis result

Hello everyone,
I am trying to find the alias between a store instruction’s pointer operand and function arguments. This is the code,
virtual void getAnalysisUsage(AnalysisUsage &AU) const {

AU.addRequiredTransitive();
AU.addPreserved();
}
virtual bool runOnFunction(Function &F) {

AliasAnalysis &AA = getAnalysis();

for(Function::iterator i=F.begin();i!=F.end();++i){
for(BasicBlock::iterator j=i->begin();j!=i->end();++j)
{

if(dyn_cast(j)){

const StoreInst *SI=dyn_cast(j);

AliasAnalysis::Location LocA = AA.getLocation(SI);

const Value *si_v= SI->getPointerOperand();

for(Function::arg_iterator k=F.arg_begin(); k!=F.arg_end();++k)
{

Value *v=dyn_cast(k);

AliasAnalysis::Location loc=AliasAnalysis::Location(v);
AliasAnalysis::AliasResult ar=AA.alias(LocA,loc);

switch(ar)
{
case 0:errs()<< “NoAlias\n”;
break;
///< No dependencies.
case 1:errs()<<“MayAlias\n”; ///< Anything goes
break;
case 2: errs()<<“PartialAlias\n”;///< Pointers differ, but pointees overlap.
break;

case 3: errs()<<“MustAlias\n”;
}

}
}

return true;
}
};
}

But I get MayAlias result even if the store instruction’s pointer operand is not referencing the function argument. Is there something wrong with the logic? Are there any files in the LLVM source code that contain code to do something similar.
Thanks:)

Hi Borya,

But I get MayAlias result even if the store instruction's pointer operand is not
referencing the function argument. Is there something wrong with the logic? Are
there any files in the LLVM source code that contain code to do something similar.

the default alias analysis is no-aa which does what the name suggests :slight_smile: Did
schedule basic-aa (for example) before your pass?

Ciao, Duncan.

That's the reason I have defined getAnalysisUsage method. Isn't that the
right way to do it?

borya043 wrote:

Hi,

That's the reason I have defined getAnalysisUsage method. Isn't that the
right way to do it?

no, that gives you access to whatever alias analysis has been computed, but
it doesn't specify what kind of alias analysis should be computed (there are
several). Try something like this:
   opt -load=my_pass.so -basic-aa -run_my_pass ...
Ciao, Duncan.

Hi Duncan,

In case “my_pass” is compiled into a standalone executable, how can I run the specific alias analysis in the code?

Thanks

Hi Welson,

In case "my_pass" is compiled into a standalone executable, how can I run the
specific alias analysis in the code?

I suggest you work out what passing -basic-aa to opt does, how it works, and do
the same in your code.

Ciao, Duncan.

OK, that’s actually quite straightforward, I will just share it here:

  1. In “my_pass”, you need this:

void getAnalysisUsage(AnalysisUsage &Info) const {
Info.addRequired();
Info.setPreservesAll();
}

  1. In the main() function, add this:

PassRegistry &Registry = *PassRegistry::getPassRegistry();
initializeBasicAliasAnalysisPass(Registry);

You can replace “BasicAliasAnalysis” with other alias analysis classes, refer to llvm/InitializePasses.h for the list.

  1. Finally, anywhere in your code, you can write this:

AliasAnalysis &AA = getAnalysis();

Hope this helps,
Welson