This is an archive of the discontinued LLVM Phabricator instance.

Create utility function to Merge Adjacent Basic Blocks
ClosedPublic

Authored by sidbav on May 26 2020, 12:56 PM.

Details

Summary

The following code from /llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp can be used by other transformations:

while (!MergeBlocks.empty()) {	
    BasicBlock *BB = *MergeBlocks.begin();
    BranchInst *Term = dyn_cast<BranchInst>(BB->getTerminator());	
    if (Term && Term->isUnconditional() && L->contains(Term->getSuccessor(0))) {	
      BasicBlock *Dest = Term->getSuccessor(0);	
      BasicBlock *Fold = Dest->getUniquePredecessor();	
      if (MergeBlockIntoPredecessor(Dest, &DTU, LI)) {	
        // Don't remove BB and add Fold as they are the same BB	
        assert(Fold == BB);	
        (void)Fold;	
        MergeBlocks.erase(Dest);	
      } else	
        MergeBlocks.erase(BB);	
    } else	
      MergeBlocks.erase(BB);	
  }

Hence it should be separated into its own utility function.

Diff Detail

Event Timeline

sidbav created this revision.May 26 2020, 12:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 26 2020, 12:56 PM
asbirlea added inline comments.May 26 2020, 2:49 PM
llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
106

Include a comment that if L is given, the blocks merged into their predecessors must be in L.

A pretty verbose renaming suggestion: MergeBlockSuccessorsIntoGivenBlocks, since this is not merging any adjacent blocks.

If the intent was "if L not given, then MergeBlocks.contains(Dest)", include that in code; this is not a usecase in unrollAndJam, so I cannot tell, but reading the method comment and name this is what I'd assume the method does. Or, rename to something more specific than "adjacentBlocks".

I do not understand this comment:

In addition, a call to MergeBlockIntoPredecessor where
/// the block following the terminal branch is passed must
/// return true.
llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
330

I don't think the UniquePredecessor argument is needed. The method MergeBlockIntoPredecessor will not merge unless Dest has a unique predecessor.

sidbav updated this revision to Diff 266534.May 27 2020, 7:31 AM

Updated the code based on review.

sidbav marked 4 inline comments as done.May 27 2020, 7:33 AM
sidbav added inline comments.
llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
106

Updated comments to include a comment regarding L.
Updated function name to be more verbose, and updated comments so the last sentences makes more sense.

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
330

Yes that is a good point, and the code has been updated to reflect this change.

sidbav marked 2 inline comments as done.May 27 2020, 8:21 AM
asbirlea accepted this revision.May 27 2020, 12:44 PM

Thanks! LGTM with 2 nits in the comment section.

llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
103

Nit: s/branch and if L/branch. If L/

104

This reads as: the call to MergeBlockIntoPredecessor must always return true, which is not the case.
I think what you mean is:

"This utility calls on another utility: MergeBlockIntoPredecessor. Blocks are successfully merged when the call to MergeBlockIntoPredecessor returns true."

This revision is now accepted and ready to land.May 27 2020, 12:44 PM
sidbav updated this revision to Diff 266653.May 27 2020, 1:42 PM

Update function description comments based on review.

sidbav marked 2 inline comments as done.May 27 2020, 1:43 PM

Updated comments based on review.

This revision was automatically updated to reflect the committed changes.