1b9c77542SJohannes Doerfert //===- Assumptions.cpp ------ Collection of helpers for assumptions -------===// 2b9c77542SJohannes Doerfert // 3b9c77542SJohannes Doerfert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4b9c77542SJohannes Doerfert // See https://llvm.org/LICENSE.txt for license information. 5b9c77542SJohannes Doerfert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6b9c77542SJohannes Doerfert // 7b9c77542SJohannes Doerfert //===----------------------------------------------------------------------===// 8b9c77542SJohannes Doerfert // 9b8a825b4SJoseph Huber // This file implements helper functions for accessing assumption infomration 10b8a825b4SJoseph Huber // inside of the "llvm.assume" metadata. 11b8a825b4SJoseph Huber // 12b9c77542SJohannes Doerfert //===----------------------------------------------------------------------===// 13b9c77542SJohannes Doerfert 14b9c77542SJohannes Doerfert #include "llvm/IR/Assumptions.h" 15b8a825b4SJoseph Huber #include "llvm/ADT/SetOperations.h" 16b8a825b4SJoseph Huber #include "llvm/ADT/StringExtras.h" 17b9c77542SJohannes Doerfert #include "llvm/IR/Attributes.h" 18b9c77542SJohannes Doerfert #include "llvm/IR/Function.h" 199e2fc0baSJoseph Huber #include "llvm/IR/InstrTypes.h" 20b9c77542SJohannes Doerfert 21b9c77542SJohannes Doerfert using namespace llvm; 22b9c77542SJohannes Doerfert 239e2fc0baSJoseph Huber namespace { 249e2fc0baSJoseph Huber bool hasAssumption(const Attribute &A, 25b9c77542SJohannes Doerfert const KnownAssumptionString &AssumptionStr) { 26b9c77542SJohannes Doerfert if (!A.isValid()) 27b9c77542SJohannes Doerfert return false; 28b9c77542SJohannes Doerfert assert(A.isStringAttribute() && "Expected a string attribute!"); 29b9c77542SJohannes Doerfert 30b9c77542SJohannes Doerfert SmallVector<StringRef, 8> Strings; 31b9c77542SJohannes Doerfert A.getValueAsString().split(Strings, ","); 32b9c77542SJohannes Doerfert 3381e9c906SKazu Hirata return llvm::is_contained(Strings, AssumptionStr); 34b9c77542SJohannes Doerfert } 35b8a825b4SJoseph Huber 36b8a825b4SJoseph Huber DenseSet<StringRef> getAssumptions(const Attribute &A) { 37b8a825b4SJoseph Huber if (!A.isValid()) 38b8a825b4SJoseph Huber return DenseSet<StringRef>(); 39b8a825b4SJoseph Huber assert(A.isStringAttribute() && "Expected a string attribute!"); 40b8a825b4SJoseph Huber 41b8a825b4SJoseph Huber DenseSet<StringRef> Assumptions; 42b8a825b4SJoseph Huber SmallVector<StringRef, 8> Strings; 43b8a825b4SJoseph Huber A.getValueAsString().split(Strings, ","); 44b8a825b4SJoseph Huber 45b8a825b4SJoseph Huber for (StringRef Str : Strings) 46b8a825b4SJoseph Huber Assumptions.insert(Str); 47b8a825b4SJoseph Huber return Assumptions; 48b8a825b4SJoseph Huber } 49b8a825b4SJoseph Huber 50b8a825b4SJoseph Huber template <typename AttrSite> 51b8a825b4SJoseph Huber bool addAssumptionsImpl(AttrSite &Site, 52b8a825b4SJoseph Huber const DenseSet<StringRef> &Assumptions) { 53b8a825b4SJoseph Huber if (Assumptions.empty()) 54b8a825b4SJoseph Huber return false; 55b8a825b4SJoseph Huber 56b8a825b4SJoseph Huber DenseSet<StringRef> CurAssumptions = getAssumptions(Site); 57b8a825b4SJoseph Huber 58b8a825b4SJoseph Huber if (!set_union(CurAssumptions, Assumptions)) 59b8a825b4SJoseph Huber return false; 60b8a825b4SJoseph Huber 61b8a825b4SJoseph Huber LLVMContext &Ctx = Site.getContext(); 62b8a825b4SJoseph Huber Site.addFnAttr(llvm::Attribute::get( 63b8a825b4SJoseph Huber Ctx, llvm::AssumptionAttrKey, 64b8a825b4SJoseph Huber llvm::join(CurAssumptions.begin(), CurAssumptions.end(), ","))); 65b8a825b4SJoseph Huber 66b8a825b4SJoseph Huber return true; 67b8a825b4SJoseph Huber } 689e2fc0baSJoseph Huber } // namespace 699e2fc0baSJoseph Huber 70b8a825b4SJoseph Huber bool llvm::hasAssumption(const Function &F, 719e2fc0baSJoseph Huber const KnownAssumptionString &AssumptionStr) { 729e2fc0baSJoseph Huber const Attribute &A = F.getFnAttribute(AssumptionAttrKey); 739e2fc0baSJoseph Huber return ::hasAssumption(A, AssumptionStr); 749e2fc0baSJoseph Huber } 759e2fc0baSJoseph Huber 76b8a825b4SJoseph Huber bool llvm::hasAssumption(const CallBase &CB, 779e2fc0baSJoseph Huber const KnownAssumptionString &AssumptionStr) { 789e2fc0baSJoseph Huber if (Function *F = CB.getCalledFunction()) 799e2fc0baSJoseph Huber if (hasAssumption(*F, AssumptionStr)) 809e2fc0baSJoseph Huber return true; 819e2fc0baSJoseph Huber 829e2fc0baSJoseph Huber const Attribute &A = CB.getFnAttr(AssumptionAttrKey); 839e2fc0baSJoseph Huber return ::hasAssumption(A, AssumptionStr); 849e2fc0baSJoseph Huber } 85b9c77542SJohannes Doerfert 86b8a825b4SJoseph Huber DenseSet<StringRef> llvm::getAssumptions(const Function &F) { 87b8a825b4SJoseph Huber const Attribute &A = F.getFnAttribute(AssumptionAttrKey); 88b8a825b4SJoseph Huber return ::getAssumptions(A); 89b8a825b4SJoseph Huber } 90b8a825b4SJoseph Huber 91b8a825b4SJoseph Huber DenseSet<StringRef> llvm::getAssumptions(const CallBase &CB) { 92b8a825b4SJoseph Huber const Attribute &A = CB.getFnAttr(AssumptionAttrKey); 93b8a825b4SJoseph Huber return ::getAssumptions(A); 94b8a825b4SJoseph Huber } 95b8a825b4SJoseph Huber 96b8a825b4SJoseph Huber bool llvm::addAssumptions(Function &F, const DenseSet<StringRef> &Assumptions) { 97b8a825b4SJoseph Huber return ::addAssumptionsImpl(F, Assumptions); 98b8a825b4SJoseph Huber } 99b8a825b4SJoseph Huber 100b8a825b4SJoseph Huber bool llvm::addAssumptions(CallBase &CB, 101b8a825b4SJoseph Huber const DenseSet<StringRef> &Assumptions) { 102b8a825b4SJoseph Huber return ::addAssumptionsImpl(CB, Assumptions); 103b8a825b4SJoseph Huber } 104b8a825b4SJoseph Huber 105b9c77542SJohannes Doerfert StringSet<> llvm::KnownAssumptionStrings({ 106b9c77542SJohannes Doerfert "omp_no_openmp", // OpenMP 5.1 107b9c77542SJohannes Doerfert "omp_no_openmp_routines", // OpenMP 5.1 108b9c77542SJohannes Doerfert "omp_no_parallelism", // OpenMP 5.1 109514c033dSJohannes Doerfert "ompx_spmd_amenable", // OpenMPOpt extension 1107df2eba7SJohannes Doerfert "ompx_no_call_asm", // OpenMPOpt extension 111*723a3e74SJoseph Huber "ompx_aligned_barrier", // OpenMPOpt extension 112b9c77542SJohannes Doerfert }); 113