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

    <tr>
        <th>Summary</th>
        <td>
            [ValueTracking] Failure to remove unnecessary mask from indvar
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:instcombine,
            missed-optimization
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          RKSimon
      </td>
    </tr>
</table>

<pre>
    https://godbolt.org/z/WM1x4oz5z

Partially reduced test case:
```ll
define i32 @func(ptr nocapture noundef %src, i64 %val, i32 %count) {
entry:
  %cmp10 = icmp ult i32 %count, 32
 br i1 %cmp10, label %for.body.preheader, label %for.cond.cleanup

for.body.preheader:                               ; preds = %entry
  %0 = trunc i64 %val to i32
  br label %for.body

for.cond.cleanup:                                 ; preds = %for.body, %entry
  ret i32 0

for.body: ; preds = %for.body, %for.body.preheader
  %indvars.iv = phi i32 [ %count, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
  %1 = sext i32 %indvars.iv to i64
  %arrayidx = getelementptr inbounds [32 x [2 x i32]], ptr %src, i64 0, i64 %1
  %arrayidx.1 = getelementptr inbounds [32 x [2 x i32]], ptr %src, i64 0, i64 %1, i64 1
  %add.i = add i32 %indvars.iv, %0
  store i32 %add.i, ptr %arrayidx, align 4
  %and2.i = and i32 %indvars.iv, 31
  store i32 %and2.i, ptr %arrayidx.1, align 4
 %indvars.iv.next = add nuw nsw i32 %indvars.iv, 1
  %exitcond.not = icmp eq i32 %indvars.iv.next, 32
  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
}
```
We fail to remove the %and2.i mask despite knowing that %count is less than 32 and that the loop ends when %indvars.iv.next reaches 32
```asm
func: # @func
        cmpl    $31, %edx
        ja .LBB0_3
        movslq  %edx, %rax
        leaq    4(%rdi,%rax,8), %rax
.LBB0_2:                                # %for.body
        leal (%rsi,%rdx), %ecx
        movl    %ecx, -4(%rax)
        movl    %edx, %ecx
        andl    $31, %ecx     <- UNNECESSARY AND + MOV
 movl    %ecx, (%rax)
        incl    %edx
        addq    $8, %rax
 cmpl    $32, %edx
        jne     .LBB0_2
.LBB0_3: # %for.cond.cleanup
        xorl    %eax, %eax
 retq
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vk2T2ygT_jX4Qo1KQpJHPvjgj_jyvsluZXaT2tMWhrZNBoEGkMczv34L9GHZ0iR7WVcqGkF3P08_3bSg1oqjAliifI3y7YzW7qTN8uv_nkSp1Wyv-dvy5FxlUbpCZIfI7qj5XksXaXNEZPeOyO775-SS6ff8HcVbFK-a_3-nxgkq5Rs2wGsGHDuwDjNqwYdqLOdx80_KZoHDQSjAIiUYZfGhVgyRonIGK81o5WoDWOlacThgRHJrGCIbLOaZfztTGd68L8mZrpVDZIHR47qJDcqZtx4aB6OySmKM0i0WrKxwLd2d-wanpLXfGyyS3slvSboH6VcO2kReqKgycALKwYy2mVY8YhKoqquhShOu6Qr__IfSNa4McBuYI5I3mV3TalJyplZsIA522mfX2e3NOIF7Zjesf8lrglkfmGxGRA00asdTeni0X0Wb0O6qgVD8TI2NxDl4VyfRlDZf35R3MgxG-TZsNsbXUJGCy71bML7iJgHOwqVvpQETX4F5NrCmxtA3wS_B6QgOJJSgnO94ofa-061nkRJ88U__8CXMty1Db3h7EOLBiUgmkKLkv8Jq_74B5TwSAY9yPtajVTLuPKzTBjqz4DvA7RLwS1SKo8I3QipOOij1AVSaTAMF1wmkKBljjduhT0_Vr1jZ12nsoSpwES6cLKXddfjAy9iz77ebY9uMoWGUn4-bqVHVnrnH7d0gbl6_Az5QESaGgVKfAbsTDGQuqX3GHGwlHOBnpV-FOmJ3oq4_W1hYLMFav6pwSkJVgoWPJLWuMPiGez2BmhLVAGUnsH3iPUFqy3ZS-I-DnxIk7b8VrUbNj5WVDEOJZGnSDSB-uTX6QXH0__U6_ju9XS_12coX3Pk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