LLVM 22.0.0git
DropUnnecessaryAssumes.cpp
Go to the documentation of this file.
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
15
16using namespace llvm;
17using namespace llvm::PatternMatch;
18
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.
36 continue;
37
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();
53 Changed = true;
54 }
55
56 if (Changed) {
59 return PA;
60 }
62}
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define F(x, y, z)
Definition MD5.cpp:55
FunctionAnalysisManager FAM
const SmallVectorImpl< MachineOperand > & Cond
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptions()
Access the list of assumption handles currently tracked for this function.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
LLVM Value Representation.
Definition Value.h:75
Changed
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
bool match(Val *V, const Pattern &P)
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:533
auto cast_or_null(const Y &Val)
Definition Casting.h:720
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)