Skip to content

Commit 12e0015

Browse files
committed
[InstCombine] Add option to clean up some assumptions.
1 parent abfd197 commit 12e0015

File tree

7 files changed

+36
-12
lines changed

7 files changed

+36
-12
lines changed

llvm/include/llvm/Transforms/InstCombine/InstCombine.h

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct InstCombineOptions {
3131
// Verify that a fix point has been reached after MaxIterations.
3232
bool VerifyFixpoint = false;
3333
unsigned MaxIterations = InstCombineDefaultMaxIterations;
34+
bool CleanupAssumptions = false;
3435

3536
InstCombineOptions() = default;
3637

@@ -43,6 +44,11 @@ struct InstCombineOptions {
4344
MaxIterations = Value;
4445
return *this;
4546
}
47+
48+
InstCombineOptions &setCleanupAssumptions(bool Value) {
49+
CleanupAssumptions = Value;
50+
return *this;
51+
}
4652
};
4753

4854
class InstCombinePass : public PassInfoMixin<InstCombinePass> {

llvm/lib/Passes/PassBuilder.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,8 @@ Expected<InstCombineOptions> parseInstCombineOptions(StringRef Params) {
918918
ParamName).str(),
919919
inconvertibleErrorCode());
920920
Result.setMaxIterations((unsigned)MaxIterations.getZExtValue());
921+
} else if (ParamName == "cleanup-assumptions") {
922+
Result.setCleanupAssumptions(Enable);
921923
} else {
922924
return make_error<StringError>(
923925
formatv("invalid InstCombine pass parameter '{0}' ", ParamName).str(),

llvm/lib/Passes/PassBuilderPipelines.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
13051305
FPM.addPass(LoopLoadEliminationPass());
13061306
}
13071307
// Cleanup after the loop optimization passes.
1308-
FPM.addPass(InstCombinePass());
1308+
FPM.addPass(
1309+
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
13091310

13101311
if (Level.getSpeedupLevel() > 1 && ExtraVectorizerPasses) {
13111312
ExtraFunctionPassManager<ShouldRunExtraVectorPasses> ExtraPasses;
@@ -1317,7 +1318,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
13171318
// dead (or speculatable) control flows or more combining opportunities.
13181319
ExtraPasses.addPass(EarlyCSEPass());
13191320
ExtraPasses.addPass(CorrelatedValuePropagationPass());
1320-
ExtraPasses.addPass(InstCombinePass());
1321+
ExtraPasses.addPass(
1322+
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
13211323
LoopPassManager LPM;
13221324
LPM.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap,
13231325
/*AllowSpeculation=*/true));
@@ -1328,7 +1330,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
13281330
/*UseBlockFrequencyInfo=*/true));
13291331
ExtraPasses.addPass(
13301332
SimplifyCFGPass(SimplifyCFGOptions().convertSwitchRangeToICmp(true)));
1331-
ExtraPasses.addPass(InstCombinePass());
1333+
ExtraPasses.addPass(
1334+
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
13321335
FPM.addPass(std::move(ExtraPasses));
13331336
}
13341337

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

13521355
if (IsFullLTO) {
13531356
FPM.addPass(SCCPPass());
1354-
FPM.addPass(InstCombinePass());
1357+
FPM.addPass(
1358+
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
13551359
FPM.addPass(BDCEPass());
13561360
}
13571361

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

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

13941399
FPM.addPass(InferAlignmentPass());
1395-
FPM.addPass(InstCombinePass());
1400+
FPM.addPass(
1401+
InstCombinePass(InstCombineOptions().setCleanupAssumptions(true)));
13961402

13971403
// This is needed for two reasons:
13981404
// 1. It works around problems that instcombine introduces, such as sinking

llvm/lib/Passes/PassRegistry.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ FUNCTION_PASS_WITH_PARAMS(
539539
[](InstCombineOptions Opts) { return InstCombinePass(Opts); },
540540
parseInstCombineOptions,
541541
"no-use-loop-info;use-loop-info;no-verify-fixpoint;verify-fixpoint;"
542-
"max-iterations=N")
542+
"max-iterations=N;cleanup-assumptions")
543543
FUNCTION_PASS_WITH_PARAMS(
544544
"loop-unroll", "LoopUnrollPass",
545545
[](LoopUnrollOptions Opts) { return LoopUnrollPass(Opts); },

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,6 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
32263226
MaybeSimplifyHint(OBU.Inputs[0]);
32273227
MaybeSimplifyHint(OBU.Inputs[1]);
32283228
}
3229-
32303229
if (OBU.getTagName() == "align" && OBU.Inputs.size() == 2) {
32313230
RetainedKnowledge RK = getKnowledgeFromBundle(
32323231
*cast<AssumeInst>(II), II->bundle_op_info_begin()[Idx]);
@@ -3262,6 +3261,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
32623261
auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
32633262
return New;
32643263
}
3264+
3265+
// Try to clean up some assumption that are not very useful after this
3266+
// point.
3267+
if (CleanupAssumptions && OBU.getTagName() == "align") {
3268+
auto *New = CallBase::removeOperandBundle(II, OBU.getTagID());
3269+
return New;
3270+
}
32653271
}
32663272

32673273
// Convert nonnull assume like:

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,20 @@ class User;
5959
class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
6060
: public InstCombiner,
6161
public InstVisitor<InstCombinerImpl, Instruction *> {
62+
bool CleanupAssumptions = false;
63+
6264
public:
6365
InstCombinerImpl(InstructionWorklist &Worklist, BuilderTy &Builder,
6466
bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
6567
TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
6668
DominatorTree &DT, OptimizationRemarkEmitter &ORE,
6769
BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI,
6870
ProfileSummaryInfo *PSI, const DataLayout &DL,
69-
ReversePostOrderTraversal<BasicBlock *> &RPOT)
71+
ReversePostOrderTraversal<BasicBlock *> &RPOT,
72+
bool CleanupAssumptions)
7073
: InstCombiner(Worklist, Builder, MinimizeSize, AA, AC, TLI, TTI, DT, ORE,
71-
BFI, BPI, PSI, DL, RPOT) {}
74+
BFI, BPI, PSI, DL, RPOT),
75+
CleanupAssumptions(CleanupAssumptions) {}
7276

7377
virtual ~InstCombinerImpl() = default;
7478

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5545,7 +5545,7 @@ static bool combineInstructionsOverFunction(
55455545
<< F.getName() << "\n");
55465546

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

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

0 commit comments

Comments
 (0)