Replace pattern-matching with existing SCEV and Loop APIs as a more
robust way of identifying the loop increment and trip count. Also
rename 'Limit' as 'TripCount' to be consistent with terminology.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
llvm/lib/Transforms/Scalar/LoopFlatten.cpp | ||
---|---|---|
134 | I am not sure if an induction phi can have > 2 incoming values. If it can then the assert needs to be a return, and a test case is missing for this. But if getNumIncomingValues() == 2 is guaranteed by getInductionVariable(), then this is fine. Can you double check by looking at getInductionVariable()? | |
155–156 | Just for clarity, I was wondering if we should rename limit to trip count, because that is what it is at the moment with the restriction that our loops start counting at 0. I think that makes things clearer now that we start using SCEV and request trip counts. |
- Removed assert on the number of incoming phi values
- Rename 'Limit' to 'TripCount' for consistency
llvm/lib/Transforms/Scalar/LoopFlatten.cpp | ||
---|---|---|
134 | This assert was actually already there, I just moved it up to be where the induction phi is identified. But I agree, since the loop is in simplified form it will only have 2 incoming values so I'll remove this assert. |
llvm/lib/Transforms/Scalar/LoopFlatten.cpp | ||
---|---|---|
74 | Perhaps we can document some assumptions and limitations here. | |
79 | This corresponds to a loop induction variable, which we expect to have a start value of 0. | |
155–156 | Perhaps I have a slight preference here for: f (!L->isCanonical(*SE)) return false; so we don't need the Increment = nullptr; here. | |
155–156 | Then, this can just be: if (Increment->hasNUsesOrMore(3)) | |
155–156 | Ah, nice one, isCanonical checks exactly what we were assuming and pattern matching: it checks that the loop starts at 0 and increments by 1. Do we need the isZero() check above? |
- Added comment about induction variable assumptions
- Moved isCanonical check on loop to be higher so we exit earlier if loop is not canonical
- Removed Increment = nullptr
- Removed code which determines trip count in the case where another transformation changes the compare, and return false instead (and added negative test case for this)
llvm/lib/Transforms/Scalar/LoopFlatten.cpp | ||
---|---|---|
165 | Can this be !=? | |
165–190 | We are not doing this sanity check when the IV is widened. ExtTC = Tripcount->getOperand(0); if (!isa<ZExt>(ExtTC) || !isa<SExt>(ExtTC) || SE->getSCEV(ExtTC) != SCEVTripCount ) { LLVM_DEBUG(dbgs() << "Could not find valid extended trip count\n"); return false; } |
llvm/lib/Transforms/Scalar/LoopFlatten.cpp | ||
---|---|---|
109 | Ok sure, there is also a note mentioning this in lines 77/78. I will add some clarification that this is to simplify the implementation before committing. | |
160 | No, we don't rely on it being an Add instruction. Using SCEV, it is now guaranteed that the loop starts at 0 and increments with 1. Most of the pattern matching that we replaced was to ensure that. The increment is also marked as an "IterationInstruction", but that's only for cost-modeling purposes and it doesn't rely on Adds. |
Perhaps we can document some assumptions and limitations here.