<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/122929>122929</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [DependenceAnalysis] Missing dependency detection for instructions accessing same memory location on different iterations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          1997alireza
      </td>
    </tr>
</table>

<pre>
    Dependence analysis is capable of checking the dependency of an instruction with itself but there is a bug there; If the dependency occurs by an instruction through accessing the same memory location in different loop iterations, DA is not able to detect the dependency. For example, the expression `A[i - j]` where `i = j = 0` and `i = j = 1` accesses the same memory location at different iterations, but the current analysis fails to detect this dependency, which will lead to incorrect analysis in some optimization passes such as loop-interchange.
Consider the following code:
```cpp
int main() {
 const int N = 21;
    int A[N] = {0};
    int *B = &A[10];

    for (int i = 1; i <= 10; i++)
        for (int j = 10; j >= 1; j--)
            B[i - j] = 2 * i - j;

    return 0;
}
```
This example can pass the legality check in loop interchange pass based on the information provided by DA, which would give incorrect results if loop interchange was performed. In the original code, the final value is stored in B[0] when `i = j = 10`, so B[0] is set to 10. After loop interchange, the last access to B[0] happens at `i = j = 1`, which incorrectly sets B[0] to 1.
The issue arises from an incorrect reasoning in the depends() API. There are two variables in the depends() API that are involved in this issue. First one is `PossiblyLoopIndependent` which will be set to false if the src and dst cannot have loop-independent dependency. Another variable is `AllEqual` that is computed within the API and is set to true if the dependency vector elements are all `Equals`.
For our case, when we analyse the dependency of the B[i-j] store instruction with itself, PossiblyLoopIndependent is set to false, and AllEqual is true. According to [the API](https://github.com/llvm/llvm-project/blob/19557a4c8fab0dbfe9d9c53b99b7960ef211684e/llvm/lib/Analysis/DependenceAnalysis.cpp#L3970) if AllEqual == True and PossiblyLoopIndependent == false, DA concludes there is no dependency, which is not correct and has caused the bug.

Additionally, by modifying a test case in DA, we can expose the same bug. In [this test case](https://github.com/llvm/llvm-project/blob/19557a4c8fab0dbfe9d9c53b99b7960ef211684e/llvm/test/Analysis/DependenceAnalysis/ExactRDIV.ll#L583), storing to `A[11*i - j]` has no dependency with itself. By modifying the subscript to `A[10*i - j]`, the memory location `A[0]` is accessed in iterations `i = j = 0` and `i = 1, j = 10`. Still, DA does not detect any dependency.

\+ In addition to this bug, it seems that the PossiblyLoopIndependent variable has lost its intended purpose. This flag is always set to true in all cases when depends() is called, hence we may want to reconsider the utility of this variable and possibly remove it from the function signature entirely. We can post a patch for that.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8V0-P47oN_zSaCzGGrYzz55CDM9kAA7w-PLSL9izLdKytIrmSnGzepy9IO4lndgYPvXQR7CQSRZE_kj9SKkZzdIhbUe5EuX9SQ-p82BabzUpZE_BP9VT75rrdY4-uQacRlFP2Gk0EE0GrXtUWwbegO9T_Nu4IqUNobuJX2lIOjIspDDoZ7-BiUgcmRbQt1EOiAwFJm4J6OI4_xWIHb-0vurQeQoT6-lFl6oIfjh0orTHGmxVRnRBOePLhCtZrxaLGQWPaFgO6BNb7HkzCwHtRyFfYV2SK8wnYs-ShwYQ6fbAlg4MPgD_VqbdI52gbf_aB7vcOxDKvRLkz8Aw_RLkXyxwu7KdY5gbEYg8_-P-cdpRrPq4XvM7uYPzaG5Vm3rx3ZIIW9BB49x63Vhkb3zlm4swzOnvpjO7gYqwFi6ohYeO0D4HkHwngIPoTgu-TOZk_R4t6xRbHQXegIiP8bFzCoDvljpiJvHr1LpoGA5vXemv9hUKmfYNiUYm8Est8_Oi-F3llXIKTMk7ItZAbEKudyCvQ3sUEtPc7IyYLseANAF4l-H8X5Z43xWqXi9X-vYSQ1W7clUuSLnKKFItMUq0PIOSahM0UlsWOv77yr5x_Crnjz2Y69eHkFFCWpe_f7op-PD-_P0X_drO0GR0jQ2FcmhsXMA3BQT4trvZz5ERefae4ThkKWo2hYcwtHpU16ToWLcVxLIRHmEbZWkV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