[LLVMdev] Using an alias analysis pass
Daniel Berlin
dberlin at dberlin.org
Tue Apr 21 15:36:38 PDT 2015
On Tue, Apr 21, 2015 at 2:52 PM, Félix Cloutier <felixcca at yahoo.ca> wrote:
> Hello LLVMdev,
>
> I’m using LLVM to do static analysis exclusively (without any code
> generation). To implement this analysis, I’m using multiple address spaces
> to disambiguate the purpose of the pointed memory. Since address spaces
> never alias in my model, I set on to implement an alias analysis pass that
> would exactly provide this information, as I’m seeing a couple of otherwise
> dead store that won’t go away. However, I’m struggling to get it to work.
>
> Four years ago, Tobias Grosser implemented such a pass, so I based mine off
> his. And one year ago, Matthew O’Connor ran into a similar issue, and I
> tried to use the solution that worked for him. The result code is this:
>
> namespace
> {
> struct AddressSpaceAliasAnalysis : public FunctionPass, public AliasAnalysis
> {
> static char ID;
> AddressSpaceAliasAnalysis() : FunctionPass(ID) {
> }
>
>
> virtual bool runOnFunction(Function& f) override
> {
> InitializeAliasAnalysis(this, &f.getParent()->getDataLayout());
> return false;
> }
>
>
> virtual void getAnalysisUsage(AnalysisUsage &AU) const override
> {
> AliasAnalysis::getAnalysisUsage(AU);
> }
>
>
> virtual AliasResult alias(const Location &LocA, const Location &LocB)
> override
> {
> const Value *V1 = LocA.Ptr;
> const Value *V2 = LocB.Ptr;
>
>
> const PointerType *PT1 = dyn_cast<const PointerType>(V1->getType());
> const PointerType *PT2 = dyn_cast<const PointerType>(V2->getType());
>
>
> // The logic here is very simple: pointers to two different address spaces
> // cannot alias.
> if (PT1 != nullptr && PT2 != nullptr)
> {
> if (PT1->getAddressSpace() != PT2->getAddressSpace())
> {
> return NoAlias;
> }
> }
>
>
> return AliasAnalysis::alias(LocA, LocB);
> }
>
>
> virtual void *getAdjustedAnalysisPointer(AnalysisID PI) override
> {
> if (PI == &AliasAnalysis::ID)
> return (AliasAnalysis*)this;
> return this;
> }
> };
> }
>
> // Register this pass...
> char AddressSpaceAliasAnalysis::ID = 0;
>
> static RegisterPass<AddressSpaceAliasAnalysis> aasa("asaa", "NoAlias for
> pointers in different address spaces", false, true);
> static RegisterAnalysisGroup<AliasAnalysis> aag(aasa);
>
> FunctionPass* createAddressSpaceAliasAnalysisPass() {
> return new AddressSpaceAliasAnalysis();
> }
>
>
> I made it a FunctionPass instead of an ImmutablePass because
> InitializeAliasAnalysis now needs a DataLayout argument, and I wasn’t sure
> how to get that one from an ImmutablePass.
See CFLAliasAnalysis.cpp. You can get the datalayout from
doInitialization from the module.
>
> The problem: I can add the pass to a `legacy::PassManager` object, and the
> `runOnFunction` method is called, but `alias` never is, and neither is
> `getAdjustedAnalysisPointer`.
>
> I use a `PassManagerBuilder` object to populate a `legacy::PassManager`,
> which I then run on my module. I tried two different approaches to insert my
> AA pass.
>
> The first was to add the AA pass before calling `populateModulePassManager`,
> since the AA passes are the first ones to be added, and this would put mine
> first. This didn’t help: `runOnFunction` was called, but `alias` never was.
Okay, so without seeing what passes you are adding, i can't say why
alias was never called.
You need to ensure you are adding the pass in the right order.
The first pass added to AA is the last pass queried (IE it's reverse order)
Search the codebase for UseCFLAA
More information about the llvm-dev
mailing list