Using some SCEV functionality helped to entirely remove SCEVCache class and FindConstantPointers SCEV visitor.
Also, this makes the code more universal - I'll take advandate of it in next patches where I start handling additional types of instructions.
Details
Diff Detail
- Repository
- rL LLVM
Event Timeline
Thanks, Andy!
I also want to wait for a response from Chandler since he put a lot of efforts into this code.
Michael
lib/Transforms/Scalar/LoopUnrollPass.cpp | ||
---|---|---|
304 ↗ | (On Diff #27020) | There is no typo here, but probably I should've added \param: /// \brief Try to simplify instruction \param I using its SCEV expression. |
326 ↗ | (On Diff #27020) | Thanks for the catch! |
- Not only GetElementPtrInst could have a base pointer - allow for that in GEPPointerBases type.
As Andy said, really nice use of SCEV. This is exactly the direction I was hoping for here.
Detailed comments below. Mostly this is about sorting out minor details, naming, and how to cache these things.
lib/Transforms/Scalar/LoopUnrollPass.cpp | ||
---|---|---|
281–288 ↗ | (On Diff #27089) | Based on this (very nice) comment, the name should just be "PointerBases"... Or even better, BasePointers. But see below... |
315 ↗ | (On Diff #27089) | I think a better name for this would be 'simplifyInstWithSCEV' |
326 ↗ | (On Diff #27089) | I'd compute this as part of the constructor (and thus once). |
338–339 ↗ | (On Diff #27089) | For pointer types, is this the byte difference or are the units in something other than bytes? |
340–341 ↗ | (On Diff #27089) | I don't think this quite works. The offset value, while constant, is not the simplification of this instruction. For example, if the offset is 2, and we see 'icmp eq' of I and the constant '2', it shouldn't be able to constant fold. I think you want to store a mapping from instruction to a pair of Value* and Constant*, "BasePointersAndOffsets" or some such. Not sure of a good name here. |
361–362 ↗ | (On Diff #27089) | Is this better than constant folding? It's not clear to me that it is, it seems like it might be much more expensive. I think I would change the *end* of this function to 'if (SimpleV) return true;' and then add a final return that was essentially 'return Base::visitBinaryOperator(I);' so its clear you're just delegating. |
387–389 ↗ | (On Diff #27089) | This shouldn't be needed now, right? |
407–411 ↗ | (On Diff #27089) | The comment above doesn't make much sense any more. =] I'd also consider bailing or asserting if the offset requires lots of bits here rather than limiting it. |
- Rebase on master.
- Rename SCEVSimplify to simplifyInstWithSCEV.
- Use assert + getSExtValue instead of getLimitedValue and remove no
- Introduce SimplifiedOffsets.
- Remove unneeded code.
- Move initialize of IterationNumber to constructor.
- Doxygenify comment for SimplifiedValues.
- Fallback to Base visitor if failed to constant fold (and only then call simplifyInstWithSCEV).
This is looking really good. A bunch of minor stuff below. I'm happy for you to just address most of this before it gets committed, but I don't see the definition of 'simplifyUsingOffsets'. That's the only thing I really want to see before LGTM-ing.
lib/Transforms/Scalar/LoopUnrollPass.cpp | ||
---|---|---|
275 ↗ | (On Diff #27259) | I think this should just be a ConstantInt, it'll save you casting below. |
290–296 ↗ | (On Diff #27259) | I'd call this 'SimplifiedAddresses'. That also corresponds nicely to them being pointers. |
340 ↗ | (On Diff #27259) | I would invert this to use early exit. auto *AR = dyn_cast<SCEVAddRecExpr>(S); if (!AR) return false; ... |
348–349 ↗ | (On Diff #27259) | Should we check that I is a pointer? Shouldn't this be dyn_cast_or_null? |
356–357 ↗ | (On Diff #27259) | If you're going to assert, you can just write 'cast<ConstantInt>' and it will assert for you. I think that means you can just write: SimplifiedOffsets[I] = {Base->getValue(), cast<ConstantInt>(Offset->getValue())}; |
387 ↗ | (On Diff #27259) | Where is this defined? |
410–412 ↗ | (On Diff #27259) | I think I've mentioned this before, but please don't use 'count' and then 'lookup'. It queries the hashtable twice. You should just use 'find' and the iterator. |
Hi Chandler,
Thanks for the review! I'll address the concerns and commit the patch soon. Please also find some comments inline:
lib/Transforms/Scalar/LoopUnrollPass.cpp | ||
---|---|---|
348–349 ↗ | (On Diff #27259) | No, this is correct. |
387 ↗ | (On Diff #27259) | Oops, it crept to the subsequent patch (D10206). I'll move it to this one before committing, but do you mind taking a glance there too? |