diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombine.h b/llvm/include/llvm/Transforms/InstCombine/InstCombine.h index c12d749709cd255..dd9d05cbbcf7c97 100644 --- a/llvm/include/llvm/Transforms/InstCombine/InstCombine.h +++ b/llvm/include/llvm/Transforms/InstCombine/InstCombine.h @@ -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; @@ -43,6 +44,11 @@ struct InstCombineOptions { MaxIterations = Value; return *this; } + + InstCombineOptions &setCleanupAssumptions(bool Value) { + CleanupAssumptions = Value; + return *this; + } }; class InstCombinePass : public PassInfoMixin { diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp index a936f5381137c63..6910c7977c296f0 100644 --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -915,6 +915,8 @@ Expected parseInstCombineOptions(StringRef Params) { ParamName).str(), inconvertibleErrorCode()); Result.setMaxIterations((unsigned)MaxIterations.getZExtValue()); + } else if (ParamName == "cleanup-assumptions") { + Result.setCleanupAssumptions(Enable); } else { return make_error( formatv("invalid InstCombine pass parameter '{0}' ", ParamName).str(), diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp index d737ea5ab070a90..fc47ac9d793d189 100644 --- a/llvm/lib/Passes/PassBuilderPipelines.cpp +++ b/llvm/lib/Passes/PassBuilderPipelines.cpp @@ -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 ExtraPasses; diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def index 9f0b09278edcca9..78cdc3ecbfd69c5 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -540,7 +540,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); }, diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index fd38738e3be80b7..035536ce47ea1ee 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -3172,6 +3172,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { MaybeSimplifyHint(OBU.Inputs[0]); MaybeSimplifyHint(OBU.Inputs[1]); } + + // Try to clean up some assumption that are not very useful after this + // point. + if (CleanupAssumptions && (OBU.getTagName() == "dereferenceable")) { + auto *New = CallBase::removeOperandBundle(II, OBU.getTagID()); + return New; + } } // Convert nonnull assume like: diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h index 3a074ee70dc487a..6cfb703ed58f5cd 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -59,6 +59,8 @@ class User; class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final : public InstCombiner, public InstVisitor { + bool CleanupAssumptions = false; + public: InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder, bool MinimizeSize, AAResults *AA, AssumptionCache &AC, @@ -66,9 +68,11 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const DataLayout &DL, - ReversePostOrderTraversal &RPOT) + ReversePostOrderTraversal &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; diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 934156f04f7fdd4..5a11996182e7c58 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -5524,7 +5524,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();