1b9c77542SJohannes Doerfert //===- Assumptions.cpp ------ Collection of helpers for assumptions -------===//
2b9c77542SJohannes Doerfert //
3b9c77542SJohannes Doerfert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b9c77542SJohannes Doerfert // See https://llvm.org/LICENSE.txt for license information.
5b9c77542SJohannes Doerfert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b9c77542SJohannes Doerfert //
7b9c77542SJohannes Doerfert //===----------------------------------------------------------------------===//
8b9c77542SJohannes Doerfert //
9*b8a825b4SJoseph Huber //  This file implements helper functions for accessing assumption infomration
10*b8a825b4SJoseph Huber //  inside of the "llvm.assume" metadata.
11*b8a825b4SJoseph Huber //
12b9c77542SJohannes Doerfert //===----------------------------------------------------------------------===//
13b9c77542SJohannes Doerfert 
14b9c77542SJohannes Doerfert #include "llvm/IR/Assumptions.h"
15*b8a825b4SJoseph Huber #include "llvm/ADT/SetOperations.h"
16*b8a825b4SJoseph Huber #include "llvm/ADT/StringExtras.h"
17b9c77542SJohannes Doerfert #include "llvm/IR/Attributes.h"
18b9c77542SJohannes Doerfert #include "llvm/IR/Function.h"
199e2fc0baSJoseph Huber #include "llvm/IR/InstrTypes.h"
20b9c77542SJohannes Doerfert 
21b9c77542SJohannes Doerfert using namespace llvm;
22b9c77542SJohannes Doerfert 
239e2fc0baSJoseph Huber namespace {
249e2fc0baSJoseph Huber bool hasAssumption(const Attribute &A,
25b9c77542SJohannes Doerfert                    const KnownAssumptionString &AssumptionStr) {
26b9c77542SJohannes Doerfert   if (!A.isValid())
27b9c77542SJohannes Doerfert     return false;
28b9c77542SJohannes Doerfert   assert(A.isStringAttribute() && "Expected a string attribute!");
29b9c77542SJohannes Doerfert 
30b9c77542SJohannes Doerfert   SmallVector<StringRef, 8> Strings;
31b9c77542SJohannes Doerfert   A.getValueAsString().split(Strings, ",");
32b9c77542SJohannes Doerfert 
3381e9c906SKazu Hirata   return llvm::is_contained(Strings, AssumptionStr);
34b9c77542SJohannes Doerfert }
35*b8a825b4SJoseph Huber 
36*b8a825b4SJoseph Huber DenseSet<StringRef> getAssumptions(const Attribute &A) {
37*b8a825b4SJoseph Huber   if (!A.isValid())
38*b8a825b4SJoseph Huber     return DenseSet<StringRef>();
39*b8a825b4SJoseph Huber   assert(A.isStringAttribute() && "Expected a string attribute!");
40*b8a825b4SJoseph Huber 
41*b8a825b4SJoseph Huber   DenseSet<StringRef> Assumptions;
42*b8a825b4SJoseph Huber   SmallVector<StringRef, 8> Strings;
43*b8a825b4SJoseph Huber   A.getValueAsString().split(Strings, ",");
44*b8a825b4SJoseph Huber 
45*b8a825b4SJoseph Huber   for (StringRef Str : Strings)
46*b8a825b4SJoseph Huber     Assumptions.insert(Str);
47*b8a825b4SJoseph Huber   return Assumptions;
48*b8a825b4SJoseph Huber }
49*b8a825b4SJoseph Huber 
50*b8a825b4SJoseph Huber template <typename AttrSite>
51*b8a825b4SJoseph Huber bool addAssumptionsImpl(AttrSite &Site,
52*b8a825b4SJoseph Huber                         const DenseSet<StringRef> &Assumptions) {
53*b8a825b4SJoseph Huber   if (Assumptions.empty())
54*b8a825b4SJoseph Huber     return false;
55*b8a825b4SJoseph Huber 
56*b8a825b4SJoseph Huber   DenseSet<StringRef> CurAssumptions = getAssumptions(Site);
57*b8a825b4SJoseph Huber 
58*b8a825b4SJoseph Huber   if (!set_union(CurAssumptions, Assumptions))
59*b8a825b4SJoseph Huber     return false;
60*b8a825b4SJoseph Huber 
61*b8a825b4SJoseph Huber   LLVMContext &Ctx = Site.getContext();
62*b8a825b4SJoseph Huber   Site.addFnAttr(llvm::Attribute::get(
63*b8a825b4SJoseph Huber       Ctx, llvm::AssumptionAttrKey,
64*b8a825b4SJoseph Huber       llvm::join(CurAssumptions.begin(), CurAssumptions.end(), ",")));
65*b8a825b4SJoseph Huber 
66*b8a825b4SJoseph Huber   return true;
67*b8a825b4SJoseph Huber }
689e2fc0baSJoseph Huber } // namespace
699e2fc0baSJoseph Huber 
70*b8a825b4SJoseph Huber bool llvm::hasAssumption(const Function &F,
719e2fc0baSJoseph Huber                          const KnownAssumptionString &AssumptionStr) {
729e2fc0baSJoseph Huber   const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
739e2fc0baSJoseph Huber   return ::hasAssumption(A, AssumptionStr);
749e2fc0baSJoseph Huber }
759e2fc0baSJoseph Huber 
76*b8a825b4SJoseph Huber bool llvm::hasAssumption(const CallBase &CB,
779e2fc0baSJoseph Huber                          const KnownAssumptionString &AssumptionStr) {
789e2fc0baSJoseph Huber   if (Function *F = CB.getCalledFunction())
799e2fc0baSJoseph Huber     if (hasAssumption(*F, AssumptionStr))
809e2fc0baSJoseph Huber       return true;
819e2fc0baSJoseph Huber 
829e2fc0baSJoseph Huber   const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
839e2fc0baSJoseph Huber   return ::hasAssumption(A, AssumptionStr);
849e2fc0baSJoseph Huber }
85b9c77542SJohannes Doerfert 
86*b8a825b4SJoseph Huber DenseSet<StringRef> llvm::getAssumptions(const Function &F) {
87*b8a825b4SJoseph Huber   const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
88*b8a825b4SJoseph Huber   return ::getAssumptions(A);
89*b8a825b4SJoseph Huber }
90*b8a825b4SJoseph Huber 
91*b8a825b4SJoseph Huber DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) {
92*b8a825b4SJoseph Huber   const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
93*b8a825b4SJoseph Huber   return ::getAssumptions(A);
94*b8a825b4SJoseph Huber }
95*b8a825b4SJoseph Huber 
96*b8a825b4SJoseph Huber bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) {
97*b8a825b4SJoseph Huber   return ::addAssumptionsImpl(F, Assumptions);
98*b8a825b4SJoseph Huber }
99*b8a825b4SJoseph Huber 
100*b8a825b4SJoseph Huber bool llvm::addAssumptions(CallBase &CB,
101*b8a825b4SJoseph Huber                           const DenseSet<StringRef> &Assumptions) {
102*b8a825b4SJoseph Huber   return ::addAssumptionsImpl(CB, Assumptions);
103*b8a825b4SJoseph Huber }
104*b8a825b4SJoseph Huber 
105b9c77542SJohannes Doerfert StringSet<> llvm::KnownAssumptionStrings({
106b9c77542SJohannes Doerfert     "omp_no_openmp",          // OpenMP 5.1
107b9c77542SJohannes Doerfert     "omp_no_openmp_routines", // OpenMP 5.1
108b9c77542SJohannes Doerfert     "omp_no_parallelism",     // OpenMP 5.1
109514c033dSJohannes Doerfert     "ompx_spmd_amenable",     // OpenMPOpt extension
110b9c77542SJohannes Doerfert });
111