LLVM 19.0.0git
Assumptions.cpp
Go to the documentation of this file.
1//===- Assumptions.cpp ------ Collection of helpers for assumptions -------===//
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// This file implements helper functions for accessing assumption infomration
10// inside of the "llvm.assume" metadata.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/Assumptions.h"
17#include "llvm/IR/Attributes.h"
18#include "llvm/IR/Function.h"
19#include "llvm/IR/InstrTypes.h"
20
21using namespace llvm;
22
23namespace {
24bool hasAssumption(const Attribute &A,
25 const KnownAssumptionString &AssumptionStr) {
26 if (!A.isValid())
27 return false;
28 assert(A.isStringAttribute() && "Expected a string attribute!");
29
31 A.getValueAsString().split(Strings, ",");
32
33 return llvm::is_contained(Strings, AssumptionStr);
34}
35
37 if (!A.isValid())
38 return DenseSet<StringRef>();
39 assert(A.isStringAttribute() && "Expected a string attribute!");
40
41 DenseSet<StringRef> Assumptions;
43 A.getValueAsString().split(Strings, ",");
44
45 for (StringRef Str : Strings)
46 Assumptions.insert(Str);
47 return Assumptions;
48}
49
50template <typename AttrSite>
51bool addAssumptionsImpl(AttrSite &Site,
52 const DenseSet<StringRef> &Assumptions) {
53 if (Assumptions.empty())
54 return false;
55
56 DenseSet<StringRef> CurAssumptions = getAssumptions(Site);
57
58 if (!set_union(CurAssumptions, Assumptions))
59 return false;
60
61 LLVMContext &Ctx = Site.getContext();
62 Site.addFnAttr(llvm::Attribute::get(
64 llvm::join(CurAssumptions.begin(), CurAssumptions.end(), ",")));
65
66 return true;
67}
68} // namespace
69
71 const KnownAssumptionString &AssumptionStr) {
72 const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
73 return ::hasAssumption(A, AssumptionStr);
74}
75
77 const KnownAssumptionString &AssumptionStr) {
78 if (Function *F = CB.getCalledFunction())
79 if (hasAssumption(*F, AssumptionStr))
80 return true;
81
83 return ::hasAssumption(A, AssumptionStr);
84}
85
87 const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
88 return ::getAssumptions(A);
89}
90
93 return ::getAssumptions(A);
94}
95
97 return ::addAssumptionsImpl(F, Assumptions);
98}
99
101 const DenseSet<StringRef> &Assumptions) {
102 return ::addAssumptionsImpl(CB, Assumptions);
103}
104
106 "omp_no_openmp", // OpenMP 5.1
107 "omp_no_openmp_routines", // OpenMP 5.1
108 "omp_no_parallelism", // OpenMP 5.1
109 "ompx_spmd_amenable", // OpenMPOpt extension
110 "ompx_no_call_asm", // OpenMPOpt extension
111});
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines generic set operations that may be used on set's of different types,...
This file contains some functions that are useful when dealing with strings.
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:93
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1461
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1709
Attribute getFnAttr(StringRef Kind) const
Get the attribute of a given kind for the function.
Definition: InstrTypes.h:1944
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool addAssumptions(Function &F, const DenseSet< StringRef > &Assumptions)
Appends the set of assumptions Assumptions to \F.
Definition: Assumptions.cpp:96
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
Definition: SetOperations.h:23
bool hasAssumption(const Function &F, const KnownAssumptionString &AssumptionStr)
Return true if F has the assumption AssumptionStr attached.
Definition: Assumptions.cpp:70
StringSet KnownAssumptionStrings
A set of known assumption strings that are accepted without warning and which can be recommended as t...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
DenseSet< StringRef > getAssumptions(const Function &F)
Return the set of all assumptions for the function F.
Definition: Assumptions.cpp:86
constexpr StringRef AssumptionAttrKey
The key we use for assumption attributes.
Definition: Assumptions.h:28
Helper that allows to insert a new assumption string in the known assumption set by creating a (stati...
Definition: Assumptions.h:36