Skip to content

Commit

Permalink
[InstCombine] Move worklist preparation into InstCombinerImpl (NFC)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 31, 2023
1 parent 933fde3 commit 09156b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H

#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/TargetFolder.h"
#include "llvm/Analysis/ValueTracking.h"
Expand Down Expand Up @@ -73,6 +74,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final

virtual ~InstCombinerImpl() = default;

/// Perform early cleanup and prepare the InstCombine worklist.
bool prepareWorklist(Function &F,
ReversePostOrderTraversal<BasicBlock *> &RPOT);

/// Run the combiner over the entire worklist until it is empty.
///
/// \returns true if the IR is changed.
Expand Down
24 changes: 9 additions & 15 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
Expand Down Expand Up @@ -4119,11 +4118,8 @@ class AliasScopeTracker {
/// them to the worklist (this significantly speeds up instcombine on code where
/// many instructions are dead or constant). Additionally, if we find a branch
/// whose condition is a known constant, we only visit the reachable successors.
static bool
prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
const TargetLibraryInfo *TLI,
InstructionWorklist &ICWorklist,
ReversePostOrderTraversal<BasicBlock *> &RPOT) {
bool InstCombinerImpl::prepareWorklist(
Function &F, ReversePostOrderTraversal<BasicBlock *> &RPOT) {
bool MadeIRChange = false;
SmallPtrSet<BasicBlock *, 32> LiveBlocks;
LiveBlocks.insert(&F.front());
Expand All @@ -4140,12 +4136,12 @@ prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
// ConstantProp instruction if trivially constant.
if (!Inst.use_empty() &&
(Inst.getNumOperands() == 0 || isa<Constant>(Inst.getOperand(0))))
if (Constant *C = ConstantFoldInstruction(&Inst, DL, TLI)) {
if (Constant *C = ConstantFoldInstruction(&Inst, DL, &TLI)) {
LLVM_DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " << Inst
<< '\n');
Inst.replaceAllUsesWith(C);
++NumConstProp;
if (isInstructionTriviallyDead(&Inst, TLI))
if (isInstructionTriviallyDead(&Inst, &TLI))
Inst.eraseFromParent();
MadeIRChange = true;
continue;
Expand All @@ -4159,7 +4155,7 @@ prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
auto *C = cast<Constant>(U);
Constant *&FoldRes = FoldedConstants[C];
if (!FoldRes)
FoldRes = ConstantFoldConstant(C, DL, TLI);
FoldRes = ConstantFoldConstant(C, DL, &TLI);

if (FoldRes != C) {
LLVM_DEBUG(dbgs() << "IC: ConstFold operand of: " << Inst
Expand Down Expand Up @@ -4227,11 +4223,11 @@ prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
// of the function down. This jives well with the way that it adds all uses
// of instructions to the worklist after doing a transformation, thus avoiding
// some N^2 behavior in pathological cases.
ICWorklist.reserve(InstrsForInstructionWorklist.size());
Worklist.reserve(InstrsForInstructionWorklist.size());
for (Instruction *Inst : reverse(InstrsForInstructionWorklist)) {
// DCE instruction if trivially dead. As we iterate in reverse program
// order here, we will clean up whole chains of dead instructions.
if (isInstructionTriviallyDead(Inst, TLI) ||
if (isInstructionTriviallyDead(Inst, &TLI) ||
SeenAliasScopes.isNoAliasScopeDeclDead(Inst)) {
++NumDeadInst;
LLVM_DEBUG(dbgs() << "IC: DCE: " << *Inst << '\n');
Expand All @@ -4241,7 +4237,7 @@ prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
continue;
}

ICWorklist.push(Inst);
Worklist.push(Inst);
}

return MadeIRChange;
Expand Down Expand Up @@ -4289,12 +4285,10 @@ static bool combineInstructionsOverFunction(
LLVM_DEBUG(dbgs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
<< F.getName() << "\n");

bool MadeChangeInThisIteration =
prepareICWorklistFromFunction(F, DL, &TLI, Worklist, RPOT);

InstCombinerImpl IC(Worklist, Builder, F.hasMinSize(), AA, AC, TLI, TTI, DT,
ORE, BFI, PSI, DL, LI);
IC.MaxArraySizeForCombine = MaxArraySize;
bool MadeChangeInThisIteration = IC.prepareWorklist(F, RPOT);
MadeChangeInThisIteration |= IC.run();
if (!MadeChangeInThisIteration)
break;
Expand Down

0 comments on commit 09156b3

Please sign in to comment.