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>