[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / IR / Assumptions.cpp
blob27977d5d56b0de2cd4bf95e0ec5e507985f4d8c7
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.
12 //===----------------------------------------------------------------------===//
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"
21 using namespace llvm;
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!");
30 SmallVector<StringRef, 8> Strings;
31 A.getValueAsString().split(Strings, ",");
33 return llvm::is_contained(Strings, AssumptionStr);
36 DenseSet<StringRef> getAssumptions(const Attribute &A) {
37 if (!A.isValid())
38 return DenseSet<StringRef>();
39 assert(A.isStringAttribute() && "Expected a string attribute!");
41 DenseSet<StringRef> Assumptions;
42 SmallVector<StringRef, 8> Strings;
43 A.getValueAsString().split(Strings, ",");
45 for (StringRef Str : Strings)
46 Assumptions.insert(Str);
47 return Assumptions;
50 template <typename AttrSite>
51 bool addAssumptionsImpl(AttrSite &Site,
52 const DenseSet<StringRef> &Assumptions) {
53 if (Assumptions.empty())
54 return false;
56 DenseSet<StringRef> CurAssumptions = getAssumptions(Site);
58 if (!set_union(CurAssumptions, Assumptions))
59 return false;
61 LLVMContext &Ctx = Site.getContext();
62 Site.addFnAttr(llvm::Attribute::get(
63 Ctx, llvm::AssumptionAttrKey,
64 llvm::join(CurAssumptions.begin(), CurAssumptions.end(), ",")));
66 return true;
68 } // namespace
70 bool llvm::hasAssumption(const Function &F,
71 const KnownAssumptionString &AssumptionStr) {
72 const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
73 return ::hasAssumption(A, AssumptionStr);
76 bool llvm::hasAssumption(const CallBase &CB,
77 const KnownAssumptionString &AssumptionStr) {
78 if (Function *F = CB.getCalledFunction())
79 if (hasAssumption(*F, AssumptionStr))
80 return true;
82 const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
83 return ::hasAssumption(A, AssumptionStr);
86 DenseSet<StringRef> llvm::getAssumptions(const Function &F) {
87 const Attribute &A = F.getFnAttribute(AssumptionAttrKey);
88 return ::getAssumptions(A);
91 DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) {
92 const Attribute &A = CB.getFnAttr(AssumptionAttrKey);
93 return ::getAssumptions(A);
96 bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) {
97 return ::addAssumptionsImpl(F, Assumptions);
100 bool llvm::addAssumptions(CallBase &CB,
101 const DenseSet<StringRef> &Assumptions) {
102 return ::addAssumptionsImpl(CB, Assumptions);
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