This is an archive of the discontinued LLVM Phabricator instance.

Restrict the definition of loop preheader to avoid special blocks
ClosedPublic

Authored by andrew.w.kaylor on Jun 21 2017, 4:55 PM.

Details

Summary

This patch modifies the LoopBase::getLoopPreheader() so that it will not identify blocks as preheaders if instructions cannot legally be hoisted into the block. This currently just eliminates special Windows EH blocks to avoid hoisting instructions into funclets.

This change was motivated by a bug a found in loop rotate and the discussion in the comments of that bug (https://bugs.llvm.org/show_bug.cgi?id=33393).

Because getLoopPreheader() is in the LoopBase<BlockT*> template class and we can't examine terminator instructions from there I couldn't simply check the predecessor block's terminator for side effects as suggested. Looking at the uses of getLoopPreheader() it seemed that it is generally used to find a place to hoist instructions, and so I decided to add a method that would check for the legality of doing so.

Diff Detail

Repository
rL LLVM

Event Timeline

majnemer added inline comments.
lib/IR/BasicBlock.cpp
364–371 ↗(On Diff #103492)

It should be impossible to get here if Term has no successors, no? Couldn't we assert(Term->getNumSuccessors > 0)?

Also, could we simplify this by just returning return !Term->IsExceptional() ?

lib/IR/BasicBlock.cpp
364–371 ↗(On Diff #103492)

I wanted to write this as a generic function that could be used from anywhere. It is difficult to imagine why anyone would ever call it for a block without successors, but it seems cheap and easy to give a sensible answer in that case. I don't have strong feelings about it either way. The assertion would serve as a useful flag to callers that they are doing something nonsensical.

You're right that I could use IsExceptional() here. I guess my implementation reflects my thought process while writing the function a little more directly than it ought to.

Addressed initial review feedback

This revision is now accepted and ready to land.Jun 22 2017, 1:49 PM
This revision was automatically updated to reflect the committed changes.
davide edited edge metadata.Jun 22 2017, 4:30 PM

LGTM

llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp
232–234

nit, you can simplify return !isReturnBlock() && !hasEHPadSuccessors()