Skip to content

Commit 902ddda

Browse files
authored
[DropUnnecessaryAssumes] Add pass for dropping assumes (#159403)
This adds a new pass for dropping assumes that are unlikely to be useful for further optimization. It works by discarding any assumes whose affected values are one-use (which implies that they are only used by the assume, i.e. ephemeral). This pass currently runs at the start of the module optimization pipeline, that is post-inline and post-link. Before that point, it is more likely for previously "useless" assumes to become useful again, e.g. because an additional user of the value is introduced after inlining + CSE.
1 parent 09e0f1e commit 902ddda

16 files changed

+206
-38
lines changed

clang/test/CodeGen/inline-asm-x86-flag-output.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ int test_assume_boolean_flag(long nr, volatile long *addr) {
389389
: "=@cca"(x), "=@ccae"(y), "=m"(*(volatile long *)(addr))
390390
: "r"(nr)
391391
: "cc");
392-
if (x)
392+
if (x && y)
393393
return 0;
394394
return 1;
395395
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===------------------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Pass that drops assumes that are unlikely to be useful.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_TRANSFORMS_SCALAR_DROPUNNECESSARYASSUMES_H
14+
#define LLVM_TRANSFORMS_SCALAR_DROPUNNECESSARYASSUMES_H
15+
16+
#include "llvm/IR/PassManager.h"
17+
18+
namespace llvm {
19+
20+
struct DropUnnecessaryAssumesPass
21+
: public PassInfoMixin<DropUnnecessaryAssumesPass> {
22+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
23+
};
24+
25+
} // end namespace llvm
26+
27+
#endif // LLVM_TRANSFORMS_SCALAR_DROPUNNECESSARYASSUMES_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
#include "llvm/Transforms/Scalar/DFAJumpThreading.h"
274274
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
275275
#include "llvm/Transforms/Scalar/DivRemPairs.h"
276+
#include "llvm/Transforms/Scalar/DropUnnecessaryAssumes.h"
276277
#include "llvm/Transforms/Scalar/EarlyCSE.h"
277278
#include "llvm/Transforms/Scalar/FlattenCFG.h"
278279
#include "llvm/Transforms/Scalar/Float2Int.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#include "llvm/Transforms/Scalar/DFAJumpThreading.h"
9393
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
9494
#include "llvm/Transforms/Scalar/DivRemPairs.h"
95+
#include "llvm/Transforms/Scalar/DropUnnecessaryAssumes.h"
9596
#include "llvm/Transforms/Scalar/EarlyCSE.h"
9697
#include "llvm/Transforms/Scalar/Float2Int.h"
9798
#include "llvm/Transforms/Scalar/GVN.h"
@@ -1498,6 +1499,13 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
14981499
invokeOptimizerEarlyEPCallbacks(MPM, Level, LTOPhase);
14991500

15001501
FunctionPassManager OptimizePM;
1502+
1503+
// Only drop unnecessary assumes post-inline and post-link, as otherwise
1504+
// additional uses of the affected value may be introduced through inlining
1505+
// and CSE.
1506+
if (!isLTOPreLink(LTOPhase))
1507+
OptimizePM.addPass(DropUnnecessaryAssumesPass());
1508+
15011509
// Scheduling LoopVersioningLICM when inlining is over, because after that
15021510
// we may see more accurate aliasing. Reason to run this late is that too
15031511
// early versioning may prevent further inlining due to increase of code

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ FUNCTION_PASS("dot-post-dom", PostDomPrinter())
425425
FUNCTION_PASS("dot-post-dom-only", PostDomOnlyPrinter())
426426
FUNCTION_PASS("dse", DSEPass())
427427
FUNCTION_PASS("dwarf-eh-prepare", DwarfEHPreparePass(TM))
428+
FUNCTION_PASS("drop-unnecessary-assumes", DropUnnecessaryAssumesPass())
428429
FUNCTION_PASS("expand-large-div-rem", ExpandLargeDivRemPass(TM))
429430
FUNCTION_PASS("expand-memcmp", ExpandMemCmpPass(TM))
430431
FUNCTION_PASS("expand-reductions", ExpandReductionsPass())

llvm/lib/Transforms/Scalar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_llvm_component_library(LLVMScalarOpts
1111
DeadStoreElimination.cpp
1212
DFAJumpThreading.cpp
1313
DivRemPairs.cpp
14+
DropUnnecessaryAssumes.cpp
1415
EarlyCSE.cpp
1516
FlattenCFGPass.cpp
1617
Float2Int.cpp
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===------------------------------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/Transforms/Scalar/DropUnnecessaryAssumes.h"
10+
#include "llvm/Analysis/AssumptionCache.h"
11+
#include "llvm/Analysis/ValueTracking.h"
12+
#include "llvm/IR/IntrinsicInst.h"
13+
#include "llvm/IR/PatternMatch.h"
14+
#include "llvm/Transforms/Utils/Local.h"
15+
16+
using namespace llvm;
17+
using namespace llvm::PatternMatch;
18+
19+
PreservedAnalyses
20+
DropUnnecessaryAssumesPass::run(Function &F, FunctionAnalysisManager &FAM) {
21+
AssumptionCache &AC = FAM.getResult<AssumptionAnalysis>(F);
22+
bool Changed = false;
23+
24+
for (AssumptionCache::ResultElem &Elem : AC.assumptions()) {
25+
auto *Assume = cast_or_null<AssumeInst>(Elem.Assume);
26+
if (!Assume)
27+
continue;
28+
29+
// TODO: Handle assumes with operand bundles.
30+
if (Assume->hasOperandBundles())
31+
continue;
32+
33+
Value *Cond = Assume->getArgOperand(0);
34+
// Don't drop type tests, which have special semantics.
35+
if (match(Cond, m_Intrinsic<Intrinsic::type_test>()))
36+
continue;
37+
38+
SmallPtrSet<Value *, 8> Affected;
39+
findValuesAffectedByCondition(Cond, /*IsAssume=*/true,
40+
[&](Value *A) { Affected.insert(A); });
41+
42+
// If all the affected uses have only one use (part of the assume), then
43+
// the assume does not provide useful information. Note that additional
44+
// users may appear as a result of inlining and CSE, so we should only
45+
// make this assumption late in the optimization pipeline.
46+
// TODO: Handle dead cyclic usages.
47+
// TODO: Handle multiple dead assumes on the same value.
48+
if (!all_of(Affected, match_fn(m_OneUse(m_Value()))))
49+
continue;
50+
51+
Assume->eraseFromParent();
52+
RecursivelyDeleteTriviallyDeadInstructions(Cond);
53+
Changed = true;
54+
}
55+
56+
if (Changed) {
57+
PreservedAnalyses PA;
58+
PA.preserveSet<CFGAnalyses>();
59+
return PA;
60+
}
61+
return PreservedAnalyses::all();
62+
}

llvm/test/Other/new-pm-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
; CHECK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass
245245
; CHECK-O-NEXT: Running pass: RecomputeGlobalsAAPass
246246
; CHECK-EP-OPTIMIZER-EARLY: Running pass: NoOpModulePass
247+
; CHECK-DEFAULT-NEXT: Running pass: DropUnnecessaryAssumesPass
247248
; CHECK-O-NEXT: Running pass: Float2IntPass
248249
; CHECK-O-NEXT: Running pass: LowerConstantIntrinsicsPass on foo
249250
; CHECK-MATRIX: Running pass: LowerMatrixIntrinsicsPass on f

llvm/test/Other/new-pm-thinlto-postlink-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
; CHECK-POSTLINK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass
168168
; CHECK-POSTLINK-O-NEXT: Running pass: RecomputeGlobalsAAPass
169169
; CHECK-POST-EP-OPT-EARLY-NEXT: Running pass: NoOpModulePass
170+
; CHECK-POSTLINK-O-NEXT: Running pass: DropUnnecessaryAssumesPass
170171
; CHECK-POSTLINK-O-NEXT: Running pass: Float2IntPass
171172
; CHECK-POSTLINK-O-NEXT: Running pass: LowerConstantIntrinsicsPass
172173
; CHECK-POSTLINK-O3-NEXT: Running pass: ControlHeightReductionPass

llvm/test/Other/new-pm-thinlto-postlink-pgo-defaults.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
; CHECK-O-NEXT: Running pass: EliminateAvailableExternallyPass
151151
; CHECK-O-NEXT: Running pass: ReversePostOrderFunctionAttrsPass
152152
; CHECK-O-NEXT: Running pass: RecomputeGlobalsAAPass
153+
; CHECK-O-NEXT: Running pass: DropUnnecessaryAssumesPass
153154
; CHECK-O-NEXT: Running pass: Float2IntPass
154155
; CHECK-O-NEXT: Running pass: LowerConstantIntrinsicsPass
155156
; CHECK-O3-NEXT: Running pass: ControlHeightReductionPass

0 commit comments

Comments
 (0)