xref: /freebsd/contrib/llvm-project/llvm/lib/IR/Assumptions.cpp (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
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 
21 using namespace llvm;
22 
23 namespace {
24 bool hasAssumption(const Attribute &A,
25                    const KnownAssumptionString &AssumptionStr) {
26   if (!A.isValid())
27     return false;
28   assert(A.isStringAttribute() && "Expected a string attribute!");
29 
30   SmallVector<StringRef, 8> Strings;
31   A.getValueAsString().split(Strings, ",");
32 
33   return llvm::is_contained(Strings, AssumptionStr);
34 }
35 
36 DenseSet<StringRef> getAssumptions(const Attribute &A) {
37   if (!A.isValid())
38     return DenseSet<StringRef>();
39   assert(A.isStringAttribute() && "Expected a string attribute!");
40 
41   DenseSet<StringRef> Assumptions;
42   SmallVector<StringRef, 8> Strings;
43   A.getValueAsString().split(Strings, ",");
44 
45   for (StringRef Str : Strings)
46     Assumptions.insert(Str);
47   return Assumptions;
48 }
49 
50 template <typename AttrSite>
51 bool addAssumptionsImpl(AttrSite &Site,
52                         const DenseSet<StringRef> &Assumptions) {
53   if (Assumptions.empty())
54     return false;
55 
56   DenseSet<StringRef> CurAssumptions = getAssumptions(Site);
57 
58   if (!set_union(CurAssumptions, Assumptions))
59     return false;
60 
61   LLVMContext &Ctx = Site.getContext();
62   Site.addFnAttr(llvm::Attribute::get(
63       Ctx, llvm::AssumptionAttrKey,
64       llvm::join(CurAssumptions.begin(), CurAssumptions.end(), ",")));
65 
66   return true;
67 }
68 } // namespace
69 
70 bool llvm::hasAssumption(const Function &F,
71                          const KnownAssumptionString &AssumptionStr) {
72   const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
73   return ::hasAssumption(A, AssumptionStr);
74 }
75 
76 bool llvm::hasAssumption(const CallBase &CB,
77                          const KnownAssumptionString &AssumptionStr) {
78   if (Function *F = CB.getCalledFunction())
79     if (hasAssumption(*F, AssumptionStr))
80       return true;
81 
82   const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
83   return ::hasAssumption(A, AssumptionStr);
84 }
85 
86 DenseSet<StringRef> llvm::getAssumptions(const Function &F) {
87   const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
88   return ::getAssumptions(A);
89 }
90 
91 DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) {
92   const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
93   return ::getAssumptions(A);
94 }
95 
96 bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) {
97   return ::addAssumptionsImpl(F, Assumptions);
98 }
99 
100 bool llvm::addAssumptions(CallBase &CB,
101                           const DenseSet<StringRef> &Assumptions) {
102   return ::addAssumptionsImpl(CB, Assumptions);
103 }
104 
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
111 });
112