Something must be wrong, more probable on my side. So the C source code is unchanged, I just did another experiment to first extract all the GEPs in the code, and call AliasAnalysis::alias on each pair of GEPs. Here is the code:<br>
<br><span style="font-family:courier new,monospace">      AliasAnalysis &AA = getAnalysis<AliasAnalysis>();</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">      TargetData &TD = getAnalysis<TargetData>();</span><br style="font-family:courier new,monospace">
<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">      for (Module::iterator it = M.begin(); it != M.end(); it++){</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        Function &F = *it;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">        errs().write_escaped(F.getName()) << '\n';</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">          Instruction *Inst = &*I;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          if ( GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(Inst) )</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">            geps.push_back(gep);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        }</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">      }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">      errs() << "List of GEPs:\n";</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">      for(std::list<GetElementPtrInst* >::iterator itGep1</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">            = geps.begin(); itGep1 != geps.end(); itGep1++){</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">        GetElementPtrInst *gep1 = *itGep1;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        Type *t1 = ((PointerType *)gep1->getType())->getElementType();</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">        int64_t size1 = TD.getTypeAllocSize(t1);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        std::list<GetElementPtrInst* >::iterator itGep2 = itGep1;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">        for(; itGep2 != geps.end(); itGep2++){</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          GetElementPtrInst *gep2 = *itGep2;</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">          gep1->dump();</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          errs() << "-->";</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">          gep2->dump();</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          </span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">          Type *t2 = ((PointerType *)gep2->getType())->getElementType();</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          int64_t size2 = TD.getTypeAllocSize(t2);</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">          AliasAnalysis::AliasResult aa = AA.alias(gep1, size1, </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          errs() << "   alias: " << aa << "\n";</span><br style="font-family:courier new,monospace">
<span style="font-family:courier new,monospace">        }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">      }</span><br><br><br>Here is the result:<br>  %9 = getelementptr inbounds i32* %8, i32 %7<br>
-->  %9 = getelementptr inbounds i32* %8, i32 %7<br>   alias: 1<br>  %9 = getelementptr inbounds i32* %8, i32 %7<br>-->  %13 = getelementptr inbounds i32* %12, i32 %11<br>   alias: 1<br>  %9 = getelementptr inbounds i32* %8, i32 %7<br>
-->  %18 = getelementptr inbounds i32* %17, i32 %16<br>   alias: 1<br>  %9 = getelementptr inbounds i32* %8, i32 %7<br>-->  %8 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 %7<br>   alias: 1<br>  %9 = getelementptr inbounds i32* %8, i32 %7<br>
-->  %13 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 0<br>   alias: 1<br>  %9 = getelementptr inbounds i32* %8, i32 %7<br>-->  %14 = getelementptr inbounds [10 x i32]* %b, i32 0, i32 0<br>   alias: 1<br>  %13 = getelementptr inbounds i32* %12, i32 %11<br>
-->  %13 = getelementptr inbounds i32* %12, i32 %11<br>   alias: 1<br>  %13 = getelementptr inbounds i32* %12, i32 %11<br>-->  %18 = getelementptr inbounds i32* %17, i32 %16<br>   alias: 1<br>  %13 = getelementptr inbounds i32* %12, i32 %11<br>
-->  %8 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 %7<br>   alias: 1<br>  %13 = getelementptr inbounds i32* %12, i32 %11<br>-->  %13 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 0<br>   alias: 1<br>  %13 = getelementptr inbounds i32* %12, i32 %11<br>
-->  %14 = getelementptr inbounds [10 x i32]* %b, i32 0, i32 0<br>   alias: 1<br>  %18 = getelementptr inbounds i32* %17, i32 %16<br>-->  %18 = getelementptr inbounds i32* %17, i32 %16<br>   alias: 1<br>  %18 = getelementptr inbounds i32* %17, i32 %16<br>
-->  %8 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 %7<br>   alias: 1<br>  %18 = getelementptr inbounds i32* %17, i32 %16<br>-->  %13 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 0<br>   alias: 1<br>  %18 = getelementptr inbounds i32* %17, i32 %16<br>
-->  %14 = getelementptr inbounds [10 x i32]* %b, i32 0, i32 0<br>   alias: 1<br>  %8 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 %7<br>-->  %8 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 %7<br>   alias: 1<br>
  %8 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 %7<br>-->  %13 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 0<br>   alias: 1<br>  %8 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 %7<br>-->  %14 = getelementptr inbounds [10 x i32]* %b, i32 0, i32 0<br>
   alias: 1<br>  %13 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 0<br>-->  %13 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 0<br>   alias: 1<br>  %13 = getelementptr inbounds [10 x i32]* %a, i32 0, i32 0<br>
