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