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

    <tr>
        <th>Summary</th>
        <td>
            [LoopVectorizer] Loop fails to vectorize in presence of inttoptr casts.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            loopoptim,
            vectorization
      </td>
    </tr>

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

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

<pre>
    When there is `inttoptr` cast inside the loop the LoopVectorizer can produce especially bad code, probably because SCEV gives up and returns unknown when it sees such a cast.
This is surprising, considering the equivalent code with the cast moved out of the loop vectorizes just fine (or in this case, gets recognized as a memset). GCC has no issue with such pattern.

Example IR (compile with `opt -O3`):
```ll
define dso_local void @foo(i64 noundef %p, i64 noundef %n, i8 noundef signext %b) local_unnamed_addr #0 {
entry:
  %cmp3 = icmp sgt i64 %n, 0
  br i1 %cmp3, label %for.body, label %for.end

for.body: ; preds = %entry, %for.body
  %i.04 = phi i64 [ %inc, %for.body ], [ 0, %entry ]
  %add = add nsw i64 %i.04, %p
  %0 = inttoptr i64 %add to ptr
  store i8 %b, ptr %0, align 1
  %inc = add nuw nsw i64 %i.04, 1
  %cmp = icmp slt i64 %inc, %n
  br i1 %cmp, label %for.body, label %for.end

for.end: ; preds = %for.body, %entry
  ret void
}

attributes #0 = { nofree noinline norecurse nosync nounwind memory(write, inaccessiblemem: none) uwtable "frame-pointer"="all""min-legal-vector-width"="0""no-trapping-math"="true""stack-protector-buffer-size"="8""target-cpu"="skylake""target-features"="+adx,+aes,+avx,+avx2,+bmi,+bmi2,+clflushopt,+crc32,+cx16,+cx8,+f16c,+fma,+fsgsbase,+fxsr,+invpcid,+lzcnt,+mmx,+movbe,+pclmul,+popcnt,+prfchw,+rdrnd,+rdseed,+sahf,+sgx,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87,+xsave,+xsavec,+xsaveopt,+xsaves" }
```

Godbolt link for examples in C:
https://godbolt.org/z/rY4c1nex6
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVkuP2zYQ_jX0ZWBDpl_SwYesnQ0KFCjQFil6CihyJDFLkSyHsr376wtSstab5lTAsD7O8xty-BBEurWIR7Z7YrvzQgyxc-GohNVG9GZRO_V6_KtDC7HDgKAJ2L7QNkbnY2D7AqSgCNqSVphswDjnM_jVOf8VZXRBv2EAKSz44NQgEZA8Si2MeYVaKJBOIeOnpK5FnYQoxUAIf5w-f4VWX5Bg8CCsgoBxCJZgsC_WXS1cEzUdgRAJaJAdiMxoxYozKz792WlKnGkIPmjStk15pMt0g7ZtZor_DPoiDNqYqcBVxy4rcm29u6ACN0RwzXuFl3tlBN8HitBoi8B46QLoNFmakncuq8VIEFC61uo3VCAIBPTYE0bGqxV8OZ2gEwTWgSYapvy5GC9ixGCnasb_zzfRe4Pwy-8pn3S912byYfvC-QjL3zZsXzBesc3kk4b5Z8woUJgJK3LfjJPCwMVpBWxbNM4xXur9FqwbrMIGGN_5VMYPMptl5SzKfXSLSVUzXkEO-22wVvSovgmlAjC-KYAdnkYKaGN4nRlCcpS93wDbnEHL3gO1MSe9JyvulnUAvb7bJ40RNZokaFxYpZb9jxCtepzD2W7zCdjmCXxARTkz47uRGD99CPhOUq-KbTb1nR757Z6y3MofnIDtzlm0e4Ji0uXgWfEeUSiVA6avpeu96JRo8vIP1sU4Q9MWvBsn3-ggbcrJlKJLG7acVuSUdNk_YWF0a2H9WJaV7ySG68-IrD8u1cNKmXml3mfB_my5_vdqJdHPFusxxrx2U-KAMff1FOtwfgwqYgy6HiLS1Jcp3uEJrGsCIlinrUlbxLqAcgiUEL1amRv-qq1KW9ilRimvQce807UVUiKRrg322CfC1llM22G4RlGbdETwJogel95pGzEwztnmzDgXxiTMea_t0mArzHI8Y5ZXrWI32xWjlXXLGIT32rbLXjzoYxhwNKEo5MvSBxfHOPXQNBiWp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