[SimplifyCFG][TranformUtils]Do not simplify away a trivial basic block if both this...
authorMingming Liu <mingmingl@google.com>
Mon, 19 Sep 2022 00:33:09 +0000 (17:33 -0700)
committerMingming Liu <mingmingl@google.com>
Wed, 28 Sep 2022 17:48:14 +0000 (10:48 -0700)
commitac28efa6c100cde81304f01b589d44b40b93ffeb
tree47d7eb7f85ea9b72dd12bf29104fc680b3e17488
parent4d06861950978b223f6ebdacee071e8203d0911b
[SimplifyCFG][TranformUtils]Do not simplify away a trivial basic block if both this block and at least one of its predecessors are loop latches.

- Before this patch, loop metadata (if exists) will override the metadata of each predecessor; if the predecessor block already has loop metadata, the orignal loop metadata won't be preserved and could cause missed loop transformations (see 'test2' in llvm/test/Transforms/SimplifyCFG/preserve-llvm-loop-metadata.ll).

To illustrate how inner-loop metadata might be dropped before this patch:

CFG Before

      entry
        |
        v
 ---> while.cond   ------------->  while.end
 |       |
 |       v
 |   while.body
 |       |
 |       v
 |    for.body <---- (md1)
 |       |  |______|
 |       v
 |    while.cond.exit (md2)
 |       |
 |_______|

CFG After

       entry
         |
         v
 ---> while.cond.rewrite  ------------->  while.end
 |       |
 |       v
 |   while.body
 |       |
 |       v
 |    for.body <---- (md2)
 |_______|  |______|

Basically, when 'while.cond.exit' is folded into 'while.cond', 'md2' overrides 'md1' and 'md1' is dropped from the CFG.

Differential Revision: https://reviews.llvm.org/D134152
llvm/lib/Transforms/Utils/Local.cpp
llvm/test/Transforms/SimplifyCFG/preserve-llvm-loop-metadata.ll