This is an archive of the discontinued LLVM Phabricator instance.

Simplify conditional branch on constant condition and remove unreachable blocks in LoopUnswitch.
Needs ReviewPublic

Authored by trentxintong on Jan 6 2017, 2:17 PM.

Details

Summary

we can turn the branch with const condition into a unconditional branch.

With this transformation, we can then run a dead block deletion algorithm to remove
unreachable blocks in the loop.

This is useful as the next time we unswitch this loop, we will have fewer
blocks to duplicate and fewer instructions to remap, etc.

This should also give us a better idea of the real unswitch cost of the
loop (even though we still cant use this information right now).

Additionally, its good for us to clean up as soon as possible in loop unswitch,
otherwise we duplicate a lot of dead code which need to be cleaned up by
later passes, this is suboptimal as this leads to more compilation time.

Note: I have not fixed all the LIT tests yet. There are quite a number of them to FIX.
I will upload again again once I fix them. Would like for people to take a look first
while I work on fixing the tests.

Event Timeline

trentxintong retitled this revision from to Simplify conditional branch on constant condition and remove unreachable blocks..
trentxintong updated this object.
trentxintong added a subscriber: llvm-commits.
trentxintong retitled this revision from Simplify conditional branch on constant condition and remove unreachable blocks. to Simplify conditional branch on constant condition and remove unreachable blocks in LoopUnswitch..Jan 6 2017, 2:20 PM
trentxintong updated this object.
efriedma added inline comments.
lib/Transforms/Scalar/LoopUnswitch.cpp
1374

See the comment near the beginning of LoopUnswitch::TryTrivialLoopUnswitch for a description of all the state you aren't updating correctly.

Also, I think you meant "isa<ConstantInt>(BI->getCondition())".

trentxintong added inline comments.Jan 6 2017, 3:44 PM
lib/Transforms/Scalar/LoopUnswitch.cpp
1374

Thanks @efriedma. In llvm, a change in CFG like this, do we usually expect to update the dominator tree dynamic/manually or we are ok with DT->recalculate.

efriedma added inline comments.Jan 6 2017, 3:58 PM
lib/Transforms/Scalar/LoopUnswitch.cpp
1374

LoopUnswitch already calls DT->recalculate... but only once loop, and only if we unswitched that loop. In theory, doing it that way is O(N^2) in the number of loops in the function (since DT->recalculate scans the whole function), so it's something we should fix at some point, it doesn't seem to cause issues in practice. If you want to call it more frequently than that, it could lead to problems.

davidxl edited edge metadata.Feb 1 2017, 3:38 PM

Do you see missed optimizations without the on-the-fly CFG cleanup or is it just for compile time improvement? Do you have data on the impact of compile time with this patch?