-->  %14 = getelementptr inbounds [10 x i32]* %b, i32 0, i32 0<br>   alias: 1<br>  %14 = getelementptr inbounds [10 x i32]* %b, i32 0, i32 0<br>-->  %14 = getelementptr inbounds [10 x i32]* %b, i32 0, i32 0<br>   alias: 1<br>
<br>How can be each pair MayAlias? That's simply wrong! For example:   <br><br>%13 = getelementptr inbounds [10 x i32]* <b>%a</b>, i32 0, i32 0<br>
-->  %14 = getelementptr inbounds [10 x i32]* <b>%b</b>, i32 0, i32 0<br>Must not alias,<br><br>while<br>  %14 = getelementptr inbounds [10 x i32]* <b>%b</b>, i32 0, i32 0<br>
-->  %14 = getelementptr inbounds [10 x i32]* <b>%b</b>, i32 0, i32 0<br>
Must alias.<br><br>Can someone help identifying where is wrong with my code?<br><br><br>Thanks!<br>Welson<br><br><br><br><br><br><br><br><br><br><div class="gmail_quote">On Tue, Feb 14, 2012 at 5:12 PM, Welson Sun <span dir="ltr"><<a href="mailto:welson.sun@gmail.com">welson.sun@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Just want to test out the LLVM's AliasAnalysis::getModRefInfo API. The input C code is very simple:<br><br><span style="font-family:courier new,monospace">void foo(int *a, int *b)</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">{</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">  for(int i=0; i<10; i++)</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">    b[i] = a[i]*a[i];</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">}</span><br style="font-family:courier new,monospace">

<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">int main()</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">{</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">  int a[10];</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">  int b[10];</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">  </span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">  for(int i=0; i<10; i++)</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">    a[i] = i;</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">  foo(a,b);</span><br style="font-family:courier new,monospace">

<br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">  return 0;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">}</span><br style="font-family:courier new,monospace">

<br>Obviously, for "foo", it only reads from array "a" and only writes to array "b".<br><br>The LLVM pass:<br><span style="font-family:courier new,monospace">    virtual bool runOnFunction(Function &F) {</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">      ++HelloCounter;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">      errs() << "Hello: ";</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">      errs().write_escaped(F.getName()) << '\n';</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">      AliasAnalysis &AA = getAnalysis<AliasAnalysis>();</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">      for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">        Instruction *Inst = &*I;</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">        if ( CallInst *ci = dyn_cast<CallInst>(Inst) ){</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          ci->dump();</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">          for(int i = 0; i < ci->getNumArgOperands(); i++){</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">            Value *v = ci->getArgOperand(i);</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">            if (GetElementPtrInst *vi = dyn_cast<GetElementPtrInst>(v)){</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">              Value *vPtr = vi->getPointerOperand();</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">              vPtr->dump();</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">              if ( AllocaInst *allo = dyn_cast<AllocaInst>(vPtr) ) {</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">                const Type *t = allo->getAllocatedType();</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">                if ( const ArrayType *at = dyn_cast<ArrayType>(t) ) {</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">                  int64_t size = at->getNumElements() * at->getElementType()->getPrimitiveSizeInBits() / 8;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">                  ImmutableCallSite cs(ci);</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">                  AliasAnalysis::Location loc(v, size);</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">                  errs() << AA.getModRefInfo(ci, loc) << "\n";</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">                }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">              }</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">            }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">          }</span><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">        }</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">      }</span><br style="font-family:courier new,monospace"><br style="font-family:courier new,monospace">

<span style="font-family:courier new,monospace">      return false;</span><br style="font-family:courier new,monospace"><span style="font-family:courier new,monospace">    }</span><br style="font-family:courier new,monospace">

<br><br>However, the result is "3" for both a and b, which is both read and write. What's the problem? I am not quite sure if I get the AliasAnalysis::Location right, what is exactly "address-units" for the size of the location? And did I get the starting address of the Location right? I tried v, vi and vPtr, same result.<br>

<br><br>Any insight helps,<br>Welson<br>
</blockquote></div><br><br clear="all"><br>-- <br>Welson<div><br></div><div>Phone: (408) 418-8385</div><div>Email:  <a href="mailto:welson.sun@gmail.com" target="_blank">welson.sun@gmail.com</a></div><div><br></div><br>