Skip to content

Commit

Permalink
[InstCombine] Add droppable users back to worklist (NFCI)
Browse files Browse the repository at this point in the history
When sinking and users are dropped, add the using instructions
to the worklist, as they can likely be removed as well.

This should be NFC apart from worklist order effects.
  • Loading branch information
nikic committed May 23, 2023
1 parent 9ac452b commit 18a5bd7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
Instruction *SimplifyAnyMemSet(AnyMemSetInst *MI);

Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);

bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock);
};

class Negator final {
Expand Down
17 changes: 10 additions & 7 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3475,8 +3475,8 @@ static bool SoleWriteToDeadLocal(Instruction *I, TargetLibraryInfo &TLI) {
/// beginning of DestBlock, which can only happen if it's safe to move the
/// instruction past all of the instructions between it and the end of its
/// block.
static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock,
TargetLibraryInfo &TLI) {
bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
BasicBlock *DestBlock) {
BasicBlock *SrcBlock = I->getParent();

// Cannot move control-flow-involving, volatile loads, vaarg, etc.
Expand Down Expand Up @@ -3523,10 +3523,13 @@ static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock,
return false;
}

I->dropDroppableUses([DestBlock](const Use *U) {
if (auto *I = dyn_cast<Instruction>(U->getUser()))
return I->getParent() != DestBlock;
return true;
I->dropDroppableUses([&](const Use *U) {
auto *I = dyn_cast<Instruction>(U->getUser());
if (I && I->getParent() != DestBlock) {
Worklist.add(I);
return true;
}
return false;
});
/// FIXME: We could remove droppable uses that are not dominated by
/// the new position.
Expand Down Expand Up @@ -3699,7 +3702,7 @@ bool InstCombinerImpl::run() {
if (OptBB) {
auto *UserParent = *OptBB;
// Okay, the CFG is simple enough, try to sink this instruction.
if (TryToSinkInstruction(I, UserParent, TLI)) {
if (tryToSinkInstruction(I, UserParent)) {
LLVM_DEBUG(dbgs() << "IC: Sink: " << *I << '\n');
MadeIRChange = true;
// We'll add uses of the sunk instruction below, but since
Expand Down

0 comments on commit 18a5bd7

Please sign in to comment.