The current code in LoopUnswtich::processCurrentLoop() mixes trivial loop unswitch and non-trivial loop unswitch together. It goes over all basic blocks in the loop and checks if a condition is trivial or non-trivial unswitch condition. However, trivial unswitch condition can only occur in the loop header basic block (where it controls whether or not the loop does something at all). This refactoring separate trivial loop unswitch and non-trivial loop unswitch. Before going over all basic blocks in the loop, it checks if the loop header contains a trivial unswitch condition. If so, unswitch it. Otherwise, go over all blocks like before but don't check trivial condition any more since they are not possible to be in the other blocks. This code has no functionality change.
Details
Diff Detail
Event Timeline
Comments inline.
p.s. What's the motivation for this? Is it a step towards a transform or just general code cleanup?
lib/Transforms/Scalar/LoopUnswitch.cpp | ||
---|---|---|
746 | why is the header the only possible trivial unswitch? (I know the answer, but it's not obvious from the comment.) | |
747 | spelling | |
753 | This code can be greatly simplified by inlining the definition of FindLIVLoopCondition and IsTrivialUnswitchCondition. If I'm reading the code right, you'd basically end up with the existing code in isTrivialUnswitchCondition plus getting the condition from the branch and checking that it's loop invariant. To put it differently, the generality of this code is actively confusing, not helping. | |
767 | You're missing the NumBranches and NumSwitches statistic updates here. You might be better off introducing new statistics for the trivial cases for reporting though. |
It's actually both. This separation is needed for further improvement on trivial loop unswitch. However, I think it is still a good cleanup if no future patch is landed based on this.
lib/Transforms/Scalar/LoopUnswitch.cpp | ||
---|---|---|
746 | More comments will be added to address this. | |
753 | I will fix this. | |
767 | I have actually updated NumBranches in the caller of this function. I forgot to separate NumBranches and NumSwitches though. I will redo correctly. Trivial cases are already updated inside UnswitchTrivialCondition function. |
Update w.r.t Philip's comments.
After inlining isTrivialUnswitchCondition() into TryTrivialLoopUnswitch(), isTrivialUnswitchCondition() is basically dead code (no use anywhere). A following patch will be submitted to clean it up.
After inlining isTrivialUnswitchCondition() into TryTrivialLoopUnswitch(), isTrivialUnswitchCondition() is basically dead code (no use anywhere). A following patch will be submitted to clean it up.
Please delete it in this commit. It'll generate a warning and some bots treat warnings as errors.
Really close to ready. I'm not quite willing to give a conditional LGTM, but one more round should do it.
lib/Transforms/Scalar/LoopUnswitch.cpp | ||
---|---|---|
463 | Why? (in the comment) | |
757 | range for loop | |
771 | This doesn't look correct. The old code doesn't unwitch unless the *condition* is itself the LIV condition. The new code might split an and/or and unsplit one part. Doing that as a separate change is debatable, but not in a refactor please. In fact, if you could add a negative test case for this, that would be great. |
Delete IsTrivialUnswitchCondition() and move related comments to TryTrivialLoopUnswitch()
Philip, sorry I didn't see your inlined comments (I was preparing deleting IsTrivialUnswitchCondition() the same time). I will address it with a new update.
Why? (in the comment)