LLVM 22.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 Assumptions.insert_range(Strings);
46 return Assumptions;
47}
48
49template <typename AttrSite>
50bool addAssumptionsImpl(AttrSite &Site,
51 const DenseSet<StringRef> &Assumptions) {
52 if (Assumptions.empty())
53 return false;
54
55 DenseSet<StringRef> CurAssumptions = getAssumptions(Site);
56
57 if (!set_union(CurAssumptions, Assumptions))
58 return false;
59
60 LLVMContext &Ctx = Site.getContext();
61 Site.addFnAttr(llvm::Attribute::get(
63 llvm::join(CurAssumptions.begin(), CurAssumptions.end(), ",")));
64
65 return true;
66}
67} // namespace
68
70 const KnownAssumptionString &AssumptionStr) {
71 const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
72 return ::hasAssumption(A, AssumptionStr);
73}
74
76 const KnownAssumptionString &AssumptionStr) {
77 if (Function *F = CB.getCalledFunction())
78 if (hasAssumption(*F, AssumptionStr))
79 return true;
80
82 return ::hasAssumption(A, AssumptionStr);
83}
84
86 const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
87 return ::getAssumptions(A);
88}
89
92 return ::getAssumptions(A);
93}
94
96 return ::addAssumptionsImpl(F, Assumptions);
97}
98
100 const DenseSet<StringRef> &Assumptions) {
101 return ::addAssumptionsImpl(CB, Assumptions);
102}
103
105 static StringSet<> Object({
106 "omp_no_openmp", // OpenMP 5.1
107 "omp_no_openmp_routines", // OpenMP 5.1
108 "omp_no_parallelism", // OpenMP 5.1
109 "omp_no_openmp_constructs", // OpenMP 6.0
110 "ompx_spmd_amenable", // OpenMPOpt extension
111 "ompx_no_call_asm", // OpenMPOpt extension
112 "ompx_aligned_barrier", // OpenMPOpt extension
113 });
114
115 return Object;
116}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
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.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Attribute getFnAttr(StringRef Kind) const
Get the attribute of a given kind for the function.
Implements a dense probed hash-table based set.
Definition DenseSet.h:261
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:25
void insert_range(Range &&R)
Definition DenseSet.h:220
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool addAssumptions(Function &F, const DenseSet< StringRef > &Assumptions)
Appends the set of assumptions Assumptions to \F.
LLVM_ABI StringSet & getKnownAssumptionStrings()
A set of known assumption strings that are accepted without warning and which can be recommended as t...
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
LLVM_ABI bool hasAssumption(const Function &F, const KnownAssumptionString &AssumptionStr)
Return true if F has the assumption AssumptionStr attached.
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1879
LLVM_ABI DenseSet< StringRef > getAssumptions(const Function &F)
Return the set of all assumptions for the function F.
constexpr StringRef AssumptionAttrKey
The key we use for assumption attributes.
Definition Assumptions.h:29
Helper that allows to insert a new assumption string in the known assumption set by creating a (stati...
Definition Assumptions.h:37