[llvm] [LoopFlatten] Recognise gep+gep (PR #72515)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 22 06:43:49 PST 2023
================
@@ -207,6 +207,12 @@ struct FlattenInfo {
match(MatchedMul, m_c_Mul(m_Trunc(m_Specific(OuterInductionPHI)),
m_Value(MatchedItCount)));
+ // Matches the pattern ptr+i*M+j, with the two additions being done via GEP.
+ bool IsGEP = match(U, m_GEP(m_GEP(m_Value(), m_Value(MatchedMul)),
----------------
john-brawn-arm wrote:
I've been working on trying to use SCEV here, but it turns out to be pretty hard. We get a SCEV expression that looks like
```
{{%A,+,(4 * %N)}<%for.inner.preheader>,+,4}<%for.inner>
```
There's no longer any explicit mul, instead we have an AddRec for an expression in the inner loop whose start value is the AddRec for the outer loop, and this makes checking that we have something that looks like i*M+j rather hard. I _think_ the equivalent would be using evaluateAtIteration to check the value of this SCEV after one full execution of the inner loop and checking that that's the step value of the outer loop, but then I get problems with the tests in widen-iv.ll where that gives expressions that differ in zext vs sext.
Other problems I noticed:
* If we have something like ``(ptr+k)[i*M+j]`` then the ``+k`` gets hoisted to outside the AddRec, so we have an Add which contains the AddRec, which complicates things. This can probably also happen with other kinds of expressions, but I haven't checked that yet.
* Using SCEV means we can match some things that currently aren't, like multi-dimensional array accesses, which is good but the code in DoFlattenLoopPair can't handle it. I think the solution is to use SCEVExpander, but that causes some incidental changes to the output (e.g. using scevgep which does all pointer arithmetic using i8) and I haven't checked the effects of that.
None of this look insurmountable, but it's more work than I would like to do for my original goal, which was just updating LoopFlatten to match the recent InstCombine change.
https://github.com/llvm/llvm-project/pull/72515
More information about the llvm-commits
mailing list