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 //===----------------------------------------------------------------------===//
10 
11 #include "llvm/IR/Assumptions.h"
12 #include "llvm/IR/Attributes.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/InstrTypes.h"
15 
16 using namespace llvm;
17 
18 namespace {
19 bool hasAssumption(const Attribute &A,
20                    const KnownAssumptionString &AssumptionStr) {
21   if (!A.isValid())
22     return false;
23   assert(A.isStringAttribute() && "Expected a string attribute!");
24 
25   SmallVector<StringRef, 8> Strings;
26   A.getValueAsString().split(Strings, ",");
27 
28   return llvm::any_of(Strings, [=](StringRef Assumption) {
29     return Assumption == AssumptionStr;
30   });
31 }
32 } // namespace
33 
34 bool llvm::hasAssumption(Function &F,
35                          const KnownAssumptionString &AssumptionStr) {
36   const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
37   return ::hasAssumption(A, AssumptionStr);
38 }
39 
40 bool llvm::hasAssumption(CallBase &CB,
41                          const KnownAssumptionString &AssumptionStr) {
42   if (Function *F = CB.getCalledFunction())
43     if (hasAssumption(*F, AssumptionStr))
44       return true;
45 
46   const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
47   return ::hasAssumption(A, AssumptionStr);
48 }
49 
50 StringSet<> llvm::KnownAssumptionStrings({
51     "omp_no_openmp",          // OpenMP 5.1
52     "omp_no_openmp_routines", // OpenMP 5.1
53     "omp_no_parallelism",     // OpenMP 5.1
54     "ompx_spmd_amenable",     // OpenMPOpt extension
55 });
56