<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Different index types in GEPs -> non-aliasing"
   href="https://llvm.org/bugs/show_bug.cgi?id=27418">27418</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Different index types in GEPs -> non-aliasing
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Global Analyses
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>mikael.holmen@ericsson.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16233" name="attach_16233" title="Reproducer ll file">attachment 16233</a> <a href="attachment.cgi?id=16233&action=edit" title="Reproducer ll file">[details]</a></span>
Reproducer ll file

It seems that opt thinks that the two pointers

 %_tmp2 = getelementptr [3 x i16], [3 x i16]* %a, i16 0, i64 1
and
 %_tmp4 = getelementptr [3 x i16], [3 x i16]* %a, i16 0, i16 1

does not alias? Is this intended or a bug?


Details below:
--------------

I found this when I ran opt on:

define i16 @f () {
 %a = alloca [3 x i16]

 ; Write 98 at index 1 in the array.
 ; NB: using i64 as type of the index argument!
 %_tmp2 = getelementptr [3 x i16], [3 x i16]* %a, i16 0, i64 1
 store i16 98, i16* %_tmp2

 ; Read at index 1 in the array
 ; NB: using i16 as type of the index argument!
 %_tmp4 = getelementptr [3 x i16], [3 x i16]* %a, i16 0, i16 1
 %_tmp5 = load i16, i16* %_tmp4

 ; Check read value. If not 98, jump to bb1
 %_tmp6 = icmp ne i16 %_tmp5, 98
 br i1 %_tmp6, label %bb1, label %bb2

bb1:
 ; Error
 ret i16 1;
bb2:
 ; Ok
 ret i16 0;
}

opt -S -gvn foo.ll

and I got the result

; ModuleID = 'foo.ll'

define i16 @f() {
  %a = alloca [3 x i16]
  %_tmp2 = getelementptr [3 x i16], [3 x i16]* %a, i16 0, i64 1
  store i16 98, i16* %_tmp2
  %_tmp4 = getelementptr [3 x i16], [3 x i16]* %a, i16 0, i16 1
  br i1 undef, label %bb1, label %bb2

bb1:                                              ; preds = %0
  ret i16 1

bb2:                                              ; preds = %0
  ret i16 0
}

Note the "undef" in the branch which GVN put there:

GVN iteration: 0
GVN removed:   %_tmp5 = load i16, i16* %_tmp4
GVN removed:   %_tmp6 = icmp ne i16 undef, 98

When digging into why GVN does this, I found this little piece of code in
GVN::AnalyzeLoadAvailability that triggers

  // Loading the allocation -> undef.
  if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
      // Loading immediately after lifetime begin -> undef.
      isLifetimeStart(DepInst)) {
    Res = AvailableValue::get(UndefValue::get(LI->getType()));
    return true;
  }

And the DepInst here is

 %a = alloca [3 x i16]

and not the store
 store i16 98, i16* %_tmp2

Digging further it seems the reason for this is that the alias analysis
considers the two pointers used to store and load the value 98 are
non-aliasing, even if both the pointer and the indices has the same values. The
only difference is the type of the last value.

In e.g. BasicAliasAnalysis.cpp we have

bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
                                                  const Value *V2) {
  if (V != V2)
    return false;

and in

aliasSameBasePointerGEPs:

  // If the last (struct) indices are constants and are equal, the other
indices
  // might be also be dynamically equal, so the GEPs can alias.
  if (C1 && C2 && C1 == C2)
    return MayAlias;

so here we consider two pointers to be completely different just because the
types of the last indices differ.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>