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
21using namespace llvm;
22
23static bool hasAssumption(const Attribute &A,
24 const KnownAssumptionString &AssumptionStr) {
25 if (!A.isValid())
26 return false;
27 assert(A.isStringAttribute() && "Expected a string attribute!");
28
29 SmallVector<StringRef, 8> Strings;
30 A.getValueAsString().split(A&: Strings, Separator: ",");
31
32 return llvm::is_contained(Range&: Strings, Element: AssumptionStr);
33}
34
35static DenseSet<StringRef> getAssumptions(const Attribute &A) {
36 if (!A.isValid())
37 return DenseSet<StringRef>();
38 assert(A.isStringAttribute() && "Expected a string attribute!");
39
40 DenseSet<StringRef> Assumptions;
41 SmallVector<StringRef, 8> Strings;
42 A.getValueAsString().split(A&: Strings, Separator: ",");
43
44 Assumptions.insert_range(R&: Strings);
45 return Assumptions;
46}
47
48template <typename AttrSite>
49static bool addAssumptionsImpl(AttrSite &Site,
50 const DenseSet<StringRef> &Assumptions) {
51 if (Assumptions.empty())
52 return false;
53
54 DenseSet<StringRef> CurAssumptions = getAssumptions(Site);
55
56 if (!set_union(S1&: CurAssumptions, S2: Assumptions))
57 return false;
58
59 LLVMContext &Ctx = Site.getContext();
60 Site.addFnAttr(llvm::Attribute::get(
61 Context&: Ctx, Kind: llvm::AssumptionAttrKey,
62 Val: llvm::join(Begin: CurAssumptions.begin(), End: CurAssumptions.end(), Separator: ",")));
63
64 return true;
65}
66
67bool llvm::hasAssumption(const Function &F,
68 const KnownAssumptionString &AssumptionStr) {
69 const Attribute &A = F.getFnAttribute(Kind: AssumptionAttrKey);
70 return ::hasAssumption(A, AssumptionStr);
71}
72
73bool llvm::hasAssumption(const CallBase &CB,
74 const KnownAssumptionString &AssumptionStr) {
75 if (Function *F = CB.getCalledFunction())
76 if (hasAssumption(F: *F, AssumptionStr))
77 return true;
78
79 const Attribute &A = CB.getFnAttr(Kind: AssumptionAttrKey);
80 return ::hasAssumption(A, AssumptionStr);
81}
82
83DenseSet<StringRef> llvm::getAssumptions(const Function &F) {
84 const Attribute &A = F.getFnAttribute(Kind: AssumptionAttrKey);
85 return ::getAssumptions(A);
86}
87
88DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) {
89 const Attribute &A = CB.getFnAttr(Kind: AssumptionAttrKey);
90 return ::getAssumptions(A);
91}
92
93bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) {
94 return ::addAssumptionsImpl(Site&: F, Assumptions);
95}
96
97bool llvm::addAssumptions(CallBase &CB,
98 const DenseSet<StringRef> &Assumptions) {
99 return ::addAssumptionsImpl(Site&: CB, Assumptions);
100}
101
102StringSet<> &llvm::getKnownAssumptionStrings() {
103 static StringSet<> Object({
104 "omp_no_openmp", // OpenMP 5.1
105 "omp_no_openmp_routines", // OpenMP 5.1
106 "omp_no_parallelism", // OpenMP 5.1
107 "omp_no_openmp_constructs", // OpenMP 6.0
108 "ompx_spmd_amenable", // OpenMPOpt extension
109 "ompx_no_call_asm", // OpenMPOpt extension
110 "ompx_aligned_barrier", // OpenMPOpt extension
111 });
112
113 return Object;
114}
115