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(A&: Strings, Separator: "," ); |
32 | |
33 | return llvm::is_contained(Range&: Strings, Element: 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(A&: Strings, Separator: "," ); |
44 | |
45 | for (StringRef Str : Strings) |
46 | Assumptions.insert(V: 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(S1&: CurAssumptions, S2: Assumptions)) |
59 | return false; |
60 | |
61 | LLVMContext &Ctx = Site.getContext(); |
62 | Site.addFnAttr(llvm::Attribute::get( |
63 | Context&: Ctx, Kind: llvm::AssumptionAttrKey, |
64 | Val: llvm::join(Begin: CurAssumptions.begin(), End: CurAssumptions.end(), Separator: "," ))); |
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(Kind: 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: *F, AssumptionStr)) |
80 | return true; |
81 | |
82 | const Attribute &A = CB.getFnAttr(Kind: AssumptionAttrKey); |
83 | return ::hasAssumption(A, AssumptionStr); |
84 | } |
85 | |
86 | DenseSet<StringRef> llvm::getAssumptions(const Function &F) { |
87 | const Attribute &A = F.getFnAttribute(Kind: AssumptionAttrKey); |
88 | return ::getAssumptions(A); |
89 | } |
90 | |
91 | DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) { |
92 | const Attribute &A = CB.getFnAttr(Kind: AssumptionAttrKey); |
93 | return ::getAssumptions(A); |
94 | } |
95 | |
96 | bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) { |
97 | return ::addAssumptionsImpl(Site&: F, Assumptions); |
98 | } |
99 | |
100 | bool llvm::addAssumptions(CallBase &CB, |
101 | const DenseSet<StringRef> &Assumptions) { |
102 | return ::addAssumptionsImpl(Site&: 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 | |