xref: /llvm-project/llvm/lib/IR/Assumptions.cpp (revision 81e9c90686f7bfbdd721b8e1a780152c95c258b0)
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::is_contained(Strings, AssumptionStr);
29 }
30 } // namespace
31 
32 bool llvm::hasAssumption(Function &F,
33                          const KnownAssumptionString &AssumptionStr) {
34   const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
35   return ::hasAssumption(A, AssumptionStr);
36 }
37 
38 bool llvm::hasAssumption(CallBase &CB,
39                          const KnownAssumptionString &AssumptionStr) {
40   if (Function *F = CB.getCalledFunction())
41     if (hasAssumption(*F, AssumptionStr))
42       return true;
43 
44   const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
45   return ::hasAssumption(A, AssumptionStr);
46 }
47 
48 StringSet<> llvm::KnownAssumptionStrings({
49     "omp_no_openmp",          // OpenMP 5.1
50     "omp_no_openmp_routines", // OpenMP 5.1
51     "omp_no_parallelism",     // OpenMP 5.1
52     "ompx_spmd_amenable",     // OpenMPOpt extension
53 });
54