1 //===--- Assumptions.h - Assumption handling and organization ---*- C++ -*-===// 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 // String assumptions that are known to optimization passes should be placed in 10 // the KnownAssumptionStrings set. This can be done in various ways, i.a., 11 // via a static KnownAssumptionString object. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_IR_ASSUMPTIONS_H 16 #define LLVM_IR_ASSUMPTIONS_H 17 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/StringSet.h" 21 #include "llvm/Support/Compiler.h" 22 23 namespace llvm { 24 25 class Function; 26 class CallBase; 27 28 /// The key we use for assumption attributes. 29 constexpr StringRef AssumptionAttrKey = "llvm.assume"; 30 31 /// A set of known assumption strings that are accepted without warning and 32 /// which can be recommended as typo correction. 33 LLVM_ABI extern StringSet<> KnownAssumptionStrings; 34 35 /// Helper that allows to insert a new assumption string in the known assumption 36 /// set by creating a (static) object. 37 struct KnownAssumptionString { KnownAssumptionStringKnownAssumptionString38 KnownAssumptionString(const char *AssumptionStr) 39 : AssumptionStr(AssumptionStr) { 40 KnownAssumptionStrings.insert(AssumptionStr); 41 } KnownAssumptionStringKnownAssumptionString42 KnownAssumptionString(StringRef AssumptionStr) 43 : AssumptionStr(AssumptionStr) { 44 KnownAssumptionStrings.insert(AssumptionStr); 45 } StringRefKnownAssumptionString46 operator StringRef() const { return AssumptionStr; } 47 48 private: 49 StringRef AssumptionStr; 50 }; 51 52 /// Return true if \p F has the assumption \p AssumptionStr attached. 53 LLVM_ABI bool hasAssumption(const Function &F, 54 const KnownAssumptionString &AssumptionStr); 55 56 /// Return true if \p CB or the callee has the assumption \p AssumptionStr 57 /// attached. 58 LLVM_ABI bool hasAssumption(const CallBase &CB, 59 const KnownAssumptionString &AssumptionStr); 60 61 /// Return the set of all assumptions for the function \p F. 62 LLVM_ABI DenseSet<StringRef> getAssumptions(const Function &F); 63 64 /// Return the set of all assumptions for the call \p CB. 65 LLVM_ABI DenseSet<StringRef> getAssumptions(const CallBase &CB); 66 67 /// Appends the set of assumptions \p Assumptions to \F. 68 LLVM_ABI bool addAssumptions(Function &F, 69 const DenseSet<StringRef> &Assumptions); 70 71 /// Appends the set of assumptions \p Assumptions to \CB. 72 LLVM_ABI bool addAssumptions(CallBase &CB, 73 const DenseSet<StringRef> &Assumptions); 74 75 } // namespace llvm 76 77 #endif 78