| 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 | Assumptions.insert_range(R&: Strings); |
| 46 | return Assumptions; |
| 47 | } |
| 48 | |
| 49 | template <typename AttrSite> |
| 50 | bool addAssumptionsImpl(AttrSite &Site, |
| 51 | const DenseSet<StringRef> &Assumptions) { |
| 52 | if (Assumptions.empty()) |
| 53 | return false; |
| 54 | |
| 55 | DenseSet<StringRef> CurAssumptions = getAssumptions(Site); |
| 56 | |
| 57 | if (!set_union(S1&: CurAssumptions, S2: Assumptions)) |
| 58 | return false; |
| 59 | |
| 60 | LLVMContext &Ctx = Site.getContext(); |
| 61 | Site.addFnAttr(llvm::Attribute::get( |
| 62 | Context&: Ctx, Kind: llvm::AssumptionAttrKey, |
| 63 | Val: llvm::join(Begin: CurAssumptions.begin(), End: CurAssumptions.end(), Separator: "," ))); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | bool llvm::hasAssumption(const Function &F, |
| 70 | const KnownAssumptionString &AssumptionStr) { |
| 71 | const Attribute &A = F.getFnAttribute(Kind: AssumptionAttrKey); |
| 72 | return ::hasAssumption(A, AssumptionStr); |
| 73 | } |
| 74 | |
| 75 | bool llvm::hasAssumption(const CallBase &CB, |
| 76 | const KnownAssumptionString &AssumptionStr) { |
| 77 | if (Function *F = CB.getCalledFunction()) |
| 78 | if (hasAssumption(F: *F, AssumptionStr)) |
| 79 | return true; |
| 80 | |
| 81 | const Attribute &A = CB.getFnAttr(Kind: AssumptionAttrKey); |
| 82 | return ::hasAssumption(A, AssumptionStr); |
| 83 | } |
| 84 | |
| 85 | DenseSet<StringRef> llvm::getAssumptions(const Function &F) { |
| 86 | const Attribute &A = F.getFnAttribute(Kind: AssumptionAttrKey); |
| 87 | return ::getAssumptions(A); |
| 88 | } |
| 89 | |
| 90 | DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) { |
| 91 | const Attribute &A = CB.getFnAttr(Kind: AssumptionAttrKey); |
| 92 | return ::getAssumptions(A); |
| 93 | } |
| 94 | |
| 95 | bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) { |
| 96 | return ::addAssumptionsImpl(Site&: F, Assumptions); |
| 97 | } |
| 98 | |
| 99 | bool llvm::addAssumptions(CallBase &CB, |
| 100 | const DenseSet<StringRef> &Assumptions) { |
| 101 | return ::addAssumptionsImpl(Site&: CB, Assumptions); |
| 102 | } |
| 103 | |
| 104 | StringSet<> llvm::KnownAssumptionStrings({ |
| 105 | "omp_no_openmp" , // OpenMP 5.1 |
| 106 | "omp_no_openmp_routines" , // OpenMP 5.1 |
| 107 | "omp_no_parallelism" , // OpenMP 5.1 |
| 108 | "omp_no_openmp_constructs" , // OpenMP 6.0 |
| 109 | "ompx_spmd_amenable" , // OpenMPOpt extension |
| 110 | "ompx_no_call_asm" , // OpenMPOpt extension |
| 111 | "ompx_aligned_barrier" , // OpenMPOpt extension |
| 112 | }); |
| 113 | |