LLVM  15.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"
15 #include "llvm/ADT/SetOperations.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/InstrTypes.h"
20 
21 using namespace llvm;
22 
23 namespace {
24 bool 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 
50 template <typename AttrSite>
51 bool 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 
82  const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
83  return ::hasAssumption(A, AssumptionStr);
84 }
85 
87  const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
89 }
90 
92  const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
94 }
95 
96 bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) {
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 });
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
SetOperations.h
llvm::Function
Definition: Function.h:60
llvm::Attribute
Definition: Attributes.h:52
llvm::set_union
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
llvm::Attribute::get
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:91
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::getAssumptions
DenseSet< StringRef > getAssumptions(const Function &F)
Return the set of all assumptions for the function F.
Definition: Assumptions.cpp:86
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::end
iterator end()
Definition: DenseSet.h:174
llvm::KnownAssumptionStrings
StringSet KnownAssumptionStrings
A set of known assumption strings that are accepted without warning and which can be recommended as t...
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
InstrTypes.h
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1396
llvm::hasAssumption
bool hasAssumption(const Function &F, const KnownAssumptionString &AssumptionStr)
Return true if F has the assumption AssumptionStr attached.
Definition: Assumptions.cpp:70
Assumptions.h
llvm::KnownAssumptionString
Helper that allows to insert a new assumption string in the known assumption set by creating a (stati...
Definition: Assumptions.h:36
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
StringExtras.h
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::empty
bool empty() const
Definition: DenseSet.h:80
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1672
llvm::AssumptionAttrKey
constexpr StringRef AssumptionAttrKey
The key we use for assumption attributes.
Definition: Assumptions.h:28
llvm::detail::DenseSetImpl< ValueT, DenseMap< ValueT, detail::DenseSetEmpty, DenseMapInfo< ValueT >, detail::DenseSetPair< ValueT > >, DenseMapInfo< ValueT > >::begin
iterator begin()
Definition: DenseSet.h:173
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
llvm::CallBase::getFnAttr
Attribute getFnAttr(StringRef Kind) const
Get the attribute of a given kind for the function.
Definition: InstrTypes.h:1615
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Attributes.h
llvm::addAssumptions
bool addAssumptions(Function &F, const DenseSet< StringRef > &Assumptions)
Appends the set of assumptions Assumptions to \F.
Definition: Assumptions.cpp:96
Function.h
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174