Skip to content

Commit

Permalink
[InstCombine] Add option to clean up some assumptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
fhahn committed Jan 14, 2025
1 parent abfd197 commit 12e0015
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions llvm/include/llvm/Transforms/InstCombine/InstCombine.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct InstCombineOptions {
// Verify that a fix point has been reached after MaxIterations.
bool VerifyFixpoint = false;
unsigned MaxIterations = InstCombineDefaultMaxIterations;
bool CleanupAssumptions = false;

InstCombineOptions() = default;

Expand All @@ -43,6 +44,11 @@ struct InstCombineOptions {
MaxIterations = Value;
return *this;
}

InstCombineOptions &setCleanupAssumptions(bool Value) {
CleanupAssumptions = Value;
return *this;
}
};

class InstCombinePass : public PassInfoMixin<InstCombinePass> {
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,8 @@ Expected<InstCombineOptions> parseInstCombineOptions(StringRef Params) {
ParamName).str(),
inconvertibleErrorCode());
Result.setMaxIterations((unsigned)MaxIterations.getZExtValue());
} else if (ParamName == "cleanup-assumptions") {
Result.setCleanupAssumptions(Enable);
} else {
return make_error<StringError>(
formatv("invalid InstCombine pass parameter '{0}' ", ParamName).str(),
Expand Down
18 changes: 12 additions & 6 deletions llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
FPM.addPass(LoopLoadEliminationPass());
}
// Cleanup after the loop optimization passes.
FPM.addPass(InstCombinePass());
FPM.addPass(
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));

if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) {
ExtraFunctionPassManager<ShouldRunExtraVectorPasses> ExtraPasses;
Expand All @@ -1317,7 +1318,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
// dead (or speculatable) control flows or more combining opportunities.
ExtraPasses.addPass(EarlyCSEPass());
ExtraPasses.addPass(CorrelatedValuePropagationPass());
ExtraPasses.addPass(InstCombinePass());
ExtraPasses.addPass(
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
LoopPassManager LPM;
LPM.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap,
/*AllowSpeculation=*/true));
Expand All @@ -1328,7 +1330,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
/*UseBlockFrequencyInfo=*/true));
ExtraPasses.addPass(
SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));
ExtraPasses.addPass(InstCombinePass());
ExtraPasses.addPass(
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
FPM.addPass(std::move(ExtraPasses));
}

Expand All @@ -1351,7 +1354,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,

if (IsFullLTO) {
FPM.addPass(SCCPPass());
FPM.addPass(InstCombinePass());
FPM.addPass(
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
FPM.addPass(BDCEPass());
}

Expand All @@ -1366,7 +1370,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
FPM.addPass(VectorCombinePass());

if (!IsFullLTO) {
FPM.addPass(InstCombinePass());
FPM.addPass(
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
// Unroll small loops to hide loop backedge latency and saturate any
// parallel execution resources of an out-of-order processor. We also then
// need to clean up redundancies and loop invariant code.
Expand All @@ -1392,7 +1397,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
}

FPM.addPass(InferAlignmentPass());
FPM.addPass(InstCombinePass());
FPM.addPass(
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));

// This is needed for two reasons:
// 1. It works around problems that instcombine introduces, such as sinking
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ FUNCTION_PASS_WITH_PARAMS(
[](InstCombineOptions Opts) { return InstCombinePass(Opts); },
parseInstCombineOptions,
"no-use-loop-info;use-loop-info;no-verify-fixpoint;verify-fixpoint;"
"max-iterations=N")
"max-iterations=N;cleanup-assumptions")
FUNCTION_PASS_WITH_PARAMS(
"loop-unroll", "LoopUnrollPass",
[](LoopUnrollOptions Opts) { return LoopUnrollPass(Opts); },
Expand Down
8 changes: 7 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3226,7 +3226,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
MaybeSimplifyHint(OBU.Inputs[0]);
MaybeSimplifyHint(OBU.Inputs[1]);
}

if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
RetainedKnowledge RK = getKnowledgeFromBundle(
*cast<AssumeInst>(II), II->bundle_op_info_begin()[Idx]);
Expand Down Expand Up @@ -3262,6 +3261,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
return New;
}

// Try to clean up some assumption that are not very useful after this
// point.
if (CleanupAssumptions && OBU.getTagName() == "align") {
auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
return New;
}
}

// Convert nonnull assume like:
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ class User;
class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
: public InstCombiner,
public InstVisitor<InstCombinerImpl, Instruction *> {
bool CleanupAssumptions = false;

public:
InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder,
bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
DominatorTree &DT, OptimizationRemarkEmitter &ORE,
BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
ProfileSummaryInfo *PSI, const DataLayout &DL,
ReversePostOrderTraversal<BasicBlock *> &RPOT)
ReversePostOrderTraversal<BasicBlock *> &RPOT,
bool CleanupAssumptions)
: InstCombiner(Worklist, Builder, MinimizeSize, AA, AC, TLI, TTI, DT, ORE,
BFI, BPI, PSI, DL, RPOT) {}
BFI, BPI, PSI, DL, RPOT),
CleanupAssumptions(CleanupAssumptions) {}

virtual ~InstCombinerImpl() = default;

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5545,7 +5545,7 @@ static bool combineInstructionsOverFunction(
<< F.getName() << "\n");

InstCombinerImpl IC(Worklist, Builder, F.hasMinSize(), AA, AC, TLI, TTI, DT,
ORE, BFI, BPI, PSI, DL, RPOT);
ORE, BFI, BPI, PSI, DL, RPOT, Opts.CleanupAssumptions);
IC.MaxArraySizeForCombine = MaxArraySize;
bool MadeChangeInThisIteration = IC.prepareWorklist(F);
MadeChangeInThisIteration |= IC.run();
Expand Down Expand Up @@ -5594,7 +5594,7 @@ PreservedAnalyses InstCombinePass::run(Function &F,
FunctionAnalysisManager &AM) {
auto &LRT = AM.getResult<LastRunTrackingAnalysis>(F);
// No changes since last InstCombine pass, exit early.
if (LRT.shouldSkip(&ID))
if (LRT.shouldSkip(&ID) && !Options.CleanupAssumptions)
return PreservedAnalyses::all();

auto &AC = AM.getResult<AssumptionAnalysis>(F);
Expand Down

0 comments on commit 12e0015

Please sign in to comment.