1 //===- Assumptions.cpp ------ Collection of helpers for assumptions -------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements helper functions for accessing assumption infomration
10 // inside of the "llvm.assume" metadata.
12 //===----------------------------------------------------------------------===//
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"
24 bool hasAssumption(const Attribute
&A
,
25 const KnownAssumptionString
&AssumptionStr
) {
28 assert(A
.isStringAttribute() && "Expected a string attribute!");
30 SmallVector
<StringRef
, 8> Strings
;
31 A
.getValueAsString().split(Strings
, ",");
33 return llvm::is_contained(Strings
, AssumptionStr
);
36 DenseSet
<StringRef
> getAssumptions(const Attribute
&A
) {
38 return DenseSet
<StringRef
>();
39 assert(A
.isStringAttribute() && "Expected a string attribute!");
41 DenseSet
<StringRef
> Assumptions
;
42 SmallVector
<StringRef
, 8> Strings
;
43 A
.getValueAsString().split(Strings
, ",");
45 for (StringRef Str
: Strings
)
46 Assumptions
.insert(Str
);
50 template <typename AttrSite
>
51 bool addAssumptionsImpl(AttrSite
&Site
,
52 const DenseSet
<StringRef
> &Assumptions
) {
53 if (Assumptions
.empty())
56 DenseSet
<StringRef
> CurAssumptions
= getAssumptions(Site
);
58 if (!set_union(CurAssumptions
, Assumptions
))
61 LLVMContext
&Ctx
= Site
.getContext();
62 Site
.addFnAttr(llvm::Attribute::get(
63 Ctx
, llvm::AssumptionAttrKey
,
64 llvm::join(CurAssumptions
.begin(), CurAssumptions
.end(), ",")));
70 bool llvm::hasAssumption(const Function
&F
,
71 const KnownAssumptionString
&AssumptionStr
) {
72 const Attribute
&A
= F
.getFnAttribute(AssumptionAttrKey
);
73 return ::hasAssumption(A
, AssumptionStr
);
76 bool llvm::hasAssumption(const CallBase
&CB
,
77 const KnownAssumptionString
&AssumptionStr
) {
78 if (Function
*F
= CB
.getCalledFunction())
79 if (hasAssumption(*F
, AssumptionStr
))
82 const Attribute
&A
= CB
.getFnAttr(AssumptionAttrKey
);
83 return ::hasAssumption(A
, AssumptionStr
);
86 DenseSet
<StringRef
> llvm::getAssumptions(const Function
&F
) {
87 const Attribute
&A
= F
.getFnAttribute(AssumptionAttrKey
);
88 return ::getAssumptions(A
);
91 DenseSet
<StringRef
> llvm::getAssumptions(const CallBase
&CB
) {
92 const Attribute
&A
= CB
.getFnAttr(AssumptionAttrKey
);
93 return ::getAssumptions(A
);
96 bool llvm::addAssumptions(Function
&F
, const DenseSet
<StringRef
> &Assumptions
) {
97 return ::addAssumptionsImpl(F
, Assumptions
);
100 bool llvm::addAssumptions(CallBase
&CB
,
101 const DenseSet
<StringRef
> &Assumptions
) {
102 return ::addAssumptionsImpl(CB
, Assumptions
);
105 StringSet
<> llvm::KnownAssumptionStrings({
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