Skip to content

Commit 42ef8db

Browse files
committed
[DropUnnecessaryAssumes] Add pass for dropping assumes
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 ephemeral (that is, only used by the assume). 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 e7db709 commit 42ef8db

File tree

16 files changed

+200
-38
lines changed

16 files changed

+200
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
class DropUnnecessaryAssumesPass
21+
: public PassInfoMixin<DropUnnecessaryAssumesPass> {
22+
public:
23+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
24+
};
25+
26+
} // end namespace llvm
27+
28+
#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: 4 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,9 @@ PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
14981499
invokeOptimizerEarlyEPCallbacks(MPM, Level, LTOPhase);
14991500

15001501
FunctionPassManager OptimizePM;
1502+
if (!isLTOPreLink(LTOPhase))
1503+
OptimizePM.addPass(DropUnnecessaryAssumesPass());
1504+
15011505
// Scheduling LoopVersioningLICM when inlining is over, because after that
15021506
// we may see more accurate aliasing. Reason to run this late is that too
15031507
// 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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
18+
PreservedAnalyses
19+
DropUnnecessaryAssumesPass::run(Function &F, FunctionAnalysisManager &FAM) {
20+
AssumptionCache &AC = FAM.getResult<AssumptionAnalysis>(F);
21+
bool Changed = false;
22+
23+
for (AssumptionCache::ResultElem &Elem : AC.assumptions()) {
24+
auto *Assume = cast_or_null<AssumeInst>(Elem.Assume);
25+
if (!Assume)
26+
continue;
27+
28+
// TODO: Handle assumes with operand bundles.
29+
if (Assume->hasOperandBundles())
30+
continue;
31+
32+
Value *Cond = Assume->getArgOperand(0);
33+
// Don't drop type tests, which have special semantics.
34+
if (match(Cond, PatternMatch::m_Intrinsic<Intrinsic::type_test>()))
35+
continue;
36+
37+
SmallPtrSet<Value *, 8> Affected;
38+
findValuesAffectedByCondition(Cond, /*IsAssume=*/true,
39+
[&](Value *A) { Affected.insert(A); });
40+
41+
// If all the affected uses have only one use (part of the assume), then
42+
// the assume does not provide useful information. Note that additional
43+
// users may appear as a result of inlining and CSE, so we should only
44+
// make this assumption late in the optimization pipeline.
45+
// TODO: Handle dead cyclic usages.
46+
if (!all_of(Affected, [](Value *V) { return V->hasOneUse(); }))
47+
continue;
48+
49+
Assume->eraseFromParent();
50+
RecursivelyDeleteTriviallyDeadInstructions(Cond);
51+
}
52+
53+
if (Changed) {
54+
PreservedAnalyses PA;
55+
PA.preserveSet<CFGAnalyses>();
56+
return PA;
57+
}
58+
return PreservedAnalyses::all();
59+
}

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)