1e8d8bef9SDimitry Andric //===- Assumptions.cpp ------ Collection of helpers for assumptions -------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9349cc55cSDimitry Andric // This file implements helper functions for accessing assumption infomration
10349cc55cSDimitry Andric // inside of the "llvm.assume" metadata.
11349cc55cSDimitry Andric //
12e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13e8d8bef9SDimitry Andric
14e8d8bef9SDimitry Andric #include "llvm/IR/Assumptions.h"
15349cc55cSDimitry Andric #include "llvm/ADT/SetOperations.h"
16349cc55cSDimitry Andric #include "llvm/ADT/StringExtras.h"
17e8d8bef9SDimitry Andric #include "llvm/IR/Attributes.h"
18e8d8bef9SDimitry Andric #include "llvm/IR/Function.h"
19349cc55cSDimitry Andric #include "llvm/IR/InstrTypes.h"
20e8d8bef9SDimitry Andric
21e8d8bef9SDimitry Andric using namespace llvm;
22e8d8bef9SDimitry Andric
23349cc55cSDimitry Andric namespace {
hasAssumption(const Attribute & A,const KnownAssumptionString & AssumptionStr)24349cc55cSDimitry Andric bool hasAssumption(const Attribute &A,
25e8d8bef9SDimitry Andric const KnownAssumptionString &AssumptionStr) {
26e8d8bef9SDimitry Andric if (!A.isValid())
27e8d8bef9SDimitry Andric return false;
28e8d8bef9SDimitry Andric assert(A.isStringAttribute() && "Expected a string attribute!");
29e8d8bef9SDimitry Andric
30e8d8bef9SDimitry Andric SmallVector<StringRef, 8> Strings;
31e8d8bef9SDimitry Andric A.getValueAsString().split(Strings, ",");
32e8d8bef9SDimitry Andric
33349cc55cSDimitry Andric return llvm::is_contained(Strings, AssumptionStr);
34349cc55cSDimitry Andric }
35349cc55cSDimitry Andric
getAssumptions(const Attribute & A)36349cc55cSDimitry Andric DenseSet<StringRef> getAssumptions(const Attribute &A) {
37349cc55cSDimitry Andric if (!A.isValid())
38349cc55cSDimitry Andric return DenseSet<StringRef>();
39349cc55cSDimitry Andric assert(A.isStringAttribute() && "Expected a string attribute!");
40349cc55cSDimitry Andric
41349cc55cSDimitry Andric DenseSet<StringRef> Assumptions;
42349cc55cSDimitry Andric SmallVector<StringRef, 8> Strings;
43349cc55cSDimitry Andric A.getValueAsString().split(Strings, ",");
44349cc55cSDimitry Andric
45349cc55cSDimitry Andric for (StringRef Str : Strings)
46349cc55cSDimitry Andric Assumptions.insert(Str);
47349cc55cSDimitry Andric return Assumptions;
48349cc55cSDimitry Andric }
49349cc55cSDimitry Andric
50349cc55cSDimitry Andric template <typename AttrSite>
addAssumptionsImpl(AttrSite & Site,const DenseSet<StringRef> & Assumptions)51349cc55cSDimitry Andric bool addAssumptionsImpl(AttrSite &Site,
52349cc55cSDimitry Andric const DenseSet<StringRef> &Assumptions) {
53349cc55cSDimitry Andric if (Assumptions.empty())
54349cc55cSDimitry Andric return false;
55349cc55cSDimitry Andric
56349cc55cSDimitry Andric DenseSet<StringRef> CurAssumptions = getAssumptions(Site);
57349cc55cSDimitry Andric
58349cc55cSDimitry Andric if (!set_union(CurAssumptions, Assumptions))
59349cc55cSDimitry Andric return false;
60349cc55cSDimitry Andric
61349cc55cSDimitry Andric LLVMContext &Ctx = Site.getContext();
62349cc55cSDimitry Andric Site.addFnAttr(llvm::Attribute::get(
63349cc55cSDimitry Andric Ctx, llvm::AssumptionAttrKey,
64349cc55cSDimitry Andric llvm::join(CurAssumptions.begin(), CurAssumptions.end(), ",")));
65349cc55cSDimitry Andric
66349cc55cSDimitry Andric return true;
67349cc55cSDimitry Andric }
68349cc55cSDimitry Andric } // namespace
69349cc55cSDimitry Andric
hasAssumption(const Function & F,const KnownAssumptionString & AssumptionStr)70349cc55cSDimitry Andric bool llvm::hasAssumption(const Function &F,
71349cc55cSDimitry Andric const KnownAssumptionString &AssumptionStr) {
72349cc55cSDimitry Andric const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
73349cc55cSDimitry Andric return ::hasAssumption(A, AssumptionStr);
74349cc55cSDimitry Andric }
75349cc55cSDimitry Andric
hasAssumption(const CallBase & CB,const KnownAssumptionString & AssumptionStr)76349cc55cSDimitry Andric bool llvm::hasAssumption(const CallBase &CB,
77349cc55cSDimitry Andric const KnownAssumptionString &AssumptionStr) {
78349cc55cSDimitry Andric if (Function *F = CB.getCalledFunction())
79349cc55cSDimitry Andric if (hasAssumption(*F, AssumptionStr))
80349cc55cSDimitry Andric return true;
81349cc55cSDimitry Andric
82349cc55cSDimitry Andric const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
83349cc55cSDimitry Andric return ::hasAssumption(A, AssumptionStr);
84349cc55cSDimitry Andric }
85349cc55cSDimitry Andric
getAssumptions(const Function & F)86349cc55cSDimitry Andric DenseSet<StringRef> llvm::getAssumptions(const Function &F) {
87349cc55cSDimitry Andric const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
88349cc55cSDimitry Andric return ::getAssumptions(A);
89349cc55cSDimitry Andric }
90349cc55cSDimitry Andric
getAssumptions(const CallBase & CB)91349cc55cSDimitry Andric DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) {
92349cc55cSDimitry Andric const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
93349cc55cSDimitry Andric return ::getAssumptions(A);
94349cc55cSDimitry Andric }
95349cc55cSDimitry Andric
addAssumptions(Function & F,const DenseSet<StringRef> & Assumptions)96349cc55cSDimitry Andric bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) {
97349cc55cSDimitry Andric return ::addAssumptionsImpl(F, Assumptions);
98349cc55cSDimitry Andric }
99349cc55cSDimitry Andric
addAssumptions(CallBase & CB,const DenseSet<StringRef> & Assumptions)100349cc55cSDimitry Andric bool llvm::addAssumptions(CallBase &CB,
101349cc55cSDimitry Andric const DenseSet<StringRef> &Assumptions) {
102349cc55cSDimitry Andric return ::addAssumptionsImpl(CB, Assumptions);
103e8d8bef9SDimitry Andric }
104e8d8bef9SDimitry Andric
105e8d8bef9SDimitry Andric StringSet<> llvm::KnownAssumptionStrings({
106e8d8bef9SDimitry Andric "omp_no_openmp", // OpenMP 5.1
107e8d8bef9SDimitry Andric "omp_no_openmp_routines", // OpenMP 5.1
108e8d8bef9SDimitry Andric "omp_no_parallelism", // OpenMP 5.1
109fe6060f1SDimitry Andric "ompx_spmd_amenable", // OpenMPOpt extension
110*81ad6265SDimitry Andric "ompx_no_call_asm", // OpenMPOpt extension
111e8d8bef9SDimitry Andric });
112