LLVM  12.0.0git
AssumeBundleQueries.h
Go to the documentation of this file.
1 //===- AssumeBundleQueries.h - utilities to query assume bundles *- 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 // This file contain tools to query into assume bundles. assume bundles can be
10 // built using utilities from Transform/Utils/AssumeBundleBuilder.h
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_UTILS_ASSUMEBUNDLEQUERIES_H
15 #define LLVM_TRANSFORMS_UTILS_ASSUMEBUNDLEQUERIES_H
16 
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/ADT/DenseMap.h"
20 
21 namespace llvm {
22 class IntrinsicInst;
23 class AssumptionCache;
24 class DominatorTree;
25 
26 /// Index of elements in the operand bundle.
27 /// If the element exist it is guaranteed to be what is specified in this enum
28 /// but it may not exist.
30  ABA_WasOn = 0,
32 };
33 
34 /// Query the operand bundle of an llvm.assume to find a single attribute of
35 /// the specified kind applied on a specified Value.
36 ///
37 /// This has a non-constant complexity. It should only be used when a single
38 /// attribute is going to be queried.
39 ///
40 /// Return true iff the queried attribute was found.
41 /// If ArgVal is set. the argument will be stored to ArgVal.
42 bool hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn, StringRef AttrName,
43  uint64_t *ArgVal = nullptr);
44 inline bool hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn,
46  uint64_t *ArgVal = nullptr) {
47  return hasAttributeInAssume(AssumeCI, IsOn,
49 }
50 
51 template<> struct DenseMapInfo<Attribute::AttrKind> {
53  return Attribute::EmptyKey;
54  }
57  }
58  static unsigned getHashValue(Attribute::AttrKind AK) {
59  return hash_combine(AK);
60  }
62  return LHS == RHS;
63  }
64 };
65 
66 /// The map Key contains the Value on for which the attribute is valid and
67 /// the Attribute that is valid for that value.
68 /// If the Attribute is not on any value, the Value is nullptr.
69 using RetainedKnowledgeKey = std::pair<Value *, Attribute::AttrKind>;
70 
71 struct MinMax {
72  unsigned Min;
73  unsigned Max;
74 };
75 
76 /// A mapping from intrinsics (=`llvm.assume` calls) to a value range
77 /// (=knowledge) that is encoded in them. How the value range is interpreted
78 /// depends on the RetainedKnowledgeKey that was used to get this out of the
79 /// RetainedKnowledgeMap.
81 
84 
85 /// Insert into the map all the informations contained in the operand bundles of
86 /// the llvm.assume. This should be used instead of hasAttributeInAssume when
87 /// many queries are going to be made on the same llvm.assume.
88 /// String attributes are not inserted in the map.
89 /// If the IR changes the map will be outdated.
90 void fillMapFromAssume(CallInst &AssumeCI, RetainedKnowledgeMap &Result);
91 
92 /// Represent one information held inside an operand bundle of an llvm.assume.
93 /// AttrKind is the property that holds.
94 /// WasOn if not null is that Value for which AttrKind holds.
95 /// ArgValue is optionally an argument of the attribute.
96 /// For example if we know that %P has an alignment of at least four:
97 /// - AttrKind will be Attribute::Alignment.
98 /// - WasOn will be %P.
99 /// - ArgValue will be 4.
102  unsigned ArgValue = 0;
103  Value *WasOn = nullptr;
105  return AttrKind == Other.AttrKind && WasOn == Other.WasOn &&
106  ArgValue == Other.ArgValue;
107  }
108  bool operator!=(RetainedKnowledge Other) const { return !(*this == Other); }
109  operator bool() const { return AttrKind != Attribute::None; }
110  static RetainedKnowledge none() { return RetainedKnowledge{}; }
111 };
112 
113 /// Retreive the information help by Assume on the operand at index Idx.
114 /// Assume should be an llvm.assume and Idx should be in the operand bundle.
115 RetainedKnowledge getKnowledgeFromOperandInAssume(CallInst &Assume,
116  unsigned Idx);
117 
118 /// Retreive the information help by the Use U of an llvm.assume. the use should
119 /// be in the operand bundle.
121  return getKnowledgeFromOperandInAssume(*cast<CallInst>(U->getUser()),
122  U->getOperandNo());
123 }
124 
125 /// Tag in operand bundle indicating that this bundle should be ignored.
126 constexpr StringRef IgnoreBundleTag = "ignore";
127 
128 /// Return true iff the operand bundles of the provided llvm.assume doesn't
129 /// contain any valuable information. This is true when:
130 /// - The operand bundle is empty
131 /// - The operand bundle only contains information about dropped values or
132 /// constant folded values.
133 ///
134 /// the argument to the call of llvm.assume may still be useful even if the
135 /// function returned true.
136 bool isAssumeWithEmptyBundle(CallInst &Assume);
137 
138 /// Return a valid Knowledge associated to the Use U if its Attribute kind is
139 /// in AttrKinds.
142 
143 /// Return a valid Knowledge associated to the Value V if its Attribute kind is
144 /// in AttrKinds and it matches the Filter.
146  const Value *V, ArrayRef<Attribute::AttrKind> AttrKinds,
147  AssumptionCache *AC = nullptr,
149  const CallBase::BundleOpInfo *)>
150  Filter = [](auto...) { return true; });
151 
152 /// Return a valid Knowledge associated to the Value V if its Attribute kind is
153 /// in AttrKinds and the knowledge is suitable to be used in the context of
154 /// CtxI.
155 RetainedKnowledge getKnowledgeValidInContext(
156  const Value *V, ArrayRef<Attribute::AttrKind> AttrKinds,
157  const Instruction *CtxI, const DominatorTree *DT = nullptr,
158  AssumptionCache *AC = nullptr);
159 
160 /// This extracts the Knowledge from an element of an operand bundle.
161 /// This is mostly for use in the assume builder.
162 RetainedKnowledge getKnowledgeFromBundle(CallInst &Assume,
163  const CallBase::BundleOpInfo &BOI);
164 
165 } // namespace llvm
166 
167 #endif
Use as Tombstone key for DenseMap of AttrKind.
Definition: Attributes.h:78
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static bool isEqual(Attribute::AttrKind LHS, Attribute::AttrKind RHS)
This class represents a function call, abstracting a target machine's calling convention.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:176
A cache of @llvm.assume calls within a function.
RetainedKnowledge getKnowledgeFromOperandInAssume(CallInst &Assume, unsigned Idx)
Retreive the information help by Assume on the operand at index Idx.
static Attribute::AttrKind getTombstoneKey()
bool operator!=(RetainedKnowledge Other) const
RetainedKnowledge getKnowledgeForValue(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache *AC=nullptr, function_ref< bool(RetainedKnowledge, Instruction *, const CallBase::BundleOpInfo *)> Filter=[](auto...) { return true;})
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and it match...
Used to keep track of an operand bundle.
Definition: InstrTypes.h:2075
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
This file contains the simple types necessary to represent the attributes associated with functions a...
No attributes have been set.
Definition: Attributes.h:72
static StringRef getNameFromAttrKind(Attribute::AttrKind AttrKind)
Definition: Attributes.cpp:204
Use as Empty key for DenseMap of AttrKind.
Definition: Attributes.h:77
constexpr StringRef IgnoreBundleTag
Tag in operand bundle indicating that this bundle should be ignored.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
static unsigned getHashValue(Attribute::AttrKind AK)
Attribute::AttrKind AttrKind
RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, const Instruction *CtxI, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
bool hasAttributeInAssume(CallInst &AssumeCI, Value *IsOn, StringRef AttrName, uint64_t *ArgVal=nullptr)
Query the operand bundle of an llvm.assume to find a single attribute of the specified kind applied o...
bool operator==(RetainedKnowledge Other) const
void fillMapFromAssume(CallInst &AssumeCI, RetainedKnowledgeMap &Result)
Insert into the map all the informations contained in the operand bundles of the llvm....
static RetainedKnowledge none()
std::pair< Value *, Attribute::AttrKind > RetainedKnowledgeKey
The map Key contains the Value on for which the attribute is valid and the Attribute that is valid fo...
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:604
AssumeBundleArg
Index of elements in the operand bundle.
RetainedKnowledge getKnowledgeFromUseInAssume(const Use *U)
Retreive the information help by the Use U of an llvm.assume.
static Attribute::AttrKind getEmptyKey()
bool isAssumeWithEmptyBundle(CallInst &Assume)
Return true iff the operand bundles of the provided llvm.assume doesn't contain any valuable informat...
LLVM Value Representation.
Definition: Value.h:75
RetainedKnowledge getKnowledgeFromBundle(CallInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
RetainedKnowledge getKnowledgeFromUse(const Use *U, ArrayRef< Attribute::AttrKind > AttrKinds)
Return a valid Knowledge associated to the Use U if its Attribute kind is in AttrKinds.
Represent one information held inside an operand bundle of an llvm.assume.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:70