Bug Summary

File:src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support/Alignment.h
Warning:line 85, column 47
The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'uint64_t'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name Attributor.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/IPO/Attributor.cpp

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Transforms/IPO/Attributor.cpp

1//===- Attributor.cpp - Module-wide attribute deduction -------------------===//
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 an interprocedural pass that deduces and/or propagates
10// attributes. This is done in an abstract interpretation style fixpoint
11// iteration. See the Attributor.h file comment and the class descriptions in
12// that file for more information.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Transforms/IPO/Attributor.h"
17
18#include "llvm/ADT/GraphTraits.h"
19#include "llvm/ADT/PointerIntPair.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/TinyPtrVector.h"
23#include "llvm/Analysis/InlineCost.h"
24#include "llvm/Analysis/LazyValueInfo.h"
25#include "llvm/Analysis/MemorySSAUpdater.h"
26#include "llvm/Analysis/MustExecute.h"
27#include "llvm/Analysis/ValueTracking.h"
28#include "llvm/IR/Attributes.h"
29#include "llvm/IR/Constant.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/GlobalValue.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/IRBuilder.h"
34#include "llvm/IR/Instruction.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/NoFolder.h"
38#include "llvm/IR/ValueHandle.h"
39#include "llvm/IR/Verifier.h"
40#include "llvm/InitializePasses.h"
41#include "llvm/Support/Casting.h"
42#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/DebugCounter.h"
45#include "llvm/Support/FileSystem.h"
46#include "llvm/Support/GraphWriter.h"
47#include "llvm/Support/raw_ostream.h"
48#include "llvm/Transforms/Utils/BasicBlockUtils.h"
49#include "llvm/Transforms/Utils/Cloning.h"
50#include "llvm/Transforms/Utils/Local.h"
51
52#include <cassert>
53#include <string>
54
55using namespace llvm;
56
57#define DEBUG_TYPE"attributor" "attributor"
58
59DEBUG_COUNTER(ManifestDBGCounter, "attributor-manifest",static const unsigned ManifestDBGCounter = DebugCounter::registerCounter
("attributor-manifest", "Determine what attributes are manifested in the IR"
)
60 "Determine what attributes are manifested in the IR")static const unsigned ManifestDBGCounter = DebugCounter::registerCounter
("attributor-manifest", "Determine what attributes are manifested in the IR"
)
;
61
62STATISTIC(NumFnDeleted, "Number of function deleted")static llvm::Statistic NumFnDeleted = {"attributor", "NumFnDeleted"
, "Number of function deleted"}
;
63STATISTIC(NumFnWithExactDefinition,static llvm::Statistic NumFnWithExactDefinition = {"attributor"
, "NumFnWithExactDefinition", "Number of functions with exact definitions"
}
64 "Number of functions with exact definitions")static llvm::Statistic NumFnWithExactDefinition = {"attributor"
, "NumFnWithExactDefinition", "Number of functions with exact definitions"
}
;
65STATISTIC(NumFnWithoutExactDefinition,static llvm::Statistic NumFnWithoutExactDefinition = {"attributor"
, "NumFnWithoutExactDefinition", "Number of functions without exact definitions"
}
66 "Number of functions without exact definitions")static llvm::Statistic NumFnWithoutExactDefinition = {"attributor"
, "NumFnWithoutExactDefinition", "Number of functions without exact definitions"
}
;
67STATISTIC(NumFnShallowWrappersCreated, "Number of shallow wrappers created")static llvm::Statistic NumFnShallowWrappersCreated = {"attributor"
, "NumFnShallowWrappersCreated", "Number of shallow wrappers created"
}
;
68STATISTIC(NumAttributesTimedOut,static llvm::Statistic NumAttributesTimedOut = {"attributor",
"NumAttributesTimedOut", "Number of abstract attributes timed out before fixpoint"
}
69 "Number of abstract attributes timed out before fixpoint")static llvm::Statistic NumAttributesTimedOut = {"attributor",
"NumAttributesTimedOut", "Number of abstract attributes timed out before fixpoint"
}
;
70STATISTIC(NumAttributesValidFixpoint,static llvm::Statistic NumAttributesValidFixpoint = {"attributor"
, "NumAttributesValidFixpoint", "Number of abstract attributes in a valid fixpoint state"
}
71 "Number of abstract attributes in a valid fixpoint state")static llvm::Statistic NumAttributesValidFixpoint = {"attributor"
, "NumAttributesValidFixpoint", "Number of abstract attributes in a valid fixpoint state"
}
;
72STATISTIC(NumAttributesManifested,static llvm::Statistic NumAttributesManifested = {"attributor"
, "NumAttributesManifested", "Number of abstract attributes manifested in IR"
}
73 "Number of abstract attributes manifested in IR")static llvm::Statistic NumAttributesManifested = {"attributor"
, "NumAttributesManifested", "Number of abstract attributes manifested in IR"
}
;
74
75// TODO: Determine a good default value.
76//
77// In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
78// (when run with the first 5 abstract attributes). The results also indicate
79// that we never reach 32 iterations but always find a fixpoint sooner.
80//
81// This will become more evolved once we perform two interleaved fixpoint
82// iterations: bottom-up and top-down.
83static cl::opt<unsigned>
84 SetFixpointIterations("attributor-max-iterations", cl::Hidden,
85 cl::desc("Maximal number of fixpoint iterations."),
86 cl::init(32));
87
88static cl::opt<unsigned, true> MaxInitializationChainLengthX(
89 "attributor-max-initialization-chain-length", cl::Hidden,
90 cl::desc(
91 "Maximal number of chained initializations (to avoid stack overflows)"),
92 cl::location(MaxInitializationChainLength), cl::init(1024));
93unsigned llvm::MaxInitializationChainLength;
94
95static cl::opt<bool> VerifyMaxFixpointIterations(
96 "attributor-max-iterations-verify", cl::Hidden,
97 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
98 cl::init(false));
99
100static cl::opt<bool> AnnotateDeclarationCallSites(
101 "attributor-annotate-decl-cs", cl::Hidden,
102 cl::desc("Annotate call sites of function declarations."), cl::init(false));
103
104static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
105 cl::init(true), cl::Hidden);
106
107static cl::opt<bool>
108 AllowShallowWrappers("attributor-allow-shallow-wrappers", cl::Hidden,
109 cl::desc("Allow the Attributor to create shallow "
110 "wrappers for non-exact definitions."),
111 cl::init(false));
112
113static cl::opt<bool>
114 AllowDeepWrapper("attributor-allow-deep-wrappers", cl::Hidden,
115 cl::desc("Allow the Attributor to use IP information "
116 "derived from non-exact functions via cloning"),
117 cl::init(false));
118
119// These options can only used for debug builds.
120#ifndef NDEBUG1
121static cl::list<std::string>
122 SeedAllowList("attributor-seed-allow-list", cl::Hidden,
123 cl::desc("Comma seperated list of attribute names that are "
124 "allowed to be seeded."),
125 cl::ZeroOrMore, cl::CommaSeparated);
126
127static cl::list<std::string> FunctionSeedAllowList(
128 "attributor-function-seed-allow-list", cl::Hidden,
129 cl::desc("Comma seperated list of function names that are "
130 "allowed to be seeded."),
131 cl::ZeroOrMore, cl::CommaSeparated);
132#endif
133
134static cl::opt<bool>
135 DumpDepGraph("attributor-dump-dep-graph", cl::Hidden,
136 cl::desc("Dump the dependency graph to dot files."),
137 cl::init(false));
138
139static cl::opt<std::string> DepGraphDotFileNamePrefix(
140 "attributor-depgraph-dot-filename-prefix", cl::Hidden,
141 cl::desc("The prefix used for the CallGraph dot file names."));
142
143static cl::opt<bool> ViewDepGraph("attributor-view-dep-graph", cl::Hidden,
144 cl::desc("View the dependency graph."),
145 cl::init(false));
146
147static cl::opt<bool> PrintDependencies("attributor-print-dep", cl::Hidden,
148 cl::desc("Print attribute dependencies"),
149 cl::init(false));
150
151static cl::opt<bool> EnableCallSiteSpecific(
152 "attributor-enable-call-site-specific-deduction", cl::Hidden,
153 cl::desc("Allow the Attributor to do call site specific analysis"),
154 cl::init(false));
155
156static cl::opt<bool>
157 PrintCallGraph("attributor-print-call-graph", cl::Hidden,
158 cl::desc("Print Attributor's internal call graph"),
159 cl::init(false));
160
161static cl::opt<bool> SimplifyAllLoads("attributor-simplify-all-loads",
162 cl::Hidden,
163 cl::desc("Try to simplify all loads."),
164 cl::init(true));
165
166/// Logic operators for the change status enum class.
167///
168///{
169ChangeStatus llvm::operator|(ChangeStatus L, ChangeStatus R) {
170 return L == ChangeStatus::CHANGED ? L : R;
171}
172ChangeStatus &llvm::operator|=(ChangeStatus &L, ChangeStatus R) {
173 L = L | R;
174 return L;
175}
176ChangeStatus llvm::operator&(ChangeStatus L, ChangeStatus R) {
177 return L == ChangeStatus::UNCHANGED ? L : R;
178}
179ChangeStatus &llvm::operator&=(ChangeStatus &L, ChangeStatus R) {
180 L = L & R;
181 return L;
182}
183///}
184
185bool AA::isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
186 const Value &V) {
187 if (auto *C = dyn_cast<Constant>(&V))
188 return !C->isThreadDependent();
189 // TODO: Inspect and cache more complex instructions.
190 if (auto *CB = dyn_cast<CallBase>(&V))
191 return CB->getNumOperands() == 0 && !CB->mayHaveSideEffects() &&
192 !CB->mayReadFromMemory();
193 const Function *Scope = nullptr;
194 if (auto *I = dyn_cast<Instruction>(&V))
195 Scope = I->getFunction();
196 if (auto *A = dyn_cast<Argument>(&V))
197 Scope = A->getParent();
198 if (!Scope)
199 return false;
200 auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
201 QueryingAA, IRPosition::function(*Scope), DepClassTy::OPTIONAL);
202 return NoRecurseAA.isAssumedNoRecurse();
203}
204
205Constant *AA::getInitialValueForObj(Value &Obj, Type &Ty) {
206 if (isa<AllocaInst>(Obj))
207 return UndefValue::get(&Ty);
208 auto *GV = dyn_cast<GlobalVariable>(&Obj);
209 if (!GV || !GV->hasLocalLinkage())
210 return nullptr;
211 if (!GV->hasInitializer())
212 return UndefValue::get(&Ty);
213 return dyn_cast_or_null<Constant>(getWithType(*GV->getInitializer(), Ty));
214}
215
216bool AA::isValidInScope(const Value &V, const Function *Scope) {
217 if (isa<Constant>(V))
218 return true;
219 if (auto *I = dyn_cast<Instruction>(&V))
220 return I->getFunction() == Scope;
221 if (auto *A = dyn_cast<Argument>(&V))
222 return A->getParent() == Scope;
223 return false;
224}
225
226bool AA::isValidAtPosition(const Value &V, const Instruction &CtxI,
227 InformationCache &InfoCache) {
228 if (isa<Constant>(V))
229 return true;
230 const Function *Scope = CtxI.getFunction();
231 if (auto *A = dyn_cast<Argument>(&V))
232 return A->getParent() == Scope;
233 if (auto *I = dyn_cast<Instruction>(&V))
234 if (I->getFunction() == Scope) {
235 const DominatorTree *DT =
236 InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Scope);
237 return DT && DT->dominates(I, &CtxI);
238 }
239 return false;
240}
241
242Value *AA::getWithType(Value &V, Type &Ty) {
243 if (V.getType() == &Ty)
244 return &V;
245 if (isa<PoisonValue>(V))
246 return PoisonValue::get(&Ty);
247 if (isa<UndefValue>(V))
248 return UndefValue::get(&Ty);
249 if (auto *C = dyn_cast<Constant>(&V)) {
250 if (C->isNullValue())
251 return Constant::getNullValue(&Ty);
252 if (C->getType()->isPointerTy() && Ty.isPointerTy())
253 return ConstantExpr::getPointerCast(C, &Ty);
254 if (C->getType()->getPrimitiveSizeInBits() >= Ty.getPrimitiveSizeInBits()) {
255 if (C->getType()->isIntegerTy() && Ty.isIntegerTy())
256 return ConstantExpr::getTrunc(C, &Ty, /* OnlyIfReduced */ true);
257 if (C->getType()->isFloatingPointTy() && Ty.isFloatingPointTy())
258 return ConstantExpr::getFPTrunc(C, &Ty, /* OnlyIfReduced */ true);
259 }
260 }
261 return nullptr;
262}
263
264Optional<Value *>
265AA::combineOptionalValuesInAAValueLatice(const Optional<Value *> &A,
266 const Optional<Value *> &B, Type *Ty) {
267 if (A == B)
268 return A;
269 if (!B.hasValue())
270 return A;
271 if (*B == nullptr)
272 return nullptr;
273 if (!A.hasValue())
274 return Ty ? getWithType(**B, *Ty) : nullptr;
275 if (*A == nullptr)
276 return nullptr;
277 if (!Ty)
278 Ty = (*A)->getType();
279 if (isa_and_nonnull<UndefValue>(*A))
280 return getWithType(**B, *Ty);
281 if (isa<UndefValue>(*B))
282 return A;
283 if (*A && *B && *A == getWithType(**B, *Ty))
284 return A;
285 return nullptr;
286}
287
288bool AA::getPotentialCopiesOfStoredValue(
289 Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
290 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation) {
291
292 Value &Ptr = *SI.getPointerOperand();
293 SmallVector<Value *, 8> Objects;
294 if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, QueryingAA, &SI)) {
295 LLVM_DEBUG(do { } while (false)
296 dbgs() << "Underlying objects stored into could not be determined\n";)do { } while (false);
297 return false;
298 }
299
300 SmallVector<const AAPointerInfo *> PIs;
301 SmallVector<Value *> NewCopies;
302
303 for (Value *Obj : Objects) {
304 LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n")do { } while (false);
305 if (isa<UndefValue>(Obj))
306 continue;
307 if (isa<ConstantPointerNull>(Obj)) {
308 // A null pointer access can be undefined but any offset from null may
309 // be OK. We do not try to optimize the latter.
310 if (!NullPointerIsDefined(SI.getFunction(),
311 Ptr.getType()->getPointerAddressSpace()) &&
312 A.getAssumedSimplified(Ptr, QueryingAA, UsedAssumedInformation) ==
313 Obj)
314 continue;
315 LLVM_DEBUG(do { } while (false)
316 dbgs() << "Underlying object is a valid nullptr, giving up.\n";)do { } while (false);
317 return false;
318 }
319 if (!isa<AllocaInst>(Obj) && !isa<GlobalVariable>(Obj)) {
320 LLVM_DEBUG(dbgs() << "Underlying object is not supported yet: " << *Objdo { } while (false)
321 << "\n";)do { } while (false);
322 return false;
323 }
324 if (auto *GV = dyn_cast<GlobalVariable>(Obj))
325 if (!GV->hasLocalLinkage()) {
326 LLVM_DEBUG(dbgs() << "Underlying object is global with external "do { } while (false)
327 "linkage, not supported yet: "do { } while (false)
328 << *Obj << "\n";)do { } while (false);
329 return false;
330 }
331
332 auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
333 if (!Acc.isRead())
334 return true;
335 auto *LI = dyn_cast<LoadInst>(Acc.getRemoteInst());
336 if (!LI) {
337 LLVM_DEBUG(dbgs() << "Underlying object read through a non-load "do { } while (false)
338 "instruction not supported yet: "do { } while (false)
339 << *Acc.getRemoteInst() << "\n";)do { } while (false);
340 return false;
341 }
342 NewCopies.push_back(LI);
343 return true;
344 };
345
346 auto &PI = A.getAAFor<AAPointerInfo>(QueryingAA, IRPosition::value(*Obj),
347 DepClassTy::NONE);
348 if (!PI.forallInterferingAccesses(SI, CheckAccess)) {
349 LLVM_DEBUG(do { } while (false)
350 dbgs()do { } while (false)
351 << "Failed to verify all interfering accesses for underlying object: "do { } while (false)
352 << *Obj << "\n")do { } while (false);
353 return false;
354 }
355 PIs.push_back(&PI);
356 }
357
358 for (auto *PI : PIs) {
359 if (!PI->getState().isAtFixpoint())
360 UsedAssumedInformation = true;
361 A.recordDependence(*PI, QueryingAA, DepClassTy::OPTIONAL);
362 }
363 PotentialCopies.insert(NewCopies.begin(), NewCopies.end());
364
365 return true;
366}
367
368/// Return true if \p New is equal or worse than \p Old.
369static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
370 if (!Old.isIntAttribute())
371 return true;
372
373 return Old.getValueAsInt() >= New.getValueAsInt();
374}
375
376/// Return true if the information provided by \p Attr was added to the
377/// attribute list \p Attrs. This is only the case if it was not already present
378/// in \p Attrs at the position describe by \p PK and \p AttrIdx.
379static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
380 AttributeList &Attrs, int AttrIdx,
381 bool ForceReplace = false) {
382
383 if (Attr.isEnumAttribute()) {
384 Attribute::AttrKind Kind = Attr.getKindAsEnum();
385 if (Attrs.hasAttribute(AttrIdx, Kind))
386 if (!ForceReplace &&
387 isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
388 return false;
389 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
390 return true;
391 }
392 if (Attr.isStringAttribute()) {
393 StringRef Kind = Attr.getKindAsString();
394 if (Attrs.hasAttribute(AttrIdx, Kind))
395 if (!ForceReplace &&
396 isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
397 return false;
398 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
399 return true;
400 }
401 if (Attr.isIntAttribute()) {
402 Attribute::AttrKind Kind = Attr.getKindAsEnum();
403 if (Attrs.hasAttribute(AttrIdx, Kind))
404 if (!ForceReplace &&
405 isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
406 return false;
407 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
408 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
409 return true;
410 }
411
412 llvm_unreachable("Expected enum or string attribute!")__builtin_unreachable();
413}
414
415Argument *IRPosition::getAssociatedArgument() const {
416 if (getPositionKind() == IRP_ARGUMENT)
417 return cast<Argument>(&getAnchorValue());
418
419 // Not an Argument and no argument number means this is not a call site
420 // argument, thus we cannot find a callback argument to return.
421 int ArgNo = getCallSiteArgNo();
422 if (ArgNo < 0)
423 return nullptr;
424
425 // Use abstract call sites to make the connection between the call site
426 // values and the ones in callbacks. If a callback was found that makes use
427 // of the underlying call site operand, we want the corresponding callback
428 // callee argument and not the direct callee argument.
429 Optional<Argument *> CBCandidateArg;
430 SmallVector<const Use *, 4> CallbackUses;
431 const auto &CB = cast<CallBase>(getAnchorValue());
432 AbstractCallSite::getCallbackUses(CB, CallbackUses);
433 for (const Use *U : CallbackUses) {
434 AbstractCallSite ACS(U);
435 assert(ACS && ACS.isCallbackCall())((void)0);
436 if (!ACS.getCalledFunction())
437 continue;
438
439 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
440
441 // Test if the underlying call site operand is argument number u of the
442 // callback callee.
443 if (ACS.getCallArgOperandNo(u) != ArgNo)
444 continue;
445
446 assert(ACS.getCalledFunction()->arg_size() > u &&((void)0)
447 "ACS mapped into var-args arguments!")((void)0);
448 if (CBCandidateArg.hasValue()) {
449 CBCandidateArg = nullptr;
450 break;
451 }
452 CBCandidateArg = ACS.getCalledFunction()->getArg(u);
453 }
454 }
455
456 // If we found a unique callback candidate argument, return it.
457 if (CBCandidateArg.hasValue() && CBCandidateArg.getValue())
458 return CBCandidateArg.getValue();
459
460 // If no callbacks were found, or none used the underlying call site operand
461 // exclusively, use the direct callee argument if available.
462 const Function *Callee = CB.getCalledFunction();
463 if (Callee && Callee->arg_size() > unsigned(ArgNo))
464 return Callee->getArg(ArgNo);
465
466 return nullptr;
467}
468
469ChangeStatus AbstractAttribute::update(Attributor &A) {
470 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
471 if (getState().isAtFixpoint())
472 return HasChanged;
473
474 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n")do { } while (false);
475
476 HasChanged = updateImpl(A);
477
478 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *thisdo { } while (false)
479 << "\n")do { } while (false);
480
481 return HasChanged;
482}
483
484ChangeStatus
485IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
486 const ArrayRef<Attribute> &DeducedAttrs,
487 bool ForceReplace) {
488 Function *ScopeFn = IRP.getAnchorScope();
489 IRPosition::Kind PK = IRP.getPositionKind();
490
491 // In the following some generic code that will manifest attributes in
492 // DeducedAttrs if they improve the current IR. Due to the different
493 // annotation positions we use the underlying AttributeList interface.
494
495 AttributeList Attrs;
496 switch (PK) {
497 case IRPosition::IRP_INVALID:
498 case IRPosition::IRP_FLOAT:
499 return ChangeStatus::UNCHANGED;
500 case IRPosition::IRP_ARGUMENT:
501 case IRPosition::IRP_FUNCTION:
502 case IRPosition::IRP_RETURNED:
503 Attrs = ScopeFn->getAttributes();
504 break;
505 case IRPosition::IRP_CALL_SITE:
506 case IRPosition::IRP_CALL_SITE_RETURNED:
507 case IRPosition::IRP_CALL_SITE_ARGUMENT:
508 Attrs = cast<CallBase>(IRP.getAnchorValue()).getAttributes();
509 break;
510 }
511
512 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
513 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
514 for (const Attribute &Attr : DeducedAttrs) {
515 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx(), ForceReplace))
516 continue;
517
518 HasChanged = ChangeStatus::CHANGED;
519 }
520
521 if (HasChanged == ChangeStatus::UNCHANGED)
522 return HasChanged;
523
524 switch (PK) {
525 case IRPosition::IRP_ARGUMENT:
526 case IRPosition::IRP_FUNCTION:
527 case IRPosition::IRP_RETURNED:
528 ScopeFn->setAttributes(Attrs);
529 break;
530 case IRPosition::IRP_CALL_SITE:
531 case IRPosition::IRP_CALL_SITE_RETURNED:
532 case IRPosition::IRP_CALL_SITE_ARGUMENT:
533 cast<CallBase>(IRP.getAnchorValue()).setAttributes(Attrs);
534 break;
535 case IRPosition::IRP_INVALID:
536 case IRPosition::IRP_FLOAT:
537 break;
538 }
539
540 return HasChanged;
541}
542
543const IRPosition IRPosition::EmptyKey(DenseMapInfo<void *>::getEmptyKey());
544const IRPosition
545 IRPosition::TombstoneKey(DenseMapInfo<void *>::getTombstoneKey());
546
547SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
548 IRPositions.emplace_back(IRP);
549
550 // Helper to determine if operand bundles on a call site are benin or
551 // potentially problematic. We handle only llvm.assume for now.
552 auto CanIgnoreOperandBundles = [](const CallBase &CB) {
553 return (isa<IntrinsicInst>(CB) &&
554 cast<IntrinsicInst>(CB).getIntrinsicID() == Intrinsic ::assume);
555 };
556
557 const auto *CB = dyn_cast<CallBase>(&IRP.getAnchorValue());
558 switch (IRP.getPositionKind()) {
559 case IRPosition::IRP_INVALID:
560 case IRPosition::IRP_FLOAT:
561 case IRPosition::IRP_FUNCTION:
562 return;
563 case IRPosition::IRP_ARGUMENT:
564 case IRPosition::IRP_RETURNED:
565 IRPositions.emplace_back(IRPosition::function(*IRP.getAnchorScope()));
566 return;
567 case IRPosition::IRP_CALL_SITE:
568 assert(CB && "Expected call site!")((void)0);
569 // TODO: We need to look at the operand bundles similar to the redirection
570 // in CallBase.
571 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB))
572 if (const Function *Callee = CB->getCalledFunction())
573 IRPositions.emplace_back(IRPosition::function(*Callee));
574 return;
575 case IRPosition::IRP_CALL_SITE_RETURNED:
576 assert(CB && "Expected call site!")((void)0);
577 // TODO: We need to look at the operand bundles similar to the redirection
578 // in CallBase.
579 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
580 if (const Function *Callee = CB->getCalledFunction()) {
581 IRPositions.emplace_back(IRPosition::returned(*Callee));
582 IRPositions.emplace_back(IRPosition::function(*Callee));
583 for (const Argument &Arg : Callee->args())
584 if (Arg.hasReturnedAttr()) {
585 IRPositions.emplace_back(
586 IRPosition::callsite_argument(*CB, Arg.getArgNo()));
587 IRPositions.emplace_back(
588 IRPosition::value(*CB->getArgOperand(Arg.getArgNo())));
589 IRPositions.emplace_back(IRPosition::argument(Arg));
590 }
591 }
592 }
593 IRPositions.emplace_back(IRPosition::callsite_function(*CB));
594 return;
595 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
596 assert(CB && "Expected call site!")((void)0);
597 // TODO: We need to look at the operand bundles similar to the redirection
598 // in CallBase.
599 if (!CB->hasOperandBundles() || CanIgnoreOperandBundles(*CB)) {
600 const Function *Callee = CB->getCalledFunction();
601 if (Callee) {
602 if (Argument *Arg = IRP.getAssociatedArgument())
603 IRPositions.emplace_back(IRPosition::argument(*Arg));
604 IRPositions.emplace_back(IRPosition::function(*Callee));
605 }
606 }
607 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
608 return;
609 }
610 }
611}
612
613bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
614 bool IgnoreSubsumingPositions, Attributor *A) const {
615 SmallVector<Attribute, 4> Attrs;
616 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
617 for (Attribute::AttrKind AK : AKs)
618 if (EquivIRP.getAttrsFromIRAttr(AK, Attrs))
619 return true;
620 // The first position returned by the SubsumingPositionIterator is
621 // always the position itself. If we ignore subsuming positions we
622 // are done after the first iteration.
623 if (IgnoreSubsumingPositions)
624 break;
625 }
626 if (A)
627 for (Attribute::AttrKind AK : AKs)
628 if (getAttrsFromAssumes(AK, Attrs, *A))
629 return true;
630 return false;
631}
632
633void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
634 SmallVectorImpl<Attribute> &Attrs,
635 bool IgnoreSubsumingPositions, Attributor *A) const {
636 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
637 for (Attribute::AttrKind AK : AKs)
638 EquivIRP.getAttrsFromIRAttr(AK, Attrs);
639 // The first position returned by the SubsumingPositionIterator is
640 // always the position itself. If we ignore subsuming positions we
641 // are done after the first iteration.
642 if (IgnoreSubsumingPositions)
643 break;
644 }
645 if (A)
646 for (Attribute::AttrKind AK : AKs)
647 getAttrsFromAssumes(AK, Attrs, *A);
648}
649
650bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK,
651 SmallVectorImpl<Attribute> &Attrs) const {
652 if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
653 return false;
654
655 AttributeList AttrList;
656 if (const auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
657 AttrList = CB->getAttributes();
658 else
659 AttrList = getAssociatedFunction()->getAttributes();
660
661 bool HasAttr = AttrList.hasAttribute(getAttrIdx(), AK);
662 if (HasAttr)
663 Attrs.push_back(AttrList.getAttribute(getAttrIdx(), AK));
664 return HasAttr;
665}
666
667bool IRPosition::getAttrsFromAssumes(Attribute::AttrKind AK,
668 SmallVectorImpl<Attribute> &Attrs,
669 Attributor &A) const {
670 assert(getPositionKind() != IRP_INVALID && "Did expect a valid position!")((void)0);
671 Value &AssociatedValue = getAssociatedValue();
672
673 const Assume2KnowledgeMap &A2K =
674 A.getInfoCache().getKnowledgeMap().lookup({&AssociatedValue, AK});
675
676 // Check if we found any potential assume use, if not we don't need to create
677 // explorer iterators.
678 if (A2K.empty())
679 return false;
680
681 LLVMContext &Ctx = AssociatedValue.getContext();
682 unsigned AttrsSize = Attrs.size();
683 MustBeExecutedContextExplorer &Explorer =
684 A.getInfoCache().getMustBeExecutedContextExplorer();
685 auto EIt = Explorer.begin(getCtxI()), EEnd = Explorer.end(getCtxI());
686 for (auto &It : A2K)
687 if (Explorer.findInContextOf(It.first, EIt, EEnd))
688 Attrs.push_back(Attribute::get(Ctx, AK, It.second.Max));
689 return AttrsSize != Attrs.size();
690}
691
692void IRPosition::verify() {
693#ifdef EXPENSIVE_CHECKS
694 switch (getPositionKind()) {
695 case IRP_INVALID:
696 assert((CBContext == nullptr) &&((void)0)
697 "Invalid position must not have CallBaseContext!")((void)0);
698 assert(!Enc.getOpaqueValue() &&((void)0)
699 "Expected a nullptr for an invalid position!")((void)0);
700 return;
701 case IRP_FLOAT:
702 assert((!isa<CallBase>(&getAssociatedValue()) &&((void)0)
703 !isa<Argument>(&getAssociatedValue())) &&((void)0)
704 "Expected specialized kind for call base and argument values!")((void)0);
705 return;
706 case IRP_RETURNED:
707 assert(isa<Function>(getAsValuePtr()) &&((void)0)
708 "Expected function for a 'returned' position!")((void)0);
709 assert(getAsValuePtr() == &getAssociatedValue() &&((void)0)
710 "Associated value mismatch!")((void)0);
711 return;
712 case IRP_CALL_SITE_RETURNED:
713 assert((CBContext == nullptr) &&((void)0)
714 "'call site returned' position must not have CallBaseContext!")((void)0);
715 assert((isa<CallBase>(getAsValuePtr())) &&((void)0)
716 "Expected call base for 'call site returned' position!")((void)0);
717 assert(getAsValuePtr() == &getAssociatedValue() &&((void)0)
718 "Associated value mismatch!")((void)0);
719 return;
720 case IRP_CALL_SITE:
721 assert((CBContext == nullptr) &&((void)0)
722 "'call site function' position must not have CallBaseContext!")((void)0);
723 assert((isa<CallBase>(getAsValuePtr())) &&((void)0)
724 "Expected call base for 'call site function' position!")((void)0);
725 assert(getAsValuePtr() == &getAssociatedValue() &&((void)0)
726 "Associated value mismatch!")((void)0);
727 return;
728 case IRP_FUNCTION:
729 assert(isa<Function>(getAsValuePtr()) &&((void)0)
730 "Expected function for a 'function' position!")((void)0);
731 assert(getAsValuePtr() == &getAssociatedValue() &&((void)0)
732 "Associated value mismatch!")((void)0);
733 return;
734 case IRP_ARGUMENT:
735 assert(isa<Argument>(getAsValuePtr()) &&((void)0)
736 "Expected argument for a 'argument' position!")((void)0);
737 assert(getAsValuePtr() == &getAssociatedValue() &&((void)0)
738 "Associated value mismatch!")((void)0);
739 return;
740 case IRP_CALL_SITE_ARGUMENT: {
741 assert((CBContext == nullptr) &&((void)0)
742 "'call site argument' position must not have CallBaseContext!")((void)0);
743 Use *U = getAsUsePtr();
744 assert(U && "Expected use for a 'call site argument' position!")((void)0);
745 assert(isa<CallBase>(U->getUser()) &&((void)0)
746 "Expected call base user for a 'call site argument' position!")((void)0);
747 assert(cast<CallBase>(U->getUser())->isArgOperand(U) &&((void)0)
748 "Expected call base argument operand for a 'call site argument' "((void)0)
749 "position")((void)0);
750 assert(cast<CallBase>(U->getUser())->getArgOperandNo(U) ==((void)0)
751 unsigned(getCallSiteArgNo()) &&((void)0)
752 "Argument number mismatch!")((void)0);
753 assert(U->get() == &getAssociatedValue() && "Associated value mismatch!")((void)0);
754 return;
755 }
756 }
757#endif
758}
759
760Optional<Constant *>
761Attributor::getAssumedConstant(const IRPosition &IRP,
762 const AbstractAttribute &AA,
763 bool &UsedAssumedInformation) {
764 // First check all callbacks provided by outside AAs. If any of them returns
765 // a non-null value that is different from the associated value, or None, we
766 // assume it's simpliied.
767 for (auto &CB : SimplificationCallbacks.lookup(IRP)) {
768 Optional<Value *> SimplifiedV = CB(IRP, &AA, UsedAssumedInformation);
769 if (!SimplifiedV.hasValue())
770 return llvm::None;
771 if (isa_and_nonnull<Constant>(*SimplifiedV))
772 return cast<Constant>(*SimplifiedV);
773 return nullptr;
774 }
775 const auto &ValueSimplifyAA =
776 getAAFor<AAValueSimplify>(AA, IRP, DepClassTy::NONE);
777 Optional<Value *> SimplifiedV =
778 ValueSimplifyAA.getAssumedSimplifiedValue(*this);
779 bool IsKnown = ValueSimplifyAA.isAtFixpoint();
780 UsedAssumedInformation |= !IsKnown;
781 if (!SimplifiedV.hasValue()) {
782 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
783 return llvm::None;
784 }
785 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) {
786 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
787 return UndefValue::get(IRP.getAssociatedType());
788 }
789 Constant *CI = dyn_cast_or_null<Constant>(SimplifiedV.getValue());
790 if (CI)
791 CI = dyn_cast_or_null<Constant>(
792 AA::getWithType(*CI, *IRP.getAssociatedType()));
793 if (CI)
794 recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
795 return CI;
796}
797
798Optional<Value *>
799Attributor::getAssumedSimplified(const IRPosition &IRP,
800 const AbstractAttribute *AA,
801 bool &UsedAssumedInformation) {
802 // First check all callbacks provided by outside AAs. If any of them returns
803 // a non-null value that is different from the associated value, or None, we
804 // assume it's simpliied.
805 for (auto &CB : SimplificationCallbacks.lookup(IRP))
806 return CB(IRP, AA, UsedAssumedInformation);
807
808 // If no high-level/outside simplification occured, use AAValueSimplify.
809 const auto &ValueSimplifyAA =
810 getOrCreateAAFor<AAValueSimplify>(IRP, AA, DepClassTy::NONE);
811 Optional<Value *> SimplifiedV =
812 ValueSimplifyAA.getAssumedSimplifiedValue(*this);
813 bool IsKnown = ValueSimplifyAA.isAtFixpoint();
814 UsedAssumedInformation |= !IsKnown;
815 if (!SimplifiedV.hasValue()) {
816 if (AA)
817 recordDependence(ValueSimplifyAA, *AA, DepClassTy::OPTIONAL);
818 return llvm::None;
819 }
820 if (*SimplifiedV == nullptr)
821 return const_cast<Value *>(&IRP.getAssociatedValue());
822 if (Value *SimpleV =
823 AA::getWithType(**SimplifiedV, *IRP.getAssociatedType())) {
824 if (AA)
825 recordDependence(ValueSimplifyAA, *AA, DepClassTy::OPTIONAL);
826 return SimpleV;
827 }
828 return const_cast<Value *>(&IRP.getAssociatedValue());
829}
830
831Optional<Value *> Attributor::translateArgumentToCallSiteContent(
832 Optional<Value *> V, CallBase &CB, const AbstractAttribute &AA,
833 bool &UsedAssumedInformation) {
834 if (!V.hasValue())
835 return V;
836 if (*V == nullptr || isa<Constant>(*V))
837 return V;
838 if (auto *Arg = dyn_cast<Argument>(*V))
839 if (CB.getCalledFunction() == Arg->getParent())
840 if (!Arg->hasPointeeInMemoryValueAttr())
841 return getAssumedSimplified(
842 IRPosition::callsite_argument(CB, Arg->getArgNo()), AA,
843 UsedAssumedInformation);
844 return nullptr;
845}
846
847Attributor::~Attributor() {
848 // The abstract attributes are allocated via the BumpPtrAllocator Allocator,
849 // thus we cannot delete them. We can, and want to, destruct them though.
850 for (auto &DepAA : DG.SyntheticRoot.Deps) {
851 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
852 AA->~AbstractAttribute();
853 }
854}
855
856bool Attributor::isAssumedDead(const AbstractAttribute &AA,
857 const AAIsDead *FnLivenessAA,
858 bool &UsedAssumedInformation,
859 bool CheckBBLivenessOnly, DepClassTy DepClass) {
860 const IRPosition &IRP = AA.getIRPosition();
861 if (!Functions.count(IRP.getAnchorScope()))
862 return false;
863 return isAssumedDead(IRP, &AA, FnLivenessAA, UsedAssumedInformation,
864 CheckBBLivenessOnly, DepClass);
865}
866
867bool Attributor::isAssumedDead(const Use &U,
868 const AbstractAttribute *QueryingAA,
869 const AAIsDead *FnLivenessAA,
870 bool &UsedAssumedInformation,
871 bool CheckBBLivenessOnly, DepClassTy DepClass) {
872 Instruction *UserI = dyn_cast<Instruction>(U.getUser());
873 if (!UserI)
874 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
875 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
876
877 if (auto *CB = dyn_cast<CallBase>(UserI)) {
878 // For call site argument uses we can check if the argument is
879 // unused/dead.
880 if (CB->isArgOperand(&U)) {
881 const IRPosition &CSArgPos =
882 IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
883 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
884 UsedAssumedInformation, CheckBBLivenessOnly,
885 DepClass);
886 }
887 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
888 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
889 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA,
890 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
891 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
892 BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
893 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
894 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
895 }
896
897 return isAssumedDead(IRPosition::value(*UserI), QueryingAA, FnLivenessAA,
898 UsedAssumedInformation, CheckBBLivenessOnly, DepClass);
899}
900
901bool Attributor::isAssumedDead(const Instruction &I,
902 const AbstractAttribute *QueryingAA,
903 const AAIsDead *FnLivenessAA,
904 bool &UsedAssumedInformation,
905 bool CheckBBLivenessOnly, DepClassTy DepClass) {
906 const IRPosition::CallBaseContext *CBCtx =
907 QueryingAA ? QueryingAA->getCallBaseContext() : nullptr;
908
909 if (ManifestAddedBlocks.contains(I.getParent()))
910 return false;
911
912 if (!FnLivenessAA)
913 FnLivenessAA =
914 lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction(), CBCtx),
915 QueryingAA, DepClassTy::NONE);
916
917 // If we have a context instruction and a liveness AA we use it.
918 if (FnLivenessAA &&
919 FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
920 FnLivenessAA->isAssumedDead(&I)) {
921 if (QueryingAA)
922 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
923 if (!FnLivenessAA->isKnownDead(&I))
924 UsedAssumedInformation = true;
925 return true;
926 }
927
928 if (CheckBBLivenessOnly)
929 return false;
930
931 const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>(
932 IRPosition::value(I, CBCtx), QueryingAA, DepClassTy::NONE);
933 // Don't check liveness for AAIsDead.
934 if (QueryingAA == &IsDeadAA)
935 return false;
936
937 if (IsDeadAA.isAssumedDead()) {
938 if (QueryingAA)
939 recordDependence(IsDeadAA, *QueryingAA, DepClass);
940 if (!IsDeadAA.isKnownDead())
941 UsedAssumedInformation = true;
942 return true;
943 }
944
945 return false;
946}
947
948bool Attributor::isAssumedDead(const IRPosition &IRP,
949 const AbstractAttribute *QueryingAA,
950 const AAIsDead *FnLivenessAA,
951 bool &UsedAssumedInformation,
952 bool CheckBBLivenessOnly, DepClassTy DepClass) {
953 Instruction *CtxI = IRP.getCtxI();
954 if (CtxI &&
955 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA, UsedAssumedInformation,
956 /* CheckBBLivenessOnly */ true,
957 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
958 return true;
959
960 if (CheckBBLivenessOnly)
961 return false;
962
963 // If we haven't succeeded we query the specific liveness info for the IRP.
964 const AAIsDead *IsDeadAA;
965 if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
966 IsDeadAA = &getOrCreateAAFor<AAIsDead>(
967 IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
968 QueryingAA, DepClassTy::NONE);
969 else
970 IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA, DepClassTy::NONE);
971 // Don't check liveness for AAIsDead.
972 if (QueryingAA == IsDeadAA)
973 return false;
974
975 if (IsDeadAA->isAssumedDead()) {
976 if (QueryingAA)
977 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
978 if (!IsDeadAA->isKnownDead())
979 UsedAssumedInformation = true;
980 return true;
981 }
982
983 return false;
984}
985
986bool Attributor::isAssumedDead(const BasicBlock &BB,
987 const AbstractAttribute *QueryingAA,
988 const AAIsDead *FnLivenessAA,
989 DepClassTy DepClass) {
990 if (!FnLivenessAA)
991 FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*BB.getParent()),
992 QueryingAA, DepClassTy::NONE);
993 if (FnLivenessAA->isAssumedDead(&BB)) {
994 if (QueryingAA)
995 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
996 return true;
997 }
998
999 return false;
1000}
1001
1002bool Attributor::checkForAllUses(function_ref<bool(const Use &, bool &)> Pred,
1003 const AbstractAttribute &QueryingAA,
1004 const Value &V, bool CheckBBLivenessOnly,
1005 DepClassTy LivenessDepClass) {
1006
1007 // Check the trivial case first as it catches void values.
1008 if (V.use_empty())
1009 return true;
1010
1011 const IRPosition &IRP = QueryingAA.getIRPosition();
1012 SmallVector<const Use *, 16> Worklist;
1013 SmallPtrSet<const Use *, 16> Visited;
1014
1015 for (const Use &U : V.uses())
1016 Worklist.push_back(&U);
1017
1018 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()do { } while (false)
1019 << " initial uses to check\n")do { } while (false);
1020
1021 const Function *ScopeFn = IRP.getAnchorScope();
1022 const auto *LivenessAA =
1023 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
1024 DepClassTy::NONE)
1025 : nullptr;
1026
1027 while (!Worklist.empty()) {
1028 const Use *U = Worklist.pop_back_val();
1029 if (isa<PHINode>(U->getUser()) && !Visited.insert(U).second)
1030 continue;
1031 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in "do { } while (false)
1032 << *U->getUser() << "\n")do { } while (false);
1033 bool UsedAssumedInformation = false;
1034 if (isAssumedDead(*U, &QueryingAA, LivenessAA, UsedAssumedInformation,
1035 CheckBBLivenessOnly, LivenessDepClass)) {
1036 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n")do { } while (false);
1037 continue;
1038 }
1039 if (U->getUser()->isDroppable()) {
1040 LLVM_DEBUG(dbgs() << "[Attributor] Droppable user, skip!\n")do { } while (false);
1041 continue;
1042 }
1043
1044 if (auto *SI = dyn_cast<StoreInst>(U->getUser())) {
1045 if (&SI->getOperandUse(0) == U) {
1046 SmallSetVector<Value *, 4> PotentialCopies;
1047 if (AA::getPotentialCopiesOfStoredValue(*this, *SI, PotentialCopies,
1048 QueryingAA,
1049 UsedAssumedInformation)) {
1050 LLVM_DEBUG(dbgs() << "[Attributor] Value is stored, continue with "do { } while (false)
1051 << PotentialCopies.size()do { } while (false)
1052 << " potential copies instead!\n")do { } while (false);
1053 for (Value *PotentialCopy : PotentialCopies)
1054 for (const Use &U : PotentialCopy->uses())
1055 Worklist.push_back(&U);
1056 continue;
1057 }
1058 }
1059 }
1060
1061 bool Follow = false;
1062 if (!Pred(*U, Follow))
1063 return false;
1064 if (!Follow)
1065 continue;
1066 for (const Use &UU : U->getUser()->uses())
1067 Worklist.push_back(&UU);
1068 }
1069
1070 return true;
1071}
1072
1073bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
1074 const AbstractAttribute &QueryingAA,
1075 bool RequireAllCallSites,
1076 bool &AllCallSitesKnown) {
1077 // We can try to determine information from
1078 // the call sites. However, this is only possible all call sites are known,
1079 // hence the function has internal linkage.
1080 const IRPosition &IRP = QueryingAA.getIRPosition();
1081 const Function *AssociatedFunction = IRP.getAssociatedFunction();
1082 if (!AssociatedFunction) {
1083 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRPdo { } while (false)
1084 << "\n")do { } while (false);
1085 AllCallSitesKnown = false;
1086 return false;
1087 }
1088
1089 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
1090 &QueryingAA, AllCallSitesKnown);
1091}
1092
1093bool Attributor::checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
1094 const Function &Fn,
1095 bool RequireAllCallSites,
1096 const AbstractAttribute *QueryingAA,
1097 bool &AllCallSitesKnown) {
1098 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
1099 LLVM_DEBUG(do { } while (false)
1100 dbgs()do { } while (false)
1101 << "[Attributor] Function " << Fn.getName()do { } while (false)
1102 << " has no internal linkage, hence not all call sites are known\n")do { } while (false);
1103 AllCallSitesKnown = false;
1104 return false;
1105 }
1106
1107 // If we do not require all call sites we might not see all.
1108 AllCallSitesKnown = RequireAllCallSites;
1109
1110 SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
1111 for (unsigned u = 0; u < Uses.size(); ++u) {
1112 const Use &U = *Uses[u];
1113 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in "do { } while (false)
1114 << *U.getUser() << "\n")do { } while (false);
1115 bool UsedAssumedInformation = false;
1116 if (isAssumedDead(U, QueryingAA, nullptr, UsedAssumedInformation,
1117 /* CheckBBLivenessOnly */ true)) {
1118 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n")do { } while (false);
1119 continue;
1120 }
1121 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
1122 if (CE->isCast() && CE->getType()->isPointerTy() &&
1123 CE->getType()->getPointerElementType()->isFunctionTy()) {
1124 for (const Use &CEU : CE->uses())
1125 Uses.push_back(&CEU);
1126 continue;
1127 }
1128 }
1129
1130 AbstractCallSite ACS(&U);
1131 if (!ACS) {
1132 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()do { } while (false)
1133 << " has non call site use " << *U.get() << " in "do { } while (false)
1134 << *U.getUser() << "\n")do { } while (false);
1135 // BlockAddress users are allowed.
1136 if (isa<BlockAddress>(U.getUser()))
1137 continue;
1138 return false;
1139 }
1140
1141 const Use *EffectiveUse =
1142 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
1143 if (!ACS.isCallee(EffectiveUse)) {
1144 if (!RequireAllCallSites)
1145 continue;
1146 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()do { } while (false)
1147 << " is an invalid use of " << Fn.getName() << "\n")do { } while (false);
1148 return false;
1149 }
1150
1151 // Make sure the arguments that can be matched between the call site and the
1152 // callee argee on their type. It is unlikely they do not and it doesn't
1153 // make sense for all attributes to know/care about this.
1154 assert(&Fn == ACS.getCalledFunction() && "Expected known callee")((void)0);
1155 unsigned MinArgsParams =
1156 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
1157 for (unsigned u = 0; u < MinArgsParams; ++u) {
1158 Value *CSArgOp = ACS.getCallArgOperand(u);
1159 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
1160 LLVM_DEBUG(do { } while (false)
1161 dbgs() << "[Attributor] Call site / callee argument type mismatch ["do { } while (false)
1162 << u << "@" << Fn.getName() << ": "do { } while (false)
1163 << *Fn.getArg(u)->getType() << " vs. "do { } while (false)
1164 << *ACS.getCallArgOperand(u)->getType() << "\n")do { } while (false);
1165 return false;
1166 }
1167 }
1168
1169 if (Pred(ACS))
1170 continue;
1171
1172 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "do { } while (false)
1173 << *ACS.getInstruction() << "\n")do { } while (false);
1174 return false;
1175 }
1176
1177 return true;
1178}
1179
1180bool Attributor::shouldPropagateCallBaseContext(const IRPosition &IRP) {
1181 // TODO: Maintain a cache of Values that are
1182 // on the pathway from a Argument to a Instruction that would effect the
1183 // liveness/return state etc.
1184 return EnableCallSiteSpecific;
1185}
1186
1187bool Attributor::checkForAllReturnedValuesAndReturnInsts(
1188 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
1189 const AbstractAttribute &QueryingAA) {
1190
1191 const IRPosition &IRP = QueryingAA.getIRPosition();
1192 // Since we need to provide return instructions we have to have an exact
1193 // definition.
1194 const Function *AssociatedFunction = IRP.getAssociatedFunction();
1195 if (!AssociatedFunction)
1196 return false;
1197
1198 // If this is a call site query we use the call site specific return values
1199 // and liveness information.
1200 // TODO: use the function scope once we have call site AAReturnedValues.
1201 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
1202 const auto &AARetVal =
1203 getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
1204 if (!AARetVal.getState().isValidState())
1205 return false;
1206
1207 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
1208}
1209
1210bool Attributor::checkForAllReturnedValues(
1211 function_ref<bool(Value &)> Pred, const AbstractAttribute &QueryingAA) {
1212
1213 const IRPosition &IRP = QueryingAA.getIRPosition();
1214 const Function *AssociatedFunction = IRP.getAssociatedFunction();
1215 if (!AssociatedFunction)
1216 return false;
1217
1218 // TODO: use the function scope once we have call site AAReturnedValues.
1219 const IRPosition &QueryIRP = IRPosition::function(
1220 *AssociatedFunction, QueryingAA.getCallBaseContext());
1221 const auto &AARetVal =
1222 getAAFor<AAReturnedValues>(QueryingAA, QueryIRP, DepClassTy::REQUIRED);
1223 if (!AARetVal.getState().isValidState())
1224 return false;
1225
1226 return AARetVal.checkForAllReturnedValuesAndReturnInsts(
1227 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
1228 return Pred(RV);
1229 });
1230}
1231
1232static bool checkForAllInstructionsImpl(
1233 Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap,
1234 function_ref<bool(Instruction &)> Pred, const AbstractAttribute *QueryingAA,
1235 const AAIsDead *LivenessAA, const ArrayRef<unsigned> &Opcodes,
1236 bool &UsedAssumedInformation, bool CheckBBLivenessOnly = false,
1237 bool CheckPotentiallyDead = false) {
1238 for (unsigned Opcode : Opcodes) {
1239 // Check if we have instructions with this opcode at all first.
1240 auto *Insts = OpcodeInstMap.lookup(Opcode);
1241 if (!Insts)
1242 continue;
1243
1244 for (Instruction *I : *Insts) {
1245 // Skip dead instructions.
1246 if (A && !CheckPotentiallyDead &&
1247 A->isAssumedDead(IRPosition::value(*I), QueryingAA, LivenessAA,
1248 UsedAssumedInformation, CheckBBLivenessOnly))
1249 continue;
1250
1251 if (!Pred(*I))
1252 return false;
1253 }
1254 }
1255 return true;
1256}
1257
1258bool Attributor::checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
1259 const AbstractAttribute &QueryingAA,
1260 const ArrayRef<unsigned> &Opcodes,
1261 bool &UsedAssumedInformation,
1262 bool CheckBBLivenessOnly,
1263 bool CheckPotentiallyDead) {
1264
1265 const IRPosition &IRP = QueryingAA.getIRPosition();
1266 // Since we need to provide instructions we have to have an exact definition.
1267 const Function *AssociatedFunction = IRP.getAssociatedFunction();
1268 if (!AssociatedFunction)
1269 return false;
1270
1271 if (AssociatedFunction->isDeclaration())
1272 return false;
1273
1274 // TODO: use the function scope once we have call site AAReturnedValues.
1275 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
1276 const auto *LivenessAA =
1277 (CheckBBLivenessOnly || CheckPotentiallyDead)
1278 ? nullptr
1279 : &(getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE));
1280
1281 auto &OpcodeInstMap =
1282 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
1283 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA,
1284 LivenessAA, Opcodes, UsedAssumedInformation,
1285 CheckBBLivenessOnly, CheckPotentiallyDead))
1286 return false;
1287
1288 return true;
1289}
1290
1291bool Attributor::checkForAllReadWriteInstructions(
1292 function_ref<bool(Instruction &)> Pred, AbstractAttribute &QueryingAA,
1293 bool &UsedAssumedInformation) {
1294
1295 const Function *AssociatedFunction =
1296 QueryingAA.getIRPosition().getAssociatedFunction();
1297 if (!AssociatedFunction
0.1
'AssociatedFunction' is non-null
0.1
'AssociatedFunction' is non-null
0.1
'AssociatedFunction' is non-null
0.1
'AssociatedFunction' is non-null
)
1
Taking false branch
1298 return false;
1299
1300 // TODO: use the function scope once we have call site AAReturnedValues.
1301 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
1302 const auto &LivenessAA =
1303 getAAFor<AAIsDead>(QueryingAA, QueryIRP, DepClassTy::NONE);
1304
1305 for (Instruction *I :
1306 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
2
Calling 'InformationCache::getReadOrWriteInstsForFunction'
1307 // Skip dead instructions.
1308 if (isAssumedDead(IRPosition::value(*I), &QueryingAA, &LivenessAA,
1309 UsedAssumedInformation))
1310 continue;
1311
1312 if (!Pred(*I))
1313 return false;
1314 }
1315
1316 return true;
1317}
1318
1319void Attributor::runTillFixpoint() {
1320 TimeTraceScope TimeScope("Attributor::runTillFixpoint");
1321 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "do { } while (false)
1322 << DG.SyntheticRoot.Deps.size()do { } while (false)
1323 << " abstract attributes.\n")do { } while (false);
1324
1325 // Now that all abstract attributes are collected and initialized we start
1326 // the abstract analysis.
1327
1328 unsigned IterationCounter = 1;
1329 unsigned MaxFixedPointIterations;
1330 if (MaxFixpointIterations)
1331 MaxFixedPointIterations = MaxFixpointIterations.getValue();
1332 else
1333 MaxFixedPointIterations = SetFixpointIterations;
1334
1335 SmallVector<AbstractAttribute *, 32> ChangedAAs;
1336 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
1337 Worklist.insert(DG.SyntheticRoot.begin(), DG.SyntheticRoot.end());
1338
1339 do {
1340 // Remember the size to determine new attributes.
1341 size_t NumAAs = DG.SyntheticRoot.Deps.size();
1342 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounterdo { } while (false)
1343 << ", Worklist size: " << Worklist.size() << "\n")do { } while (false);
1344
1345 // For invalid AAs we can fix dependent AAs that have a required dependence,
1346 // thereby folding long dependence chains in a single step without the need
1347 // to run updates.
1348 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
1349 AbstractAttribute *InvalidAA = InvalidAAs[u];
1350
1351 // Check the dependences to fast track invalidation.
1352 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "do { } while (false)
1353 << InvalidAA->Deps.size()do { } while (false)
1354 << " required & optional dependences\n")do { } while (false);
1355 while (!InvalidAA->Deps.empty()) {
1356 const auto &Dep = InvalidAA->Deps.back();
1357 InvalidAA->Deps.pop_back();
1358 AbstractAttribute *DepAA = cast<AbstractAttribute>(Dep.getPointer());
1359 if (Dep.getInt() == unsigned(DepClassTy::OPTIONAL)) {
1360 Worklist.insert(DepAA);
1361 continue;
1362 }
1363 DepAA->getState().indicatePessimisticFixpoint();
1364 assert(DepAA->getState().isAtFixpoint() && "Expected fixpoint state!")((void)0);
1365 if (!DepAA->getState().isValidState())
1366 InvalidAAs.insert(DepAA);
1367 else
1368 ChangedAAs.push_back(DepAA);
1369 }
1370 }
1371
1372 // Add all abstract attributes that are potentially dependent on one that
1373 // changed to the work list.
1374 for (AbstractAttribute *ChangedAA : ChangedAAs)
1375 while (!ChangedAA->Deps.empty()) {
1376 Worklist.insert(
1377 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
1378 ChangedAA->Deps.pop_back();
1379 }
1380
1381 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounterdo { } while (false)
1382 << ", Worklist+Dependent size: " << Worklist.size()do { } while (false)
1383 << "\n")do { } while (false);
1384
1385 // Reset the changed and invalid set.
1386 ChangedAAs.clear();
1387 InvalidAAs.clear();
1388
1389 // Update all abstract attribute in the work list and record the ones that
1390 // changed.
1391 for (AbstractAttribute *AA : Worklist) {
1392 const auto &AAState = AA->getState();
1393 if (!AAState.isAtFixpoint())
1394 if (updateAA(*AA) == ChangeStatus::CHANGED)
1395 ChangedAAs.push_back(AA);
1396
1397 // Use the InvalidAAs vector to propagate invalid states fast transitively
1398 // without requiring updates.
1399 if (!AAState.isValidState())
1400 InvalidAAs.insert(AA);
1401 }
1402
1403 // Add attributes to the changed set if they have been created in the last
1404 // iteration.
1405 ChangedAAs.append(DG.SyntheticRoot.begin() + NumAAs,
1406 DG.SyntheticRoot.end());
1407
1408 // Reset the work list and repopulate with the changed abstract attributes.
1409 // Note that dependent ones are added above.
1410 Worklist.clear();
1411 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
1412
1413 } while (!Worklist.empty() && (IterationCounter++ < MaxFixedPointIterations ||
1414 VerifyMaxFixpointIterations));
1415
1416 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "do { } while (false)
1417 << IterationCounter << "/" << MaxFixpointIterationsdo { } while (false)
1418 << " iterations\n")do { } while (false);
1419
1420 // Reset abstract arguments not settled in a sound fixpoint by now. This
1421 // happens when we stopped the fixpoint iteration early. Note that only the
1422 // ones marked as "changed" *and* the ones transitively depending on them
1423 // need to be reverted to a pessimistic state. Others might not be in a
1424 // fixpoint state but we can use the optimistic results for them anyway.
1425 SmallPtrSet<AbstractAttribute *, 32> Visited;
1426 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
1427 AbstractAttribute *ChangedAA = ChangedAAs[u];
1428 if (!Visited.insert(ChangedAA).second)
1429 continue;
1430
1431 AbstractState &State = ChangedAA->getState();
1432 if (!State.isAtFixpoint()) {
1433 State.indicatePessimisticFixpoint();
1434
1435 NumAttributesTimedOut++;
1436 }
1437
1438 while (!ChangedAA->Deps.empty()) {
1439 ChangedAAs.push_back(
1440 cast<AbstractAttribute>(ChangedAA->Deps.back().getPointer()));
1441 ChangedAA->Deps.pop_back();
1442 }
1443 }
1444
1445 LLVM_DEBUG({do { } while (false)
1446 if (!Visited.empty())do { } while (false)
1447 dbgs() << "\n[Attributor] Finalized " << Visited.size()do { } while (false)
1448 << " abstract attributes.\n";do { } while (false)
1449 })do { } while (false);
1450
1451 if (VerifyMaxFixpointIterations &&
1452 IterationCounter != MaxFixedPointIterations) {
1453 errs() << "\n[Attributor] Fixpoint iteration done after: "
1454 << IterationCounter << "/" << MaxFixedPointIterations
1455 << " iterations\n";
1456 llvm_unreachable("The fixpoint was not reached with exactly the number of "__builtin_unreachable()
1457 "specified iterations!")__builtin_unreachable();
1458 }
1459}
1460
1461ChangeStatus Attributor::manifestAttributes() {
1462 TimeTraceScope TimeScope("Attributor::manifestAttributes");
1463 size_t NumFinalAAs = DG.SyntheticRoot.Deps.size();
1464
1465 unsigned NumManifested = 0;
1466 unsigned NumAtFixpoint = 0;
1467 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
1468 for (auto &DepAA : DG.SyntheticRoot.Deps) {
1469 AbstractAttribute *AA = cast<AbstractAttribute>(DepAA.getPointer());
1470 AbstractState &State = AA->getState();
1471
1472 // If there is not already a fixpoint reached, we can now take the
1473 // optimistic state. This is correct because we enforced a pessimistic one
1474 // on abstract attributes that were transitively dependent on a changed one
1475 // already above.
1476 if (!State.isAtFixpoint())
1477 State.indicateOptimisticFixpoint();
1478
1479 // We must not manifest Attributes that use Callbase info.
1480 if (AA->hasCallBaseContext())
1481 continue;
1482 // If the state is invalid, we do not try to manifest it.
1483 if (!State.isValidState())
1484 continue;
1485
1486 // Skip dead code.
1487 bool UsedAssumedInformation = false;
1488 if (isAssumedDead(*AA, nullptr, UsedAssumedInformation,
1489 /* CheckBBLivenessOnly */ true))
1490 continue;
1491 // Check if the manifest debug counter that allows skipping manifestation of
1492 // AAs
1493 if (!DebugCounter::shouldExecute(ManifestDBGCounter))
1494 continue;
1495 // Manifest the state and record if we changed the IR.
1496 ChangeStatus LocalChange = AA->manifest(*this);
1497 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
1498 AA->trackStatistics();
1499 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AAdo { } while (false)
1500 << "\n")do { } while (false);
1501
1502 ManifestChange = ManifestChange | LocalChange;
1503
1504 NumAtFixpoint++;
1505 NumManifested += (LocalChange == ChangeStatus::CHANGED);
1506 }
1507
1508 (void)NumManifested;
1509 (void)NumAtFixpoint;
1510 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifesteddo { } while (false)
1511 << " arguments while " << NumAtFixpointdo { } while (false)
1512 << " were in a valid fixpoint state\n")do { } while (false);
1513
1514 NumAttributesManifested += NumManifested;
1515 NumAttributesValidFixpoint += NumAtFixpoint;
1516
1517 (void)NumFinalAAs;
1518 if (NumFinalAAs != DG.SyntheticRoot.Deps.size()) {
1519 for (unsigned u = NumFinalAAs; u < DG.SyntheticRoot.Deps.size(); ++u)
1520 errs() << "Unexpected abstract attribute: "
1521 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
1522 << " :: "
1523 << cast<AbstractAttribute>(DG.SyntheticRoot.Deps[u].getPointer())
1524 ->getIRPosition()
1525 .getAssociatedValue()
1526 << "\n";
1527 llvm_unreachable("Expected the final number of abstract attributes to "__builtin_unreachable()
1528 "remain unchanged!")__builtin_unreachable();
1529 }
1530 return ManifestChange;
1531}
1532
1533void Attributor::identifyDeadInternalFunctions() {
1534 // Early exit if we don't intend to delete functions.
1535 if (!DeleteFns)
1536 return;
1537
1538 // Identify dead internal functions and delete them. This happens outside
1539 // the other fixpoint analysis as we might treat potentially dead functions
1540 // as live to lower the number of iterations. If they happen to be dead, the
1541 // below fixpoint loop will identify and eliminate them.
1542 SmallVector<Function *, 8> InternalFns;
1543 for (Function *F : Functions)
1544 if (F->hasLocalLinkage())
1545 InternalFns.push_back(F);
1546
1547 SmallPtrSet<Function *, 8> LiveInternalFns;
1548 bool FoundLiveInternal = true;
1549 while (FoundLiveInternal) {
1550 FoundLiveInternal = false;
1551 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
1552 Function *F = InternalFns[u];
1553 if (!F)
1554 continue;
1555
1556 bool AllCallSitesKnown;
1557 if (checkForAllCallSites(
1558 [&](AbstractCallSite ACS) {
1559 Function *Callee = ACS.getInstruction()->getFunction();
1560 return ToBeDeletedFunctions.count(Callee) ||
1561 (Functions.count(Callee) && Callee->hasLocalLinkage() &&
1562 !LiveInternalFns.count(Callee));
1563 },
1564 *F, true, nullptr, AllCallSitesKnown)) {
1565 continue;
1566 }
1567
1568 LiveInternalFns.insert(F);
1569 InternalFns[u] = nullptr;
1570 FoundLiveInternal = true;
1571 }
1572 }
1573
1574 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u)
1575 if (Function *F = InternalFns[u])
1576 ToBeDeletedFunctions.insert(F);
1577}
1578
1579ChangeStatus Attributor::cleanupIR() {
1580 TimeTraceScope TimeScope("Attributor::cleanupIR");
1581 // Delete stuff at the end to avoid invalid references and a nice order.
1582 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete/replace at least "do { } while (false)
1583 << ToBeDeletedFunctions.size() << " functions and "do { } while (false)
1584 << ToBeDeletedBlocks.size() << " blocks and "do { } while (false)
1585 << ToBeDeletedInsts.size() << " instructions and "do { } while (false)
1586 << ToBeChangedValues.size() << " values and "do { } while (false)
1587 << ToBeChangedUses.size() << " uses. "do { } while (false)
1588 << "Preserve manifest added " << ManifestAddedBlocks.size()do { } while (false)
1589 << " blocks\n")do { } while (false);
1590
1591 SmallVector<WeakTrackingVH, 32> DeadInsts;
1592 SmallVector<Instruction *, 32> TerminatorsToFold;
1593
1594 auto ReplaceUse = [&](Use *U, Value *NewV) {
1595 Value *OldV = U->get();
1596
1597 // If we plan to replace NewV we need to update it at this point.
1598 do {
1599 const auto &Entry = ToBeChangedValues.lookup(NewV);
1600 if (!Entry.first)
1601 break;
1602 NewV = Entry.first;
1603 } while (true);
1604
1605 // Do not replace uses in returns if the value is a must-tail call we will
1606 // not delete.
1607 if (auto *RI = dyn_cast<ReturnInst>(U->getUser())) {
1608 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
1609 if (CI->isMustTailCall() &&
1610 (!ToBeDeletedInsts.count(CI) || !isRunOn(*CI->getCaller())))
1611 return;
1612 // If we rewrite a return and the new value is not an argument, strip the
1613 // `returned` attribute as it is wrong now.
1614 if (!isa<Argument>(NewV))
1615 for (auto &Arg : RI->getFunction()->args())
1616 Arg.removeAttr(Attribute::Returned);
1617 }
1618
1619 // Do not perform call graph altering changes outside the SCC.
1620 if (auto *CB = dyn_cast<CallBase>(U->getUser()))
1621 if (CB->isCallee(U) && !isRunOn(*CB->getCaller()))
1622 return;
1623
1624 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()do { } while (false)
1625 << " instead of " << *OldV << "\n")do { } while (false);
1626 U->set(NewV);
1627
1628 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
1629 CGModifiedFunctions.insert(I->getFunction());
1630 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
1631 isInstructionTriviallyDead(I))
1632 DeadInsts.push_back(I);
1633 }
1634 if (isa<UndefValue>(NewV) && isa<CallBase>(U->getUser())) {
1635 auto *CB = cast<CallBase>(U->getUser());
1636 if (CB->isArgOperand(U)) {
1637 unsigned Idx = CB->getArgOperandNo(U);
1638 CB->removeParamAttr(Idx, Attribute::NoUndef);
1639 Function *Fn = CB->getCalledFunction();
1640 if (Fn && Fn->arg_size() > Idx)
1641 Fn->removeParamAttr(Idx, Attribute::NoUndef);
1642 }
1643 }
1644 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
1645 Instruction *UserI = cast<Instruction>(U->getUser());
1646 if (isa<UndefValue>(NewV)) {
1647 ToBeChangedToUnreachableInsts.insert(UserI);
1648 } else {
1649 TerminatorsToFold.push_back(UserI);
1650 }
1651 }
1652 };
1653
1654 for (auto &It : ToBeChangedUses) {
1655 Use *U = It.first;
1656 Value *NewV = It.second;
1657 ReplaceUse(U, NewV);
1658 }
1659
1660 SmallVector<Use *, 4> Uses;
1661 for (auto &It : ToBeChangedValues) {
1662 Value *OldV = It.first;
1663 auto &Entry = It.second;
1664 Value *NewV = Entry.first;
1665 Uses.clear();
1666 for (auto &U : OldV->uses())
1667 if (Entry.second || !U.getUser()->isDroppable())
1668 Uses.push_back(&U);
1669 for (Use *U : Uses)
1670 ReplaceUse(U, NewV);
1671 }
1672
1673 for (auto &V : InvokeWithDeadSuccessor)
1674 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
1675 assert(isRunOn(*II->getFunction()) &&((void)0)
1676 "Cannot replace an invoke outside the current SCC!")((void)0);
1677 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
1678 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
1679 bool Invoke2CallAllowed =
1680 !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction());
1681 assert((UnwindBBIsDead || NormalBBIsDead) &&((void)0)
1682 "Invoke does not have dead successors!")((void)0);
1683 BasicBlock *BB = II->getParent();
1684 BasicBlock *NormalDestBB = II->getNormalDest();
1685 if (UnwindBBIsDead) {
1686 Instruction *NormalNextIP = &NormalDestBB->front();
1687 if (Invoke2CallAllowed) {
1688 changeToCall(II);
1689 NormalNextIP = BB->getTerminator();
1690 }
1691 if (NormalBBIsDead)
1692 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
1693 } else {
1694 assert(NormalBBIsDead && "Broken invariant!")((void)0);
1695 if (!NormalDestBB->getUniquePredecessor())
1696 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
1697 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
1698 }
1699 }
1700 for (Instruction *I : TerminatorsToFold) {
1701 if (!isRunOn(*I->getFunction()))
1702 continue;
1703 CGModifiedFunctions.insert(I->getFunction());
1704 ConstantFoldTerminator(I->getParent());
1705 }
1706 for (auto &V : ToBeChangedToUnreachableInsts)
1707 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
1708 if (!isRunOn(*I->getFunction()))
1709 continue;
1710 CGModifiedFunctions.insert(I->getFunction());
1711 changeToUnreachable(I);
1712 }
1713
1714 for (auto &V : ToBeDeletedInsts) {
1715 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
1716 if (auto *CB = dyn_cast<CallBase>(I)) {
1717 if (!isRunOn(*I->getFunction()))
1718 continue;
1719 if (!isa<IntrinsicInst>(CB))
1720 CGUpdater.removeCallSite(*CB);
1721 }
1722 I->dropDroppableUses();
1723 CGModifiedFunctions.insert(I->getFunction());
1724 if (!I->getType()->isVoidTy())
1725 I->replaceAllUsesWith(UndefValue::get(I->getType()));
1726 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
1727 DeadInsts.push_back(I);
1728 else
1729 I->eraseFromParent();
1730 }
1731 }
1732
1733 llvm::erase_if(DeadInsts, [&](WeakTrackingVH I) {
1734 return !I || !isRunOn(*cast<Instruction>(I)->getFunction());
1735 });
1736
1737 LLVM_DEBUG({do { } while (false)
1738 dbgs() << "[Attributor] DeadInsts size: " << DeadInsts.size() << "\n";do { } while (false)
1739 for (auto &I : DeadInsts)do { } while (false)
1740 if (I)do { } while (false)
1741 dbgs() << " - " << *I << "\n";do { } while (false)
1742 })do { } while (false);
1743
1744 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
1745
1746 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
1747 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
1748 ToBeDeletedBBs.reserve(NumDeadBlocks);
1749 for (BasicBlock *BB : ToBeDeletedBlocks) {
1750 assert(isRunOn(*BB->getParent()) &&((void)0)
1751 "Cannot delete a block outside the current SCC!")((void)0);
1752 CGModifiedFunctions.insert(BB->getParent());
1753 // Do not delete BBs added during manifests of AAs.
1754 if (ManifestAddedBlocks.contains(BB))
1755 continue;
1756 ToBeDeletedBBs.push_back(BB);
1757 }
1758 // Actually we do not delete the blocks but squash them into a single
1759 // unreachable but untangling branches that jump here is something we need
1760 // to do in a more generic way.
1761 DetatchDeadBlocks(ToBeDeletedBBs, nullptr);
1762 }
1763
1764 identifyDeadInternalFunctions();
1765
1766 // Rewrite the functions as requested during manifest.
1767 ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions);
1768
1769 for (Function *Fn : CGModifiedFunctions)
1770 if (!ToBeDeletedFunctions.count(Fn) && Functions.count(Fn))
1771 CGUpdater.reanalyzeFunction(*Fn);
1772
1773 for (Function *Fn : ToBeDeletedFunctions) {
1774 if (!Functions.count(Fn))
1775 continue;
1776 CGUpdater.removeFunction(*Fn);
1777 }
1778
1779 if (!ToBeChangedUses.empty())
1780 ManifestChange = ChangeStatus::CHANGED;
1781
1782 if (!ToBeChangedToUnreachableInsts.empty())
1783 ManifestChange = ChangeStatus::CHANGED;
1784
1785 if (!ToBeDeletedFunctions.empty())
1786 ManifestChange = ChangeStatus::CHANGED;
1787
1788 if (!ToBeDeletedBlocks.empty())
1789 ManifestChange = ChangeStatus::CHANGED;
1790
1791 if (!ToBeDeletedInsts.empty())
1792 ManifestChange = ChangeStatus::CHANGED;
1793
1794 if (!InvokeWithDeadSuccessor.empty())
1795 ManifestChange = ChangeStatus::CHANGED;
1796
1797 if (!DeadInsts.empty())
1798 ManifestChange = ChangeStatus::CHANGED;
1799
1800 NumFnDeleted += ToBeDeletedFunctions.size();
1801
1802 LLVM_DEBUG(dbgs() << "[Attributor] Deleted " << ToBeDeletedFunctions.size()do { } while (false)
1803 << " functions after manifest.\n")do { } while (false);
1804
1805#ifdef EXPENSIVE_CHECKS
1806 for (Function *F : Functions) {
1807 if (ToBeDeletedFunctions.count(F))
1808 continue;
1809 assert(!verifyFunction(*F, &errs()) && "Module verification failed!")((void)0);
1810 }
1811#endif
1812
1813 return ManifestChange;
1814}
1815
1816ChangeStatus Attributor::run() {
1817 TimeTraceScope TimeScope("Attributor::run");
1818 AttributorCallGraph ACallGraph(*this);
1819
1820 if (PrintCallGraph)
1821 ACallGraph.populateAll();
1822
1823 Phase = AttributorPhase::UPDATE;
1824 runTillFixpoint();
1825
1826 // dump graphs on demand
1827 if (DumpDepGraph)
1828 DG.dumpGraph();
1829
1830 if (ViewDepGraph)
1831 DG.viewGraph();
1832
1833 if (PrintDependencies)
1834 DG.print();
1835
1836 Phase = AttributorPhase::MANIFEST;
1837 ChangeStatus ManifestChange = manifestAttributes();
1838
1839 Phase = AttributorPhase::CLEANUP;
1840 ChangeStatus CleanupChange = cleanupIR();
1841
1842 if (PrintCallGraph)
1843 ACallGraph.print();
1844
1845 return ManifestChange | CleanupChange;
1846}
1847
1848ChangeStatus Attributor::updateAA(AbstractAttribute &AA) {
1849 TimeTraceScope TimeScope(
1850 AA.getName() + std::to_string(AA.getIRPosition().getPositionKind()) +
1851 "::updateAA");
1852 assert(Phase == AttributorPhase::UPDATE &&((void)0)
1853 "We can update AA only in the update stage!")((void)0);
1854
1855 // Use a new dependence vector for this update.
1856 DependenceVector DV;
1857 DependenceStack.push_back(&DV);
1858
1859 auto &AAState = AA.getState();
1860 ChangeStatus CS = ChangeStatus::UNCHANGED;
1861 bool UsedAssumedInformation = false;
1862 if (!isAssumedDead(AA, nullptr, UsedAssumedInformation,
1863 /* CheckBBLivenessOnly */ true))
1864 CS = AA.update(*this);
1865
1866 if (DV.empty()) {
1867 // If the attribute did not query any non-fix information, the state
1868 // will not change and we can indicate that right away.
1869 AAState.indicateOptimisticFixpoint();
1870 }
1871
1872 if (!AAState.isAtFixpoint())
1873 rememberDependences();
1874
1875 // Verify the stack was used properly, that is we pop the dependence vector we
1876 // put there earlier.
1877 DependenceVector *PoppedDV = DependenceStack.pop_back_val();
1878 (void)PoppedDV;
1879 assert(PoppedDV == &DV && "Inconsistent usage of the dependence stack!")((void)0);
1880
1881 return CS;
1882}
1883
1884void Attributor::createShallowWrapper(Function &F) {
1885 assert(!F.isDeclaration() && "Cannot create a wrapper around a declaration!")((void)0);
1886
1887 Module &M = *F.getParent();
1888 LLVMContext &Ctx = M.getContext();
1889 FunctionType *FnTy = F.getFunctionType();
1890
1891 Function *Wrapper =
1892 Function::Create(FnTy, F.getLinkage(), F.getAddressSpace(), F.getName());
1893 F.setName(""); // set the inside function anonymous
1894 M.getFunctionList().insert(F.getIterator(), Wrapper);
1895
1896 F.setLinkage(GlobalValue::InternalLinkage);
1897
1898 F.replaceAllUsesWith(Wrapper);
1899 assert(F.use_empty() && "Uses remained after wrapper was created!")((void)0);
1900
1901 // Move the COMDAT section to the wrapper.
1902 // TODO: Check if we need to keep it for F as well.
1903 Wrapper->setComdat(F.getComdat());
1904 F.setComdat(nullptr);
1905
1906 // Copy all metadata and attributes but keep them on F as well.
1907 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
1908 F.getAllMetadata(MDs);
1909 for (auto MDIt : MDs)
1910 Wrapper->addMetadata(MDIt.first, *MDIt.second);
1911 Wrapper->setAttributes(F.getAttributes());
1912
1913 // Create the call in the wrapper.
1914 BasicBlock *EntryBB = BasicBlock::Create(Ctx, "entry", Wrapper);
1915
1916 SmallVector<Value *, 8> Args;
1917 Argument *FArgIt = F.arg_begin();
1918 for (Argument &Arg : Wrapper->args()) {
1919 Args.push_back(&Arg);
1920 Arg.setName((FArgIt++)->getName());
1921 }
1922
1923 CallInst *CI = CallInst::Create(&F, Args, "", EntryBB);
1924 CI->setTailCall(true);
1925 CI->addAttribute(AttributeList::FunctionIndex, Attribute::NoInline);
1926 ReturnInst::Create(Ctx, CI->getType()->isVoidTy() ? nullptr : CI, EntryBB);
1927
1928 NumFnShallowWrappersCreated++;
1929}
1930
1931bool Attributor::isInternalizable(Function &F) {
1932 if (F.isDeclaration() || F.hasLocalLinkage() ||
1933 GlobalValue::isInterposableLinkage(F.getLinkage()))
1934 return false;
1935 return true;
1936}
1937
1938Function *Attributor::internalizeFunction(Function &F, bool Force) {
1939 if (!AllowDeepWrapper && !Force)
1940 return nullptr;
1941 if (!isInternalizable(F))
1942 return nullptr;
1943
1944 SmallPtrSet<Function *, 2> FnSet = {&F};
1945 DenseMap<Function *, Function *> InternalizedFns;
1946 internalizeFunctions(FnSet, InternalizedFns);
1947
1948 return InternalizedFns[&F];
1949}
1950
1951bool Attributor::internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet,
1952 DenseMap<Function *, Function *> &FnMap) {
1953 for (Function *F : FnSet)
1954 if (!Attributor::isInternalizable(*F))
1955 return false;
1956
1957 FnMap.clear();
1958 // Generate the internalized version of each function.
1959 for (Function *F : FnSet) {
1960 Module &M = *F->getParent();
1961 FunctionType *FnTy = F->getFunctionType();
1962
1963 // Create a copy of the current function
1964 Function *Copied =
1965 Function::Create(FnTy, F->getLinkage(), F->getAddressSpace(),
1966 F->getName() + ".internalized");
1967 ValueToValueMapTy VMap;
1968 auto *NewFArgIt = Copied->arg_begin();
1969 for (auto &Arg : F->args()) {
1970 auto ArgName = Arg.getName();
1971 NewFArgIt->setName(ArgName);
1972 VMap[&Arg] = &(*NewFArgIt++);
1973 }
1974 SmallVector<ReturnInst *, 8> Returns;
1975
1976 // Copy the body of the original function to the new one
1977 CloneFunctionInto(Copied, F, VMap,
1978 CloneFunctionChangeType::LocalChangesOnly, Returns);
1979
1980 // Set the linakage and visibility late as CloneFunctionInto has some
1981 // implicit requirements.
1982 Copied->setVisibility(GlobalValue::DefaultVisibility);
1983 Copied->setLinkage(GlobalValue::PrivateLinkage);
1984
1985 // Copy metadata
1986 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
1987 F->getAllMetadata(MDs);
1988 for (auto MDIt : MDs)
1989 if (!Copied->hasMetadata())
1990 Copied->addMetadata(MDIt.first, *MDIt.second);
1991
1992 M.getFunctionList().insert(F->getIterator(), Copied);
1993 Copied->setDSOLocal(true);
1994 FnMap[F] = Copied;
1995 }
1996
1997 // Replace all uses of the old function with the new internalized function
1998 // unless the caller is a function that was just internalized.
1999 for (Function *F : FnSet) {
2000 auto &InternalizedFn = FnMap[F];
2001 auto IsNotInternalized = [&](Use &U) -> bool {
2002 if (auto *CB = dyn_cast<CallBase>(U.getUser()))
2003 return !FnMap.lookup(CB->getCaller());
2004 return false;
2005 };
2006 F->replaceUsesWithIf(InternalizedFn, IsNotInternalized);
2007 }
2008
2009 return true;
2010}
2011
2012bool Attributor::isValidFunctionSignatureRewrite(
2013 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
2014
2015 if (!RewriteSignatures)
2016 return false;
2017
2018 auto CallSiteCanBeChanged = [](AbstractCallSite ACS) {
2019 // Forbid the call site to cast the function return type. If we need to
2020 // rewrite these functions we need to re-create a cast for the new call site
2021 // (if the old had uses).
2022 if (!ACS.getCalledFunction() ||
2023 ACS.getInstruction()->getType() !=
2024 ACS.getCalledFunction()->getReturnType())
2025 return false;
2026 // Forbid must-tail calls for now.
2027 return !ACS.isCallbackCall() && !ACS.getInstruction()->isMustTailCall();
2028 };
2029
2030 Function *Fn = Arg.getParent();
2031 // Avoid var-arg functions for now.
2032 if (Fn->isVarArg()) {
2033 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n")do { } while (false);
2034 return false;
2035 }
2036
2037 // Avoid functions with complicated argument passing semantics.
2038 AttributeList FnAttributeList = Fn->getAttributes();
2039 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
2040 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
2041 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca) ||
2042 FnAttributeList.hasAttrSomewhere(Attribute::Preallocated)) {
2043 LLVM_DEBUG(do { } while (false)
2044 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n")do { } while (false);
2045 return false;
2046 }
2047
2048 // Avoid callbacks for now.
2049 bool AllCallSitesKnown;
2050 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
2051 AllCallSitesKnown)) {
2052 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n")do { } while (false);
2053 return false;
2054 }
2055
2056 auto InstPred = [](Instruction &I) {
2057 if (auto *CI = dyn_cast<CallInst>(&I))
2058 return !CI->isMustTailCall();
2059 return true;
2060 };
2061
2062 // Forbid must-tail calls for now.
2063 // TODO:
2064 bool UsedAssumedInformation = false;
2065 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
2066 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
2067 nullptr, {Instruction::Call},
2068 UsedAssumedInformation)) {
2069 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n")do { } while (false);
2070 return false;
2071 }
2072
2073 return true;
2074}
2075
2076bool Attributor::registerFunctionSignatureRewrite(
2077 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2078 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
2079 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
2080 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "do { } while (false)
2081 << Arg.getParent()->getName() << " with "do { } while (false)
2082 << ReplacementTypes.size() << " replacements\n")do { } while (false);
2083 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&((void)0)
2084 "Cannot register an invalid rewrite")((void)0);
2085
2086 Function *Fn = Arg.getParent();
2087 SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
2088 ArgumentReplacementMap[Fn];
2089 if (ARIs.empty())
2090 ARIs.resize(Fn->arg_size());
2091
2092 // If we have a replacement already with less than or equal new arguments,
2093 // ignore this request.
2094 std::unique_ptr<ArgumentReplacementInfo> &ARI = ARIs[Arg.getArgNo()];
2095 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
2096 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n")do { } while (false);
2097 return false;
2098 }
2099
2100 // If we have a replacement already but we like the new one better, delete
2101 // the old.
2102 ARI.reset();
2103
2104 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "do { } while (false)
2105 << Arg.getParent()->getName() << " with "do { } while (false)
2106 << ReplacementTypes.size() << " replacements\n")do { } while (false);
2107
2108 // Remember the replacement.
2109 ARI.reset(new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
2110 std::move(CalleeRepairCB),
2111 std::move(ACSRepairCB)));
2112
2113 return true;
2114}
2115
2116bool Attributor::shouldSeedAttribute(AbstractAttribute &AA) {
2117 bool Result = true;
2118#ifndef NDEBUG1
2119 if (SeedAllowList.size() != 0)
2120 Result =
2121 std::count(SeedAllowList.begin(), SeedAllowList.end(), AA.getName());
2122 Function *Fn = AA.getAnchorScope();
2123 if (FunctionSeedAllowList.size() != 0 && Fn)
2124 Result &= std::count(FunctionSeedAllowList.begin(),
2125 FunctionSeedAllowList.end(), Fn->getName());
2126#endif
2127 return Result;
2128}
2129
2130ChangeStatus Attributor::rewriteFunctionSignatures(
2131 SmallPtrSetImpl<Function *> &ModifiedFns) {
2132 ChangeStatus Changed = ChangeStatus::UNCHANGED;
2133
2134 for (auto &It : ArgumentReplacementMap) {
2135 Function *OldFn = It.getFirst();
2136
2137 // Deleted functions do not require rewrites.
2138 if (!Functions.count(OldFn) || ToBeDeletedFunctions.count(OldFn))
2139 continue;
2140
2141 const SmallVectorImpl<std::unique_ptr<ArgumentReplacementInfo>> &ARIs =
2142 It.getSecond();
2143 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!")((void)0);
2144
2145 SmallVector<Type *, 16> NewArgumentTypes;
2146 SmallVector<AttributeSet, 16> NewArgumentAttributes;
2147
2148 // Collect replacement argument types and copy over existing attributes.
2149 AttributeList OldFnAttributeList = OldFn->getAttributes();
2150 for (Argument &Arg : OldFn->args()) {
2151 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
2152 ARIs[Arg.getArgNo()]) {
2153 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
2154 ARI->ReplacementTypes.end());
2155 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
2156 AttributeSet());
2157 } else {
2158 NewArgumentTypes.push_back(Arg.getType());
2159 NewArgumentAttributes.push_back(
2160 OldFnAttributeList.getParamAttributes(Arg.getArgNo()));
2161 }
2162 }
2163
2164 FunctionType *OldFnTy = OldFn->getFunctionType();
2165 Type *RetTy = OldFnTy->getReturnType();
2166
2167 // Construct the new function type using the new arguments types.
2168 FunctionType *NewFnTy =
2169 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
2170
2171 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()do { } while (false)
2172 << "' from " << *OldFn->getFunctionType() << " to "do { } while (false)
2173 << *NewFnTy << "\n")do { } while (false);
2174
2175 // Create the new function body and insert it into the module.
2176 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
2177 OldFn->getAddressSpace(), "");
2178 Functions.insert(NewFn);
2179 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
2180 NewFn->takeName(OldFn);
2181 NewFn->copyAttributesFrom(OldFn);
2182
2183 // Patch the pointer to LLVM function in debug info descriptor.
2184 NewFn->setSubprogram(OldFn->getSubprogram());
2185 OldFn->setSubprogram(nullptr);
2186
2187 // Recompute the parameter attributes list based on the new arguments for
2188 // the function.
2189 LLVMContext &Ctx = OldFn->getContext();
2190 NewFn->setAttributes(AttributeList::get(
2191 Ctx, OldFnAttributeList.getFnAttributes(),
2192 OldFnAttributeList.getRetAttributes(), NewArgumentAttributes));
2193
2194 // Since we have now created the new function, splice the body of the old
2195 // function right into the new function, leaving the old rotting hulk of the
2196 // function empty.
2197 NewFn->getBasicBlockList().splice(NewFn->begin(),
2198 OldFn->getBasicBlockList());
2199
2200 // Fixup block addresses to reference new function.
2201 SmallVector<BlockAddress *, 8u> BlockAddresses;
2202 for (User *U : OldFn->users())
2203 if (auto *BA = dyn_cast<BlockAddress>(U))
2204 BlockAddresses.push_back(BA);
2205 for (auto *BA : BlockAddresses)
2206 BA->replaceAllUsesWith(BlockAddress::get(NewFn, BA->getBasicBlock()));
2207
2208 // Set of all "call-like" instructions that invoke the old function mapped
2209 // to their new replacements.
2210 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
2211
2212 // Callback to create a new "call-like" instruction for a given one.
2213 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
2214 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
2215 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
2216
2217 // Collect the new argument operands for the replacement call site.
2218 SmallVector<Value *, 16> NewArgOperands;
2219 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
2220 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
2221 unsigned NewFirstArgNum = NewArgOperands.size();
2222 (void)NewFirstArgNum; // only used inside assert.
2223 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
2224 ARIs[OldArgNum]) {
2225 if (ARI->ACSRepairCB)
2226 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
2227 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==((void)0)
2228 NewArgOperands.size() &&((void)0)
2229 "ACS repair callback did not provide as many operand as new "((void)0)
2230 "types were registered!")((void)0);
2231 // TODO: Exose the attribute set to the ACS repair callback
2232 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
2233 AttributeSet());
2234 } else {
2235 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
2236 NewArgOperandAttributes.push_back(
2237 OldCallAttributeList.getParamAttributes(OldArgNum));
2238 }
2239 }
2240
2241 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&((void)0)
2242 "Mismatch # argument operands vs. # argument operand attributes!")((void)0);
2243 assert(NewArgOperands.size() == NewFn->arg_size() &&((void)0)
2244 "Mismatch # argument operands vs. # function arguments!")((void)0);
2245
2246 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
2247 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
2248
2249 // Create a new call or invoke instruction to replace the old one.
2250 CallBase *NewCB;
2251 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
2252 NewCB =
2253 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
2254 NewArgOperands, OperandBundleDefs, "", OldCB);
2255 } else {
2256 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
2257 "", OldCB);
2258 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
2259 NewCB = NewCI;
2260 }
2261
2262 // Copy over various properties and the new attributes.
2263 NewCB->copyMetadata(*OldCB, {LLVMContext::MD_prof, LLVMContext::MD_dbg});
2264 NewCB->setCallingConv(OldCB->getCallingConv());
2265 NewCB->takeName(OldCB);
2266 NewCB->setAttributes(AttributeList::get(
2267 Ctx, OldCallAttributeList.getFnAttributes(),
2268 OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes));
2269
2270 CallSitePairs.push_back({OldCB, NewCB});
2271 return true;
2272 };
2273
2274 // Use the CallSiteReplacementCreator to create replacement call sites.
2275 bool AllCallSitesKnown;
2276 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
2277 true, nullptr, AllCallSitesKnown);
2278 (void)Success;
2279 assert(Success && "Assumed call site replacement to succeed!")((void)0);
2280
2281 // Rewire the arguments.
2282 Argument *OldFnArgIt = OldFn->arg_begin();
2283 Argument *NewFnArgIt = NewFn->arg_begin();
2284 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
2285 ++OldArgNum, ++OldFnArgIt) {
2286 if (const std::unique_ptr<ArgumentReplacementInfo> &ARI =
2287 ARIs[OldArgNum]) {
2288 if (ARI->CalleeRepairCB)
2289 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
2290 NewFnArgIt += ARI->ReplacementTypes.size();
2291 } else {
2292 NewFnArgIt->takeName(&*OldFnArgIt);
2293 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
2294 ++NewFnArgIt;
2295 }
2296 }
2297
2298 // Eliminate the instructions *after* we visited all of them.
2299 for (auto &CallSitePair : CallSitePairs) {
2300 CallBase &OldCB = *CallSitePair.first;
2301 CallBase &NewCB = *CallSitePair.second;
2302 assert(OldCB.getType() == NewCB.getType() &&((void)0)
2303 "Cannot handle call sites with different types!")((void)0);
2304 ModifiedFns.insert(OldCB.getFunction());
2305 CGUpdater.replaceCallSite(OldCB, NewCB);
2306 OldCB.replaceAllUsesWith(&NewCB);
2307 OldCB.eraseFromParent();
2308 }
2309
2310 // Replace the function in the call graph (if any).
2311 CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
2312
2313 // If the old function was modified and needed to be reanalyzed, the new one
2314 // does now.
2315 if (ModifiedFns.erase(OldFn))
2316 ModifiedFns.insert(NewFn);
2317
2318 Changed = ChangeStatus::CHANGED;
2319 }
2320
2321 return Changed;
2322}
2323
2324void InformationCache::initializeInformationCache(const Function &CF,
2325 FunctionInfo &FI) {
2326 // As we do not modify the function here we can remove the const
2327 // withouth breaking implicit assumptions. At the end of the day, we could
2328 // initialize the cache eagerly which would look the same to the users.
2329 Function &F = const_cast<Function &>(CF);
2330
2331 // Walk all instructions to find interesting instructions that might be
2332 // queried by abstract attributes during their initialization or update.
2333 // This has to happen before we create attributes.
2334
2335 for (Instruction &I : instructions(&F)) {
2336 bool IsInterestingOpcode = false;
2337
2338 // To allow easy access to all instructions in a function with a given
2339 // opcode we store them in the InfoCache. As not all opcodes are interesting
2340 // to concrete attributes we only cache the ones that are as identified in
2341 // the following switch.
2342 // Note: There are no concrete attributes now so this is initially empty.
2343 switch (I.getOpcode()) {
2344 default:
2345 assert(!isa<CallBase>(&I) &&((void)0)
2346 "New call base instruction type needs to be known in the "((void)0)
2347 "Attributor.")((void)0);
2348 break;
2349 case Instruction::Call:
2350 // Calls are interesting on their own, additionally:
2351 // For `llvm.assume` calls we also fill the KnowledgeMap as we find them.
2352 // For `must-tail` calls we remember the caller and callee.
2353 if (auto *Assume = dyn_cast<AssumeInst>(&I)) {
2354 fillMapFromAssume(*Assume, KnowledgeMap);
2355 } else if (cast<CallInst>(I).isMustTailCall()) {
2356 FI.ContainsMustTailCall = true;
2357 if (const Function *Callee = cast<CallInst>(I).getCalledFunction())
2358 getFunctionInfo(*Callee).CalledViaMustTail = true;
2359 }
2360 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2361 case Instruction::CallBr:
2362 case Instruction::Invoke:
2363 case Instruction::CleanupRet:
2364 case Instruction::CatchSwitch:
2365 case Instruction::AtomicRMW:
2366 case Instruction::AtomicCmpXchg:
2367 case Instruction::Br:
2368 case Instruction::Resume:
2369 case Instruction::Ret:
2370 case Instruction::Load:
2371 // The alignment of a pointer is interesting for loads.
2372 case Instruction::Store:
2373 // The alignment of a pointer is interesting for stores.
2374 case Instruction::Alloca:
2375 case Instruction::AddrSpaceCast:
2376 IsInterestingOpcode = true;
2377 }
2378 if (IsInterestingOpcode) {
2379 auto *&Insts = FI.OpcodeInstMap[I.getOpcode()];
2380 if (!Insts)
2381 Insts = new (Allocator) InstructionVectorTy();
2382 Insts->push_back(&I);
2383 }
2384 if (I.mayReadOrWriteMemory())
2385 FI.RWInsts.push_back(&I);
2386 }
2387
2388 if (F.hasFnAttribute(Attribute::AlwaysInline) &&
2389 isInlineViable(F).isSuccess())
2390 InlineableFunctions.insert(&F);
2391}
2392
2393AAResults *InformationCache::getAAResultsForFunction(const Function &F) {
2394 return AG.getAnalysis<AAManager>(F);
2395}
2396
2397InformationCache::FunctionInfo::~FunctionInfo() {
2398 // The instruction vectors are allocated using a BumpPtrAllocator, we need to
2399 // manually destroy them.
2400 for (auto &It : OpcodeInstMap)
2401 It.getSecond()->~InstructionVectorTy();
2402}
2403
2404void Attributor::recordDependence(const AbstractAttribute &FromAA,
2405 const AbstractAttribute &ToAA,
2406 DepClassTy DepClass) {
2407 if (DepClass == DepClassTy::NONE)
2408 return;
2409 // If we are outside of an update, thus before the actual fixpoint iteration
2410 // started (= when we create AAs), we do not track dependences because we will
2411 // put all AAs into the initial worklist anyway.
2412 if (DependenceStack.empty())
2413 return;
2414 if (FromAA.getState().isAtFixpoint())
2415 return;
2416 DependenceStack.back()->push_back({&FromAA, &ToAA, DepClass});
2417}
2418
2419void Attributor::rememberDependences() {
2420 assert(!DependenceStack.empty() && "No dependences to remember!")((void)0);
2421
2422 for (DepInfo &DI : *DependenceStack.back()) {
2423 assert((DI.DepClass == DepClassTy::REQUIRED ||((void)0)
2424 DI.DepClass == DepClassTy::OPTIONAL) &&((void)0)
2425 "Expected required or optional dependence (1 bit)!")((void)0);
2426 auto &DepAAs = const_cast<AbstractAttribute &>(*DI.FromAA).Deps;
2427 DepAAs.push_back(AbstractAttribute::DepTy(
2428 const_cast<AbstractAttribute *>(DI.ToAA), unsigned(DI.DepClass)));
2429 }
2430}
2431
2432void Attributor::identifyDefaultAbstractAttributes(Function &F) {
2433 if (!VisitedFunctions.insert(&F).second)
2434 return;
2435 if (F.isDeclaration())
2436 return;
2437
2438 // In non-module runs we need to look at the call sites of a function to
2439 // determine if it is part of a must-tail call edge. This will influence what
2440 // attributes we can derive.
2441 InformationCache::FunctionInfo &FI = InfoCache.getFunctionInfo(F);
2442 if (!isModulePass() && !FI.CalledViaMustTail) {
2443 for (const Use &U : F.uses())
2444 if (const auto *CB = dyn_cast<CallBase>(U.getUser()))
2445 if (CB->isCallee(&U) && CB->isMustTailCall())
2446 FI.CalledViaMustTail = true;
2447 }
2448
2449 IRPosition FPos = IRPosition::function(F);
2450
2451 // Check for dead BasicBlocks in every function.
2452 // We need dead instruction detection because we do not want to deal with
2453 // broken IR in which SSA rules do not apply.
2454 getOrCreateAAFor<AAIsDead>(FPos);
2455
2456 // Every function might be "will-return".
2457 getOrCreateAAFor<AAWillReturn>(FPos);
2458
2459 // Every function might contain instructions that cause "undefined behavior".
2460 getOrCreateAAFor<AAUndefinedBehavior>(FPos);
2461
2462 // Every function can be nounwind.
2463 getOrCreateAAFor<AANoUnwind>(FPos);
2464
2465 // Every function might be marked "nosync"
2466 getOrCreateAAFor<AANoSync>(FPos);
2467
2468 // Every function might be "no-free".
2469 getOrCreateAAFor<AANoFree>(FPos);
2470
2471 // Every function might be "no-return".
2472 getOrCreateAAFor<AANoReturn>(FPos);
2473
2474 // Every function might be "no-recurse".
2475 getOrCreateAAFor<AANoRecurse>(FPos);
2476
2477 // Every function might be "readnone/readonly/writeonly/...".
2478 getOrCreateAAFor<AAMemoryBehavior>(FPos);
2479
2480 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
2481 getOrCreateAAFor<AAMemoryLocation>(FPos);
2482
2483 // Every function might be applicable for Heap-To-Stack conversion.
2484 if (EnableHeapToStack)
2485 getOrCreateAAFor<AAHeapToStack>(FPos);
2486
2487 // Return attributes are only appropriate if the return type is non void.
2488 Type *ReturnType = F.getReturnType();
2489 if (!ReturnType->isVoidTy()) {
2490 // Argument attribute "returned" --- Create only one per function even
2491 // though it is an argument attribute.
2492 getOrCreateAAFor<AAReturnedValues>(FPos);
2493
2494 IRPosition RetPos = IRPosition::returned(F);
2495
2496 // Every returned value might be dead.
2497 getOrCreateAAFor<AAIsDead>(RetPos);
2498
2499 // Every function might be simplified.
2500 getOrCreateAAFor<AAValueSimplify>(RetPos);
2501
2502 // Every returned value might be marked noundef.
2503 getOrCreateAAFor<AANoUndef>(RetPos);
2504
2505 if (ReturnType->isPointerTy()) {
2506
2507 // Every function with pointer return type might be marked align.
2508 getOrCreateAAFor<AAAlign>(RetPos);
2509
2510 // Every function with pointer return type might be marked nonnull.
2511 getOrCreateAAFor<AANonNull>(RetPos);
2512
2513 // Every function with pointer return type might be marked noalias.
2514 getOrCreateAAFor<AANoAlias>(RetPos);
2515
2516 // Every function with pointer return type might be marked
2517 // dereferenceable.
2518 getOrCreateAAFor<AADereferenceable>(RetPos);
2519 }
2520 }
2521
2522 for (Argument &Arg : F.args()) {
2523 IRPosition ArgPos = IRPosition::argument(Arg);
2524
2525 // Every argument might be simplified. We have to go through the Attributor
2526 // interface though as outside AAs can register custom simplification
2527 // callbacks.
2528 bool UsedAssumedInformation = false;
2529 getAssumedSimplified(ArgPos, /* AA */ nullptr, UsedAssumedInformation);
2530
2531 // Every argument might be dead.
2532 getOrCreateAAFor<AAIsDead>(ArgPos);
2533
2534 // Every argument might be marked noundef.
2535 getOrCreateAAFor<AANoUndef>(ArgPos);
2536
2537 if (Arg.getType()->isPointerTy()) {
2538 // Every argument with pointer type might be marked nonnull.
2539 getOrCreateAAFor<AANonNull>(ArgPos);
2540
2541 // Every argument with pointer type might be marked noalias.
2542 getOrCreateAAFor<AANoAlias>(ArgPos);
2543
2544 // Every argument with pointer type might be marked dereferenceable.
2545 getOrCreateAAFor<AADereferenceable>(ArgPos);
2546
2547 // Every argument with pointer type might be marked align.
2548 getOrCreateAAFor<AAAlign>(ArgPos);
2549
2550 // Every argument with pointer type might be marked nocapture.
2551 getOrCreateAAFor<AANoCapture>(ArgPos);
2552
2553 // Every argument with pointer type might be marked
2554 // "readnone/readonly/writeonly/..."
2555 getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
2556
2557 // Every argument with pointer type might be marked nofree.
2558 getOrCreateAAFor<AANoFree>(ArgPos);
2559
2560 // Every argument with pointer type might be privatizable (or promotable)
2561 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
2562 }
2563 }
2564
2565 auto CallSitePred = [&](Instruction &I) -> bool {
2566 auto &CB = cast<CallBase>(I);
2567 IRPosition CBRetPos = IRPosition::callsite_returned(CB);
2568
2569 // Call sites might be dead if they do not have side effects and no live
2570 // users. The return value might be dead if there are no live users.
2571 getOrCreateAAFor<AAIsDead>(CBRetPos);
2572
2573 Function *Callee = CB.getCalledFunction();
2574 // TODO: Even if the callee is not known now we might be able to simplify
2575 // the call/callee.
2576 if (!Callee)
2577 return true;
2578
2579 // Skip declarations except if annotations on their call sites were
2580 // explicitly requested.
2581 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
2582 !Callee->hasMetadata(LLVMContext::MD_callback))
2583 return true;
2584
2585 if (!Callee->getReturnType()->isVoidTy() && !CB.use_empty()) {
2586
2587 IRPosition CBRetPos = IRPosition::callsite_returned(CB);
2588 getOrCreateAAFor<AAValueSimplify>(CBRetPos);
2589 }
2590
2591 for (int I = 0, E = CB.getNumArgOperands(); I < E; ++I) {
2592
2593 IRPosition CBArgPos = IRPosition::callsite_argument(CB, I);
2594
2595 // Every call site argument might be dead.
2596 getOrCreateAAFor<AAIsDead>(CBArgPos);
2597
2598 // Call site argument might be simplified. We have to go through the
2599 // Attributor interface though as outside AAs can register custom
2600 // simplification callbacks.
2601 bool UsedAssumedInformation = false;
2602 getAssumedSimplified(CBArgPos, /* AA */ nullptr, UsedAssumedInformation);
2603
2604 // Every call site argument might be marked "noundef".
2605 getOrCreateAAFor<AANoUndef>(CBArgPos);
2606
2607 if (!CB.getArgOperand(I)->getType()->isPointerTy())
2608 continue;
2609
2610 // Call site argument attribute "non-null".
2611 getOrCreateAAFor<AANonNull>(CBArgPos);
2612
2613 // Call site argument attribute "nocapture".
2614 getOrCreateAAFor<AANoCapture>(CBArgPos);
2615
2616 // Call site argument attribute "no-alias".
2617 getOrCreateAAFor<AANoAlias>(CBArgPos);
2618
2619 // Call site argument attribute "dereferenceable".
2620 getOrCreateAAFor<AADereferenceable>(CBArgPos);
2621
2622 // Call site argument attribute "align".
2623 getOrCreateAAFor<AAAlign>(CBArgPos);
2624
2625 // Call site argument attribute
2626 // "readnone/readonly/writeonly/..."
2627 getOrCreateAAFor<AAMemoryBehavior>(CBArgPos);
2628
2629 // Call site argument attribute "nofree".
2630 getOrCreateAAFor<AANoFree>(CBArgPos);
2631 }
2632 return true;
2633 };
2634
2635 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
2636 bool Success;
2637 bool UsedAssumedInformation = false;
2638 Success = checkForAllInstructionsImpl(
2639 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
2640 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
2641 (unsigned)Instruction::Call},
2642 UsedAssumedInformation);
2643 (void)Success;
2644 assert(Success && "Expected the check call to be successful!")((void)0);
2645
2646 auto LoadStorePred = [&](Instruction &I) -> bool {
2647 if (isa<LoadInst>(I)) {
2648 getOrCreateAAFor<AAAlign>(
2649 IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
2650 if (SimplifyAllLoads)
2651 getOrCreateAAFor<AAValueSimplify>(IRPosition::value(I));
2652 } else
2653 getOrCreateAAFor<AAAlign>(
2654 IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
2655 return true;
2656 };
2657 Success = checkForAllInstructionsImpl(
2658 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
2659 {(unsigned)Instruction::Load, (unsigned)Instruction::Store},
2660 UsedAssumedInformation);
2661 (void)Success;
2662 assert(Success && "Expected the check call to be successful!")((void)0);
2663}
2664
2665/// Helpers to ease debugging through output streams and print calls.
2666///
2667///{
2668raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
2669 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
2670}
2671
2672raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
2673 switch (AP) {
2674 case IRPosition::IRP_INVALID:
2675 return OS << "inv";
2676 case IRPosition::IRP_FLOAT:
2677 return OS << "flt";
2678 case IRPosition::IRP_RETURNED:
2679 return OS << "fn_ret";
2680 case IRPosition::IRP_CALL_SITE_RETURNED:
2681 return OS << "cs_ret";
2682 case IRPosition::IRP_FUNCTION:
2683 return OS << "fn";
2684 case IRPosition::IRP_CALL_SITE:
2685 return OS << "cs";
2686 case IRPosition::IRP_ARGUMENT:
2687 return OS << "arg";
2688 case IRPosition::IRP_CALL_SITE_ARGUMENT:
2689 return OS << "cs_arg";
2690 }
2691 llvm_unreachable("Unknown attribute position!")__builtin_unreachable();
2692}
2693
2694raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
2695 const Value &AV = Pos.getAssociatedValue();
2696 OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
2697 << Pos.getAnchorValue().getName() << "@" << Pos.getCallSiteArgNo() << "]";
2698
2699 if (Pos.hasCallBaseContext())
2700 OS << "[cb_context:" << *Pos.getCallBaseContext() << "]";
2701 return OS << "}";
2702}
2703
2704raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
2705 OS << "range-state(" << S.getBitWidth() << ")<";
2706 S.getKnown().print(OS);
2707 OS << " / ";
2708 S.getAssumed().print(OS);
2709 OS << ">";
2710
2711 return OS << static_cast<const AbstractState &>(S);
2712}
2713
2714raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
2715 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
2716}
2717
2718raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
2719 AA.print(OS);
2720 return OS;
2721}
2722
2723raw_ostream &llvm::operator<<(raw_ostream &OS,
2724 const PotentialConstantIntValuesState &S) {
2725 OS << "set-state(< {";
2726 if (!S.isValidState())
2727 OS << "full-set";
2728 else {
2729 for (auto &it : S.getAssumedSet())
2730 OS << it << ", ";
2731 if (S.undefIsContained())
2732 OS << "undef ";
2733 }
2734 OS << "} >)";
2735
2736 return OS;
2737}
2738
2739void AbstractAttribute::print(raw_ostream &OS) const {
2740 OS << "[";
2741 OS << getName();
2742 OS << "] for CtxI ";
2743
2744 if (auto *I = getCtxI()) {
2745 OS << "'";
2746 I->print(OS);
2747 OS << "'";
2748 } else
2749 OS << "<<null inst>>";
2750
2751 OS << " at position " << getIRPosition() << " with state " << getAsStr()
2752 << '\n';
2753}
2754
2755void AbstractAttribute::printWithDeps(raw_ostream &OS) const {
2756 print(OS);
2757
2758 for (const auto &DepAA : Deps) {
2759 auto *AA = DepAA.getPointer();
2760 OS << " updates ";
2761 AA->print(OS);
2762 }
2763
2764 OS << '\n';
2765}
2766
2767raw_ostream &llvm::operator<<(raw_ostream &OS,
2768 const AAPointerInfo::Access &Acc) {
2769 OS << " [" << Acc.getKind() << "] " << *Acc.getRemoteInst();
2770 if (Acc.getLocalInst() != Acc.getRemoteInst())
2771 OS << " via " << *Acc.getLocalInst();
2772 if (Acc.getContent().hasValue())
2773 OS << " [" << *Acc.getContent() << "]";
2774 return OS;
2775}
2776///}
2777
2778/// ----------------------------------------------------------------------------
2779/// Pass (Manager) Boilerplate
2780/// ----------------------------------------------------------------------------
2781
2782static bool runAttributorOnFunctions(InformationCache &InfoCache,
2783 SetVector<Function *> &Functions,
2784 AnalysisGetter &AG,
2785 CallGraphUpdater &CGUpdater,
2786 bool DeleteFns) {
2787 if (Functions.empty())
2788 return false;
2789
2790 LLVM_DEBUG({do { } while (false)
2791 dbgs() << "[Attributor] Run on module with " << Functions.size()do { } while (false)
2792 << " functions:\n";do { } while (false)
2793 for (Function *Fn : Functions)do { } while (false)
2794 dbgs() << " - " << Fn->getName() << "\n";do { } while (false)
2795 })do { } while (false);
2796
2797 // Create an Attributor and initially empty information cache that is filled
2798 // while we identify default attribute opportunities.
2799 Attributor A(Functions, InfoCache, CGUpdater, /* Allowed */ nullptr,
2800 DeleteFns);
2801
2802 // Create shallow wrappers for all functions that are not IPO amendable
2803 if (AllowShallowWrappers)
2804 for (Function *F : Functions)
2805 if (!A.isFunctionIPOAmendable(*F))
2806 Attributor::createShallowWrapper(*F);
2807
2808 // Internalize non-exact functions
2809 // TODO: for now we eagerly internalize functions without calculating the
2810 // cost, we need a cost interface to determine whether internalizing
2811 // a function is "benefitial"
2812 if (AllowDeepWrapper) {
2813 unsigned FunSize = Functions.size();
2814 for (unsigned u = 0; u < FunSize; u++) {
2815 Function *F = Functions[u];
2816 if (!F->isDeclaration() && !F->isDefinitionExact() && F->getNumUses() &&
2817 !GlobalValue::isInterposableLinkage(F->getLinkage())) {
2818 Function *NewF = Attributor::internalizeFunction(*F);
2819 assert(NewF && "Could not internalize function.")((void)0);
2820 Functions.insert(NewF);
2821
2822 // Update call graph
2823 CGUpdater.replaceFunctionWith(*F, *NewF);
2824 for (const Use &U : NewF->uses())
2825 if (CallBase *CB = dyn_cast<CallBase>(U.getUser())) {
2826 auto *CallerF = CB->getCaller();
2827 CGUpdater.reanalyzeFunction(*CallerF);
2828 }
2829 }
2830 }
2831 }
2832
2833 for (Function *F : Functions) {
2834 if (F->hasExactDefinition())
2835 NumFnWithExactDefinition++;
2836 else
2837 NumFnWithoutExactDefinition++;
2838
2839 // We look at internal functions only on-demand but if any use is not a
2840 // direct call or outside the current set of analyzed functions, we have
2841 // to do it eagerly.
2842 if (F->hasLocalLinkage()) {
2843 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
2844 const auto *CB = dyn_cast<CallBase>(U.getUser());
2845 return CB && CB->isCallee(&U) &&
2846 Functions.count(const_cast<Function *>(CB->getCaller()));
2847 }))
2848 continue;
2849 }
2850
2851 // Populate the Attributor with abstract attribute opportunities in the
2852 // function and the information cache with IR information.
2853 A.identifyDefaultAbstractAttributes(*F);
2854 }
2855
2856 ChangeStatus Changed = A.run();
2857
2858 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()do { } while (false)
2859 << " functions, result: " << Changed << ".\n")do { } while (false);
2860 return Changed == ChangeStatus::CHANGED;
2861}
2862
2863void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); }
2864
2865void AADepGraph::dumpGraph() {
2866 static std::atomic<int> CallTimes;
2867 std::string Prefix;
2868
2869 if (!DepGraphDotFileNamePrefix.empty())
2870 Prefix = DepGraphDotFileNamePrefix;
2871 else
2872 Prefix = "dep_graph";
2873 std::string Filename =
2874 Prefix + "_" + std::to_string(CallTimes.load()) + ".dot";
2875
2876 outs() << "Dependency graph dump to " << Filename << ".\n";
2877
2878 std::error_code EC;
2879
2880 raw_fd_ostream File(Filename, EC, sys::fs::OF_TextWithCRLF);
2881 if (!EC)
2882 llvm::WriteGraph(File, this);
2883
2884 CallTimes++;
2885}
2886
2887void AADepGraph::print() {
2888 for (auto DepAA : SyntheticRoot.Deps)
2889 cast<AbstractAttribute>(DepAA.getPointer())->printWithDeps(outs());
2890}
2891
2892PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
2893 FunctionAnalysisManager &FAM =
2894 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
2895 AnalysisGetter AG(FAM);
2896
2897 SetVector<Function *> Functions;
2898 for (Function &F : M)
2899 Functions.insert(&F);
2900
2901 CallGraphUpdater CGUpdater;
2902 BumpPtrAllocator Allocator;
2903 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
2904 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
2905 /* DeleteFns */ true)) {
2906 // FIXME: Think about passes we will preserve and add them here.
2907 return PreservedAnalyses::none();
2908 }
2909 return PreservedAnalyses::all();
2910}
2911
2912PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
2913 CGSCCAnalysisManager &AM,
2914 LazyCallGraph &CG,
2915 CGSCCUpdateResult &UR) {
2916 FunctionAnalysisManager &FAM =
2917 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
2918 AnalysisGetter AG(FAM);
2919
2920 SetVector<Function *> Functions;
2921 for (LazyCallGraph::Node &N : C)
2922 Functions.insert(&N.getFunction());
2923
2924 if (Functions.empty())
2925 return PreservedAnalyses::all();
2926
2927 Module &M = *Functions.back()->getParent();
2928 CallGraphUpdater CGUpdater;
2929 CGUpdater.initialize(CG, C, AM, UR);
2930 BumpPtrAllocator Allocator;
2931 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
2932 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
2933 /* DeleteFns */ false)) {
2934 // FIXME: Think about passes we will preserve and add them here.
2935 PreservedAnalyses PA;
2936 PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
2937 return PA;
2938 }
2939 return PreservedAnalyses::all();
2940}
2941
2942namespace llvm {
2943
2944template <> struct GraphTraits<AADepGraphNode *> {
2945 using NodeRef = AADepGraphNode *;
2946 using DepTy = PointerIntPair<AADepGraphNode *, 1>;
2947 using EdgeRef = PointerIntPair<AADepGraphNode *, 1>;
2948
2949 static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; }
2950 static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); }
2951
2952 using ChildIteratorType =
2953 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
2954 using ChildEdgeIteratorType = TinyPtrVector<DepTy>::iterator;
2955
2956 static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); }
2957
2958 static ChildIteratorType child_end(NodeRef N) { return N->child_end(); }
2959};
2960
2961template <>
2962struct GraphTraits<AADepGraph *> : public GraphTraits<AADepGraphNode *> {
2963 static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); }
2964
2965 using nodes_iterator =
2966 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
2967
2968 static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); }
2969
2970 static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); }
2971};
2972
2973template <> struct DOTGraphTraits<AADepGraph *> : public DefaultDOTGraphTraits {
2974 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
2975
2976 static std::string getNodeLabel(const AADepGraphNode *Node,
2977 const AADepGraph *DG) {
2978 std::string AAString;
2979 raw_string_ostream O(AAString);
2980 Node->print(O);
2981 return AAString;
2982 }
2983};
2984
2985} // end namespace llvm
2986
2987namespace {
2988
2989struct AttributorLegacyPass : public ModulePass {
2990 static char ID;
2991
2992 AttributorLegacyPass() : ModulePass(ID) {
2993 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
2994 }
2995
2996 bool runOnModule(Module &M) override {
2997 if (skipModule(M))
2998 return false;
2999
3000 AnalysisGetter AG;
3001 SetVector<Function *> Functions;
3002 for (Function &F : M)
3003 Functions.insert(&F);
3004
3005 CallGraphUpdater CGUpdater;
3006 BumpPtrAllocator Allocator;
3007 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ nullptr);
3008 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
3009 /* DeleteFns*/ true);
3010 }
3011
3012 void getAnalysisUsage(AnalysisUsage &AU) const override {
3013 // FIXME: Think about passes we will preserve and add them here.
3014 AU.addRequired<TargetLibraryInfoWrapperPass>();
3015 }
3016};
3017
3018struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
3019 static char ID;
3020
3021 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
3022 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
3023 }
3024
3025 bool runOnSCC(CallGraphSCC &SCC) override {
3026 if (skipSCC(SCC))
3027 return false;
3028
3029 SetVector<Function *> Functions;
3030 for (CallGraphNode *CGN : SCC)
3031 if (Function *Fn = CGN->getFunction())
3032 if (!Fn->isDeclaration())
3033 Functions.insert(Fn);
3034
3035 if (Functions.empty())
3036 return false;
3037
3038 AnalysisGetter AG;
3039 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
3040 CallGraphUpdater CGUpdater;
3041 CGUpdater.initialize(CG, SCC);
3042 Module &M = *Functions.back()->getParent();
3043 BumpPtrAllocator Allocator;
3044 InformationCache InfoCache(M, AG, Allocator, /* CGSCC */ &Functions);
3045 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater,
3046 /* DeleteFns */ false);
3047 }
3048
3049 void getAnalysisUsage(AnalysisUsage &AU) const override {
3050 // FIXME: Think about passes we will preserve and add them here.
3051 AU.addRequired<TargetLibraryInfoWrapperPass>();
3052 CallGraphSCCPass::getAnalysisUsage(AU);
3053 }
3054};
3055
3056} // end anonymous namespace
3057
3058Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
3059Pass *llvm::createAttributorCGSCCLegacyPass() {
3060 return new AttributorCGSCCLegacyPass();
3061}
3062
3063char AttributorLegacyPass::ID = 0;
3064char AttributorCGSCCLegacyPass::ID = 0;
3065
3066INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",static void *initializeAttributorLegacyPassPassOnce(PassRegistry
&Registry) {
3067 "Deduce and propagate attributes", false, false)static void *initializeAttributorLegacyPassPassOnce(PassRegistry
&Registry) {
3068INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
3069INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",PassInfo *PI = new PassInfo( "Deduce and propagate attributes"
, "attributor", &AttributorLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<AttributorLegacyPass>), false, false);
Registry.registerPass(*PI, true); return PI; } static llvm::
once_flag InitializeAttributorLegacyPassPassFlag; void llvm::
initializeAttributorLegacyPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeAttributorLegacyPassPassFlag, initializeAttributorLegacyPassPassOnce
, std::ref(Registry)); }
3070 "Deduce and propagate attributes", false, false)PassInfo *PI = new PassInfo( "Deduce and propagate attributes"
, "attributor", &AttributorLegacyPass::ID, PassInfo::NormalCtor_t
(callDefaultCtor<AttributorLegacyPass>), false, false);
Registry.registerPass(*PI, true); return PI; } static llvm::
once_flag InitializeAttributorLegacyPassPassFlag; void llvm::
initializeAttributorLegacyPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeAttributorLegacyPassPassFlag, initializeAttributorLegacyPassPassOnce
, std::ref(Registry)); }
3071INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",static void *initializeAttributorCGSCCLegacyPassPassOnce(PassRegistry
&Registry) {
3072 "Deduce and propagate attributes (CGSCC pass)", false,static void *initializeAttributorCGSCCLegacyPassPassOnce(PassRegistry
&Registry) {
3073 false)static void *initializeAttributorCGSCCLegacyPassPassOnce(PassRegistry
&Registry) {
3074INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
3075INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)initializeCallGraphWrapperPassPass(Registry);
3076INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",PassInfo *PI = new PassInfo( "Deduce and propagate attributes (CGSCC pass)"
, "attributor-cgscc", &AttributorCGSCCLegacyPass::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AttributorCGSCCLegacyPass>
), false, false); Registry.registerPass(*PI, true); return PI
; } static llvm::once_flag InitializeAttributorCGSCCLegacyPassPassFlag
; void llvm::initializeAttributorCGSCCLegacyPassPass(PassRegistry
&Registry) { llvm::call_once(InitializeAttributorCGSCCLegacyPassPassFlag
, initializeAttributorCGSCCLegacyPassPassOnce, std::ref(Registry
)); }
3077 "Deduce and propagate attributes (CGSCC pass)", false,PassInfo *PI = new PassInfo( "Deduce and propagate attributes (CGSCC pass)"
, "attributor-cgscc", &AttributorCGSCCLegacyPass::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AttributorCGSCCLegacyPass>
), false, false); Registry.registerPass(*PI, true); return PI
; } static llvm::once_flag InitializeAttributorCGSCCLegacyPassPassFlag
; void llvm::initializeAttributorCGSCCLegacyPassPass(PassRegistry
&Registry) { llvm::call_once(InitializeAttributorCGSCCLegacyPassPassFlag
, initializeAttributorCGSCCLegacyPassPassOnce, std::ref(Registry
)); }
3078 false)PassInfo *PI = new PassInfo( "Deduce and propagate attributes (CGSCC pass)"
, "attributor-cgscc", &AttributorCGSCCLegacyPass::ID, PassInfo
::NormalCtor_t(callDefaultCtor<AttributorCGSCCLegacyPass>
), false, false); Registry.registerPass(*PI, true); return PI
; } static llvm::once_flag InitializeAttributorCGSCCLegacyPassPassFlag
; void llvm::initializeAttributorCGSCCLegacyPassPass(PassRegistry
&Registry) { llvm::call_once(InitializeAttributorCGSCCLegacyPassPassFlag
, initializeAttributorCGSCCLegacyPassPassOnce, std::ref(Registry
)); }

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO/Attributor.h

1//===- Attributor.h --- Module-wide attribute deduction ---------*- 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// Attributor: An inter procedural (abstract) "attribute" deduction framework.
10//
11// The Attributor framework is an inter procedural abstract analysis (fixpoint
12// iteration analysis). The goal is to allow easy deduction of new attributes as
13// well as information exchange between abstract attributes in-flight.
14//
15// The Attributor class is the driver and the link between the various abstract
16// attributes. The Attributor will iterate until a fixpoint state is reached by
17// all abstract attributes in-flight, or until it will enforce a pessimistic fix
18// point because an iteration limit is reached.
19//
20// Abstract attributes, derived from the AbstractAttribute class, actually
21// describe properties of the code. They can correspond to actual LLVM-IR
22// attributes, or they can be more general, ultimately unrelated to LLVM-IR
23// attributes. The latter is useful when an abstract attributes provides
24// information to other abstract attributes in-flight but we might not want to
25// manifest the information. The Attributor allows to query in-flight abstract
26// attributes through the `Attributor::getAAFor` method (see the method
27// description for an example). If the method is used by an abstract attribute
28// P, and it results in an abstract attribute Q, the Attributor will
29// automatically capture a potential dependence from Q to P. This dependence
30// will cause P to be reevaluated whenever Q changes in the future.
31//
32// The Attributor will only reevaluate abstract attributes that might have
33// changed since the last iteration. That means that the Attribute will not
34// revisit all instructions/blocks/functions in the module but only query
35// an update from a subset of the abstract attributes.
36//
37// The update method `AbstractAttribute::updateImpl` is implemented by the
38// specific "abstract attribute" subclasses. The method is invoked whenever the
39// currently assumed state (see the AbstractState class) might not be valid
40// anymore. This can, for example, happen if the state was dependent on another
41// abstract attribute that changed. In every invocation, the update method has
42// to adjust the internal state of an abstract attribute to a point that is
43// justifiable by the underlying IR and the current state of abstract attributes
44// in-flight. Since the IR is given and assumed to be valid, the information
45// derived from it can be assumed to hold. However, information derived from
46// other abstract attributes is conditional on various things. If the justifying
47// state changed, the `updateImpl` has to revisit the situation and potentially
48// find another justification or limit the optimistic assumes made.
49//
50// Change is the key in this framework. Until a state of no-change, thus a
51// fixpoint, is reached, the Attributor will query the abstract attributes
52// in-flight to re-evaluate their state. If the (current) state is too
53// optimistic, hence it cannot be justified anymore through other abstract
54// attributes or the state of the IR, the state of the abstract attribute will
55// have to change. Generally, we assume abstract attribute state to be a finite
56// height lattice and the update function to be monotone. However, these
57// conditions are not enforced because the iteration limit will guarantee
58// termination. If an optimistic fixpoint is reached, or a pessimistic fix
59// point is enforced after a timeout, the abstract attributes are tasked to
60// manifest their result in the IR for passes to come.
61//
62// Attribute manifestation is not mandatory. If desired, there is support to
63// generate a single or multiple LLVM-IR attributes already in the helper struct
64// IRAttribute. In the simplest case, a subclass inherits from IRAttribute with
65// a proper Attribute::AttrKind as template parameter. The Attributor
66// manifestation framework will then create and place a new attribute if it is
67// allowed to do so (based on the abstract state). Other use cases can be
68// achieved by overloading AbstractAttribute or IRAttribute methods.
69//
70//
71// The "mechanics" of adding a new "abstract attribute":
72// - Define a class (transitively) inheriting from AbstractAttribute and one
73// (which could be the same) that (transitively) inherits from AbstractState.
74// For the latter, consider the already available BooleanState and
75// {Inc,Dec,Bit}IntegerState if they fit your needs, e.g., you require only a
76// number tracking or bit-encoding.
77// - Implement all pure methods. Also use overloading if the attribute is not
78// conforming with the "default" behavior: A (set of) LLVM-IR attribute(s) for
79// an argument, call site argument, function return value, or function. See
80// the class and method descriptions for more information on the two
81// "Abstract" classes and their respective methods.
82// - Register opportunities for the new abstract attribute in the
83// `Attributor::identifyDefaultAbstractAttributes` method if it should be
84// counted as a 'default' attribute.
85// - Add sufficient tests.
86// - Add a Statistics object for bookkeeping. If it is a simple (set of)
87// attribute(s) manifested through the Attributor manifestation framework, see
88// the bookkeeping function in Attributor.cpp.
89// - If instructions with a certain opcode are interesting to the attribute, add
90// that opcode to the switch in `Attributor::identifyAbstractAttributes`. This
91// will make it possible to query all those instructions through the
92// `InformationCache::getOpcodeInstMapForFunction` interface and eliminate the
93// need to traverse the IR repeatedly.
94//
95//===----------------------------------------------------------------------===//
96
97#ifndef LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
98#define LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
99
100#include "llvm/ADT/DenseSet.h"
101#include "llvm/ADT/GraphTraits.h"
102#include "llvm/ADT/MapVector.h"
103#include "llvm/ADT/STLExtras.h"
104#include "llvm/ADT/SetVector.h"
105#include "llvm/ADT/Triple.h"
106#include "llvm/ADT/iterator.h"
107#include "llvm/Analysis/AssumeBundleQueries.h"
108#include "llvm/Analysis/CFG.h"
109#include "llvm/Analysis/CGSCCPassManager.h"
110#include "llvm/Analysis/LazyCallGraph.h"
111#include "llvm/Analysis/LoopInfo.h"
112#include "llvm/Analysis/MustExecute.h"
113#include "llvm/Analysis/OptimizationRemarkEmitter.h"
114#include "llvm/Analysis/PostDominators.h"
115#include "llvm/Analysis/TargetLibraryInfo.h"
116#include "llvm/IR/AbstractCallSite.h"
117#include "llvm/IR/ConstantRange.h"
118#include "llvm/IR/PassManager.h"
119#include "llvm/Support/Allocator.h"
120#include "llvm/Support/Casting.h"
121#include "llvm/Support/GraphWriter.h"
122#include "llvm/Support/TimeProfiler.h"
123#include "llvm/Transforms/Utils/CallGraphUpdater.h"
124
125namespace llvm {
126
127struct AADepGraphNode;
128struct AADepGraph;
129struct Attributor;
130struct AbstractAttribute;
131struct InformationCache;
132struct AAIsDead;
133struct AttributorCallGraph;
134
135class AAManager;
136class AAResults;
137class Function;
138
139/// Abstract Attribute helper functions.
140namespace AA {
141
142/// Return true if \p V is dynamically unique, that is, there are no two
143/// "instances" of \p V at runtime with different values.
144bool isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
145 const Value &V);
146
147/// Return true if \p V is a valid value in \p Scope, that is a constant or an
148/// instruction/argument of \p Scope.
149bool isValidInScope(const Value &V, const Function *Scope);
150
151/// Return true if \p V is a valid value at position \p CtxI, that is a
152/// constant, an argument of the same function as \p CtxI, or an instruction in
153/// that function that dominates \p CtxI.
154bool isValidAtPosition(const Value &V, const Instruction &CtxI,
155 InformationCache &InfoCache);
156
157/// Try to convert \p V to type \p Ty without introducing new instructions. If
158/// this is not possible return `nullptr`. Note: this function basically knows
159/// how to cast various constants.
160Value *getWithType(Value &V, Type &Ty);
161
162/// Return the combination of \p A and \p B such that the result is a possible
163/// value of both. \p B is potentially casted to match the type \p Ty or the
164/// type of \p A if \p Ty is null.
165///
166/// Examples:
167/// X + none => X
168/// not_none + undef => not_none
169/// V1 + V2 => nullptr
170Optional<Value *>
171combineOptionalValuesInAAValueLatice(const Optional<Value *> &A,
172 const Optional<Value *> &B, Type *Ty);
173
174/// Return the initial value of \p Obj with type \p Ty if that is a constant.
175Constant *getInitialValueForObj(Value &Obj, Type &Ty);
176
177/// Collect all potential underlying objects of \p Ptr at position \p CtxI in
178/// \p Objects. Assumed information is used and dependences onto \p QueryingAA
179/// are added appropriately.
180///
181/// \returns True if \p Objects contains all assumed underlying objects, and
182/// false if something went wrong and the objects could not be
183/// determined.
184bool getAssumedUnderlyingObjects(Attributor &A, const Value &Ptr,
185 SmallVectorImpl<Value *> &Objects,
186 const AbstractAttribute &QueryingAA,
187 const Instruction *CtxI);
188
189/// Collect all potential values of the one stored by \p SI into
190/// \p PotentialCopies. That is, the only copies that were made via the
191/// store are assumed to be known and all in \p PotentialCopies. Dependences
192/// onto \p QueryingAA are properly tracked, \p UsedAssumedInformation will
193/// inform the caller if assumed information was used.
194///
195/// \returns True if the assumed potential copies are all in \p PotentialCopies,
196/// false if something went wrong and the copies could not be
197/// determined.
198bool getPotentialCopiesOfStoredValue(
199 Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
200 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation);
201
202} // namespace AA
203
204/// The value passed to the line option that defines the maximal initialization
205/// chain length.
206extern unsigned MaxInitializationChainLength;
207
208///{
209enum class ChangeStatus {
210 CHANGED,
211 UNCHANGED,
212};
213
214ChangeStatus operator|(ChangeStatus l, ChangeStatus r);
215ChangeStatus &operator|=(ChangeStatus &l, ChangeStatus r);
216ChangeStatus operator&(ChangeStatus l, ChangeStatus r);
217ChangeStatus &operator&=(ChangeStatus &l, ChangeStatus r);
218
219enum class DepClassTy {
220 REQUIRED, ///< The target cannot be valid if the source is not.
221 OPTIONAL, ///< The target may be valid if the source is not.
222 NONE, ///< Do not track a dependence between source and target.
223};
224///}
225
226/// The data structure for the nodes of a dependency graph
227struct AADepGraphNode {
228public:
229 virtual ~AADepGraphNode(){};
230 using DepTy = PointerIntPair<AADepGraphNode *, 1>;
231
232protected:
233 /// Set of dependency graph nodes which should be updated if this one
234 /// is updated. The bit encodes if it is optional.
235 TinyPtrVector<DepTy> Deps;
236
237 static AADepGraphNode *DepGetVal(DepTy &DT) { return DT.getPointer(); }
238 static AbstractAttribute *DepGetValAA(DepTy &DT) {
239 return cast<AbstractAttribute>(DT.getPointer());
240 }
241
242 operator AbstractAttribute *() { return cast<AbstractAttribute>(this); }
243
244public:
245 using iterator =
246 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
247 using aaiterator =
248 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetValAA)>;
249
250 aaiterator begin() { return aaiterator(Deps.begin(), &DepGetValAA); }
251 aaiterator end() { return aaiterator(Deps.end(), &DepGetValAA); }
252 iterator child_begin() { return iterator(Deps.begin(), &DepGetVal); }
253 iterator child_end() { return iterator(Deps.end(), &DepGetVal); }
254
255 virtual void print(raw_ostream &OS) const { OS << "AADepNode Impl\n"; }
256 TinyPtrVector<DepTy> &getDeps() { return Deps; }
257
258 friend struct Attributor;
259 friend struct AADepGraph;
260};
261
262/// The data structure for the dependency graph
263///
264/// Note that in this graph if there is an edge from A to B (A -> B),
265/// then it means that B depends on A, and when the state of A is
266/// updated, node B should also be updated
267struct AADepGraph {
268 AADepGraph() {}
269 ~AADepGraph() {}
270
271 using DepTy = AADepGraphNode::DepTy;
272 static AADepGraphNode *DepGetVal(DepTy &DT) { return DT.getPointer(); }
273 using iterator =
274 mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
275
276 /// There is no root node for the dependency graph. But the SCCIterator
277 /// requires a single entry point, so we maintain a fake("synthetic") root
278 /// node that depends on every node.
279 AADepGraphNode SyntheticRoot;
280 AADepGraphNode *GetEntryNode() { return &SyntheticRoot; }
281
282 iterator begin() { return SyntheticRoot.child_begin(); }
283 iterator end() { return SyntheticRoot.child_end(); }
284
285 void viewGraph();
286
287 /// Dump graph to file
288 void dumpGraph();
289
290 /// Print dependency graph
291 void print();
292};
293
294/// Helper to describe and deal with positions in the LLVM-IR.
295///
296/// A position in the IR is described by an anchor value and an "offset" that
297/// could be the argument number, for call sites and arguments, or an indicator
298/// of the "position kind". The kinds, specified in the Kind enum below, include
299/// the locations in the attribute list, i.a., function scope and return value,
300/// as well as a distinction between call sites and functions. Finally, there
301/// are floating values that do not have a corresponding attribute list
302/// position.
303struct IRPosition {
304 // NOTE: In the future this definition can be changed to support recursive
305 // functions.
306 using CallBaseContext = CallBase;
307
308 /// The positions we distinguish in the IR.
309 enum Kind : char {
310 IRP_INVALID, ///< An invalid position.
311 IRP_FLOAT, ///< A position that is not associated with a spot suitable
312 ///< for attributes. This could be any value or instruction.
313 IRP_RETURNED, ///< An attribute for the function return value.
314 IRP_CALL_SITE_RETURNED, ///< An attribute for a call site return value.
315 IRP_FUNCTION, ///< An attribute for a function (scope).
316 IRP_CALL_SITE, ///< An attribute for a call site (function scope).
317 IRP_ARGUMENT, ///< An attribute for a function argument.
318 IRP_CALL_SITE_ARGUMENT, ///< An attribute for a call site argument.
319 };
320
321 /// Default constructor available to create invalid positions implicitly. All
322 /// other positions need to be created explicitly through the appropriate
323 /// static member function.
324 IRPosition() : Enc(nullptr, ENC_VALUE) { verify(); }
325
326 /// Create a position describing the value of \p V.
327 static const IRPosition value(const Value &V,
328 const CallBaseContext *CBContext = nullptr) {
329 if (auto *Arg = dyn_cast<Argument>(&V))
330 return IRPosition::argument(*Arg, CBContext);
331 if (auto *CB = dyn_cast<CallBase>(&V))
332 return IRPosition::callsite_returned(*CB);
333 return IRPosition(const_cast<Value &>(V), IRP_FLOAT, CBContext);
334 }
335
336 /// Create a position describing the function scope of \p F.
337 /// \p CBContext is used for call base specific analysis.
338 static const IRPosition function(const Function &F,
339 const CallBaseContext *CBContext = nullptr) {
340 return IRPosition(const_cast<Function &>(F), IRP_FUNCTION, CBContext);
341 }
342
343 /// Create a position describing the returned value of \p F.
344 /// \p CBContext is used for call base specific analysis.
345 static const IRPosition returned(const Function &F,
346 const CallBaseContext *CBContext = nullptr) {
347 return IRPosition(const_cast<Function &>(F), IRP_RETURNED, CBContext);
348 }
349
350 /// Create a position describing the argument \p Arg.
351 /// \p CBContext is used for call base specific analysis.
352 static const IRPosition argument(const Argument &Arg,
353 const CallBaseContext *CBContext = nullptr) {
354 return IRPosition(const_cast<Argument &>(Arg), IRP_ARGUMENT, CBContext);
355 }
356
357 /// Create a position describing the function scope of \p CB.
358 static const IRPosition callsite_function(const CallBase &CB) {
359 return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE);
360 }
361
362 /// Create a position describing the returned value of \p CB.
363 static const IRPosition callsite_returned(const CallBase &CB) {
364 return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE_RETURNED);
365 }
366
367 /// Create a position describing the argument of \p CB at position \p ArgNo.
368 static const IRPosition callsite_argument(const CallBase &CB,
369 unsigned ArgNo) {
370 return IRPosition(const_cast<Use &>(CB.getArgOperandUse(ArgNo)),
371 IRP_CALL_SITE_ARGUMENT);
372 }
373
374 /// Create a position describing the argument of \p ACS at position \p ArgNo.
375 static const IRPosition callsite_argument(AbstractCallSite ACS,
376 unsigned ArgNo) {
377 if (ACS.getNumArgOperands() <= ArgNo)
378 return IRPosition();
379 int CSArgNo = ACS.getCallArgOperandNo(ArgNo);
380 if (CSArgNo >= 0)
381 return IRPosition::callsite_argument(
382 cast<CallBase>(*ACS.getInstruction()), CSArgNo);
383 return IRPosition();
384 }
385
386 /// Create a position with function scope matching the "context" of \p IRP.
387 /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
388 /// will be a call site position, otherwise the function position of the
389 /// associated function.
390 static const IRPosition
391 function_scope(const IRPosition &IRP,
392 const CallBaseContext *CBContext = nullptr) {
393 if (IRP.isAnyCallSitePosition()) {
394 return IRPosition::callsite_function(
395 cast<CallBase>(IRP.getAnchorValue()));
396 }
397 assert(IRP.getAssociatedFunction())((void)0);
398 return IRPosition::function(*IRP.getAssociatedFunction(), CBContext);
399 }
400
401 bool operator==(const IRPosition &RHS) const {
402 return Enc == RHS.Enc && RHS.CBContext == CBContext;
403 }
404 bool operator!=(const IRPosition &RHS) const { return !(*this == RHS); }
405
406 /// Return the value this abstract attribute is anchored with.
407 ///
408 /// The anchor value might not be the associated value if the latter is not
409 /// sufficient to determine where arguments will be manifested. This is, so
410 /// far, only the case for call site arguments as the value is not sufficient
411 /// to pinpoint them. Instead, we can use the call site as an anchor.
412 Value &getAnchorValue() const {
413 switch (getEncodingBits()) {
414 case ENC_VALUE:
415 case ENC_RETURNED_VALUE:
416 case ENC_FLOATING_FUNCTION:
417 return *getAsValuePtr();
418 case ENC_CALL_SITE_ARGUMENT_USE:
419 return *(getAsUsePtr()->getUser());
420 default:
421 llvm_unreachable("Unkown encoding!")__builtin_unreachable();
422 };
423 }
424
425 /// Return the associated function, if any.
426 Function *getAssociatedFunction() const {
427 if (auto *CB = dyn_cast<CallBase>(&getAnchorValue())) {
428 // We reuse the logic that associates callback calles to arguments of a
429 // call site here to identify the callback callee as the associated
430 // function.
431 if (Argument *Arg = getAssociatedArgument())
432 return Arg->getParent();
433 return CB->getCalledFunction();
434 }
435 return getAnchorScope();
436 }
437
438 /// Return the associated argument, if any.
439 Argument *getAssociatedArgument() const;
440
441 /// Return true if the position refers to a function interface, that is the
442 /// function scope, the function return, or an argument.
443 bool isFnInterfaceKind() const {
444 switch (getPositionKind()) {
445 case IRPosition::IRP_FUNCTION:
446 case IRPosition::IRP_RETURNED:
447 case IRPosition::IRP_ARGUMENT:
448 return true;
449 default:
450 return false;
451 }
452 }
453
454 /// Return the Function surrounding the anchor value.
455 Function *getAnchorScope() const {
456 Value &V = getAnchorValue();
457 if (isa<Function>(V))
458 return &cast<Function>(V);
459 if (isa<Argument>(V))
460 return cast<Argument>(V).getParent();
461 if (isa<Instruction>(V))
462 return cast<Instruction>(V).getFunction();
463 return nullptr;
464 }
465
466 /// Return the context instruction, if any.
467 Instruction *getCtxI() const {
468 Value &V = getAnchorValue();
469 if (auto *I = dyn_cast<Instruction>(&V))
470 return I;
471 if (auto *Arg = dyn_cast<Argument>(&V))
472 if (!Arg->getParent()->isDeclaration())
473 return &Arg->getParent()->getEntryBlock().front();
474 if (auto *F = dyn_cast<Function>(&V))
475 if (!F->isDeclaration())
476 return &(F->getEntryBlock().front());
477 return nullptr;
478 }
479
480 /// Return the value this abstract attribute is associated with.
481 Value &getAssociatedValue() const {
482 if (getCallSiteArgNo() < 0 || isa<Argument>(&getAnchorValue()))
483 return getAnchorValue();
484 assert(isa<CallBase>(&getAnchorValue()) && "Expected a call base!")((void)0);
485 return *cast<CallBase>(&getAnchorValue())
486 ->getArgOperand(getCallSiteArgNo());
487 }
488
489 /// Return the type this abstract attribute is associated with.
490 Type *getAssociatedType() const {
491 if (getPositionKind() == IRPosition::IRP_RETURNED)
492 return getAssociatedFunction()->getReturnType();
493 return getAssociatedValue().getType();
494 }
495
496 /// Return the callee argument number of the associated value if it is an
497 /// argument or call site argument, otherwise a negative value. In contrast to
498 /// `getCallSiteArgNo` this method will always return the "argument number"
499 /// from the perspective of the callee. This may not the same as the call site
500 /// if this is a callback call.
501 int getCalleeArgNo() const {
502 return getArgNo(/* CallbackCalleeArgIfApplicable */ true);
503 }
504
505 /// Return the call site argument number of the associated value if it is an
506 /// argument or call site argument, otherwise a negative value. In contrast to
507 /// `getCalleArgNo` this method will always return the "operand number" from
508 /// the perspective of the call site. This may not the same as the callee
509 /// perspective if this is a callback call.
510 int getCallSiteArgNo() const {
511 return getArgNo(/* CallbackCalleeArgIfApplicable */ false);
512 }
513
514 /// Return the index in the attribute list for this position.
515 unsigned getAttrIdx() const {
516 switch (getPositionKind()) {
517 case IRPosition::IRP_INVALID:
518 case IRPosition::IRP_FLOAT:
519 break;
520 case IRPosition::IRP_FUNCTION:
521 case IRPosition::IRP_CALL_SITE:
522 return AttributeList::FunctionIndex;
523 case IRPosition::IRP_RETURNED:
524 case IRPosition::IRP_CALL_SITE_RETURNED:
525 return AttributeList::ReturnIndex;
526 case IRPosition::IRP_ARGUMENT:
527 case IRPosition::IRP_CALL_SITE_ARGUMENT:
528 return getCallSiteArgNo() + AttributeList::FirstArgIndex;
529 }
530 llvm_unreachable(__builtin_unreachable()
531 "There is no attribute index for a floating or invalid position!")__builtin_unreachable();
532 }
533
534 /// Return the associated position kind.
535 Kind getPositionKind() const {
536 char EncodingBits = getEncodingBits();
537 if (EncodingBits == ENC_CALL_SITE_ARGUMENT_USE)
538 return IRP_CALL_SITE_ARGUMENT;
539 if (EncodingBits == ENC_FLOATING_FUNCTION)
540 return IRP_FLOAT;
541
542 Value *V = getAsValuePtr();
543 if (!V)
544 return IRP_INVALID;
545 if (isa<Argument>(V))
546 return IRP_ARGUMENT;
547 if (isa<Function>(V))
548 return isReturnPosition(EncodingBits) ? IRP_RETURNED : IRP_FUNCTION;
549 if (isa<CallBase>(V))
550 return isReturnPosition(EncodingBits) ? IRP_CALL_SITE_RETURNED
551 : IRP_CALL_SITE;
552 return IRP_FLOAT;
553 }
554
555 /// TODO: Figure out if the attribute related helper functions should live
556 /// here or somewhere else.
557
558 /// Return true if any kind in \p AKs existing in the IR at a position that
559 /// will affect this one. See also getAttrs(...).
560 /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
561 /// e.g., the function position if this is an
562 /// argument position, should be ignored.
563 bool hasAttr(ArrayRef<Attribute::AttrKind> AKs,
564 bool IgnoreSubsumingPositions = false,
565 Attributor *A = nullptr) const;
566
567 /// Return the attributes of any kind in \p AKs existing in the IR at a
568 /// position that will affect this one. While each position can only have a
569 /// single attribute of any kind in \p AKs, there are "subsuming" positions
570 /// that could have an attribute as well. This method returns all attributes
571 /// found in \p Attrs.
572 /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
573 /// e.g., the function position if this is an
574 /// argument position, should be ignored.
575 void getAttrs(ArrayRef<Attribute::AttrKind> AKs,
576 SmallVectorImpl<Attribute> &Attrs,
577 bool IgnoreSubsumingPositions = false,
578 Attributor *A = nullptr) const;
579
580 /// Remove the attribute of kind \p AKs existing in the IR at this position.
581 void removeAttrs(ArrayRef<Attribute::AttrKind> AKs) const {
582 if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
583 return;
584
585 AttributeList AttrList;
586 auto *CB = dyn_cast<CallBase>(&getAnchorValue());
587 if (CB)
588 AttrList = CB->getAttributes();
589 else
590 AttrList = getAssociatedFunction()->getAttributes();
591
592 LLVMContext &Ctx = getAnchorValue().getContext();
593 for (Attribute::AttrKind AK : AKs)
594 AttrList = AttrList.removeAttribute(Ctx, getAttrIdx(), AK);
595
596 if (CB)
597 CB->setAttributes(AttrList);
598 else
599 getAssociatedFunction()->setAttributes(AttrList);
600 }
601
602 bool isAnyCallSitePosition() const {
603 switch (getPositionKind()) {
604 case IRPosition::IRP_CALL_SITE:
605 case IRPosition::IRP_CALL_SITE_RETURNED:
606 case IRPosition::IRP_CALL_SITE_ARGUMENT:
607 return true;
608 default:
609 return false;
610 }
611 }
612
613 /// Return true if the position is an argument or call site argument.
614 bool isArgumentPosition() const {
615 switch (getPositionKind()) {
616 case IRPosition::IRP_ARGUMENT:
617 case IRPosition::IRP_CALL_SITE_ARGUMENT:
618 return true;
619 default:
620 return false;
621 }
622 }
623
624 /// Return the same position without the call base context.
625 IRPosition stripCallBaseContext() const {
626 IRPosition Result = *this;
627 Result.CBContext = nullptr;
628 return Result;
629 }
630
631 /// Get the call base context from the position.
632 const CallBaseContext *getCallBaseContext() const { return CBContext; }
633
634 /// Check if the position has any call base context.
635 bool hasCallBaseContext() const { return CBContext != nullptr; }
636
637 /// Special DenseMap key values.
638 ///
639 ///{
640 static const IRPosition EmptyKey;
641 static const IRPosition TombstoneKey;
642 ///}
643
644 /// Conversion into a void * to allow reuse of pointer hashing.
645 operator void *() const { return Enc.getOpaqueValue(); }
646
647private:
648 /// Private constructor for special values only!
649 explicit IRPosition(void *Ptr, const CallBaseContext *CBContext = nullptr)
650 : CBContext(CBContext) {
651 Enc.setFromOpaqueValue(Ptr);
652 }
653
654 /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
655 explicit IRPosition(Value &AnchorVal, Kind PK,
656 const CallBaseContext *CBContext = nullptr)
657 : CBContext(CBContext) {
658 switch (PK) {
659 case IRPosition::IRP_INVALID:
660 llvm_unreachable("Cannot create invalid IRP with an anchor value!")__builtin_unreachable();
661 break;
662 case IRPosition::IRP_FLOAT:
663 // Special case for floating functions.
664 if (isa<Function>(AnchorVal))
665 Enc = {&AnchorVal, ENC_FLOATING_FUNCTION};
666 else
667 Enc = {&AnchorVal, ENC_VALUE};
668 break;
669 case IRPosition::IRP_FUNCTION:
670 case IRPosition::IRP_CALL_SITE:
671 Enc = {&AnchorVal, ENC_VALUE};
672 break;
673 case IRPosition::IRP_RETURNED:
674 case IRPosition::IRP_CALL_SITE_RETURNED:
675 Enc = {&AnchorVal, ENC_RETURNED_VALUE};
676 break;
677 case IRPosition::IRP_ARGUMENT:
678 Enc = {&AnchorVal, ENC_VALUE};
679 break;
680 case IRPosition::IRP_CALL_SITE_ARGUMENT:
681 llvm_unreachable(__builtin_unreachable()
682 "Cannot create call site argument IRP with an anchor value!")__builtin_unreachable();
683 break;
684 }
685 verify();
686 }
687
688 /// Return the callee argument number of the associated value if it is an
689 /// argument or call site argument. See also `getCalleeArgNo` and
690 /// `getCallSiteArgNo`.
691 int getArgNo(bool CallbackCalleeArgIfApplicable) const {
692 if (CallbackCalleeArgIfApplicable)
693 if (Argument *Arg = getAssociatedArgument())
694 return Arg->getArgNo();
695 switch (getPositionKind()) {
696 case IRPosition::IRP_ARGUMENT:
697 return cast<Argument>(getAsValuePtr())->getArgNo();
698 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
699 Use &U = *getAsUsePtr();
700 return cast<CallBase>(U.getUser())->getArgOperandNo(&U);
701 }
702 default:
703 return -1;
704 }
705 }
706
707 /// IRPosition for the use \p U. The position kind \p PK needs to be
708 /// IRP_CALL_SITE_ARGUMENT, the anchor value is the user, the associated value
709 /// the used value.
710 explicit IRPosition(Use &U, Kind PK) {
711 assert(PK == IRP_CALL_SITE_ARGUMENT &&((void)0)
712 "Use constructor is for call site arguments only!")((void)0);
713 Enc = {&U, ENC_CALL_SITE_ARGUMENT_USE};
714 verify();
715 }
716
717 /// Verify internal invariants.
718 void verify();
719
720 /// Return the attributes of kind \p AK existing in the IR as attribute.
721 bool getAttrsFromIRAttr(Attribute::AttrKind AK,
722 SmallVectorImpl<Attribute> &Attrs) const;
723
724 /// Return the attributes of kind \p AK existing in the IR as operand bundles
725 /// of an llvm.assume.
726 bool getAttrsFromAssumes(Attribute::AttrKind AK,
727 SmallVectorImpl<Attribute> &Attrs,
728 Attributor &A) const;
729
730 /// Return the underlying pointer as Value *, valid for all positions but
731 /// IRP_CALL_SITE_ARGUMENT.
732 Value *getAsValuePtr() const {
733 assert(getEncodingBits() != ENC_CALL_SITE_ARGUMENT_USE &&((void)0)
734 "Not a value pointer!")((void)0);
735 return reinterpret_cast<Value *>(Enc.getPointer());
736 }
737
738 /// Return the underlying pointer as Use *, valid only for
739 /// IRP_CALL_SITE_ARGUMENT positions.
740 Use *getAsUsePtr() const {
741 assert(getEncodingBits() == ENC_CALL_SITE_ARGUMENT_USE &&((void)0)
742 "Not a value pointer!")((void)0);
743 return reinterpret_cast<Use *>(Enc.getPointer());
744 }
745
746 /// Return true if \p EncodingBits describe a returned or call site returned
747 /// position.
748 static bool isReturnPosition(char EncodingBits) {
749 return EncodingBits == ENC_RETURNED_VALUE;
750 }
751
752 /// Return true if the encoding bits describe a returned or call site returned
753 /// position.
754 bool isReturnPosition() const { return isReturnPosition(getEncodingBits()); }
755
756 /// The encoding of the IRPosition is a combination of a pointer and two
757 /// encoding bits. The values of the encoding bits are defined in the enum
758 /// below. The pointer is either a Value* (for the first three encoding bit
759 /// combinations) or Use* (for ENC_CALL_SITE_ARGUMENT_USE).
760 ///
761 ///{
762 enum {
763 ENC_VALUE = 0b00,
764 ENC_RETURNED_VALUE = 0b01,
765 ENC_FLOATING_FUNCTION = 0b10,
766 ENC_CALL_SITE_ARGUMENT_USE = 0b11,
767 };
768
769 // Reserve the maximal amount of bits so there is no need to mask out the
770 // remaining ones. We will not encode anything else in the pointer anyway.
771 static constexpr int NumEncodingBits =
772 PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
773 static_assert(NumEncodingBits >= 2, "At least two bits are required!");
774
775 /// The pointer with the encoding bits.
776 PointerIntPair<void *, NumEncodingBits, char> Enc;
777 ///}
778
779 /// Call base context. Used for callsite specific analysis.
780 const CallBaseContext *CBContext = nullptr;
781
782 /// Return the encoding bits.
783 char getEncodingBits() const { return Enc.getInt(); }
784};
785
786/// Helper that allows IRPosition as a key in a DenseMap.
787template <> struct DenseMapInfo<IRPosition> {
788 static inline IRPosition getEmptyKey() { return IRPosition::EmptyKey; }
789 static inline IRPosition getTombstoneKey() {
790 return IRPosition::TombstoneKey;
791 }
792 static unsigned getHashValue(const IRPosition &IRP) {
793 return (DenseMapInfo<void *>::getHashValue(IRP) << 4) ^
794 (DenseMapInfo<Value *>::getHashValue(IRP.getCallBaseContext()));
795 }
796
797 static bool isEqual(const IRPosition &a, const IRPosition &b) {
798 return a == b;
799 }
800};
801
802/// A visitor class for IR positions.
803///
804/// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
805/// positions" wrt. attributes/information. Thus, if a piece of information
806/// holds for a subsuming position, it also holds for the position P.
807///
808/// The subsuming positions always include the initial position and then,
809/// depending on the position kind, additionally the following ones:
810/// - for IRP_RETURNED:
811/// - the function (IRP_FUNCTION)
812/// - for IRP_ARGUMENT:
813/// - the function (IRP_FUNCTION)
814/// - for IRP_CALL_SITE:
815/// - the callee (IRP_FUNCTION), if known
816/// - for IRP_CALL_SITE_RETURNED:
817/// - the callee (IRP_RETURNED), if known
818/// - the call site (IRP_FUNCTION)
819/// - the callee (IRP_FUNCTION), if known
820/// - for IRP_CALL_SITE_ARGUMENT:
821/// - the argument of the callee (IRP_ARGUMENT), if known
822/// - the callee (IRP_FUNCTION), if known
823/// - the position the call site argument is associated with if it is not
824/// anchored to the call site, e.g., if it is an argument then the argument
825/// (IRP_ARGUMENT)
826class SubsumingPositionIterator {
827 SmallVector<IRPosition, 4> IRPositions;
828 using iterator = decltype(IRPositions)::iterator;
829
830public:
831 SubsumingPositionIterator(const IRPosition &IRP);
832 iterator begin() { return IRPositions.begin(); }
833 iterator end() { return IRPositions.end(); }
834};
835
836/// Wrapper for FunctoinAnalysisManager.
837struct AnalysisGetter {
838 template <typename Analysis>
839 typename Analysis::Result *getAnalysis(const Function &F) {
840 if (!FAM || !F.getParent())
841 return nullptr;
842 return &FAM->getResult<Analysis>(const_cast<Function &>(F));
843 }
844
845 AnalysisGetter(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
846 AnalysisGetter() {}
847
848private:
849 FunctionAnalysisManager *FAM = nullptr;
850};
851
852/// Data structure to hold cached (LLVM-IR) information.
853///
854/// All attributes are given an InformationCache object at creation time to
855/// avoid inspection of the IR by all of them individually. This default
856/// InformationCache will hold information required by 'default' attributes,
857/// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..)
858/// is called.
859///
860/// If custom abstract attributes, registered manually through
861/// Attributor::registerAA(...), need more information, especially if it is not
862/// reusable, it is advised to inherit from the InformationCache and cast the
863/// instance down in the abstract attributes.
864struct InformationCache {
865 InformationCache(const Module &M, AnalysisGetter &AG,
866 BumpPtrAllocator &Allocator, SetVector<Function *> *CGSCC)
867 : DL(M.getDataLayout()), Allocator(Allocator),
868 Explorer(
869 /* ExploreInterBlock */ true, /* ExploreCFGForward */ true,
870 /* ExploreCFGBackward */ true,
871 /* LIGetter */
872 [&](const Function &F) { return AG.getAnalysis<LoopAnalysis>(F); },
873 /* DTGetter */
874 [&](const Function &F) {
875 return AG.getAnalysis<DominatorTreeAnalysis>(F);
876 },
877 /* PDTGetter */
878 [&](const Function &F) {
879 return AG.getAnalysis<PostDominatorTreeAnalysis>(F);
880 }),
881 AG(AG), CGSCC(CGSCC), TargetTriple(M.getTargetTriple()) {
882 if (CGSCC)
883 initializeModuleSlice(*CGSCC);
884 }
885
886 ~InformationCache() {
887 // The FunctionInfo objects are allocated via a BumpPtrAllocator, we call
888 // the destructor manually.
889 for (auto &It : FuncInfoMap)
890 It.getSecond()->~FunctionInfo();
891 }
892
893 /// Apply \p CB to all uses of \p F. If \p LookThroughConstantExprUses is
894 /// true, constant expression users are not given to \p CB but their uses are
895 /// traversed transitively.
896 template <typename CBTy>
897 static void foreachUse(Function &F, CBTy CB,
898 bool LookThroughConstantExprUses = true) {
899 SmallVector<Use *, 8> Worklist(make_pointer_range(F.uses()));
900
901 for (unsigned Idx = 0; Idx < Worklist.size(); ++Idx) {
902 Use &U = *Worklist[Idx];
903
904 // Allow use in constant bitcasts and simply look through them.
905 if (LookThroughConstantExprUses && isa<ConstantExpr>(U.getUser())) {
906 for (Use &CEU : cast<ConstantExpr>(U.getUser())->uses())
907 Worklist.push_back(&CEU);
908 continue;
909 }
910
911 CB(U);
912 }
913 }
914
915 /// Initialize the ModuleSlice member based on \p SCC. ModuleSlices contains
916 /// (a subset of) all functions that we can look at during this SCC traversal.
917 /// This includes functions (transitively) called from the SCC and the
918 /// (transitive) callers of SCC functions. We also can look at a function if
919 /// there is a "reference edge", i.a., if the function somehow uses (!=calls)
920 /// a function in the SCC or a caller of a function in the SCC.
921 void initializeModuleSlice(SetVector<Function *> &SCC) {
922 ModuleSlice.insert(SCC.begin(), SCC.end());
923
924 SmallPtrSet<Function *, 16> Seen;
925 SmallVector<Function *, 16> Worklist(SCC.begin(), SCC.end());
926 while (!Worklist.empty()) {
927 Function *F = Worklist.pop_back_val();
928 ModuleSlice.insert(F);
929
930 for (Instruction &I : instructions(*F))
931 if (auto *CB = dyn_cast<CallBase>(&I))
932 if (Function *Callee = CB->getCalledFunction())
933 if (Seen.insert(Callee).second)
934 Worklist.push_back(Callee);
935 }
936
937 Seen.clear();
938 Worklist.append(SCC.begin(), SCC.end());
939 while (!Worklist.empty()) {
940 Function *F = Worklist.pop_back_val();
941 ModuleSlice.insert(F);
942
943 // Traverse all transitive uses.
944 foreachUse(*F, [&](Use &U) {
945 if (auto *UsrI = dyn_cast<Instruction>(U.getUser()))
946 if (Seen.insert(UsrI->getFunction()).second)
947 Worklist.push_back(UsrI->getFunction());
948 });
949 }
950 }
951
952 /// The slice of the module we are allowed to look at.
953 SmallPtrSet<Function *, 8> ModuleSlice;
954
955 /// A vector type to hold instructions.
956 using InstructionVectorTy = SmallVector<Instruction *, 8>;
957
958 /// A map type from opcodes to instructions with this opcode.
959 using OpcodeInstMapTy = DenseMap<unsigned, InstructionVectorTy *>;
960
961 /// Return the map that relates "interesting" opcodes with all instructions
962 /// with that opcode in \p F.
963 OpcodeInstMapTy &getOpcodeInstMapForFunction(const Function &F) {
964 return getFunctionInfo(F).OpcodeInstMap;
965 }
966
967 /// Return the instructions in \p F that may read or write memory.
968 InstructionVectorTy &getReadOrWriteInstsForFunction(const Function &F) {
969 return getFunctionInfo(F).RWInsts;
3
Calling 'InformationCache::getFunctionInfo'
970 }
971
972 /// Return MustBeExecutedContextExplorer
973 MustBeExecutedContextExplorer &getMustBeExecutedContextExplorer() {
974 return Explorer;
975 }
976
977 /// Return TargetLibraryInfo for function \p F.
978 TargetLibraryInfo *getTargetLibraryInfoForFunction(const Function &F) {
979 return AG.getAnalysis<TargetLibraryAnalysis>(F);
980 }
981
982 /// Return AliasAnalysis Result for function \p F.
983 AAResults *getAAResultsForFunction(const Function &F);
984
985 /// Return true if \p Arg is involved in a must-tail call, thus the argument
986 /// of the caller or callee.
987 bool isInvolvedInMustTailCall(const Argument &Arg) {
988 FunctionInfo &FI = getFunctionInfo(*Arg.getParent());
989 return FI.CalledViaMustTail || FI.ContainsMustTailCall;
990 }
991
992 /// Return the analysis result from a pass \p AP for function \p F.
993 template <typename AP>
994 typename AP::Result *getAnalysisResultForFunction(const Function &F) {
995 return AG.getAnalysis<AP>(F);
996 }
997
998 /// Return SCC size on call graph for function \p F or 0 if unknown.
999 unsigned getSccSize(const Function &F) {
1000 if (CGSCC && CGSCC->count(const_cast<Function *>(&F)))
1001 return CGSCC->size();
1002 return 0;
1003 }
1004
1005 /// Return datalayout used in the module.
1006 const DataLayout &getDL() { return DL; }
1007
1008 /// Return the map conaining all the knowledge we have from `llvm.assume`s.
1009 const RetainedKnowledgeMap &getKnowledgeMap() const { return KnowledgeMap; }
1010
1011 /// Return if \p To is potentially reachable form \p From or not
1012 /// If the same query was answered, return cached result
1013 bool getPotentiallyReachable(const Instruction &From, const Instruction &To) {
1014 auto KeyPair = std::make_pair(&From, &To);
1015 auto Iter = PotentiallyReachableMap.find(KeyPair);
1016 if (Iter != PotentiallyReachableMap.end())
1017 return Iter->second;
1018 const Function &F = *From.getFunction();
1019 bool Result = true;
1020 if (From.getFunction() == To.getFunction())
1021 Result = isPotentiallyReachable(&From, &To, nullptr,
1022 AG.getAnalysis<DominatorTreeAnalysis>(F),
1023 AG.getAnalysis<LoopAnalysis>(F));
1024 PotentiallyReachableMap.insert(std::make_pair(KeyPair, Result));
1025 return Result;
1026 }
1027
1028 /// Check whether \p F is part of module slice.
1029 bool isInModuleSlice(const Function &F) {
1030 return ModuleSlice.count(const_cast<Function *>(&F));
1031 }
1032
1033 /// Return true if the stack (llvm::Alloca) can be accessed by other threads.
1034 bool stackIsAccessibleByOtherThreads() { return !targetIsGPU(); }
1035
1036 /// Return true if the target is a GPU.
1037 bool targetIsGPU() {
1038 return TargetTriple.isAMDGPU() || TargetTriple.isNVPTX();
1039 }
1040
1041private:
1042 struct FunctionInfo {
1043 ~FunctionInfo();
1044
1045 /// A nested map that remembers all instructions in a function with a
1046 /// certain instruction opcode (Instruction::getOpcode()).
1047 OpcodeInstMapTy OpcodeInstMap;
1048
1049 /// A map from functions to their instructions that may read or write
1050 /// memory.
1051 InstructionVectorTy RWInsts;
1052
1053 /// Function is called by a `musttail` call.
1054 bool CalledViaMustTail;
1055
1056 /// Function contains a `musttail` call.
1057 bool ContainsMustTailCall;
1058 };
1059
1060 /// A map type from functions to informatio about it.
1061 DenseMap<const Function *, FunctionInfo *> FuncInfoMap;
1062
1063 /// Return information about the function \p F, potentially by creating it.
1064 FunctionInfo &getFunctionInfo(const Function &F) {
1065 FunctionInfo *&FI = FuncInfoMap[&F];
1066 if (!FI) {
4
Assuming 'FI' is null
5
Taking true branch
1067 FI = new (Allocator) FunctionInfo();
6
Calling 'operator new<llvm::MallocAllocator, 4096UL, 4096UL, 128UL>'
1068 initializeInformationCache(F, *FI);
1069 }
1070 return *FI;
1071 }
1072
1073 /// Initialize the function information cache \p FI for the function \p F.
1074 ///
1075 /// This method needs to be called for all function that might be looked at
1076 /// through the information cache interface *prior* to looking at them.
1077 void initializeInformationCache(const Function &F, FunctionInfo &FI);
1078
1079 /// The datalayout used in the module.
1080 const DataLayout &DL;
1081
1082 /// The allocator used to allocate memory, e.g. for `FunctionInfo`s.
1083 BumpPtrAllocator &Allocator;
1084
1085 /// MustBeExecutedContextExplorer
1086 MustBeExecutedContextExplorer Explorer;
1087
1088 /// A map with knowledge retained in `llvm.assume` instructions.
1089 RetainedKnowledgeMap KnowledgeMap;
1090
1091 /// Getters for analysis.
1092 AnalysisGetter &AG;
1093
1094 /// The underlying CGSCC, or null if not available.
1095 SetVector<Function *> *CGSCC;
1096
1097 /// Set of inlineable functions
1098 SmallPtrSet<const Function *, 8> InlineableFunctions;
1099
1100 /// A map for caching results of queries for isPotentiallyReachable
1101 DenseMap<std::pair<const Instruction *, const Instruction *>, bool>
1102 PotentiallyReachableMap;
1103
1104 /// The triple describing the target machine.
1105 Triple TargetTriple;
1106
1107 /// Give the Attributor access to the members so
1108 /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them.
1109 friend struct Attributor;
1110};
1111
1112/// The fixpoint analysis framework that orchestrates the attribute deduction.
1113///
1114/// The Attributor provides a general abstract analysis framework (guided
1115/// fixpoint iteration) as well as helper functions for the deduction of
1116/// (LLVM-IR) attributes. However, also other code properties can be deduced,
1117/// propagated, and ultimately manifested through the Attributor framework. This
1118/// is particularly useful if these properties interact with attributes and a
1119/// co-scheduled deduction allows to improve the solution. Even if not, thus if
1120/// attributes/properties are completely isolated, they should use the
1121/// Attributor framework to reduce the number of fixpoint iteration frameworks
1122/// in the code base. Note that the Attributor design makes sure that isolated
1123/// attributes are not impacted, in any way, by others derived at the same time
1124/// if there is no cross-reasoning performed.
1125///
1126/// The public facing interface of the Attributor is kept simple and basically
1127/// allows abstract attributes to one thing, query abstract attributes
1128/// in-flight. There are two reasons to do this:
1129/// a) The optimistic state of one abstract attribute can justify an
1130/// optimistic state of another, allowing to framework to end up with an
1131/// optimistic (=best possible) fixpoint instead of one based solely on
1132/// information in the IR.
1133/// b) This avoids reimplementing various kinds of lookups, e.g., to check
1134/// for existing IR attributes, in favor of a single lookups interface
1135/// provided by an abstract attribute subclass.
1136///
1137/// NOTE: The mechanics of adding a new "concrete" abstract attribute are
1138/// described in the file comment.
1139struct Attributor {
1140
1141 using OptimizationRemarkGetter =
1142 function_ref<OptimizationRemarkEmitter &(Function *)>;
1143
1144 /// Constructor
1145 ///
1146 /// \param Functions The set of functions we are deriving attributes for.
1147 /// \param InfoCache Cache to hold various information accessible for
1148 /// the abstract attributes.
1149 /// \param CGUpdater Helper to update an underlying call graph.
1150 /// \param Allowed If not null, a set limiting the attribute opportunities.
1151 /// \param DeleteFns Whether to delete functions.
1152 /// \param RewriteSignatures Whether to rewrite function signatures.
1153 /// \param MaxFixedPointIterations Maximum number of iterations to run until
1154 /// fixpoint.
1155 Attributor(SetVector<Function *> &Functions, InformationCache &InfoCache,
1156 CallGraphUpdater &CGUpdater,
1157 DenseSet<const char *> *Allowed = nullptr, bool DeleteFns = true,
1158 bool RewriteSignatures = true)
1159 : Allocator(InfoCache.Allocator), Functions(Functions),
1160 InfoCache(InfoCache), CGUpdater(CGUpdater), Allowed(Allowed),
1161 DeleteFns(DeleteFns), RewriteSignatures(RewriteSignatures),
1162 MaxFixpointIterations(None), OREGetter(None), PassName("") {}
1163
1164 /// Constructor
1165 ///
1166 /// \param Functions The set of functions we are deriving attributes for.
1167 /// \param InfoCache Cache to hold various information accessible for
1168 /// the abstract attributes.
1169 /// \param CGUpdater Helper to update an underlying call graph.
1170 /// \param Allowed If not null, a set limiting the attribute opportunities.
1171 /// \param DeleteFns Whether to delete functions
1172 /// \param MaxFixedPointIterations Maximum number of iterations to run until
1173 /// fixpoint.
1174 /// \param OREGetter A callback function that returns an ORE object from a
1175 /// Function pointer.
1176 /// \param PassName The name of the pass emitting remarks.
1177 Attributor(SetVector<Function *> &Functions, InformationCache &InfoCache,
1178 CallGraphUpdater &CGUpdater, DenseSet<const char *> *Allowed,
1179 bool DeleteFns, bool RewriteSignatures,
1180 Optional<unsigned> MaxFixpointIterations,
1181 OptimizationRemarkGetter OREGetter, const char *PassName)
1182 : Allocator(InfoCache.Allocator), Functions(Functions),
1183 InfoCache(InfoCache), CGUpdater(CGUpdater), Allowed(Allowed),
1184 DeleteFns(DeleteFns), RewriteSignatures(RewriteSignatures),
1185 MaxFixpointIterations(MaxFixpointIterations),
1186 OREGetter(Optional<OptimizationRemarkGetter>(OREGetter)),
1187 PassName(PassName) {}
1188
1189 ~Attributor();
1190
1191 /// Run the analyses until a fixpoint is reached or enforced (timeout).
1192 ///
1193 /// The attributes registered with this Attributor can be used after as long
1194 /// as the Attributor is not destroyed (it owns the attributes now).
1195 ///
1196 /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
1197 ChangeStatus run();
1198
1199 /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
1200 /// no abstract attribute is found equivalent positions are checked, see
1201 /// SubsumingPositionIterator. Thus, the returned abstract attribute
1202 /// might be anchored at a different position, e.g., the callee if \p IRP is a
1203 /// call base.
1204 ///
1205 /// This method is the only (supported) way an abstract attribute can retrieve
1206 /// information from another abstract attribute. As an example, take an
1207 /// abstract attribute that determines the memory access behavior for a
1208 /// argument (readnone, readonly, ...). It should use `getAAFor` to get the
1209 /// most optimistic information for other abstract attributes in-flight, e.g.
1210 /// the one reasoning about the "captured" state for the argument or the one
1211 /// reasoning on the memory access behavior of the function as a whole.
1212 ///
1213 /// If the DepClass enum is set to `DepClassTy::None` the dependence from
1214 /// \p QueryingAA to the return abstract attribute is not automatically
1215 /// recorded. This should only be used if the caller will record the
1216 /// dependence explicitly if necessary, thus if it the returned abstract
1217 /// attribute is used for reasoning. To record the dependences explicitly use
1218 /// the `Attributor::recordDependence` method.
1219 template <typename AAType>
1220 const AAType &getAAFor(const AbstractAttribute &QueryingAA,
1221 const IRPosition &IRP, DepClassTy DepClass) {
1222 return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
1223 /* ForceUpdate */ false);
1224 }
1225
1226 /// Similar to getAAFor but the return abstract attribute will be updated (via
1227 /// `AbstractAttribute::update`) even if it is found in the cache. This is
1228 /// especially useful for AAIsDead as changes in liveness can make updates
1229 /// possible/useful that were not happening before as the abstract attribute
1230 /// was assumed dead.
1231 template <typename AAType>
1232 const AAType &getAndUpdateAAFor(const AbstractAttribute &QueryingAA,
1233 const IRPosition &IRP, DepClassTy DepClass) {
1234 return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
1235 /* ForceUpdate */ true);
1236 }
1237
1238 /// The version of getAAFor that allows to omit a querying abstract
1239 /// attribute. Using this after Attributor started running is restricted to
1240 /// only the Attributor itself. Initial seeding of AAs can be done via this
1241 /// function.
1242 /// NOTE: ForceUpdate is ignored in any stage other than the update stage.
1243 template <typename AAType>
1244 const AAType &getOrCreateAAFor(IRPosition IRP,
1245 const AbstractAttribute *QueryingAA,
1246 DepClassTy DepClass, bool ForceUpdate = false,
1247 bool UpdateAfterInit = true) {
1248 if (!shouldPropagateCallBaseContext(IRP))
1249 IRP = IRP.stripCallBaseContext();
1250
1251 if (AAType *AAPtr = lookupAAFor<AAType>(IRP, QueryingAA, DepClass,
1252 /* AllowInvalidState */ true)) {
1253 if (ForceUpdate && Phase == AttributorPhase::UPDATE)
1254 updateAA(*AAPtr);
1255 return *AAPtr;
1256 }
1257
1258 // No matching attribute found, create one.
1259 // Use the static create method.
1260 auto &AA = AAType::createForPosition(IRP, *this);
1261
1262 // If we are currenty seeding attributes, enforce seeding rules.
1263 if (Phase == AttributorPhase::SEEDING && !shouldSeedAttribute(AA)) {
1264 AA.getState().indicatePessimisticFixpoint();
1265 return AA;
1266 }
1267
1268 registerAA(AA);
1269
1270 // For now we ignore naked and optnone functions.
1271 bool Invalidate = Allowed && !Allowed->count(&AAType::ID);
1272 const Function *FnScope = IRP.getAnchorScope();
1273 if (FnScope)
1274 Invalidate |= FnScope->hasFnAttribute(Attribute::Naked) ||
1275 FnScope->hasFnAttribute(Attribute::OptimizeNone);
1276
1277 // Avoid too many nested initializations to prevent a stack overflow.
1278 Invalidate |= InitializationChainLength > MaxInitializationChainLength;
1279
1280 // Bootstrap the new attribute with an initial update to propagate
1281 // information, e.g., function -> call site. If it is not on a given
1282 // Allowed we will not perform updates at all.
1283 if (Invalidate) {
1284 AA.getState().indicatePessimisticFixpoint();
1285 return AA;
1286 }
1287
1288 {
1289 TimeTraceScope TimeScope(AA.getName() + "::initialize");
1290 ++InitializationChainLength;
1291 AA.initialize(*this);
1292 --InitializationChainLength;
1293 }
1294
1295 // Initialize and update is allowed for code outside of the current function
1296 // set, but only if it is part of module slice we are allowed to look at.
1297 // Only exception is AAIsDeadFunction whose initialization is prevented
1298 // directly, since we don't to compute it twice.
1299 if (FnScope && !Functions.count(const_cast<Function *>(FnScope))) {
1300 if (!getInfoCache().isInModuleSlice(*FnScope)) {
1301 AA.getState().indicatePessimisticFixpoint();
1302 return AA;
1303 }
1304 }
1305
1306 // If this is queried in the manifest stage, we force the AA to indicate
1307 // pessimistic fixpoint immediately.
1308 if (Phase == AttributorPhase::MANIFEST) {
1309 AA.getState().indicatePessimisticFixpoint();
1310 return AA;
1311 }
1312
1313 // Allow seeded attributes to declare dependencies.
1314 // Remember the seeding state.
1315 if (UpdateAfterInit) {
1316 AttributorPhase OldPhase = Phase;
1317 Phase = AttributorPhase::UPDATE;
1318
1319 updateAA(AA);
1320
1321 Phase = OldPhase;
1322 }
1323
1324 if (QueryingAA && AA.getState().isValidState())
1325 recordDependence(AA, const_cast<AbstractAttribute &>(*QueryingAA),
1326 DepClass);
1327 return AA;
1328 }
1329 template <typename AAType>
1330 const AAType &getOrCreateAAFor(const IRPosition &IRP) {
1331 return getOrCreateAAFor<AAType>(IRP, /* QueryingAA */ nullptr,
1332 DepClassTy::NONE);
1333 }
1334
1335 /// Return the attribute of \p AAType for \p IRP if existing and valid. This
1336 /// also allows non-AA users lookup.
1337 template <typename AAType>
1338 AAType *lookupAAFor(const IRPosition &IRP,
1339 const AbstractAttribute *QueryingAA = nullptr,
1340 DepClassTy DepClass = DepClassTy::OPTIONAL,
1341 bool AllowInvalidState = false) {
1342 static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1343 "Cannot query an attribute with a type not derived from "
1344 "'AbstractAttribute'!");
1345 // Lookup the abstract attribute of type AAType. If found, return it after
1346 // registering a dependence of QueryingAA on the one returned attribute.
1347 AbstractAttribute *AAPtr = AAMap.lookup({&AAType::ID, IRP});
1348 if (!AAPtr)
1349 return nullptr;
1350
1351 AAType *AA = static_cast<AAType *>(AAPtr);
1352
1353 // Do not register a dependence on an attribute with an invalid state.
1354 if (DepClass != DepClassTy::NONE && QueryingAA &&
1355 AA->getState().isValidState())
1356 recordDependence(*AA, const_cast<AbstractAttribute &>(*QueryingAA),
1357 DepClass);
1358
1359 // Return nullptr if this attribute has an invalid state.
1360 if (!AllowInvalidState && !AA->getState().isValidState())
1361 return nullptr;
1362 return AA;
1363 }
1364
1365 /// Explicitly record a dependence from \p FromAA to \p ToAA, that is if
1366 /// \p FromAA changes \p ToAA should be updated as well.
1367 ///
1368 /// This method should be used in conjunction with the `getAAFor` method and
1369 /// with the DepClass enum passed to the method set to None. This can
1370 /// be beneficial to avoid false dependences but it requires the users of
1371 /// `getAAFor` to explicitly record true dependences through this method.
1372 /// The \p DepClass flag indicates if the dependence is striclty necessary.
1373 /// That means for required dependences, if \p FromAA changes to an invalid
1374 /// state, \p ToAA can be moved to a pessimistic fixpoint because it required
1375 /// information from \p FromAA but none are available anymore.
1376 void recordDependence(const AbstractAttribute &FromAA,
1377 const AbstractAttribute &ToAA, DepClassTy DepClass);
1378
1379 /// Introduce a new abstract attribute into the fixpoint analysis.
1380 ///
1381 /// Note that ownership of the attribute is given to the Attributor. It will
1382 /// invoke delete for the Attributor on destruction of the Attributor.
1383 ///
1384 /// Attributes are identified by their IR position (AAType::getIRPosition())
1385 /// and the address of their static member (see AAType::ID).
1386 template <typename AAType> AAType &registerAA(AAType &AA) {
1387 static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1388 "Cannot register an attribute with a type not derived from "
1389 "'AbstractAttribute'!");
1390 // Put the attribute in the lookup map structure and the container we use to
1391 // keep track of all attributes.
1392 const IRPosition &IRP = AA.getIRPosition();
1393 AbstractAttribute *&AAPtr = AAMap[{&AAType::ID, IRP}];
1394
1395 assert(!AAPtr && "Attribute already in map!")((void)0);
1396 AAPtr = &AA;
1397
1398 // Register AA with the synthetic root only before the manifest stage.
1399 if (Phase == AttributorPhase::SEEDING || Phase == AttributorPhase::UPDATE)
1400 DG.SyntheticRoot.Deps.push_back(
1401 AADepGraphNode::DepTy(&AA, unsigned(DepClassTy::REQUIRED)));
1402
1403 return AA;
1404 }
1405
1406 /// Return the internal information cache.
1407 InformationCache &getInfoCache() { return InfoCache; }
1408
1409 /// Return true if this is a module pass, false otherwise.
1410 bool isModulePass() const {
1411 return !Functions.empty() &&
1412 Functions.size() == Functions.front()->getParent()->size();
1413 }
1414
1415 /// Return true if we derive attributes for \p Fn
1416 bool isRunOn(Function &Fn) const {
1417 return Functions.empty() || Functions.count(&Fn);
1418 }
1419
1420 /// Determine opportunities to derive 'default' attributes in \p F and create
1421 /// abstract attribute objects for them.
1422 ///
1423 /// \param F The function that is checked for attribute opportunities.
1424 ///
1425 /// Note that abstract attribute instances are generally created even if the
1426 /// IR already contains the information they would deduce. The most important
1427 /// reason for this is the single interface, the one of the abstract attribute
1428 /// instance, which can be queried without the need to look at the IR in
1429 /// various places.
1430 void identifyDefaultAbstractAttributes(Function &F);
1431
1432 /// Determine whether the function \p F is IPO amendable
1433 ///
1434 /// If a function is exactly defined or it has alwaysinline attribute
1435 /// and is viable to be inlined, we say it is IPO amendable
1436 bool isFunctionIPOAmendable(const Function &F) {
1437 return F.hasExactDefinition() || InfoCache.InlineableFunctions.count(&F);
1438 }
1439
1440 /// Mark the internal function \p F as live.
1441 ///
1442 /// This will trigger the identification and initialization of attributes for
1443 /// \p F.
1444 void markLiveInternalFunction(const Function &F) {
1445 assert(F.hasLocalLinkage() &&((void)0)
1446 "Only local linkage is assumed dead initially.")((void)0);
1447
1448 identifyDefaultAbstractAttributes(const_cast<Function &>(F));
1449 }
1450
1451 /// Helper function to remove callsite.
1452 void removeCallSite(CallInst *CI) {
1453 if (!CI)
1454 return;
1455
1456 CGUpdater.removeCallSite(*CI);
1457 }
1458
1459 /// Record that \p U is to be replaces with \p NV after information was
1460 /// manifested. This also triggers deletion of trivially dead istructions.
1461 bool changeUseAfterManifest(Use &U, Value &NV) {
1462 Value *&V = ToBeChangedUses[&U];
1463 if (V && (V->stripPointerCasts() == NV.stripPointerCasts() ||
1464 isa_and_nonnull<UndefValue>(V)))
1465 return false;
1466 assert((!V || V == &NV || isa<UndefValue>(NV)) &&((void)0)
1467 "Use was registered twice for replacement with different values!")((void)0);
1468 V = &NV;
1469 return true;
1470 }
1471
1472 /// Helper function to replace all uses of \p V with \p NV. Return true if
1473 /// there is any change. The flag \p ChangeDroppable indicates if dropppable
1474 /// uses should be changed too.
1475 bool changeValueAfterManifest(Value &V, Value &NV,
1476 bool ChangeDroppable = true) {
1477 auto &Entry = ToBeChangedValues[&V];
1478 Value *&CurNV = Entry.first;
1479 if (CurNV && (CurNV->stripPointerCasts() == NV.stripPointerCasts() ||
1480 isa<UndefValue>(CurNV)))
1481 return false;
1482 assert((!CurNV || CurNV == &NV || isa<UndefValue>(NV)) &&((void)0)
1483 "Value replacement was registered twice with different values!")((void)0);
1484 CurNV = &NV;
1485 Entry.second = ChangeDroppable;
1486 return true;
1487 }
1488
1489 /// Record that \p I is to be replaced with `unreachable` after information
1490 /// was manifested.
1491 void changeToUnreachableAfterManifest(Instruction *I) {
1492 ToBeChangedToUnreachableInsts.insert(I);
1493 }
1494
1495 /// Record that \p II has at least one dead successor block. This information
1496 /// is used, e.g., to replace \p II with a call, after information was
1497 /// manifested.
1498 void registerInvokeWithDeadSuccessor(InvokeInst &II) {
1499 InvokeWithDeadSuccessor.push_back(&II);
1500 }
1501
1502 /// Record that \p I is deleted after information was manifested. This also
1503 /// triggers deletion of trivially dead istructions.
1504 void deleteAfterManifest(Instruction &I) { ToBeDeletedInsts.insert(&I); }
1505
1506 /// Record that \p BB is deleted after information was manifested. This also
1507 /// triggers deletion of trivially dead istructions.
1508 void deleteAfterManifest(BasicBlock &BB) { ToBeDeletedBlocks.insert(&BB); }
1509
1510 // Record that \p BB is added during the manifest of an AA. Added basic blocks
1511 // are preserved in the IR.
1512 void registerManifestAddedBasicBlock(BasicBlock &BB) {
1513 ManifestAddedBlocks.insert(&BB);
1514 }
1515
1516 /// Record that \p F is deleted after information was manifested.
1517 void deleteAfterManifest(Function &F) {
1518 if (DeleteFns)
1519 ToBeDeletedFunctions.insert(&F);
1520 }
1521
1522 /// If \p IRP is assumed to be a constant, return it, if it is unclear yet,
1523 /// return None, otherwise return `nullptr`.
1524 Optional<Constant *> getAssumedConstant(const IRPosition &IRP,
1525 const AbstractAttribute &AA,
1526 bool &UsedAssumedInformation);
1527 Optional<Constant *> getAssumedConstant(const Value &V,
1528 const AbstractAttribute &AA,
1529 bool &UsedAssumedInformation) {
1530 return getAssumedConstant(IRPosition::value(V), AA, UsedAssumedInformation);
1531 }
1532
1533 /// If \p V is assumed simplified, return it, if it is unclear yet,
1534 /// return None, otherwise return `nullptr`.
1535 Optional<Value *> getAssumedSimplified(const IRPosition &IRP,
1536 const AbstractAttribute &AA,
1537 bool &UsedAssumedInformation) {
1538 return getAssumedSimplified(IRP, &AA, UsedAssumedInformation);
1539 }
1540 Optional<Value *> getAssumedSimplified(const Value &V,
1541 const AbstractAttribute &AA,
1542 bool &UsedAssumedInformation) {
1543 return getAssumedSimplified(IRPosition::value(V), AA,
1544 UsedAssumedInformation);
1545 }
1546
1547 /// If \p V is assumed simplified, return it, if it is unclear yet,
1548 /// return None, otherwise return `nullptr`. Same as the public version
1549 /// except that it can be used without recording dependences on any \p AA.
1550 Optional<Value *> getAssumedSimplified(const IRPosition &V,
1551 const AbstractAttribute *AA,
1552 bool &UsedAssumedInformation);
1553
1554 /// Register \p CB as a simplification callback.
1555 /// `Attributor::getAssumedSimplified` will use these callbacks before
1556 /// we it will ask `AAValueSimplify`. It is important to ensure this
1557 /// is called before `identifyDefaultAbstractAttributes`, assuming the
1558 /// latter is called at all.
1559 using SimplifictionCallbackTy = std::function<Optional<Value *>(
1560 const IRPosition &, const AbstractAttribute *, bool &)>;
1561 void registerSimplificationCallback(const IRPosition &IRP,
1562 const SimplifictionCallbackTy &CB) {
1563 SimplificationCallbacks[IRP].emplace_back(CB);
1564 }
1565
1566 /// Return true if there is a simplification callback for \p IRP.
1567 bool hasSimplificationCallback(const IRPosition &IRP) {
1568 return SimplificationCallbacks.count(IRP);
1569 }
1570
1571private:
1572 /// The vector with all simplification callbacks registered by outside AAs.
1573 DenseMap<IRPosition, SmallVector<SimplifictionCallbackTy, 1>>
1574 SimplificationCallbacks;
1575
1576public:
1577 /// Translate \p V from the callee context into the call site context.
1578 Optional<Value *>
1579 translateArgumentToCallSiteContent(Optional<Value *> V, CallBase &CB,
1580 const AbstractAttribute &AA,
1581 bool &UsedAssumedInformation);
1582
1583 /// Return true if \p AA (or its context instruction) is assumed dead.
1584 ///
1585 /// If \p LivenessAA is not provided it is queried.
1586 bool isAssumedDead(const AbstractAttribute &AA, const AAIsDead *LivenessAA,
1587 bool &UsedAssumedInformation,
1588 bool CheckBBLivenessOnly = false,
1589 DepClassTy DepClass = DepClassTy::OPTIONAL);
1590
1591 /// Return true if \p I is assumed dead.
1592 ///
1593 /// If \p LivenessAA is not provided it is queried.
1594 bool isAssumedDead(const Instruction &I, const AbstractAttribute *QueryingAA,
1595 const AAIsDead *LivenessAA, bool &UsedAssumedInformation,
1596 bool CheckBBLivenessOnly = false,
1597 DepClassTy DepClass = DepClassTy::OPTIONAL);
1598
1599 /// Return true if \p U is assumed dead.
1600 ///
1601 /// If \p FnLivenessAA is not provided it is queried.
1602 bool isAssumedDead(const Use &U, const AbstractAttribute *QueryingAA,
1603 const AAIsDead *FnLivenessAA, bool &UsedAssumedInformation,
1604 bool CheckBBLivenessOnly = false,
1605 DepClassTy DepClass = DepClassTy::OPTIONAL);
1606
1607 /// Return true if \p IRP is assumed dead.
1608 ///
1609 /// If \p FnLivenessAA is not provided it is queried.
1610 bool isAssumedDead(const IRPosition &IRP, const AbstractAttribute *QueryingAA,
1611 const AAIsDead *FnLivenessAA, bool &UsedAssumedInformation,
1612 bool CheckBBLivenessOnly = false,
1613 DepClassTy DepClass = DepClassTy::OPTIONAL);
1614
1615 /// Return true if \p BB is assumed dead.
1616 ///
1617 /// If \p LivenessAA is not provided it is queried.
1618 bool isAssumedDead(const BasicBlock &BB, const AbstractAttribute *QueryingAA,
1619 const AAIsDead *FnLivenessAA,
1620 DepClassTy DepClass = DepClassTy::OPTIONAL);
1621
1622 /// Check \p Pred on all (transitive) uses of \p V.
1623 ///
1624 /// This method will evaluate \p Pred on all (transitive) uses of the
1625 /// associated value and return true if \p Pred holds every time.
1626 bool checkForAllUses(function_ref<bool(const Use &, bool &)> Pred,
1627 const AbstractAttribute &QueryingAA, const Value &V,
1628 bool CheckBBLivenessOnly = false,
1629 DepClassTy LivenessDepClass = DepClassTy::OPTIONAL);
1630
1631 /// Emit a remark generically.
1632 ///
1633 /// This template function can be used to generically emit a remark. The
1634 /// RemarkKind should be one of the following:
1635 /// - OptimizationRemark to indicate a successful optimization attempt
1636 /// - OptimizationRemarkMissed to report a failed optimization attempt
1637 /// - OptimizationRemarkAnalysis to provide additional information about an
1638 /// optimization attempt
1639 ///
1640 /// The remark is built using a callback function \p RemarkCB that takes a
1641 /// RemarkKind as input and returns a RemarkKind.
1642 template <typename RemarkKind, typename RemarkCallBack>
1643 void emitRemark(Instruction *I, StringRef RemarkName,
1644 RemarkCallBack &&RemarkCB) const {
1645 if (!OREGetter)
1646 return;
1647
1648 Function *F = I->getFunction();
1649 auto &ORE = OREGetter.getValue()(F);
1650
1651 if (RemarkName.startswith("OMP"))
1652 ORE.emit([&]() {
1653 return RemarkCB(RemarkKind(PassName, RemarkName, I))
1654 << " [" << RemarkName << "]";
1655 });
1656 else
1657 ORE.emit([&]() { return RemarkCB(RemarkKind(PassName, RemarkName, I)); });
1658 }
1659
1660 /// Emit a remark on a function.
1661 template <typename RemarkKind, typename RemarkCallBack>
1662 void emitRemark(Function *F, StringRef RemarkName,
1663 RemarkCallBack &&RemarkCB) const {
1664 if (!OREGetter)
1665 return;
1666
1667 auto &ORE = OREGetter.getValue()(F);
1668
1669 if (RemarkName.startswith("OMP"))
1670 ORE.emit([&]() {
1671 return RemarkCB(RemarkKind(PassName, RemarkName, F))
1672 << " [" << RemarkName << "]";
1673 });
1674 else
1675 ORE.emit([&]() { return RemarkCB(RemarkKind(PassName, RemarkName, F)); });
1676 }
1677
1678 /// Helper struct used in the communication between an abstract attribute (AA)
1679 /// that wants to change the signature of a function and the Attributor which
1680 /// applies the changes. The struct is partially initialized with the
1681 /// information from the AA (see the constructor). All other members are
1682 /// provided by the Attributor prior to invoking any callbacks.
1683 struct ArgumentReplacementInfo {
1684 /// Callee repair callback type
1685 ///
1686 /// The function repair callback is invoked once to rewire the replacement
1687 /// arguments in the body of the new function. The argument replacement info
1688 /// is passed, as build from the registerFunctionSignatureRewrite call, as
1689 /// well as the replacement function and an iteratore to the first
1690 /// replacement argument.
1691 using CalleeRepairCBTy = std::function<void(
1692 const ArgumentReplacementInfo &, Function &, Function::arg_iterator)>;
1693
1694 /// Abstract call site (ACS) repair callback type
1695 ///
1696 /// The abstract call site repair callback is invoked once on every abstract
1697 /// call site of the replaced function (\see ReplacedFn). The callback needs
1698 /// to provide the operands for the call to the new replacement function.
1699 /// The number and type of the operands appended to the provided vector
1700 /// (second argument) is defined by the number and types determined through
1701 /// the replacement type vector (\see ReplacementTypes). The first argument
1702 /// is the ArgumentReplacementInfo object registered with the Attributor
1703 /// through the registerFunctionSignatureRewrite call.
1704 using ACSRepairCBTy =
1705 std::function<void(const ArgumentReplacementInfo &, AbstractCallSite,
1706 SmallVectorImpl<Value *> &)>;
1707
1708 /// Simple getters, see the corresponding members for details.
1709 ///{
1710
1711 Attributor &getAttributor() const { return A; }
1712 const Function &getReplacedFn() const { return ReplacedFn; }
1713 const Argument &getReplacedArg() const { return ReplacedArg; }
1714 unsigned getNumReplacementArgs() const { return ReplacementTypes.size(); }
1715 const SmallVectorImpl<Type *> &getReplacementTypes() const {
1716 return ReplacementTypes;
1717 }
1718
1719 ///}
1720
1721 private:
1722 /// Constructor that takes the argument to be replaced, the types of
1723 /// the replacement arguments, as well as callbacks to repair the call sites
1724 /// and new function after the replacement happened.
1725 ArgumentReplacementInfo(Attributor &A, Argument &Arg,
1726 ArrayRef<Type *> ReplacementTypes,
1727 CalleeRepairCBTy &&CalleeRepairCB,
1728 ACSRepairCBTy &&ACSRepairCB)
1729 : A(A), ReplacedFn(*Arg.getParent()), ReplacedArg(Arg),
1730 ReplacementTypes(ReplacementTypes.begin(), ReplacementTypes.end()),
1731 CalleeRepairCB(std::move(CalleeRepairCB)),
1732 ACSRepairCB(std::move(ACSRepairCB)) {}
1733
1734 /// Reference to the attributor to allow access from the callbacks.
1735 Attributor &A;
1736
1737 /// The "old" function replaced by ReplacementFn.
1738 const Function &ReplacedFn;
1739
1740 /// The "old" argument replaced by new ones defined via ReplacementTypes.
1741 const Argument &ReplacedArg;
1742
1743 /// The types of the arguments replacing ReplacedArg.
1744 const SmallVector<Type *, 8> ReplacementTypes;
1745
1746 /// Callee repair callback, see CalleeRepairCBTy.
1747 const CalleeRepairCBTy CalleeRepairCB;
1748
1749 /// Abstract call site (ACS) repair callback, see ACSRepairCBTy.
1750 const ACSRepairCBTy ACSRepairCB;
1751
1752 /// Allow access to the private members from the Attributor.
1753 friend struct Attributor;
1754 };
1755
1756 /// Check if we can rewrite a function signature.
1757 ///
1758 /// The argument \p Arg is replaced with new ones defined by the number,
1759 /// order, and types in \p ReplacementTypes.
1760 ///
1761 /// \returns True, if the replacement can be registered, via
1762 /// registerFunctionSignatureRewrite, false otherwise.
1763 bool isValidFunctionSignatureRewrite(Argument &Arg,
1764 ArrayRef<Type *> ReplacementTypes);
1765
1766 /// Register a rewrite for a function signature.
1767 ///
1768 /// The argument \p Arg is replaced with new ones defined by the number,
1769 /// order, and types in \p ReplacementTypes. The rewiring at the call sites is
1770 /// done through \p ACSRepairCB and at the callee site through
1771 /// \p CalleeRepairCB.
1772 ///
1773 /// \returns True, if the replacement was registered, false otherwise.
1774 bool registerFunctionSignatureRewrite(
1775 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
1776 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
1777 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB);
1778
1779 /// Check \p Pred on all function call sites.
1780 ///
1781 /// This method will evaluate \p Pred on call sites and return
1782 /// true if \p Pred holds in every call sites. However, this is only possible
1783 /// all call sites are known, hence the function has internal linkage.
1784 /// If true is returned, \p AllCallSitesKnown is set if all possible call
1785 /// sites of the function have been visited.
1786 bool checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
1787 const AbstractAttribute &QueryingAA,
1788 bool RequireAllCallSites, bool &AllCallSitesKnown);
1789
1790 /// Check \p Pred on all values potentially returned by \p F.
1791 ///
1792 /// This method will evaluate \p Pred on all values potentially returned by
1793 /// the function associated with \p QueryingAA. The returned values are
1794 /// matched with their respective return instructions. Returns true if \p Pred
1795 /// holds on all of them.
1796 bool checkForAllReturnedValuesAndReturnInsts(
1797 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
1798 const AbstractAttribute &QueryingAA);
1799
1800 /// Check \p Pred on all values potentially returned by the function
1801 /// associated with \p QueryingAA.
1802 ///
1803 /// This is the context insensitive version of the method above.
1804 bool checkForAllReturnedValues(function_ref<bool(Value &)> Pred,
1805 const AbstractAttribute &QueryingAA);
1806
1807 /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
1808 ///
1809 /// This method will evaluate \p Pred on all instructions with an opcode
1810 /// present in \p Opcode and return true if \p Pred holds on all of them.
1811 bool checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
1812 const AbstractAttribute &QueryingAA,
1813 const ArrayRef<unsigned> &Opcodes,
1814 bool &UsedAssumedInformation,
1815 bool CheckBBLivenessOnly = false,
1816 bool CheckPotentiallyDead = false);
1817
1818 /// Check \p Pred on all call-like instructions (=CallBased derived).
1819 ///
1820 /// See checkForAllCallLikeInstructions(...) for more information.
1821 bool checkForAllCallLikeInstructions(function_ref<bool(Instruction &)> Pred,
1822 const AbstractAttribute &QueryingAA,
1823 bool &UsedAssumedInformation,
1824 bool CheckBBLivenessOnly = false,
1825 bool CheckPotentiallyDead = false) {
1826 return checkForAllInstructions(
1827 Pred, QueryingAA,
1828 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
1829 (unsigned)Instruction::Call},
1830 UsedAssumedInformation, CheckBBLivenessOnly, CheckPotentiallyDead);
1831 }
1832
1833 /// Check \p Pred on all Read/Write instructions.
1834 ///
1835 /// This method will evaluate \p Pred on all instructions that read or write
1836 /// to memory present in the information cache and return true if \p Pred
1837 /// holds on all of them.
1838 bool checkForAllReadWriteInstructions(function_ref<bool(Instruction &)> Pred,
1839 AbstractAttribute &QueryingAA,
1840 bool &UsedAssumedInformation);
1841
1842 /// Create a shallow wrapper for \p F such that \p F has internal linkage
1843 /// afterwards. It also sets the original \p F 's name to anonymous
1844 ///
1845 /// A wrapper is a function with the same type (and attributes) as \p F
1846 /// that will only call \p F and return the result, if any.
1847 ///
1848 /// Assuming the declaration of looks like:
1849 /// rty F(aty0 arg0, ..., atyN argN);
1850 ///
1851 /// The wrapper will then look as follows:
1852 /// rty wrapper(aty0 arg0, ..., atyN argN) {
1853 /// return F(arg0, ..., argN);
1854 /// }
1855 ///
1856 static void createShallowWrapper(Function &F);
1857
1858 /// Returns true if the function \p F can be internalized. i.e. it has a
1859 /// compatible linkage.
1860 static bool isInternalizable(Function &F);
1861
1862 /// Make another copy of the function \p F such that the copied version has
1863 /// internal linkage afterwards and can be analysed. Then we replace all uses
1864 /// of the original function to the copied one
1865 ///
1866 /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
1867 /// linkage can be internalized because these linkages guarantee that other
1868 /// definitions with the same name have the same semantics as this one.
1869 ///
1870 /// This will only be run if the `attributor-allow-deep-wrappers` option is
1871 /// set, or if the function is called with \p Force set to true.
1872 ///
1873 /// If the function \p F failed to be internalized the return value will be a
1874 /// null pointer.
1875 static Function *internalizeFunction(Function &F, bool Force = false);
1876
1877 /// Make copies of each function in the set \p FnSet such that the copied
1878 /// version has internal linkage afterwards and can be analysed. Then we
1879 /// replace all uses of the original function to the copied one. The map
1880 /// \p FnMap contains a mapping of functions to their internalized versions.
1881 ///
1882 /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
1883 /// linkage can be internalized because these linkages guarantee that other
1884 /// definitions with the same name have the same semantics as this one.
1885 ///
1886 /// This version will internalize all the functions in the set \p FnSet at
1887 /// once and then replace the uses. This prevents internalized functions being
1888 /// called by external functions when there is an internalized version in the
1889 /// module.
1890 static bool internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet,
1891 DenseMap<Function *, Function *> &FnMap);
1892
1893 /// Return the data layout associated with the anchor scope.
1894 const DataLayout &getDataLayout() const { return InfoCache.DL; }
1895
1896 /// The allocator used to allocate memory, e.g. for `AbstractAttribute`s.
1897 BumpPtrAllocator &Allocator;
1898
1899private:
1900 /// This method will do fixpoint iteration until fixpoint or the
1901 /// maximum iteration count is reached.
1902 ///
1903 /// If the maximum iteration count is reached, This method will
1904 /// indicate pessimistic fixpoint on attributes that transitively depend
1905 /// on attributes that were scheduled for an update.
1906 void runTillFixpoint();
1907
1908 /// Gets called after scheduling, manifests attributes to the LLVM IR.
1909 ChangeStatus manifestAttributes();
1910
1911 /// Gets called after attributes have been manifested, cleans up the IR.
1912 /// Deletes dead functions, blocks and instructions.
1913 /// Rewrites function signitures and updates the call graph.
1914 ChangeStatus cleanupIR();
1915
1916 /// Identify internal functions that are effectively dead, thus not reachable
1917 /// from a live entry point. The functions are added to ToBeDeletedFunctions.
1918 void identifyDeadInternalFunctions();
1919
1920 /// Run `::update` on \p AA and track the dependences queried while doing so.
1921 /// Also adjust the state if we know further updates are not necessary.
1922 ChangeStatus updateAA(AbstractAttribute &AA);
1923
1924 /// Remember the dependences on the top of the dependence stack such that they
1925 /// may trigger further updates. (\see DependenceStack)
1926 void rememberDependences();
1927
1928 /// Check \p Pred on all call sites of \p Fn.
1929 ///
1930 /// This method will evaluate \p Pred on call sites and return
1931 /// true if \p Pred holds in every call sites. However, this is only possible
1932 /// all call sites are known, hence the function has internal linkage.
1933 /// If true is returned, \p AllCallSitesKnown is set if all possible call
1934 /// sites of the function have been visited.
1935 bool checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
1936 const Function &Fn, bool RequireAllCallSites,
1937 const AbstractAttribute *QueryingAA,
1938 bool &AllCallSitesKnown);
1939
1940 /// Determine if CallBase context in \p IRP should be propagated.
1941 bool shouldPropagateCallBaseContext(const IRPosition &IRP);
1942
1943 /// Apply all requested function signature rewrites
1944 /// (\see registerFunctionSignatureRewrite) and return Changed if the module
1945 /// was altered.
1946 ChangeStatus
1947 rewriteFunctionSignatures(SmallPtrSetImpl<Function *> &ModifiedFns);
1948
1949 /// Check if the Attribute \p AA should be seeded.
1950 /// See getOrCreateAAFor.
1951 bool shouldSeedAttribute(AbstractAttribute &AA);
1952
1953 /// A nested map to lookup abstract attributes based on the argument position
1954 /// on the outer level, and the addresses of the static member (AAType::ID) on
1955 /// the inner level.
1956 ///{
1957 using AAMapKeyTy = std::pair<const char *, IRPosition>;
1958 DenseMap<AAMapKeyTy, AbstractAttribute *> AAMap;
1959 ///}
1960
1961 /// Map to remember all requested signature changes (= argument replacements).
1962 DenseMap<Function *, SmallVector<std::unique_ptr<ArgumentReplacementInfo>, 8>>
1963 ArgumentReplacementMap;
1964
1965 /// The set of functions we are deriving attributes for.
1966 SetVector<Function *> &Functions;
1967
1968 /// The information cache that holds pre-processed (LLVM-IR) information.
1969 InformationCache &InfoCache;
1970
1971 /// Helper to update an underlying call graph.
1972 CallGraphUpdater &CGUpdater;
1973
1974 /// Abstract Attribute dependency graph
1975 AADepGraph DG;
1976
1977 /// Set of functions for which we modified the content such that it might
1978 /// impact the call graph.
1979 SmallPtrSet<Function *, 8> CGModifiedFunctions;
1980
1981 /// Information about a dependence. If FromAA is changed ToAA needs to be
1982 /// updated as well.
1983 struct DepInfo {
1984 const AbstractAttribute *FromAA;
1985 const AbstractAttribute *ToAA;
1986 DepClassTy DepClass;
1987 };
1988
1989 /// The dependence stack is used to track dependences during an
1990 /// `AbstractAttribute::update` call. As `AbstractAttribute::update` can be
1991 /// recursive we might have multiple vectors of dependences in here. The stack
1992 /// size, should be adjusted according to the expected recursion depth and the
1993 /// inner dependence vector size to the expected number of dependences per
1994 /// abstract attribute. Since the inner vectors are actually allocated on the
1995 /// stack we can be generous with their size.
1996 using DependenceVector = SmallVector<DepInfo, 8>;
1997 SmallVector<DependenceVector *, 16> DependenceStack;
1998
1999 /// If not null, a set limiting the attribute opportunities.
2000 const DenseSet<const char *> *Allowed;
2001
2002 /// Whether to delete functions.
2003 const bool DeleteFns;
2004
2005 /// Whether to rewrite signatures.
2006 const bool RewriteSignatures;
2007
2008 /// Maximum number of fixedpoint iterations.
2009 Optional<unsigned> MaxFixpointIterations;
2010
2011 /// A set to remember the functions we already assume to be live and visited.
2012 DenseSet<const Function *> VisitedFunctions;
2013
2014 /// Uses we replace with a new value after manifest is done. We will remove
2015 /// then trivially dead instructions as well.
2016 DenseMap<Use *, Value *> ToBeChangedUses;
2017
2018 /// Values we replace with a new value after manifest is done. We will remove
2019 /// then trivially dead instructions as well.
2020 DenseMap<Value *, std::pair<Value *, bool>> ToBeChangedValues;
2021
2022 /// Instructions we replace with `unreachable` insts after manifest is done.
2023 SmallDenseSet<WeakVH, 16> ToBeChangedToUnreachableInsts;
2024
2025 /// Invoke instructions with at least a single dead successor block.
2026 SmallVector<WeakVH, 16> InvokeWithDeadSuccessor;
2027
2028 /// A flag that indicates which stage of the process we are in. Initially, the
2029 /// phase is SEEDING. Phase is changed in `Attributor::run()`
2030 enum class AttributorPhase {
2031 SEEDING,
2032 UPDATE,
2033 MANIFEST,
2034 CLEANUP,
2035 } Phase = AttributorPhase::SEEDING;
2036
2037 /// The current initialization chain length. Tracked to avoid stack overflows.
2038 unsigned InitializationChainLength = 0;
2039
2040 /// Functions, blocks, and instructions we delete after manifest is done.
2041 ///
2042 ///{
2043 SmallPtrSet<Function *, 8> ToBeDeletedFunctions;
2044 SmallPtrSet<BasicBlock *, 8> ToBeDeletedBlocks;
2045 SmallPtrSet<BasicBlock *, 8> ManifestAddedBlocks;
2046 SmallDenseSet<WeakVH, 8> ToBeDeletedInsts;
2047 ///}
2048
2049 /// Callback to get an OptimizationRemarkEmitter from a Function *.
2050 Optional<OptimizationRemarkGetter> OREGetter;
2051
2052 /// The name of the pass to emit remarks for.
2053 const char *PassName = "";
2054
2055 friend AADepGraph;
2056 friend AttributorCallGraph;
2057};
2058
2059/// An interface to query the internal state of an abstract attribute.
2060///
2061/// The abstract state is a minimal interface that allows the Attributor to
2062/// communicate with the abstract attributes about their internal state without
2063/// enforcing or exposing implementation details, e.g., the (existence of an)
2064/// underlying lattice.
2065///
2066/// It is sufficient to be able to query if a state is (1) valid or invalid, (2)
2067/// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint
2068/// was reached or (4) a pessimistic fixpoint was enforced.
2069///
2070/// All methods need to be implemented by the subclass. For the common use case,
2071/// a single boolean state or a bit-encoded state, the BooleanState and
2072/// {Inc,Dec,Bit}IntegerState classes are already provided. An abstract
2073/// attribute can inherit from them to get the abstract state interface and
2074/// additional methods to directly modify the state based if needed. See the
2075/// class comments for help.
2076struct AbstractState {
2077 virtual ~AbstractState() {}
2078
2079 /// Return if this abstract state is in a valid state. If false, no
2080 /// information provided should be used.
2081 virtual bool isValidState() const = 0;
2082
2083 /// Return if this abstract state is fixed, thus does not need to be updated
2084 /// if information changes as it cannot change itself.
2085 virtual bool isAtFixpoint() const = 0;
2086
2087 /// Indicate that the abstract state should converge to the optimistic state.
2088 ///
2089 /// This will usually make the optimistically assumed state the known to be
2090 /// true state.
2091 ///
2092 /// \returns ChangeStatus::UNCHANGED as the assumed value should not change.
2093 virtual ChangeStatus indicateOptimisticFixpoint() = 0;
2094
2095 /// Indicate that the abstract state should converge to the pessimistic state.
2096 ///
2097 /// This will usually revert the optimistically assumed state to the known to
2098 /// be true state.
2099 ///
2100 /// \returns ChangeStatus::CHANGED as the assumed value may change.
2101 virtual ChangeStatus indicatePessimisticFixpoint() = 0;
2102};
2103
2104/// Simple state with integers encoding.
2105///
2106/// The interface ensures that the assumed bits are always a subset of the known
2107/// bits. Users can only add known bits and, except through adding known bits,
2108/// they can only remove assumed bits. This should guarantee monotoniticy and
2109/// thereby the existence of a fixpoint (if used corretly). The fixpoint is
2110/// reached when the assumed and known state/bits are equal. Users can
2111/// force/inidicate a fixpoint. If an optimistic one is indicated, the known
2112/// state will catch up with the assumed one, for a pessimistic fixpoint it is
2113/// the other way around.
2114template <typename base_ty, base_ty BestState, base_ty WorstState>
2115struct IntegerStateBase : public AbstractState {
2116 using base_t = base_ty;
2117
2118 IntegerStateBase() {}
2119 IntegerStateBase(base_t Assumed) : Assumed(Assumed) {}
2120
2121 /// Return the best possible representable state.
2122 static constexpr base_t getBestState() { return BestState; }
2123 static constexpr base_t getBestState(const IntegerStateBase &) {
2124 return getBestState();
2125 }
2126
2127 /// Return the worst possible representable state.
2128 static constexpr base_t getWorstState() { return WorstState; }
2129 static constexpr base_t getWorstState(const IntegerStateBase &) {
2130 return getWorstState();
2131 }
2132
2133 /// See AbstractState::isValidState()
2134 /// NOTE: For now we simply pretend that the worst possible state is invalid.
2135 bool isValidState() const override { return Assumed != getWorstState(); }
2136
2137 /// See AbstractState::isAtFixpoint()
2138 bool isAtFixpoint() const override { return Assumed == Known; }
2139
2140 /// See AbstractState::indicateOptimisticFixpoint(...)
2141 ChangeStatus indicateOptimisticFixpoint() override {
2142 Known = Assumed;
2143 return ChangeStatus::UNCHANGED;
2144 }
2145
2146 /// See AbstractState::indicatePessimisticFixpoint(...)
2147 ChangeStatus indicatePessimisticFixpoint() override {
2148 Assumed = Known;
2149 return ChangeStatus::CHANGED;
2150 }
2151
2152 /// Return the known state encoding
2153 base_t getKnown() const { return Known; }
2154
2155 /// Return the assumed state encoding.
2156 base_t getAssumed() const { return Assumed; }
2157
2158 /// Equality for IntegerStateBase.
2159 bool
2160 operator==(const IntegerStateBase<base_t, BestState, WorstState> &R) const {
2161 return this->getAssumed() == R.getAssumed() &&
2162 this->getKnown() == R.getKnown();
2163 }
2164
2165 /// Inequality for IntegerStateBase.
2166 bool
2167 operator!=(const IntegerStateBase<base_t, BestState, WorstState> &R) const {
2168 return !(*this == R);
2169 }
2170
2171 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2172 /// intended that only information assumed in both states will be assumed in
2173 /// this one afterwards.
2174 void operator^=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2175 handleNewAssumedValue(R.getAssumed());
2176 }
2177
2178 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2179 /// intended that information known in either state will be known in
2180 /// this one afterwards.
2181 void operator+=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2182 handleNewKnownValue(R.getKnown());
2183 }
2184
2185 void operator|=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2186 joinOR(R.getAssumed(), R.getKnown());
2187 }
2188
2189 void operator&=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
2190 joinAND(R.getAssumed(), R.getKnown());
2191 }
2192
2193protected:
2194 /// Handle a new assumed value \p Value. Subtype dependent.
2195 virtual void handleNewAssumedValue(base_t Value) = 0;
2196
2197 /// Handle a new known value \p Value. Subtype dependent.
2198 virtual void handleNewKnownValue(base_t Value) = 0;
2199
2200 /// Handle a value \p Value. Subtype dependent.
2201 virtual void joinOR(base_t AssumedValue, base_t KnownValue) = 0;
2202
2203 /// Handle a new assumed value \p Value. Subtype dependent.
2204 virtual void joinAND(base_t AssumedValue, base_t KnownValue) = 0;
2205
2206 /// The known state encoding in an integer of type base_t.
2207 base_t Known = getWorstState();
2208
2209 /// The assumed state encoding in an integer of type base_t.
2210 base_t Assumed = getBestState();
2211};
2212
2213/// Specialization of the integer state for a bit-wise encoding.
2214template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2215 base_ty WorstState = 0>
2216struct BitIntegerState
2217 : public IntegerStateBase<base_ty, BestState, WorstState> {
2218 using base_t = base_ty;
2219
2220 /// Return true if the bits set in \p BitsEncoding are "known bits".
2221 bool isKnown(base_t BitsEncoding) const {
2222 return (this->Known & BitsEncoding) == BitsEncoding;
2223 }
2224
2225 /// Return true if the bits set in \p BitsEncoding are "assumed bits".
2226 bool isAssumed(base_t BitsEncoding) const {
2227 return (this->Assumed & BitsEncoding) == BitsEncoding;
2228 }
2229
2230 /// Add the bits in \p BitsEncoding to the "known bits".
2231 BitIntegerState &addKnownBits(base_t Bits) {
2232 // Make sure we never miss any "known bits".
2233 this->Assumed |= Bits;
2234 this->Known |= Bits;
2235 return *this;
2236 }
2237
2238 /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known.
2239 BitIntegerState &removeAssumedBits(base_t BitsEncoding) {
2240 return intersectAssumedBits(~BitsEncoding);
2241 }
2242
2243 /// Remove the bits in \p BitsEncoding from the "known bits".
2244 BitIntegerState &removeKnownBits(base_t BitsEncoding) {
2245 this->Known = (this->Known & ~BitsEncoding);
2246 return *this;
2247 }
2248
2249 /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones.
2250 BitIntegerState &intersectAssumedBits(base_t BitsEncoding) {
2251 // Make sure we never loose any "known bits".
2252 this->Assumed = (this->Assumed & BitsEncoding) | this->Known;
2253 return *this;
2254 }
2255
2256private:
2257 void handleNewAssumedValue(base_t Value) override {
2258 intersectAssumedBits(Value);
2259 }
2260 void handleNewKnownValue(base_t Value) override { addKnownBits(Value); }
2261 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2262 this->Known |= KnownValue;
2263 this->Assumed |= AssumedValue;
2264 }
2265 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2266 this->Known &= KnownValue;
2267 this->Assumed &= AssumedValue;
2268 }
2269};
2270
2271/// Specialization of the integer state for an increasing value, hence ~0u is
2272/// the best state and 0 the worst.
2273template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2274 base_ty WorstState = 0>
2275struct IncIntegerState
2276 : public IntegerStateBase<base_ty, BestState, WorstState> {
2277 using super = IntegerStateBase<base_ty, BestState, WorstState>;
2278 using base_t = base_ty;
2279
2280 IncIntegerState() : super() {}
2281 IncIntegerState(base_t Assumed) : super(Assumed) {}
2282
2283 /// Return the best possible representable state.
2284 static constexpr base_t getBestState() { return BestState; }
2285 static constexpr base_t
2286 getBestState(const IncIntegerState<base_ty, BestState, WorstState> &) {
2287 return getBestState();
2288 }
2289
2290 /// Take minimum of assumed and \p Value.
2291 IncIntegerState &takeAssumedMinimum(base_t Value) {
2292 // Make sure we never loose "known value".
2293 this->Assumed = std::max(std::min(this->Assumed, Value), this->Known);
2294 return *this;
2295 }
2296
2297 /// Take maximum of known and \p Value.
2298 IncIntegerState &takeKnownMaximum(base_t Value) {
2299 // Make sure we never loose "known value".
2300 this->Assumed = std::max(Value, this->Assumed);
2301 this->Known = std::max(Value, this->Known);
2302 return *this;
2303 }
2304
2305private:
2306 void handleNewAssumedValue(base_t Value) override {
2307 takeAssumedMinimum(Value);
2308 }
2309 void handleNewKnownValue(base_t Value) override { takeKnownMaximum(Value); }
2310 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2311 this->Known = std::max(this->Known, KnownValue);
2312 this->Assumed = std::max(this->Assumed, AssumedValue);
2313 }
2314 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2315 this->Known = std::min(this->Known, KnownValue);
2316 this->Assumed = std::min(this->Assumed, AssumedValue);
2317 }
2318};
2319
2320/// Specialization of the integer state for a decreasing value, hence 0 is the
2321/// best state and ~0u the worst.
2322template <typename base_ty = uint32_t>
2323struct DecIntegerState : public IntegerStateBase<base_ty, 0, ~base_ty(0)> {
2324 using base_t = base_ty;
2325
2326 /// Take maximum of assumed and \p Value.
2327 DecIntegerState &takeAssumedMaximum(base_t Value) {
2328 // Make sure we never loose "known value".
2329 this->Assumed = std::min(std::max(this->Assumed, Value), this->Known);
2330 return *this;
2331 }
2332
2333 /// Take minimum of known and \p Value.
2334 DecIntegerState &takeKnownMinimum(base_t Value) {
2335 // Make sure we never loose "known value".
2336 this->Assumed = std::min(Value, this->Assumed);
2337 this->Known = std::min(Value, this->Known);
2338 return *this;
2339 }
2340
2341private:
2342 void handleNewAssumedValue(base_t Value) override {
2343 takeAssumedMaximum(Value);
2344 }
2345 void handleNewKnownValue(base_t Value) override { takeKnownMinimum(Value); }
2346 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2347 this->Assumed = std::min(this->Assumed, KnownValue);
2348 this->Assumed = std::min(this->Assumed, AssumedValue);
2349 }
2350 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2351 this->Assumed = std::max(this->Assumed, KnownValue);
2352 this->Assumed = std::max(this->Assumed, AssumedValue);
2353 }
2354};
2355
2356/// Simple wrapper for a single bit (boolean) state.
2357struct BooleanState : public IntegerStateBase<bool, 1, 0> {
2358 using super = IntegerStateBase<bool, 1, 0>;
2359 using base_t = IntegerStateBase::base_t;
2360
2361 BooleanState() : super() {}
2362 BooleanState(base_t Assumed) : super(Assumed) {}
2363
2364 /// Set the assumed value to \p Value but never below the known one.
2365 void setAssumed(bool Value) { Assumed &= (Known | Value); }
2366
2367 /// Set the known and asssumed value to \p Value.
2368 void setKnown(bool Value) {
2369 Known |= Value;
2370 Assumed |= Value;
2371 }
2372
2373 /// Return true if the state is assumed to hold.
2374 bool isAssumed() const { return getAssumed(); }
2375
2376 /// Return true if the state is known to hold.
2377 bool isKnown() const { return getKnown(); }
2378
2379private:
2380 void handleNewAssumedValue(base_t Value) override {
2381 if (!Value)
2382 Assumed = Known;
2383 }
2384 void handleNewKnownValue(base_t Value) override {
2385 if (Value)
2386 Known = (Assumed = Value);
2387 }
2388 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2389 Known |= KnownValue;
2390 Assumed |= AssumedValue;
2391 }
2392 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2393 Known &= KnownValue;
2394 Assumed &= AssumedValue;
2395 }
2396};
2397
2398/// State for an integer range.
2399struct IntegerRangeState : public AbstractState {
2400
2401 /// Bitwidth of the associated value.
2402 uint32_t BitWidth;
2403
2404 /// State representing assumed range, initially set to empty.
2405 ConstantRange Assumed;
2406
2407 /// State representing known range, initially set to [-inf, inf].
2408 ConstantRange Known;
2409
2410 IntegerRangeState(uint32_t BitWidth)
2411 : BitWidth(BitWidth), Assumed(ConstantRange::getEmpty(BitWidth)),
2412 Known(ConstantRange::getFull(BitWidth)) {}
2413
2414 IntegerRangeState(const ConstantRange &CR)
2415 : BitWidth(CR.getBitWidth()), Assumed(CR),
2416 Known(getWorstState(CR.getBitWidth())) {}
2417
2418 /// Return the worst possible representable state.
2419 static ConstantRange getWorstState(uint32_t BitWidth) {
2420 return ConstantRange::getFull(BitWidth);
2421 }
2422
2423 /// Return the best possible representable state.
2424 static ConstantRange getBestState(uint32_t BitWidth) {
2425 return ConstantRange::getEmpty(BitWidth);
2426 }
2427 static ConstantRange getBestState(const IntegerRangeState &IRS) {
2428 return getBestState(IRS.getBitWidth());
2429 }
2430
2431 /// Return associated values' bit width.
2432 uint32_t getBitWidth() const { return BitWidth; }
2433
2434 /// See AbstractState::isValidState()
2435 bool isValidState() const override {
2436 return BitWidth > 0 && !Assumed.isFullSet();
2437 }
2438
2439 /// See AbstractState::isAtFixpoint()
2440 bool isAtFixpoint() const override { return Assumed == Known; }
2441
2442 /// See AbstractState::indicateOptimisticFixpoint(...)
2443 ChangeStatus indicateOptimisticFixpoint() override {
2444 Known = Assumed;
2445 return ChangeStatus::CHANGED;
2446 }
2447
2448 /// See AbstractState::indicatePessimisticFixpoint(...)
2449 ChangeStatus indicatePessimisticFixpoint() override {
2450 Assumed = Known;
2451 return ChangeStatus::CHANGED;
2452 }
2453
2454 /// Return the known state encoding
2455 ConstantRange getKnown() const { return Known; }
2456
2457 /// Return the assumed state encoding.
2458 ConstantRange getAssumed() const { return Assumed; }
2459
2460 /// Unite assumed range with the passed state.
2461 void unionAssumed(const ConstantRange &R) {
2462 // Don't loose a known range.
2463 Assumed = Assumed.unionWith(R).intersectWith(Known);
2464 }
2465
2466 /// See IntegerRangeState::unionAssumed(..).
2467 void unionAssumed(const IntegerRangeState &R) {
2468 unionAssumed(R.getAssumed());
2469 }
2470
2471 /// Unite known range with the passed state.
2472 void unionKnown(const ConstantRange &R) {
2473 // Don't loose a known range.
2474 Known = Known.unionWith(R);
2475 Assumed = Assumed.unionWith(Known);
2476 }
2477
2478 /// See IntegerRangeState::unionKnown(..).
2479 void unionKnown(const IntegerRangeState &R) { unionKnown(R.getKnown()); }
2480
2481 /// Intersect known range with the passed state.
2482 void intersectKnown(const ConstantRange &R) {
2483 Assumed = Assumed.intersectWith(R);
2484 Known = Known.intersectWith(R);
2485 }
2486
2487 /// See IntegerRangeState::intersectKnown(..).
2488 void intersectKnown(const IntegerRangeState &R) {
2489 intersectKnown(R.getKnown());
2490 }
2491
2492 /// Equality for IntegerRangeState.
2493 bool operator==(const IntegerRangeState &R) const {
2494 return getAssumed() == R.getAssumed() && getKnown() == R.getKnown();
2495 }
2496
2497 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2498 /// intended that only information assumed in both states will be assumed in
2499 /// this one afterwards.
2500 IntegerRangeState operator^=(const IntegerRangeState &R) {
2501 // NOTE: `^=` operator seems like `intersect` but in this case, we need to
2502 // take `union`.
2503 unionAssumed(R);
2504 return *this;
2505 }
2506
2507 IntegerRangeState operator&=(const IntegerRangeState &R) {
2508 // NOTE: `&=` operator seems like `intersect` but in this case, we need to
2509 // take `union`.
2510 unionKnown(R);
2511 unionAssumed(R);
2512 return *this;
2513 }
2514};
2515/// Helper struct necessary as the modular build fails if the virtual method
2516/// IRAttribute::manifest is defined in the Attributor.cpp.
2517struct IRAttributeManifest {
2518 static ChangeStatus manifestAttrs(Attributor &A, const IRPosition &IRP,
2519 const ArrayRef<Attribute> &DeducedAttrs,
2520 bool ForceReplace = false);
2521};
2522
2523/// Helper to tie a abstract state implementation to an abstract attribute.
2524template <typename StateTy, typename BaseType, class... Ts>
2525struct StateWrapper : public BaseType, public StateTy {
2526 /// Provide static access to the type of the state.
2527 using StateType = StateTy;
2528
2529 StateWrapper(const IRPosition &IRP, Ts... Args)
2530 : BaseType(IRP), StateTy(Args...) {}
2531
2532 /// See AbstractAttribute::getState(...).
2533 StateType &getState() override { return *this; }
2534
2535 /// See AbstractAttribute::getState(...).
2536 const StateType &getState() const override { return *this; }
2537};
2538
2539/// Helper class that provides common functionality to manifest IR attributes.
2540template <Attribute::AttrKind AK, typename BaseType>
2541struct IRAttribute : public BaseType {
2542 IRAttribute(const IRPosition &IRP) : BaseType(IRP) {}
2543
2544 /// See AbstractAttribute::initialize(...).
2545 virtual void initialize(Attributor &A) override {
2546 const IRPosition &IRP = this->getIRPosition();
2547 if (isa<UndefValue>(IRP.getAssociatedValue()) ||
2548 this->hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ false,
2549 &A)) {
2550 this->getState().indicateOptimisticFixpoint();
2551 return;
2552 }
2553
2554 bool IsFnInterface = IRP.isFnInterfaceKind();
2555 const Function *FnScope = IRP.getAnchorScope();
2556 // TODO: Not all attributes require an exact definition. Find a way to
2557 // enable deduction for some but not all attributes in case the
2558 // definition might be changed at runtime, see also
2559 // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
2560 // TODO: We could always determine abstract attributes and if sufficient
2561 // information was found we could duplicate the functions that do not
2562 // have an exact definition.
2563 if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope)))
2564 this->getState().indicatePessimisticFixpoint();
2565 }
2566
2567 /// See AbstractAttribute::manifest(...).
2568 ChangeStatus manifest(Attributor &A) override {
2569 if (isa<UndefValue>(this->getIRPosition().getAssociatedValue()))
2570 return ChangeStatus::UNCHANGED;
2571 SmallVector<Attribute, 4> DeducedAttrs;
2572 getDeducedAttributes(this->getAnchorValue().getContext(), DeducedAttrs);
2573 return IRAttributeManifest::manifestAttrs(A, this->getIRPosition(),
2574 DeducedAttrs);
2575 }
2576
2577 /// Return the kind that identifies the abstract attribute implementation.
2578 Attribute::AttrKind getAttrKind() const { return AK; }
2579
2580 /// Return the deduced attributes in \p Attrs.
2581 virtual void getDeducedAttributes(LLVMContext &Ctx,
2582 SmallVectorImpl<Attribute> &Attrs) const {
2583 Attrs.emplace_back(Attribute::get(Ctx, getAttrKind()));
2584 }
2585};
2586
2587/// Base struct for all "concrete attribute" deductions.
2588///
2589/// The abstract attribute is a minimal interface that allows the Attributor to
2590/// orchestrate the abstract/fixpoint analysis. The design allows to hide away
2591/// implementation choices made for the subclasses but also to structure their
2592/// implementation and simplify the use of other abstract attributes in-flight.
2593///
2594/// To allow easy creation of new attributes, most methods have default
2595/// implementations. The ones that do not are generally straight forward, except
2596/// `AbstractAttribute::updateImpl` which is the location of most reasoning
2597/// associated with the abstract attribute. The update is invoked by the
2598/// Attributor in case the situation used to justify the current optimistic
2599/// state might have changed. The Attributor determines this automatically
2600/// by monitoring the `Attributor::getAAFor` calls made by abstract attributes.
2601///
2602/// The `updateImpl` method should inspect the IR and other abstract attributes
2603/// in-flight to justify the best possible (=optimistic) state. The actual
2604/// implementation is, similar to the underlying abstract state encoding, not
2605/// exposed. In the most common case, the `updateImpl` will go through a list of
2606/// reasons why its optimistic state is valid given the current information. If
2607/// any combination of them holds and is sufficient to justify the current
2608/// optimistic state, the method shall return UNCHAGED. If not, the optimistic
2609/// state is adjusted to the situation and the method shall return CHANGED.
2610///
2611/// If the manifestation of the "concrete attribute" deduced by the subclass
2612/// differs from the "default" behavior, which is a (set of) LLVM-IR
2613/// attribute(s) for an argument, call site argument, function return value, or
2614/// function, the `AbstractAttribute::manifest` method should be overloaded.
2615///
2616/// NOTE: If the state obtained via getState() is INVALID, thus if
2617/// AbstractAttribute::getState().isValidState() returns false, no
2618/// information provided by the methods of this class should be used.
2619/// NOTE: The Attributor currently has certain limitations to what we can do.
2620/// As a general rule of thumb, "concrete" abstract attributes should *for
2621/// now* only perform "backward" information propagation. That means
2622/// optimistic information obtained through abstract attributes should
2623/// only be used at positions that precede the origin of the information
2624/// with regards to the program flow. More practically, information can
2625/// *now* be propagated from instructions to their enclosing function, but
2626/// *not* from call sites to the called function. The mechanisms to allow
2627/// both directions will be added in the future.
2628/// NOTE: The mechanics of adding a new "concrete" abstract attribute are
2629/// described in the file comment.
2630struct AbstractAttribute : public IRPosition, public AADepGraphNode {
2631 using StateType = AbstractState;
2632
2633 AbstractAttribute(const IRPosition &IRP) : IRPosition(IRP) {}
2634
2635 /// Virtual destructor.
2636 virtual ~AbstractAttribute() {}
2637
2638 /// This function is used to identify if an \p DGN is of type
2639 /// AbstractAttribute so that the dyn_cast and cast can use such information
2640 /// to cast an AADepGraphNode to an AbstractAttribute.
2641 ///
2642 /// We eagerly return true here because all AADepGraphNodes except for the
2643 /// Synthethis Node are of type AbstractAttribute
2644 static bool classof(const AADepGraphNode *DGN) { return true; }
2645
2646 /// Initialize the state with the information in the Attributor \p A.
2647 ///
2648 /// This function is called by the Attributor once all abstract attributes
2649 /// have been identified. It can and shall be used for task like:
2650 /// - identify existing knowledge in the IR and use it for the "known state"
2651 /// - perform any work that is not going to change over time, e.g., determine
2652 /// a subset of the IR, or attributes in-flight, that have to be looked at
2653 /// in the `updateImpl` method.
2654 virtual void initialize(Attributor &A) {}
2655
2656 /// Return the internal abstract state for inspection.
2657 virtual StateType &getState() = 0;
2658 virtual const StateType &getState() const = 0;
2659
2660 /// Return an IR position, see struct IRPosition.
2661 const IRPosition &getIRPosition() const { return *this; };
2662 IRPosition &getIRPosition() { return *this; };
2663
2664 /// Helper functions, for debug purposes only.
2665 ///{
2666 void print(raw_ostream &OS) const override;
2667 virtual void printWithDeps(raw_ostream &OS) const;
2668 void dump() const { print(dbgs()); }
2669
2670 /// This function should return the "summarized" assumed state as string.
2671 virtual const std::string getAsStr() const = 0;
2672
2673 /// This function should return the name of the AbstractAttribute
2674 virtual const std::string getName() const = 0;
2675
2676 /// This function should return the address of the ID of the AbstractAttribute
2677 virtual const char *getIdAddr() const = 0;
2678 ///}
2679
2680 /// Allow the Attributor access to the protected methods.
2681 friend struct Attributor;
2682
2683protected:
2684 /// Hook for the Attributor to trigger an update of the internal state.
2685 ///
2686 /// If this attribute is already fixed, this method will return UNCHANGED,
2687 /// otherwise it delegates to `AbstractAttribute::updateImpl`.
2688 ///
2689 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
2690 ChangeStatus update(Attributor &A);
2691
2692 /// Hook for the Attributor to trigger the manifestation of the information
2693 /// represented by the abstract attribute in the LLVM-IR.
2694 ///
2695 /// \Return CHANGED if the IR was altered, otherwise UNCHANGED.
2696 virtual ChangeStatus manifest(Attributor &A) {
2697 return ChangeStatus::UNCHANGED;
2698 }
2699
2700 /// Hook to enable custom statistic tracking, called after manifest that
2701 /// resulted in a change if statistics are enabled.
2702 ///
2703 /// We require subclasses to provide an implementation so we remember to
2704 /// add statistics for them.
2705 virtual void trackStatistics() const = 0;
2706
2707 /// The actual update/transfer function which has to be implemented by the
2708 /// derived classes.
2709 ///
2710 /// If it is called, the environment has changed and we have to determine if
2711 /// the current information is still valid or adjust it otherwise.
2712 ///
2713 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
2714 virtual ChangeStatus updateImpl(Attributor &A) = 0;
2715};
2716
2717/// Forward declarations of output streams for debug purposes.
2718///
2719///{
2720raw_ostream &operator<<(raw_ostream &OS, const AbstractAttribute &AA);
2721raw_ostream &operator<<(raw_ostream &OS, ChangeStatus S);
2722raw_ostream &operator<<(raw_ostream &OS, IRPosition::Kind);
2723raw_ostream &operator<<(raw_ostream &OS, const IRPosition &);
2724raw_ostream &operator<<(raw_ostream &OS, const AbstractState &State);
2725template <typename base_ty, base_ty BestState, base_ty WorstState>
2726raw_ostream &
2727operator<<(raw_ostream &OS,
2728 const IntegerStateBase<base_ty, BestState, WorstState> &S) {
2729 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
2730 << static_cast<const AbstractState &>(S);
2731}
2732raw_ostream &operator<<(raw_ostream &OS, const IntegerRangeState &State);
2733///}
2734
2735struct AttributorPass : public PassInfoMixin<AttributorPass> {
2736 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
2737};
2738struct AttributorCGSCCPass : public PassInfoMixin<AttributorCGSCCPass> {
2739 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM,
2740 LazyCallGraph &CG, CGSCCUpdateResult &UR);
2741};
2742
2743Pass *createAttributorLegacyPass();
2744Pass *createAttributorCGSCCLegacyPass();
2745
2746/// Helper function to clamp a state \p S of type \p StateType with the
2747/// information in \p R and indicate/return if \p S did change (as-in update is
2748/// required to be run again).
2749template <typename StateType>
2750ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
2751 auto Assumed = S.getAssumed();
2752 S ^= R;
2753 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
2754 : ChangeStatus::CHANGED;
2755}
2756
2757/// ----------------------------------------------------------------------------
2758/// Abstract Attribute Classes
2759/// ----------------------------------------------------------------------------
2760
2761/// An abstract attribute for the returned values of a function.
2762struct AAReturnedValues
2763 : public IRAttribute<Attribute::Returned, AbstractAttribute> {
2764 AAReturnedValues(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
2765
2766 /// Return an assumed unique return value if a single candidate is found. If
2767 /// there cannot be one, return a nullptr. If it is not clear yet, return the
2768 /// Optional::NoneType.
2769 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
2770
2771 /// Check \p Pred on all returned values.
2772 ///
2773 /// This method will evaluate \p Pred on returned values and return
2774 /// true if (1) all returned values are known, and (2) \p Pred returned true
2775 /// for all returned values.
2776 ///
2777 /// Note: Unlike the Attributor::checkForAllReturnedValuesAndReturnInsts
2778 /// method, this one will not filter dead return instructions.
2779 virtual bool checkForAllReturnedValuesAndReturnInsts(
2780 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
2781 const = 0;
2782
2783 using iterator =
2784 MapVector<Value *, SmallSetVector<ReturnInst *, 4>>::iterator;
2785 using const_iterator =
2786 MapVector<Value *, SmallSetVector<ReturnInst *, 4>>::const_iterator;
2787 virtual llvm::iterator_range<iterator> returned_values() = 0;
2788 virtual llvm::iterator_range<const_iterator> returned_values() const = 0;
2789
2790 virtual size_t getNumReturnValues() const = 0;
2791
2792 /// Create an abstract attribute view for the position \p IRP.
2793 static AAReturnedValues &createForPosition(const IRPosition &IRP,
2794 Attributor &A);
2795
2796 /// See AbstractAttribute::getName()
2797 const std::string getName() const override { return "AAReturnedValues"; }
2798
2799 /// See AbstractAttribute::getIdAddr()
2800 const char *getIdAddr() const override { return &ID; }
2801
2802 /// This function should return true if the type of the \p AA is
2803 /// AAReturnedValues
2804 static bool classof(const AbstractAttribute *AA) {
2805 return (AA->getIdAddr() == &ID);
2806 }
2807
2808 /// Unique ID (due to the unique address)
2809 static const char ID;
2810};
2811
2812struct AANoUnwind
2813 : public IRAttribute<Attribute::NoUnwind,
2814 StateWrapper<BooleanState, AbstractAttribute>> {
2815 AANoUnwind(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
2816
2817 /// Returns true if nounwind is assumed.
2818 bool isAssumedNoUnwind() const { return getAssumed(); }
2819
2820 /// Returns true if nounwind is known.
2821 bool isKnownNoUnwind() const { return getKnown(); }
2822
2823 /// Create an abstract attribute view for the position \p IRP.
2824 static AANoUnwind &createForPosition(const IRPosition &IRP, Attributor &A);
2825
2826 /// See AbstractAttribute::getName()
2827 const std::string getName() const override { return "AANoUnwind"; }
2828
2829 /// See AbstractAttribute::getIdAddr()
2830 const char *getIdAddr() const override { return &ID; }
2831
2832 /// This function should return true if the type of the \p AA is AANoUnwind
2833 static bool classof(const AbstractAttribute *AA) {
2834 return (AA->getIdAddr() == &ID);
2835 }
2836
2837 /// Unique ID (due to the unique address)
2838 static const char ID;
2839};
2840
2841struct AANoSync
2842 : public IRAttribute<Attribute::NoSync,
2843 StateWrapper<BooleanState, AbstractAttribute>> {
2844 AANoSync(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
2845
2846 /// Returns true if "nosync" is assumed.
2847 bool isAssumedNoSync() const { return getAssumed(); }
2848
2849 /// Returns true if "nosync" is known.
2850 bool isKnownNoSync() const { return getKnown(); }
2851
2852 /// Create an abstract attribute view for the position \p IRP.
2853 static AANoSync &createForPosition(const IRPosition &IRP, Attributor &A);
2854
2855 /// See AbstractAttribute::getName()
2856 const std::string getName() const override { return "AANoSync"; }
2857
2858 /// See AbstractAttribute::getIdAddr()
2859 const char *getIdAddr() const override { return &ID; }
2860
2861 /// This function should return true if the type of the \p AA is AANoSync
2862 static bool classof(const AbstractAttribute *AA) {
2863 return (AA->getIdAddr() == &ID);
2864 }
2865
2866 /// Unique ID (due to the unique address)
2867 static const char ID;
2868};
2869
2870/// An abstract interface for all nonnull attributes.
2871struct AANonNull
2872 : public IRAttribute<Attribute::NonNull,
2873 StateWrapper<BooleanState, AbstractAttribute>> {
2874 AANonNull(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
2875
2876 /// Return true if we assume that the underlying value is nonnull.
2877 bool isAssumedNonNull() const { return getAssumed(); }
2878
2879 /// Return true if we know that underlying value is nonnull.
2880 bool isKnownNonNull() const { return getKnown(); }
2881
2882 /// Create an abstract attribute view for the position \p IRP.
2883 static AANonNull &createForPosition(const IRPosition &IRP, Attributor &A);
2884
2885 /// See AbstractAttribute::getName()
2886 const std::string getName() const override { return "AANonNull"; }
2887
2888 /// See AbstractAttribute::getIdAddr()
2889 const char *getIdAddr() const override { return &ID; }
2890
2891 /// This function should return true if the type of the \p AA is AANonNull
2892 static bool classof(const AbstractAttribute *AA) {
2893 return (AA->getIdAddr() == &ID);
2894 }
2895
2896 /// Unique ID (due to the unique address)
2897 static const char ID;
2898};
2899
2900/// An abstract attribute for norecurse.
2901struct AANoRecurse
2902 : public IRAttribute<Attribute::NoRecurse,
2903 StateWrapper<BooleanState, AbstractAttribute>> {
2904 AANoRecurse(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
2905
2906 /// Return true if "norecurse" is assumed.
2907 bool isAssumedNoRecurse() const { return getAssumed(); }
2908
2909 /// Return true if "norecurse" is known.
2910 bool isKnownNoRecurse() const { return getKnown(); }
2911
2912 /// Create an abstract attribute view for the position \p IRP.
2913 static AANoRecurse &createForPosition(const IRPosition &IRP, Attributor &A);
2914
2915 /// See AbstractAttribute::getName()
2916 const std::string getName() const override { return "AANoRecurse"; }
2917
2918 /// See AbstractAttribute::getIdAddr()
2919 const char *getIdAddr() const override { return &ID; }
2920
2921 /// This function should return true if the type of the \p AA is AANoRecurse
2922 static bool classof(const AbstractAttribute *AA) {
2923 return (AA->getIdAddr() == &ID);
2924 }
2925
2926 /// Unique ID (due to the unique address)
2927 static const char ID;
2928};
2929
2930/// An abstract attribute for willreturn.
2931struct AAWillReturn
2932 : public IRAttribute<Attribute::WillReturn,
2933 StateWrapper<BooleanState, AbstractAttribute>> {
2934 AAWillReturn(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
2935
2936 /// Return true if "willreturn" is assumed.
2937 bool isAssumedWillReturn() const { return getAssumed(); }
2938
2939 /// Return true if "willreturn" is known.
2940 bool isKnownWillReturn() const { return getKnown(); }
2941
2942 /// Create an abstract attribute view for the position \p IRP.
2943 static AAWillReturn &createForPosition(const IRPosition &IRP, Attributor &A);
2944
2945 /// See AbstractAttribute::getName()
2946 const std::string getName() const override { return "AAWillReturn"; }
2947
2948 /// See AbstractAttribute::getIdAddr()
2949 const char *getIdAddr() const override { return &ID; }
2950
2951 /// This function should return true if the type of the \p AA is AAWillReturn
2952 static bool classof(const AbstractAttribute *AA) {
2953 return (AA->getIdAddr() == &ID);
2954 }
2955
2956 /// Unique ID (due to the unique address)
2957 static const char ID;
2958};
2959
2960/// An abstract attribute for undefined behavior.
2961struct AAUndefinedBehavior
2962 : public StateWrapper<BooleanState, AbstractAttribute> {
2963 using Base = StateWrapper<BooleanState, AbstractAttribute>;
2964 AAUndefinedBehavior(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
2965
2966 /// Return true if "undefined behavior" is assumed.
2967 bool isAssumedToCauseUB() const { return getAssumed(); }
2968
2969 /// Return true if "undefined behavior" is assumed for a specific instruction.
2970 virtual bool isAssumedToCauseUB(Instruction *I) const = 0;
2971
2972 /// Return true if "undefined behavior" is known.
2973 bool isKnownToCauseUB() const { return getKnown(); }
2974
2975 /// Return true if "undefined behavior" is known for a specific instruction.
2976 virtual bool isKnownToCauseUB(Instruction *I) const = 0;
2977
2978 /// Create an abstract attribute view for the position \p IRP.
2979 static AAUndefinedBehavior &createForPosition(const IRPosition &IRP,
2980 Attributor &A);
2981
2982 /// See AbstractAttribute::getName()
2983 const std::string getName() const override { return "AAUndefinedBehavior"; }
2984
2985 /// See AbstractAttribute::getIdAddr()
2986 const char *getIdAddr() const override { return &ID; }
2987
2988 /// This function should return true if the type of the \p AA is
2989 /// AAUndefineBehavior
2990 static bool classof(const AbstractAttribute *AA) {
2991 return (AA->getIdAddr() == &ID);
2992 }
2993
2994 /// Unique ID (due to the unique address)
2995 static const char ID;
2996};
2997
2998/// An abstract interface to determine reachability of point A to B.
2999struct AAReachability : public StateWrapper<BooleanState, AbstractAttribute> {
3000 using Base = StateWrapper<BooleanState, AbstractAttribute>;
3001 AAReachability(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3002
3003 /// Returns true if 'From' instruction is assumed to reach, 'To' instruction.
3004 /// Users should provide two positions they are interested in, and the class
3005 /// determines (and caches) reachability.
3006 bool isAssumedReachable(Attributor &A, const Instruction &From,
3007 const Instruction &To) const {
3008 if (!getState().isValidState())
3009 return true;
3010 return A.getInfoCache().getPotentiallyReachable(From, To);
3011 }
3012
3013 /// Returns true if 'From' instruction is known to reach, 'To' instruction.
3014 /// Users should provide two positions they are interested in, and the class
3015 /// determines (and caches) reachability.
3016 bool isKnownReachable(Attributor &A, const Instruction &From,
3017 const Instruction &To) const {
3018 if (!getState().isValidState())
3019 return false;
3020 return A.getInfoCache().getPotentiallyReachable(From, To);
3021 }
3022
3023 /// Create an abstract attribute view for the position \p IRP.
3024 static AAReachability &createForPosition(const IRPosition &IRP,
3025 Attributor &A);
3026
3027 /// See AbstractAttribute::getName()
3028 const std::string getName() const override { return "AAReachability"; }
3029
3030 /// See AbstractAttribute::getIdAddr()
3031 const char *getIdAddr() const override { return &ID; }
3032
3033 /// This function should return true if the type of the \p AA is
3034 /// AAReachability
3035 static bool classof(const AbstractAttribute *AA) {
3036 return (AA->getIdAddr() == &ID);
3037 }
3038
3039 /// Unique ID (due to the unique address)
3040 static const char ID;
3041};
3042
3043/// An abstract interface for all noalias attributes.
3044struct AANoAlias
3045 : public IRAttribute<Attribute::NoAlias,
3046 StateWrapper<BooleanState, AbstractAttribute>> {
3047 AANoAlias(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3048
3049 /// Return true if we assume that the underlying value is alias.
3050 bool isAssumedNoAlias() const { return getAssumed(); }
3051
3052 /// Return true if we know that underlying value is noalias.
3053 bool isKnownNoAlias() const { return getKnown(); }
3054
3055 /// Create an abstract attribute view for the position \p IRP.
3056 static AANoAlias &createForPosition(const IRPosition &IRP, Attributor &A);
3057
3058 /// See AbstractAttribute::getName()
3059 const std::string getName() const override { return "AANoAlias"; }
3060
3061 /// See AbstractAttribute::getIdAddr()
3062 const char *getIdAddr() const override { return &ID; }
3063
3064 /// This function should return true if the type of the \p AA is AANoAlias
3065 static bool classof(const AbstractAttribute *AA) {
3066 return (AA->getIdAddr() == &ID);
3067 }
3068
3069 /// Unique ID (due to the unique address)
3070 static const char ID;
3071};
3072
3073/// An AbstractAttribute for nofree.
3074struct AANoFree
3075 : public IRAttribute<Attribute::NoFree,
3076 StateWrapper<BooleanState, AbstractAttribute>> {
3077 AANoFree(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3078
3079 /// Return true if "nofree" is assumed.
3080 bool isAssumedNoFree() const { return getAssumed(); }
3081
3082 /// Return true if "nofree" is known.
3083 bool isKnownNoFree() const { return getKnown(); }
3084
3085 /// Create an abstract attribute view for the position \p IRP.
3086 static AANoFree &createForPosition(const IRPosition &IRP, Attributor &A);
3087
3088 /// See AbstractAttribute::getName()
3089 const std::string getName() const override { return "AANoFree"; }
3090
3091 /// See AbstractAttribute::getIdAddr()
3092 const char *getIdAddr() const override { return &ID; }
3093
3094 /// This function should return true if the type of the \p AA is AANoFree
3095 static bool classof(const AbstractAttribute *AA) {
3096 return (AA->getIdAddr() == &ID);
3097 }
3098
3099 /// Unique ID (due to the unique address)
3100 static const char ID;
3101};
3102
3103/// An AbstractAttribute for noreturn.
3104struct AANoReturn
3105 : public IRAttribute<Attribute::NoReturn,
3106 StateWrapper<BooleanState, AbstractAttribute>> {
3107 AANoReturn(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3108
3109 /// Return true if the underlying object is assumed to never return.
3110 bool isAssumedNoReturn() const { return getAssumed(); }
3111
3112 /// Return true if the underlying object is known to never return.
3113 bool isKnownNoReturn() const { return getKnown(); }
3114
3115 /// Create an abstract attribute view for the position \p IRP.
3116 static AANoReturn &createForPosition(const IRPosition &IRP, Attributor &A);
3117
3118 /// See AbstractAttribute::getName()
3119 const std::string getName() const override { return "AANoReturn"; }
3120
3121 /// See AbstractAttribute::getIdAddr()
3122 const char *getIdAddr() const override { return &ID; }
3123
3124 /// This function should return true if the type of the \p AA is AANoReturn
3125 static bool classof(const AbstractAttribute *AA) {
3126 return (AA->getIdAddr() == &ID);
3127 }
3128
3129 /// Unique ID (due to the unique address)
3130 static const char ID;
3131};
3132
3133/// An abstract interface for liveness abstract attribute.
3134struct AAIsDead
3135 : public StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute> {
3136 using Base = StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute>;
3137 AAIsDead(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3138
3139 /// State encoding bits. A set bit in the state means the property holds.
3140 enum {
3141 HAS_NO_EFFECT = 1 << 0,
3142 IS_REMOVABLE = 1 << 1,
3143
3144 IS_DEAD = HAS_NO_EFFECT | IS_REMOVABLE,
3145 };
3146 static_assert(IS_DEAD == getBestState(), "Unexpected BEST_STATE value");
3147
3148protected:
3149 /// The query functions are protected such that other attributes need to go
3150 /// through the Attributor interfaces: `Attributor::isAssumedDead(...)`
3151
3152 /// Returns true if the underlying value is assumed dead.
3153 virtual bool isAssumedDead() const = 0;
3154
3155 /// Returns true if the underlying value is known dead.
3156 virtual bool isKnownDead() const = 0;
3157
3158 /// Returns true if \p BB is assumed dead.
3159 virtual bool isAssumedDead(const BasicBlock *BB) const = 0;
3160
3161 /// Returns true if \p BB is known dead.
3162 virtual bool isKnownDead(const BasicBlock *BB) const = 0;
3163
3164 /// Returns true if \p I is assumed dead.
3165 virtual bool isAssumedDead(const Instruction *I) const = 0;
3166
3167 /// Returns true if \p I is known dead.
3168 virtual bool isKnownDead(const Instruction *I) const = 0;
3169
3170 /// This method is used to check if at least one instruction in a collection
3171 /// of instructions is live.
3172 template <typename T> bool isLiveInstSet(T begin, T end) const {
3173 for (const auto &I : llvm::make_range(begin, end)) {
3174 assert(I->getFunction() == getIRPosition().getAssociatedFunction() &&((void)0)
3175 "Instruction must be in the same anchor scope function.")((void)0);
3176
3177 if (!isAssumedDead(I))
3178 return true;
3179 }
3180
3181 return false;
3182 }
3183
3184public:
3185 /// Create an abstract attribute view for the position \p IRP.
3186 static AAIsDead &createForPosition(const IRPosition &IRP, Attributor &A);
3187
3188 /// Determine if \p F might catch asynchronous exceptions.
3189 static bool mayCatchAsynchronousExceptions(const Function &F) {
3190 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
3191 }
3192
3193 /// Return if the edge from \p From BB to \p To BB is assumed dead.
3194 /// This is specifically useful in AAReachability.
3195 virtual bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const {
3196 return false;
3197 }
3198
3199 /// See AbstractAttribute::getName()
3200 const std::string getName() const override { return "AAIsDead"; }
3201
3202 /// See AbstractAttribute::getIdAddr()
3203 const char *getIdAddr() const override { return &ID; }
3204
3205 /// This function should return true if the type of the \p AA is AAIsDead
3206 static bool classof(const AbstractAttribute *AA) {
3207 return (AA->getIdAddr() == &ID);
3208 }
3209
3210 /// Unique ID (due to the unique address)
3211 static const char ID;
3212
3213 friend struct Attributor;
3214};
3215
3216/// State for dereferenceable attribute
3217struct DerefState : AbstractState {
3218
3219 static DerefState getBestState() { return DerefState(); }
3220 static DerefState getBestState(const DerefState &) { return getBestState(); }
3221
3222 /// Return the worst possible representable state.
3223 static DerefState getWorstState() {
3224 DerefState DS;
3225 DS.indicatePessimisticFixpoint();
3226 return DS;
3227 }
3228 static DerefState getWorstState(const DerefState &) {
3229 return getWorstState();
3230 }
3231
3232 /// State representing for dereferenceable bytes.
3233 IncIntegerState<> DerefBytesState;
3234
3235 /// Map representing for accessed memory offsets and sizes.
3236 /// A key is Offset and a value is size.
3237 /// If there is a load/store instruction something like,
3238 /// p[offset] = v;
3239 /// (offset, sizeof(v)) will be inserted to this map.
3240 /// std::map is used because we want to iterate keys in ascending order.
3241 std::map<int64_t, uint64_t> AccessedBytesMap;
3242
3243 /// Helper function to calculate dereferenceable bytes from current known
3244 /// bytes and accessed bytes.
3245 ///
3246 /// int f(int *A){
3247 /// *A = 0;
3248 /// *(A+2) = 2;
3249 /// *(A+1) = 1;
3250 /// *(A+10) = 10;
3251 /// }
3252 /// ```
3253 /// In that case, AccessedBytesMap is `{0:4, 4:4, 8:4, 40:4}`.
3254 /// AccessedBytesMap is std::map so it is iterated in accending order on
3255 /// key(Offset). So KnownBytes will be updated like this:
3256 ///
3257 /// |Access | KnownBytes
3258 /// |(0, 4)| 0 -> 4
3259 /// |(4, 4)| 4 -> 8
3260 /// |(8, 4)| 8 -> 12
3261 /// |(40, 4) | 12 (break)
3262 void computeKnownDerefBytesFromAccessedMap() {
3263 int64_t KnownBytes = DerefBytesState.getKnown();
3264 for (auto &Access : AccessedBytesMap) {
3265 if (KnownBytes < Access.first)
3266 break;
3267 KnownBytes = std::max(KnownBytes, Access.first + (int64_t)Access.second);
3268 }
3269
3270 DerefBytesState.takeKnownMaximum(KnownBytes);
3271 }
3272
3273 /// State representing that whether the value is globaly dereferenceable.
3274 BooleanState GlobalState;
3275
3276 /// See AbstractState::isValidState()
3277 bool isValidState() const override { return DerefBytesState.isValidState(); }
3278
3279 /// See AbstractState::isAtFixpoint()
3280 bool isAtFixpoint() const override {
3281 return !isValidState() ||
3282 (DerefBytesState.isAtFixpoint() && GlobalState.isAtFixpoint());
3283 }
3284
3285 /// See AbstractState::indicateOptimisticFixpoint(...)
3286 ChangeStatus indicateOptimisticFixpoint() override {
3287 DerefBytesState.indicateOptimisticFixpoint();
3288 GlobalState.indicateOptimisticFixpoint();
3289 return ChangeStatus::UNCHANGED;
3290 }
3291
3292 /// See AbstractState::indicatePessimisticFixpoint(...)
3293 ChangeStatus indicatePessimisticFixpoint() override {
3294 DerefBytesState.indicatePessimisticFixpoint();
3295 GlobalState.indicatePessimisticFixpoint();
3296 return ChangeStatus::CHANGED;
3297 }
3298
3299 /// Update known dereferenceable bytes.
3300 void takeKnownDerefBytesMaximum(uint64_t Bytes) {
3301 DerefBytesState.takeKnownMaximum(Bytes);
3302
3303 // Known bytes might increase.
3304 computeKnownDerefBytesFromAccessedMap();
3305 }
3306
3307 /// Update assumed dereferenceable bytes.
3308 void takeAssumedDerefBytesMinimum(uint64_t Bytes) {
3309 DerefBytesState.takeAssumedMinimum(Bytes);
3310 }
3311
3312 /// Add accessed bytes to the map.
3313 void addAccessedBytes(int64_t Offset, uint64_t Size) {
3314 uint64_t &AccessedBytes = AccessedBytesMap[Offset];
3315 AccessedBytes = std::max(AccessedBytes, Size);
3316
3317 // Known bytes might increase.
3318 computeKnownDerefBytesFromAccessedMap();
3319 }
3320
3321 /// Equality for DerefState.
3322 bool operator==(const DerefState &R) const {
3323 return this->DerefBytesState == R.DerefBytesState &&
3324 this->GlobalState == R.GlobalState;
3325 }
3326
3327 /// Inequality for DerefState.
3328 bool operator!=(const DerefState &R) const { return !(*this == R); }
3329
3330 /// See IntegerStateBase::operator^=
3331 DerefState operator^=(const DerefState &R) {
3332 DerefBytesState ^= R.DerefBytesState;
3333 GlobalState ^= R.GlobalState;
3334 return *this;
3335 }
3336
3337 /// See IntegerStateBase::operator+=
3338 DerefState operator+=(const DerefState &R) {
3339 DerefBytesState += R.DerefBytesState;
3340 GlobalState += R.GlobalState;
3341 return *this;
3342 }
3343
3344 /// See IntegerStateBase::operator&=
3345 DerefState operator&=(const DerefState &R) {
3346 DerefBytesState &= R.DerefBytesState;
3347 GlobalState &= R.GlobalState;
3348 return *this;
3349 }
3350
3351 /// See IntegerStateBase::operator|=
3352 DerefState operator|=(const DerefState &R) {
3353 DerefBytesState |= R.DerefBytesState;
3354 GlobalState |= R.GlobalState;
3355 return *this;
3356 }
3357
3358protected:
3359 const AANonNull *NonNullAA = nullptr;
3360};
3361
3362/// An abstract interface for all dereferenceable attribute.
3363struct AADereferenceable
3364 : public IRAttribute<Attribute::Dereferenceable,
3365 StateWrapper<DerefState, AbstractAttribute>> {
3366 AADereferenceable(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3367
3368 /// Return true if we assume that the underlying value is nonnull.
3369 bool isAssumedNonNull() const {
3370 return NonNullAA && NonNullAA->isAssumedNonNull();
3371 }
3372
3373 /// Return true if we know that the underlying value is nonnull.
3374 bool isKnownNonNull() const {
3375 return NonNullAA && NonNullAA->isKnownNonNull();
3376 }
3377
3378 /// Return true if we assume that underlying value is
3379 /// dereferenceable(_or_null) globally.
3380 bool isAssumedGlobal() const { return GlobalState.getAssumed(); }
3381
3382 /// Return true if we know that underlying value is
3383 /// dereferenceable(_or_null) globally.
3384 bool isKnownGlobal() const { return GlobalState.getKnown(); }
3385
3386 /// Return assumed dereferenceable bytes.
3387 uint32_t getAssumedDereferenceableBytes() const {
3388 return DerefBytesState.getAssumed();
3389 }
3390
3391 /// Return known dereferenceable bytes.
3392 uint32_t getKnownDereferenceableBytes() const {
3393 return DerefBytesState.getKnown();
3394 }
3395
3396 /// Create an abstract attribute view for the position \p IRP.
3397 static AADereferenceable &createForPosition(const IRPosition &IRP,
3398 Attributor &A);
3399
3400 /// See AbstractAttribute::getName()
3401 const std::string getName() const override { return "AADereferenceable"; }
3402
3403 /// See AbstractAttribute::getIdAddr()
3404 const char *getIdAddr() const override { return &ID; }
3405
3406 /// This function should return true if the type of the \p AA is
3407 /// AADereferenceable
3408 static bool classof(const AbstractAttribute *AA) {
3409 return (AA->getIdAddr() == &ID);
3410 }
3411
3412 /// Unique ID (due to the unique address)
3413 static const char ID;
3414};
3415
3416using AAAlignmentStateType =
3417 IncIntegerState<uint32_t, Value::MaximumAlignment, 1>;
3418/// An abstract interface for all align attributes.
3419struct AAAlign : public IRAttribute<
3420 Attribute::Alignment,
3421 StateWrapper<AAAlignmentStateType, AbstractAttribute>> {
3422 AAAlign(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3423
3424 /// Return assumed alignment.
3425 unsigned getAssumedAlign() const { return getAssumed(); }
3426
3427 /// Return known alignment.
3428 unsigned getKnownAlign() const { return getKnown(); }
3429
3430 /// See AbstractAttribute::getName()
3431 const std::string getName() const override { return "AAAlign"; }
3432
3433 /// See AbstractAttribute::getIdAddr()
3434 const char *getIdAddr() const override { return &ID; }
3435
3436 /// This function should return true if the type of the \p AA is AAAlign
3437 static bool classof(const AbstractAttribute *AA) {
3438 return (AA->getIdAddr() == &ID);
3439 }
3440
3441 /// Create an abstract attribute view for the position \p IRP.
3442 static AAAlign &createForPosition(const IRPosition &IRP, Attributor &A);
3443
3444 /// Unique ID (due to the unique address)
3445 static const char ID;
3446};
3447
3448/// An abstract interface for all nocapture attributes.
3449struct AANoCapture
3450 : public IRAttribute<
3451 Attribute::NoCapture,
3452 StateWrapper<BitIntegerState<uint16_t, 7, 0>, AbstractAttribute>> {
3453 AANoCapture(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3454
3455 /// State encoding bits. A set bit in the state means the property holds.
3456 /// NO_CAPTURE is the best possible state, 0 the worst possible state.
3457 enum {
3458 NOT_CAPTURED_IN_MEM = 1 << 0,
3459 NOT_CAPTURED_IN_INT = 1 << 1,
3460 NOT_CAPTURED_IN_RET = 1 << 2,
3461
3462 /// If we do not capture the value in memory or through integers we can only
3463 /// communicate it back as a derived pointer.
3464 NO_CAPTURE_MAYBE_RETURNED = NOT_CAPTURED_IN_MEM | NOT_CAPTURED_IN_INT,
3465
3466 /// If we do not capture the value in memory, through integers, or as a
3467 /// derived pointer we know it is not captured.
3468 NO_CAPTURE =
3469 NOT_CAPTURED_IN_MEM | NOT_CAPTURED_IN_INT | NOT_CAPTURED_IN_RET,
3470 };
3471
3472 /// Return true if we know that the underlying value is not captured in its
3473 /// respective scope.
3474 bool isKnownNoCapture() const { return isKnown(NO_CAPTURE); }
3475
3476 /// Return true if we assume that the underlying value is not captured in its
3477 /// respective scope.
3478 bool isAssumedNoCapture() const { return isAssumed(NO_CAPTURE); }
3479
3480 /// Return true if we know that the underlying value is not captured in its
3481 /// respective scope but we allow it to escape through a "return".
3482 bool isKnownNoCaptureMaybeReturned() const {
3483 return isKnown(NO_CAPTURE_MAYBE_RETURNED);
3484 }
3485
3486 /// Return true if we assume that the underlying value is not captured in its
3487 /// respective scope but we allow it to escape through a "return".
3488 bool isAssumedNoCaptureMaybeReturned() const {
3489 return isAssumed(NO_CAPTURE_MAYBE_RETURNED);
3490 }
3491
3492 /// Create an abstract attribute view for the position \p IRP.
3493 static AANoCapture &createForPosition(const IRPosition &IRP, Attributor &A);
3494
3495 /// See AbstractAttribute::getName()
3496 const std::string getName() const override { return "AANoCapture"; }
3497
3498 /// See AbstractAttribute::getIdAddr()
3499 const char *getIdAddr() const override { return &ID; }
3500
3501 /// This function should return true if the type of the \p AA is AANoCapture
3502 static bool classof(const AbstractAttribute *AA) {
3503 return (AA->getIdAddr() == &ID);
3504 }
3505
3506 /// Unique ID (due to the unique address)
3507 static const char ID;
3508};
3509
3510struct ValueSimplifyStateType : public AbstractState {
3511
3512 ValueSimplifyStateType(Type *Ty) : Ty(Ty) {}
3513
3514 static ValueSimplifyStateType getBestState(Type *Ty) {
3515 return ValueSimplifyStateType(Ty);
3516 }
3517 static ValueSimplifyStateType getBestState(const ValueSimplifyStateType &VS) {
3518 return getBestState(VS.Ty);
3519 }
3520
3521 /// Return the worst possible representable state.
3522 static ValueSimplifyStateType getWorstState(Type *Ty) {
3523 ValueSimplifyStateType DS(Ty);
3524 DS.indicatePessimisticFixpoint();
3525 return DS;
3526 }
3527 static ValueSimplifyStateType
3528 getWorstState(const ValueSimplifyStateType &VS) {
3529 return getWorstState(VS.Ty);
3530 }
3531
3532 /// See AbstractState::isValidState(...)
3533 bool isValidState() const override { return BS.isValidState(); }
3534
3535 /// See AbstractState::isAtFixpoint(...)
3536 bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
3537
3538 /// Return the assumed state encoding.
3539 ValueSimplifyStateType getAssumed() { return *this; }
3540 const ValueSimplifyStateType &getAssumed() const { return *this; }
3541
3542 /// See AbstractState::indicatePessimisticFixpoint(...)
3543 ChangeStatus indicatePessimisticFixpoint() override {
3544 return BS.indicatePessimisticFixpoint();
3545 }
3546
3547 /// See AbstractState::indicateOptimisticFixpoint(...)
3548 ChangeStatus indicateOptimisticFixpoint() override {
3549 return BS.indicateOptimisticFixpoint();
3550 }
3551
3552 /// "Clamp" this state with \p PVS.
3553 ValueSimplifyStateType operator^=(const ValueSimplifyStateType &VS) {
3554 BS ^= VS.BS;
3555 unionAssumed(VS.SimplifiedAssociatedValue);
3556 return *this;
3557 }
3558
3559 bool operator==(const ValueSimplifyStateType &RHS) const {
3560 if (isValidState() != RHS.isValidState())
3561 return false;
3562 if (!isValidState() && !RHS.isValidState())
3563 return true;
3564 return SimplifiedAssociatedValue == RHS.SimplifiedAssociatedValue;
3565 }
3566
3567protected:
3568 /// The type of the original value.
3569 Type *Ty;
3570
3571 /// Merge \p Other into the currently assumed simplified value
3572 bool unionAssumed(Optional<Value *> Other);
3573
3574 /// Helper to track validity and fixpoint
3575 BooleanState BS;
3576
3577 /// An assumed simplified value. Initially, it is set to Optional::None, which
3578 /// means that the value is not clear under current assumption. If in the
3579 /// pessimistic state, getAssumedSimplifiedValue doesn't return this value but
3580 /// returns orignal associated value.
3581 Optional<Value *> SimplifiedAssociatedValue;
3582};
3583
3584/// An abstract interface for value simplify abstract attribute.
3585struct AAValueSimplify
3586 : public StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *> {
3587 using Base = StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *>;
3588 AAValueSimplify(const IRPosition &IRP, Attributor &A)
3589 : Base(IRP, IRP.getAssociatedType()) {}
3590
3591 /// Create an abstract attribute view for the position \p IRP.
3592 static AAValueSimplify &createForPosition(const IRPosition &IRP,
3593 Attributor &A);
3594
3595 /// See AbstractAttribute::getName()
3596 const std::string getName() const override { return "AAValueSimplify"; }
3597
3598 /// See AbstractAttribute::getIdAddr()
3599 const char *getIdAddr() const override { return &ID; }
3600
3601 /// This function should return true if the type of the \p AA is
3602 /// AAValueSimplify
3603 static bool classof(const AbstractAttribute *AA) {
3604 return (AA->getIdAddr() == &ID);
3605 }
3606
3607 /// Unique ID (due to the unique address)
3608 static const char ID;
3609
3610private:
3611 /// Return an assumed simplified value if a single candidate is found. If
3612 /// there cannot be one, return original value. If it is not clear yet, return
3613 /// the Optional::NoneType.
3614 ///
3615 /// Use `Attributor::getAssumedSimplified` for value simplification.
3616 virtual Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const = 0;
3617
3618 friend struct Attributor;
3619};
3620
3621struct AAHeapToStack : public StateWrapper<BooleanState, AbstractAttribute> {
3622 using Base = StateWrapper<BooleanState, AbstractAttribute>;
3623 AAHeapToStack(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3624
3625 /// Returns true if HeapToStack conversion is assumed to be possible.
3626 virtual bool isAssumedHeapToStack(const CallBase &CB) const = 0;
3627
3628 /// Returns true if HeapToStack conversion is assumed and the CB is a
3629 /// callsite to a free operation to be removed.
3630 virtual bool isAssumedHeapToStackRemovedFree(CallBase &CB) const = 0;
3631
3632 /// Create an abstract attribute view for the position \p IRP.
3633 static AAHeapToStack &createForPosition(const IRPosition &IRP, Attributor &A);
3634
3635 /// See AbstractAttribute::getName()
3636 const std::string getName() const override { return "AAHeapToStack"; }
3637
3638 /// See AbstractAttribute::getIdAddr()
3639 const char *getIdAddr() const override { return &ID; }
3640
3641 /// This function should return true if the type of the \p AA is AAHeapToStack
3642 static bool classof(const AbstractAttribute *AA) {
3643 return (AA->getIdAddr() == &ID);
3644 }
3645
3646 /// Unique ID (due to the unique address)
3647 static const char ID;
3648};
3649
3650/// An abstract interface for privatizability.
3651///
3652/// A pointer is privatizable if it can be replaced by a new, private one.
3653/// Privatizing pointer reduces the use count, interaction between unrelated
3654/// code parts.
3655///
3656/// In order for a pointer to be privatizable its value cannot be observed
3657/// (=nocapture), it is (for now) not written (=readonly & noalias), we know
3658/// what values are necessary to make the private copy look like the original
3659/// one, and the values we need can be loaded (=dereferenceable).
3660struct AAPrivatizablePtr
3661 : public StateWrapper<BooleanState, AbstractAttribute> {
3662 using Base = StateWrapper<BooleanState, AbstractAttribute>;
3663 AAPrivatizablePtr(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3664
3665 /// Returns true if pointer privatization is assumed to be possible.
3666 bool isAssumedPrivatizablePtr() const { return getAssumed(); }
3667
3668 /// Returns true if pointer privatization is known to be possible.
3669 bool isKnownPrivatizablePtr() const { return getKnown(); }
3670
3671 /// Return the type we can choose for a private copy of the underlying
3672 /// value. None means it is not clear yet, nullptr means there is none.
3673 virtual Optional<Type *> getPrivatizableType() const = 0;
3674
3675 /// Create an abstract attribute view for the position \p IRP.
3676 static AAPrivatizablePtr &createForPosition(const IRPosition &IRP,
3677 Attributor &A);
3678
3679 /// See AbstractAttribute::getName()
3680 const std::string getName() const override { return "AAPrivatizablePtr"; }
3681
3682 /// See AbstractAttribute::getIdAddr()
3683 const char *getIdAddr() const override { return &ID; }
3684
3685 /// This function should return true if the type of the \p AA is
3686 /// AAPricatizablePtr
3687 static bool classof(const AbstractAttribute *AA) {
3688 return (AA->getIdAddr() == &ID);
3689 }
3690
3691 /// Unique ID (due to the unique address)
3692 static const char ID;
3693};
3694
3695/// An abstract interface for memory access kind related attributes
3696/// (readnone/readonly/writeonly).
3697struct AAMemoryBehavior
3698 : public IRAttribute<
3699 Attribute::ReadNone,
3700 StateWrapper<BitIntegerState<uint8_t, 3>, AbstractAttribute>> {
3701 AAMemoryBehavior(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3702
3703 /// State encoding bits. A set bit in the state means the property holds.
3704 /// BEST_STATE is the best possible state, 0 the worst possible state.
3705 enum {
3706 NO_READS = 1 << 0,
3707 NO_WRITES = 1 << 1,
3708 NO_ACCESSES = NO_READS | NO_WRITES,
3709
3710 BEST_STATE = NO_ACCESSES,
3711 };
3712 static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
3713
3714 /// Return true if we know that the underlying value is not read or accessed
3715 /// in its respective scope.
3716 bool isKnownReadNone() const { return isKnown(NO_ACCESSES); }
3717
3718 /// Return true if we assume that the underlying value is not read or accessed
3719 /// in its respective scope.
3720 bool isAssumedReadNone() const { return isAssumed(NO_ACCESSES); }
3721
3722 /// Return true if we know that the underlying value is not accessed
3723 /// (=written) in its respective scope.
3724 bool isKnownReadOnly() const { return isKnown(NO_WRITES); }
3725
3726 /// Return true if we assume that the underlying value is not accessed
3727 /// (=written) in its respective scope.
3728 bool isAssumedReadOnly() const { return isAssumed(NO_WRITES); }
3729
3730 /// Return true if we know that the underlying value is not read in its
3731 /// respective scope.
3732 bool isKnownWriteOnly() const { return isKnown(NO_READS); }
3733
3734 /// Return true if we assume that the underlying value is not read in its
3735 /// respective scope.
3736 bool isAssumedWriteOnly() const { return isAssumed(NO_READS); }
3737
3738 /// Create an abstract attribute view for the position \p IRP.
3739 static AAMemoryBehavior &createForPosition(const IRPosition &IRP,
3740 Attributor &A);
3741
3742 /// See AbstractAttribute::getName()
3743 const std::string getName() const override { return "AAMemoryBehavior"; }
3744
3745 /// See AbstractAttribute::getIdAddr()
3746 const char *getIdAddr() const override { return &ID; }
3747
3748 /// This function should return true if the type of the \p AA is
3749 /// AAMemoryBehavior
3750 static bool classof(const AbstractAttribute *AA) {
3751 return (AA->getIdAddr() == &ID);
3752 }
3753
3754 /// Unique ID (due to the unique address)
3755 static const char ID;
3756};
3757
3758/// An abstract interface for all memory location attributes
3759/// (readnone/argmemonly/inaccessiblememonly/inaccessibleorargmemonly).
3760struct AAMemoryLocation
3761 : public IRAttribute<
3762 Attribute::ReadNone,
3763 StateWrapper<BitIntegerState<uint32_t, 511>, AbstractAttribute>> {
3764 using MemoryLocationsKind = StateType::base_t;
3765
3766 AAMemoryLocation(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
3767
3768 /// Encoding of different locations that could be accessed by a memory
3769 /// access.
3770 enum {
3771 ALL_LOCATIONS = 0,
3772 NO_LOCAL_MEM = 1 << 0,
3773 NO_CONST_MEM = 1 << 1,
3774 NO_GLOBAL_INTERNAL_MEM = 1 << 2,
3775 NO_GLOBAL_EXTERNAL_MEM = 1 << 3,
3776 NO_GLOBAL_MEM = NO_GLOBAL_INTERNAL_MEM | NO_GLOBAL_EXTERNAL_MEM,
3777 NO_ARGUMENT_MEM = 1 << 4,
3778 NO_INACCESSIBLE_MEM = 1 << 5,
3779 NO_MALLOCED_MEM = 1 << 6,
3780 NO_UNKOWN_MEM = 1 << 7,
3781 NO_LOCATIONS = NO_LOCAL_MEM | NO_CONST_MEM | NO_GLOBAL_INTERNAL_MEM |
3782 NO_GLOBAL_EXTERNAL_MEM | NO_ARGUMENT_MEM |
3783 NO_INACCESSIBLE_MEM | NO_MALLOCED_MEM | NO_UNKOWN_MEM,
3784
3785 // Helper bit to track if we gave up or not.
3786 VALID_STATE = NO_LOCATIONS + 1,
3787
3788 BEST_STATE = NO_LOCATIONS | VALID_STATE,
3789 };
3790 static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
3791
3792 /// Return true if we know that the associated functions has no observable
3793 /// accesses.
3794 bool isKnownReadNone() const { return isKnown(NO_LOCATIONS); }
3795
3796 /// Return true if we assume that the associated functions has no observable
3797 /// accesses.
3798 bool isAssumedReadNone() const {
3799 return isAssumed(NO_LOCATIONS) | isAssumedStackOnly();
3800 }
3801
3802 /// Return true if we know that the associated functions has at most
3803 /// local/stack accesses.
3804 bool isKnowStackOnly() const {
3805 return isKnown(inverseLocation(NO_LOCAL_MEM, true, true));
3806 }
3807
3808 /// Return true if we assume that the associated functions has at most
3809 /// local/stack accesses.
3810 bool isAssumedStackOnly() const {
3811 return isAssumed(inverseLocation(NO_LOCAL_MEM, true, true));
3812 }
3813
3814 /// Return true if we know that the underlying value will only access
3815 /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
3816 bool isKnownInaccessibleMemOnly() const {
3817 return isKnown(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
3818 }
3819
3820 /// Return true if we assume that the underlying value will only access
3821 /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
3822 bool isAssumedInaccessibleMemOnly() const {
3823 return isAssumed(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
3824 }
3825
3826 /// Return true if we know that the underlying value will only access
3827 /// argument pointees (see Attribute::ArgMemOnly).
3828 bool isKnownArgMemOnly() const {
3829 return isKnown(inverseLocation(NO_ARGUMENT_MEM, true, true));
3830 }
3831
3832 /// Return true if we assume that the underlying value will only access
3833 /// argument pointees (see Attribute::ArgMemOnly).
3834 bool isAssumedArgMemOnly() const {
3835 return isAssumed(inverseLocation(NO_ARGUMENT_MEM, true, true));
3836 }
3837
3838 /// Return true if we know that the underlying value will only access
3839 /// inaccesible memory or argument pointees (see
3840 /// Attribute::InaccessibleOrArgMemOnly).
3841 bool isKnownInaccessibleOrArgMemOnly() const {
3842 return isKnown(
3843 inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
3844 }
3845
3846 /// Return true if we assume that the underlying value will only access
3847 /// inaccesible memory or argument pointees (see
3848 /// Attribute::InaccessibleOrArgMemOnly).
3849 bool isAssumedInaccessibleOrArgMemOnly() const {
3850 return isAssumed(
3851 inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
3852 }
3853
3854 /// Return true if the underlying value may access memory through arguement
3855 /// pointers of the associated function, if any.
3856 bool mayAccessArgMem() const { return !isAssumed(NO_ARGUMENT_MEM); }
3857
3858 /// Return true if only the memory locations specififed by \p MLK are assumed
3859 /// to be accessed by the associated function.
3860 bool isAssumedSpecifiedMemOnly(MemoryLocationsKind MLK) const {
3861 return isAssumed(MLK);
3862 }
3863
3864 /// Return the locations that are assumed to be not accessed by the associated
3865 /// function, if any.
3866 MemoryLocationsKind getAssumedNotAccessedLocation() const {
3867 return getAssumed();
3868 }
3869
3870 /// Return the inverse of location \p Loc, thus for NO_XXX the return
3871 /// describes ONLY_XXX. The flags \p AndLocalMem and \p AndConstMem determine
3872 /// if local (=stack) and constant memory are allowed as well. Most of the
3873 /// time we do want them to be included, e.g., argmemonly allows accesses via
3874 /// argument pointers or local or constant memory accesses.
3875 static MemoryLocationsKind
3876 inverseLocation(MemoryLocationsKind Loc, bool AndLocalMem, bool AndConstMem) {
3877 return NO_LOCATIONS & ~(Loc | (AndLocalMem ? NO_LOCAL_MEM : 0) |
3878 (AndConstMem ? NO_CONST_MEM : 0));
3879 };
3880
3881 /// Return the locations encoded by \p MLK as a readable string.
3882 static std::string getMemoryLocationsAsStr(MemoryLocationsKind MLK);
3883
3884 /// Simple enum to distinguish read/write/read-write accesses.
3885 enum AccessKind {
3886 NONE = 0,
3887 READ = 1 << 0,
3888 WRITE = 1 << 1,
3889 READ_WRITE = READ | WRITE,
3890 };
3891
3892 /// Check \p Pred on all accesses to the memory kinds specified by \p MLK.
3893 ///
3894 /// This method will evaluate \p Pred on all accesses (access instruction +
3895 /// underlying accessed memory pointer) and it will return true if \p Pred
3896 /// holds every time.
3897 virtual bool checkForAllAccessesToMemoryKind(
3898 function_ref<bool(const Instruction *, const Value *, AccessKind,
3899 MemoryLocationsKind)>
3900 Pred,
3901 MemoryLocationsKind MLK) const = 0;
3902
3903 /// Create an abstract attribute view for the position \p IRP.
3904 static AAMemoryLocation &createForPosition(const IRPosition &IRP,
3905 Attributor &A);
3906
3907 /// See AbstractState::getAsStr().
3908 const std::string getAsStr() const override {
3909 return getMemoryLocationsAsStr(getAssumedNotAccessedLocation());
3910 }
3911
3912 /// See AbstractAttribute::getName()
3913 const std::string getName() const override { return "AAMemoryLocation"; }
3914
3915 /// See AbstractAttribute::getIdAddr()
3916 const char *getIdAddr() const override { return &ID; }
3917
3918 /// This function should return true if the type of the \p AA is
3919 /// AAMemoryLocation
3920 static bool classof(const AbstractAttribute *AA) {
3921 return (AA->getIdAddr() == &ID);
3922 }
3923
3924 /// Unique ID (due to the unique address)
3925 static const char ID;
3926};
3927
3928/// An abstract interface for range value analysis.
3929struct AAValueConstantRange
3930 : public StateWrapper<IntegerRangeState, AbstractAttribute, uint32_t> {
3931 using Base = StateWrapper<IntegerRangeState, AbstractAttribute, uint32_t>;
3932 AAValueConstantRange(const IRPosition &IRP, Attributor &A)
3933 : Base(IRP, IRP.getAssociatedType()->getIntegerBitWidth()) {}
3934
3935 /// See AbstractAttribute::getState(...).
3936 IntegerRangeState &getState() override { return *this; }
3937 const IntegerRangeState &getState() const override { return *this; }
3938
3939 /// Create an abstract attribute view for the position \p IRP.
3940 static AAValueConstantRange &createForPosition(const IRPosition &IRP,
3941 Attributor &A);
3942
3943 /// Return an assumed range for the assocaited value a program point \p CtxI.
3944 /// If \p I is nullptr, simply return an assumed range.
3945 virtual ConstantRange
3946 getAssumedConstantRange(Attributor &A,
3947 const Instruction *CtxI = nullptr) const = 0;
3948
3949 /// Return a known range for the assocaited value at a program point \p CtxI.
3950 /// If \p I is nullptr, simply return a known range.
3951 virtual ConstantRange
3952 getKnownConstantRange(Attributor &A,
3953 const Instruction *CtxI = nullptr) const = 0;
3954
3955 /// Return an assumed constant for the assocaited value a program point \p
3956 /// CtxI.
3957 Optional<ConstantInt *>
3958 getAssumedConstantInt(Attributor &A,
3959 const Instruction *CtxI = nullptr) const {
3960 ConstantRange RangeV = getAssumedConstantRange(A, CtxI);
3961 if (auto *C = RangeV.getSingleElement())
3962 return cast<ConstantInt>(
3963 ConstantInt::get(getAssociatedValue().getType(), *C));
3964 if (RangeV.isEmptySet())
3965 return llvm::None;
3966 return nullptr;
3967 }
3968
3969 /// See AbstractAttribute::getName()
3970 const std::string getName() const override { return "AAValueConstantRange"; }
3971
3972 /// See AbstractAttribute::getIdAddr()
3973 const char *getIdAddr() const override { return &ID; }
3974
3975 /// This function should return true if the type of the \p AA is
3976 /// AAValueConstantRange
3977 static bool classof(const AbstractAttribute *AA) {
3978 return (AA->getIdAddr() == &ID);
3979 }
3980
3981 /// Unique ID (due to the unique address)
3982 static const char ID;
3983};
3984
3985/// A class for a set state.
3986/// The assumed boolean state indicates whether the corresponding set is full
3987/// set or not. If the assumed state is false, this is the worst state. The
3988/// worst state (invalid state) of set of potential values is when the set
3989/// contains every possible value (i.e. we cannot in any way limit the value
3990/// that the target position can take). That never happens naturally, we only
3991/// force it. As for the conditions under which we force it, see
3992/// AAPotentialValues.
3993template <typename MemberTy, typename KeyInfo = DenseMapInfo<MemberTy>>
3994struct PotentialValuesState : AbstractState {
3995 using SetTy = DenseSet<MemberTy, KeyInfo>;
3996
3997 PotentialValuesState() : IsValidState(true), UndefIsContained(false) {}
3998
3999 PotentialValuesState(bool IsValid)
4000 : IsValidState(IsValid), UndefIsContained(false) {}
4001
4002 /// See AbstractState::isValidState(...)
4003 bool isValidState() const override { return IsValidState.isValidState(); }
4004
4005 /// See AbstractState::isAtFixpoint(...)
4006 bool isAtFixpoint() const override { return IsValidState.isAtFixpoint(); }
4007
4008 /// See AbstractState::indicatePessimisticFixpoint(...)
4009 ChangeStatus indicatePessimisticFixpoint() override {
4010 return IsValidState.indicatePessimisticFixpoint();
4011 }
4012
4013 /// See AbstractState::indicateOptimisticFixpoint(...)
4014 ChangeStatus indicateOptimisticFixpoint() override {
4015 return IsValidState.indicateOptimisticFixpoint();
4016 }
4017
4018 /// Return the assumed state
4019 PotentialValuesState &getAssumed() { return *this; }
4020 const PotentialValuesState &getAssumed() const { return *this; }
4021
4022 /// Return this set. We should check whether this set is valid or not by
4023 /// isValidState() before calling this function.
4024 const SetTy &getAssumedSet() const {
4025 assert(isValidState() && "This set shoud not be used when it is invalid!")((void)0);
4026 return Set;
4027 }
4028
4029 /// Returns whether this state contains an undef value or not.
4030 bool undefIsContained() const {
4031 assert(isValidState() && "This flag shoud not be used when it is invalid!")((void)0);
4032 return UndefIsContained;
4033 }
4034
4035 bool operator==(const PotentialValuesState &RHS) const {
4036 if (isValidState() != RHS.isValidState())
4037 return false;
4038 if (!isValidState() && !RHS.isValidState())
4039 return true;
4040 if (undefIsContained() != RHS.undefIsContained())
4041 return false;
4042 return Set == RHS.getAssumedSet();
4043 }
4044
4045 /// Maximum number of potential values to be tracked.
4046 /// This is set by -attributor-max-potential-values command line option
4047 static unsigned MaxPotentialValues;
4048
4049 /// Return empty set as the best state of potential values.
4050 static PotentialValuesState getBestState() {
4051 return PotentialValuesState(true);
4052 }
4053
4054 static PotentialValuesState getBestState(PotentialValuesState &PVS) {
4055 return getBestState();
4056 }
4057
4058 /// Return full set as the worst state of potential values.
4059 static PotentialValuesState getWorstState() {
4060 return PotentialValuesState(false);
4061 }
4062
4063 /// Union assumed set with the passed value.
4064 void unionAssumed(const MemberTy &C) { insert(C); }
4065
4066 /// Union assumed set with assumed set of the passed state \p PVS.
4067 void unionAssumed(const PotentialValuesState &PVS) { unionWith(PVS); }
4068
4069 /// Union assumed set with an undef value.
4070 void unionAssumedWithUndef() { unionWithUndef(); }
4071
4072 /// "Clamp" this state with \p PVS.
4073 PotentialValuesState operator^=(const PotentialValuesState &PVS) {
4074 IsValidState ^= PVS.IsValidState;
4075 unionAssumed(PVS);
4076 return *this;
4077 }
4078
4079 PotentialValuesState operator&=(const PotentialValuesState &PVS) {
4080 IsValidState &= PVS.IsValidState;
4081 unionAssumed(PVS);
4082 return *this;
4083 }
4084
4085private:
4086 /// Check the size of this set, and invalidate when the size is no
4087 /// less than \p MaxPotentialValues threshold.
4088 void checkAndInvalidate() {
4089 if (Set.size() >= MaxPotentialValues)
4090 indicatePessimisticFixpoint();
4091 else
4092 reduceUndefValue();
4093 }
4094
4095 /// If this state contains both undef and not undef, we can reduce
4096 /// undef to the not undef value.
4097 void reduceUndefValue() { UndefIsContained = UndefIsContained & Set.empty(); }
4098
4099 /// Insert an element into this set.
4100 void insert(const MemberTy &C) {
4101 if (!isValidState())
4102 return;
4103 Set.insert(C);
4104 checkAndInvalidate();
4105 }
4106
4107 /// Take union with R.
4108 void unionWith(const PotentialValuesState &R) {
4109 /// If this is a full set, do nothing.
4110 if (!isValidState())
4111 return;
4112 /// If R is full set, change L to a full set.
4113 if (!R.isValidState()) {
4114 indicatePessimisticFixpoint();
4115 return;
4116 }
4117 for (const MemberTy &C : R.Set)
4118 Set.insert(C);
4119 UndefIsContained |= R.undefIsContained();
4120 checkAndInvalidate();
4121 }
4122
4123 /// Take union with an undef value.
4124 void unionWithUndef() {
4125 UndefIsContained = true;
4126 reduceUndefValue();
4127 }
4128
4129 /// Take intersection with R.
4130 void intersectWith(const PotentialValuesState &R) {
4131 /// If R is a full set, do nothing.
4132 if (!R.isValidState())
4133 return;
4134 /// If this is a full set, change this to R.
4135 if (!isValidState()) {
4136 *this = R;
4137 return;
4138 }
4139 SetTy IntersectSet;
4140 for (const MemberTy &C : Set) {
4141 if (R.Set.count(C))
4142 IntersectSet.insert(C);
4143 }
4144 Set = IntersectSet;
4145 UndefIsContained &= R.undefIsContained();
4146 reduceUndefValue();
4147 }
4148
4149 /// A helper state which indicate whether this state is valid or not.
4150 BooleanState IsValidState;
4151
4152 /// Container for potential values
4153 SetTy Set;
4154
4155 /// Flag for undef value
4156 bool UndefIsContained;
4157};
4158
4159using PotentialConstantIntValuesState = PotentialValuesState<APInt>;
4160
4161raw_ostream &operator<<(raw_ostream &OS,
4162 const PotentialConstantIntValuesState &R);
4163
4164/// An abstract interface for potential values analysis.
4165///
4166/// This AA collects potential values for each IR position.
4167/// An assumed set of potential values is initialized with the empty set (the
4168/// best state) and it will grow monotonically as we find more potential values
4169/// for this position.
4170/// The set might be forced to the worst state, that is, to contain every
4171/// possible value for this position in 2 cases.
4172/// 1. We surpassed the \p MaxPotentialValues threshold. This includes the
4173/// case that this position is affected (e.g. because of an operation) by a
4174/// Value that is in the worst state.
4175/// 2. We tried to initialize on a Value that we cannot handle (e.g. an
4176/// operator we do not currently handle).
4177///
4178/// TODO: Support values other than constant integers.
4179struct AAPotentialValues
4180 : public StateWrapper<PotentialConstantIntValuesState, AbstractAttribute> {
4181 using Base = StateWrapper<PotentialConstantIntValuesState, AbstractAttribute>;
4182 AAPotentialValues(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4183
4184 /// See AbstractAttribute::getState(...).
4185 PotentialConstantIntValuesState &getState() override { return *this; }
4186 const PotentialConstantIntValuesState &getState() const override {
4187 return *this;
4188 }
4189
4190 /// Create an abstract attribute view for the position \p IRP.
4191 static AAPotentialValues &createForPosition(const IRPosition &IRP,
4192 Attributor &A);
4193
4194 /// Return assumed constant for the associated value
4195 Optional<ConstantInt *>
4196 getAssumedConstantInt(Attributor &A,
4197 const Instruction *CtxI = nullptr) const {
4198 if (!isValidState())
4199 return nullptr;
4200 if (getAssumedSet().size() == 1)
4201 return cast<ConstantInt>(ConstantInt::get(getAssociatedValue().getType(),
4202 *(getAssumedSet().begin())));
4203 if (getAssumedSet().size() == 0) {
4204 if (undefIsContained())
4205 return cast<ConstantInt>(
4206 ConstantInt::get(getAssociatedValue().getType(), 0));
4207 return llvm::None;
4208 }
4209
4210 return nullptr;
4211 }
4212
4213 /// See AbstractAttribute::getName()
4214 const std::string getName() const override { return "AAPotentialValues"; }
4215
4216 /// See AbstractAttribute::getIdAddr()
4217 const char *getIdAddr() const override { return &ID; }
4218
4219 /// This function should return true if the type of the \p AA is
4220 /// AAPotentialValues
4221 static bool classof(const AbstractAttribute *AA) {
4222 return (AA->getIdAddr() == &ID);
4223 }
4224
4225 /// Unique ID (due to the unique address)
4226 static const char ID;
4227};
4228
4229/// An abstract interface for all noundef attributes.
4230struct AANoUndef
4231 : public IRAttribute<Attribute::NoUndef,
4232 StateWrapper<BooleanState, AbstractAttribute>> {
4233 AANoUndef(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
4234
4235 /// Return true if we assume that the underlying value is noundef.
4236 bool isAssumedNoUndef() const { return getAssumed(); }
4237
4238 /// Return true if we know that underlying value is noundef.
4239 bool isKnownNoUndef() const { return getKnown(); }
4240
4241 /// Create an abstract attribute view for the position \p IRP.
4242 static AANoUndef &createForPosition(const IRPosition &IRP, Attributor &A);
4243
4244 /// See AbstractAttribute::getName()
4245 const std::string getName() const override { return "AANoUndef"; }
4246
4247 /// See AbstractAttribute::getIdAddr()
4248 const char *getIdAddr() const override { return &ID; }
4249
4250 /// This function should return true if the type of the \p AA is AANoUndef
4251 static bool classof(const AbstractAttribute *AA) {
4252 return (AA->getIdAddr() == &ID);
4253 }
4254
4255 /// Unique ID (due to the unique address)
4256 static const char ID;
4257};
4258
4259struct AACallGraphNode;
4260struct AACallEdges;
4261
4262/// An Iterator for call edges, creates AACallEdges attributes in a lazy way.
4263/// This iterator becomes invalid if the underlying edge list changes.
4264/// So This shouldn't outlive a iteration of Attributor.
4265class AACallEdgeIterator
4266 : public iterator_adaptor_base<AACallEdgeIterator,
4267 SetVector<Function *>::iterator> {
4268 AACallEdgeIterator(Attributor &A, SetVector<Function *>::iterator Begin)
4269 : iterator_adaptor_base(Begin), A(A) {}
4270
4271public:
4272 AACallGraphNode *operator*() const;
4273
4274private:
4275 Attributor &A;
4276 friend AACallEdges;
4277 friend AttributorCallGraph;
4278};
4279
4280struct AACallGraphNode {
4281 AACallGraphNode(Attributor &A) : A(A) {}
4282 virtual ~AACallGraphNode() {}
4283
4284 virtual AACallEdgeIterator optimisticEdgesBegin() const = 0;
4285 virtual AACallEdgeIterator optimisticEdgesEnd() const = 0;
4286
4287 /// Iterator range for exploring the call graph.
4288 iterator_range<AACallEdgeIterator> optimisticEdgesRange() const {
4289 return iterator_range<AACallEdgeIterator>(optimisticEdgesBegin(),
4290 optimisticEdgesEnd());
4291 }
4292
4293protected:
4294 /// Reference to Attributor needed for GraphTraits implementation.
4295 Attributor &A;
4296};
4297
4298/// An abstract state for querying live call edges.
4299/// This interface uses the Attributor's optimistic liveness
4300/// information to compute the edges that are alive.
4301struct AACallEdges : public StateWrapper<BooleanState, AbstractAttribute>,
4302 AACallGraphNode {
4303 using Base = StateWrapper<BooleanState, AbstractAttribute>;
4304
4305 AACallEdges(const IRPosition &IRP, Attributor &A)
4306 : Base(IRP), AACallGraphNode(A) {}
4307
4308 /// Get the optimistic edges.
4309 virtual const SetVector<Function *> &getOptimisticEdges() const = 0;
4310
4311 /// Is there any call with a unknown callee.
4312 virtual bool hasUnknownCallee() const = 0;
4313
4314 /// Is there any call with a unknown callee, excluding any inline asm.
4315 virtual bool hasNonAsmUnknownCallee() const = 0;
4316
4317 /// Iterator for exploring the call graph.
4318 AACallEdgeIterator optimisticEdgesBegin() const override {
4319 return AACallEdgeIterator(A, getOptimisticEdges().begin());
4320 }
4321
4322 /// Iterator for exploring the call graph.
4323 AACallEdgeIterator optimisticEdgesEnd() const override {
4324 return AACallEdgeIterator(A, getOptimisticEdges().end());
4325 }
4326
4327 /// Create an abstract attribute view for the position \p IRP.
4328 static AACallEdges &createForPosition(const IRPosition &IRP, Attributor &A);
4329
4330 /// See AbstractAttribute::getName()
4331 const std::string getName() const override { return "AACallEdges"; }
4332
4333 /// See AbstractAttribute::getIdAddr()
4334 const char *getIdAddr() const override { return &ID; }
4335
4336 /// This function should return true if the type of the \p AA is AACallEdges.
4337 static bool classof(const AbstractAttribute *AA) {
4338 return (AA->getIdAddr() == &ID);
4339 }
4340
4341 /// Unique ID (due to the unique address)
4342 static const char ID;
4343};
4344
4345// Synthetic root node for the Attributor's internal call graph.
4346struct AttributorCallGraph : public AACallGraphNode {
4347 AttributorCallGraph(Attributor &A) : AACallGraphNode(A) {}
4348 virtual ~AttributorCallGraph() {}
4349
4350 AACallEdgeIterator optimisticEdgesBegin() const override {
4351 return AACallEdgeIterator(A, A.Functions.begin());
4352 }
4353
4354 AACallEdgeIterator optimisticEdgesEnd() const override {
4355 return AACallEdgeIterator(A, A.Functions.end());
4356 }
4357
4358 /// Force populate the entire call graph.
4359 void populateAll() const {
4360 for (const AACallGraphNode *AA : optimisticEdgesRange()) {
4361 // Nothing else to do here.
4362 (void)AA;
4363 }
4364 }
4365
4366 void print();
4367};
4368
4369template <> struct GraphTraits<AACallGraphNode *> {
4370 using NodeRef = AACallGraphNode *;
4371 using ChildIteratorType = AACallEdgeIterator;
4372
4373 static AACallEdgeIterator child_begin(AACallGraphNode *Node) {
4374 return Node->optimisticEdgesBegin();
4375 }
4376
4377 static AACallEdgeIterator child_end(AACallGraphNode *Node) {
4378 return Node->optimisticEdgesEnd();
4379 }
4380};
4381
4382template <>
4383struct GraphTraits<AttributorCallGraph *>
4384 : public GraphTraits<AACallGraphNode *> {
4385 using nodes_iterator = AACallEdgeIterator;
4386
4387 static AACallGraphNode *getEntryNode(AttributorCallGraph *G) {
4388 return static_cast<AACallGraphNode *>(G);
4389 }
4390
4391 static AACallEdgeIterator nodes_begin(const AttributorCallGraph *G) {
4392 return G->optimisticEdgesBegin();
4393 }
4394
4395 static AACallEdgeIterator nodes_end(const AttributorCallGraph *G) {
4396 return G->optimisticEdgesEnd();
4397 }
4398};
4399
4400template <>
4401struct DOTGraphTraits<AttributorCallGraph *> : public DefaultDOTGraphTraits {
4402 DOTGraphTraits(bool Simple = false) : DefaultDOTGraphTraits(Simple) {}
4403
4404 std::string getNodeLabel(const AACallGraphNode *Node,
4405 const AttributorCallGraph *Graph) {
4406 const AACallEdges *AACE = static_cast<const AACallEdges *>(Node);
4407 return AACE->getAssociatedFunction()->getName().str();
4408 }
4409
4410 static bool isNodeHidden(const AACallGraphNode *Node,
4411 const AttributorCallGraph *Graph) {
4412 // Hide the synth root.
4413 return static_cast<const AACallGraphNode *>(Graph) == Node;
4414 }
4415};
4416
4417struct AAExecutionDomain
4418 : public StateWrapper<BooleanState, AbstractAttribute> {
4419 using Base = StateWrapper<BooleanState, AbstractAttribute>;
4420 AAExecutionDomain(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4421
4422 /// Create an abstract attribute view for the position \p IRP.
4423 static AAExecutionDomain &createForPosition(const IRPosition &IRP,
4424 Attributor &A);
4425
4426 /// See AbstractAttribute::getName().
4427 const std::string getName() const override { return "AAExecutionDomain"; }
4428
4429 /// See AbstractAttribute::getIdAddr().
4430 const char *getIdAddr() const override { return &ID; }
4431
4432 /// Check if an instruction is executed only by the initial thread.
4433 virtual bool isExecutedByInitialThreadOnly(const Instruction &) const = 0;
4434
4435 /// Check if a basic block is executed only by the initial thread.
4436 virtual bool isExecutedByInitialThreadOnly(const BasicBlock &) const = 0;
4437
4438 /// This function should return true if the type of the \p AA is
4439 /// AAExecutionDomain.
4440 static bool classof(const AbstractAttribute *AA) {
4441 return (AA->getIdAddr() == &ID);
4442 }
4443
4444 /// Unique ID (due to the unique address)
4445 static const char ID;
4446};
4447
4448/// An abstract Attribute for computing reachability between functions.
4449struct AAFunctionReachability
4450 : public StateWrapper<BooleanState, AbstractAttribute> {
4451 using Base = StateWrapper<BooleanState, AbstractAttribute>;
4452
4453 AAFunctionReachability(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4454
4455 /// If the function represented by this possition can reach \p Fn.
4456 virtual bool canReach(Attributor &A, Function *Fn) const = 0;
4457
4458 /// Create an abstract attribute view for the position \p IRP.
4459 static AAFunctionReachability &createForPosition(const IRPosition &IRP,
4460 Attributor &A);
4461
4462 /// See AbstractAttribute::getName()
4463 const std::string getName() const override { return "AAFuncitonReacability"; }
4464
4465 /// See AbstractAttribute::getIdAddr()
4466 const char *getIdAddr() const override { return &ID; }
4467
4468 /// This function should return true if the type of the \p AA is AACallEdges.
4469 static bool classof(const AbstractAttribute *AA) {
4470 return (AA->getIdAddr() == &ID);
4471 }
4472
4473 /// Unique ID (due to the unique address)
4474 static const char ID;
4475
4476private:
4477 /// Can this function reach a call with unknown calee.
4478 virtual bool canReachUnknownCallee() const = 0;
4479};
4480
4481/// An abstract interface for struct information.
4482struct AAPointerInfo : public AbstractAttribute {
4483 AAPointerInfo(const IRPosition &IRP) : AbstractAttribute(IRP) {}
4484
4485 enum AccessKind {
4486 AK_READ = 1 << 0,
4487 AK_WRITE = 1 << 1,
4488 AK_READ_WRITE = AK_READ | AK_WRITE,
4489 };
4490
4491 /// An access description.
4492 struct Access {
4493 Access(Instruction *I, Optional<Value *> Content, AccessKind Kind, Type *Ty)
4494 : LocalI(I), RemoteI(I), Content(Content), Kind(Kind), Ty(Ty) {}
4495 Access(Instruction *LocalI, Instruction *RemoteI, Optional<Value *> Content,
4496 AccessKind Kind, Type *Ty)
4497 : LocalI(LocalI), RemoteI(RemoteI), Content(Content), Kind(Kind),
4498 Ty(Ty) {}
4499 Access(const Access &Other)
4500 : LocalI(Other.LocalI), RemoteI(Other.RemoteI), Content(Other.Content),
4501 Kind(Other.Kind), Ty(Other.Ty) {}
4502 Access(const Access &&Other)
4503 : LocalI(Other.LocalI), RemoteI(Other.RemoteI), Content(Other.Content),
4504 Kind(Other.Kind), Ty(Other.Ty) {}
4505
4506 Access &operator=(const Access &Other) {
4507 LocalI = Other.LocalI;
4508 RemoteI = Other.RemoteI;
4509 Content = Other.Content;
4510 Kind = Other.Kind;
4511 Ty = Other.Ty;
4512 return *this;
4513 }
4514 bool operator==(const Access &R) const {
4515 return LocalI == R.LocalI && RemoteI == R.RemoteI &&
4516 Content == R.Content && Kind == R.Kind;
4517 }
4518 bool operator!=(const Access &R) const { return !(*this == R); }
4519
4520 Access &operator&=(const Access &R) {
4521 assert(RemoteI == R.RemoteI && "Expected same instruction!")((void)0);
4522 Content =
4523 AA::combineOptionalValuesInAAValueLatice(Content, R.Content, Ty);
4524 Kind = AccessKind(Kind | R.Kind);
4525 return *this;
4526 }
4527
4528 /// Return the access kind.
4529 AccessKind getKind() const { return Kind; }
4530
4531 /// Return true if this is a read access.
4532 bool isRead() const { return Kind & AK_READ; }
4533
4534 /// Return true if this is a write access.
4535 bool isWrite() const { return Kind & AK_WRITE; }
4536
4537 /// Return the instruction that causes the access with respect to the local
4538 /// scope of the associated attribute.
4539 Instruction *getLocalInst() const { return LocalI; }
4540
4541 /// Return the actual instruction that causes the access.
4542 Instruction *getRemoteInst() const { return RemoteI; }
4543
4544 /// Return true if the value written is not known yet.
4545 bool isWrittenValueYetUndetermined() const { return !Content.hasValue(); }
4546
4547 /// Return true if the value written cannot be determined at all.
4548 bool isWrittenValueUnknown() const {
4549 return Content.hasValue() && !*Content;
4550 }
4551
4552 /// Return the type associated with the access, if known.
4553 Type *getType() const { return Ty; }
4554
4555 /// Return the value writen, if any. As long as
4556 /// isWrittenValueYetUndetermined return true this function shall not be
4557 /// called.
4558 Value *getWrittenValue() const { return *Content; }
4559
4560 /// Return the written value which can be `llvm::null` if it is not yet
4561 /// determined.
4562 Optional<Value *> getContent() const { return Content; }
4563
4564 private:
4565 /// The instruction responsible for the access with respect to the local
4566 /// scope of the associated attribute.
4567 Instruction *LocalI;
4568
4569 /// The instruction responsible for the access.
4570 Instruction *RemoteI;
4571
4572 /// The value written, if any. `llvm::none` means "not known yet", `nullptr`
4573 /// cannot be determined.
4574 Optional<Value *> Content;
4575
4576 /// The access kind, e.g., READ, as bitset (could be more than one).
4577 AccessKind Kind;
4578
4579 /// The type of the content, thus the type read/written, can be null if not
4580 /// available.
4581 Type *Ty;
4582 };
4583
4584 /// Create an abstract attribute view for the position \p IRP.
4585 static AAPointerInfo &createForPosition(const IRPosition &IRP, Attributor &A);
4586
4587 /// See AbstractAttribute::getName()
4588 const std::string getName() const override { return "AAPointerInfo"; }
4589
4590 /// See AbstractAttribute::getIdAddr()
4591 const char *getIdAddr() const override { return &ID; }
4592
4593 /// Call \p CB on all accesses that might interfere with \p LI and return true
4594 /// if all such accesses were known and the callback returned true for all of
4595 /// them, false otherwise.
4596 virtual bool forallInterferingAccesses(
4597 LoadInst &LI, function_ref<bool(const Access &, bool)> CB) const = 0;
4598 virtual bool forallInterferingAccesses(
4599 StoreInst &SI, function_ref<bool(const Access &, bool)> CB) const = 0;
4600
4601 /// This function should return true if the type of the \p AA is AAPointerInfo
4602 static bool classof(const AbstractAttribute *AA) {
4603 return (AA->getIdAddr() == &ID);
4604 }
4605
4606 /// Unique ID (due to the unique address)
4607 static const char ID;
4608};
4609
4610raw_ostream &operator<<(raw_ostream &, const AAPointerInfo::Access &);
4611
4612/// Run options, used by the pass manager.
4613enum AttributorRunOption {
4614 NONE = 0,
4615 MODULE = 1 << 0,
4616 CGSCC = 1 << 1,
4617 ALL = MODULE | CGSCC
4618};
4619
4620} // end namespace llvm
4621
4622#endif // LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support/Allocator.h

1//===- Allocator.h - Simple memory allocation abstraction -------*- 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/// \file
9///
10/// This file defines the BumpPtrAllocator interface. BumpPtrAllocator conforms
11/// to the LLVM "Allocator" concept and is similar to MallocAllocator, but
12/// objects cannot be deallocated. Their lifetime is tied to the lifetime of the
13/// allocator.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_SUPPORT_ALLOCATOR_H
18#define LLVM_SUPPORT_ALLOCATOR_H
19
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/Support/Alignment.h"
23#include "llvm/Support/AllocatorBase.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/MemAlloc.h"
28#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <cstdint>
32#include <cstdlib>
33#include <iterator>
34#include <type_traits>
35#include <utility>
36
37namespace llvm {
38
39namespace detail {
40
41// We call out to an external function to actually print the message as the
42// printing code uses Allocator.h in its implementation.
43void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated,
44 size_t TotalMemory);
45
46} // end namespace detail
47
48/// Allocate memory in an ever growing pool, as if by bump-pointer.
49///
50/// This isn't strictly a bump-pointer allocator as it uses backing slabs of
51/// memory rather than relying on a boundless contiguous heap. However, it has
52/// bump-pointer semantics in that it is a monotonically growing pool of memory
53/// where every allocation is found by merely allocating the next N bytes in
54/// the slab, or the next N bytes in the next slab.
55///
56/// Note that this also has a threshold for forcing allocations above a certain
57/// size into their own slab.
58///
59/// The BumpPtrAllocatorImpl template defaults to using a MallocAllocator
60/// object, which wraps malloc, to allocate memory, but it can be changed to
61/// use a custom allocator.
62///
63/// The GrowthDelay specifies after how many allocated slabs the allocator
64/// increases the size of the slabs.
65template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096,
66 size_t SizeThreshold = SlabSize, size_t GrowthDelay = 128>
67class BumpPtrAllocatorImpl
68 : public AllocatorBase<BumpPtrAllocatorImpl<AllocatorT, SlabSize,
69 SizeThreshold, GrowthDelay>>,
70 private AllocatorT {
71public:
72 static_assert(SizeThreshold <= SlabSize,
73 "The SizeThreshold must be at most the SlabSize to ensure "
74 "that objects larger than a slab go into their own memory "
75 "allocation.");
76 static_assert(GrowthDelay > 0,
77 "GrowthDelay must be at least 1 which already increases the"
78 "slab size after each allocated slab.");
79
80 BumpPtrAllocatorImpl() = default;
81
82 template <typename T>
83 BumpPtrAllocatorImpl(T &&Allocator)
84 : AllocatorT(std::forward<T &&>(Allocator)) {}
85
86 // Manually implement a move constructor as we must clear the old allocator's
87 // slabs as a matter of correctness.
88 BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old)
89 : AllocatorT(static_cast<AllocatorT &&>(Old)), CurPtr(Old.CurPtr),
90 End(Old.End), Slabs(std::move(Old.Slabs)),
91 CustomSizedSlabs(std::move(Old.CustomSizedSlabs)),
92 BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize) {
93 Old.CurPtr = Old.End = nullptr;
94 Old.BytesAllocated = 0;
95 Old.Slabs.clear();
96 Old.CustomSizedSlabs.clear();
97 }
98
99 ~BumpPtrAllocatorImpl() {
100 DeallocateSlabs(Slabs.begin(), Slabs.end());
101 DeallocateCustomSizedSlabs();
102 }
103
104 BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) {
105 DeallocateSlabs(Slabs.begin(), Slabs.end());
106 DeallocateCustomSizedSlabs();
107
108 CurPtr = RHS.CurPtr;
109 End = RHS.End;
110 BytesAllocated = RHS.BytesAllocated;
111 RedZoneSize = RHS.RedZoneSize;
112 Slabs = std::move(RHS.Slabs);
113 CustomSizedSlabs = std::move(RHS.CustomSizedSlabs);
114 AllocatorT::operator=(static_cast<AllocatorT &&>(RHS));
115
116 RHS.CurPtr = RHS.End = nullptr;
117 RHS.BytesAllocated = 0;
118 RHS.Slabs.clear();
119 RHS.CustomSizedSlabs.clear();
120 return *this;
121 }
122
123 /// Deallocate all but the current slab and reset the current pointer
124 /// to the beginning of it, freeing all memory allocated so far.
125 void Reset() {
126 // Deallocate all but the first slab, and deallocate all custom-sized slabs.
127 DeallocateCustomSizedSlabs();
128 CustomSizedSlabs.clear();
129
130 if (Slabs.empty())
131 return;
132
133 // Reset the state.
134 BytesAllocated = 0;
135 CurPtr = (char *)Slabs.front();
136 End = CurPtr + SlabSize;
137
138 __asan_poison_memory_region(*Slabs.begin(), computeSlabSize(0));
139 DeallocateSlabs(std::next(Slabs.begin()), Slabs.end());
140 Slabs.erase(std::next(Slabs.begin()), Slabs.end());
141 }
142
143 /// Allocate space at the specified alignment.
144 LLVM_ATTRIBUTE_RETURNS_NONNULL__attribute__((returns_nonnull)) LLVM_ATTRIBUTE_RETURNS_NOALIAS__attribute__((__malloc__)) void *
145 Allocate(size_t Size, Align Alignment) {
146 // Keep track of how many bytes we've allocated.
147 BytesAllocated += Size;
148
149 size_t Adjustment = offsetToAlignedAddr(CurPtr, Alignment);
9
Calling 'offsetToAlignedAddr'
150 assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow")((void)0);
151
152 size_t SizeToAllocate = Size;
153#if LLVM_ADDRESS_SANITIZER_BUILD0
154 // Add trailing bytes as a "red zone" under ASan.
155 SizeToAllocate += RedZoneSize;
156#endif
157
158 // Check if we have enough space.
159 if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)) {
160 char *AlignedPtr = CurPtr + Adjustment;
161 CurPtr = AlignedPtr + SizeToAllocate;
162 // Update the allocation point of this memory block in MemorySanitizer.
163 // Without this, MemorySanitizer messages for values originated from here
164 // will point to the allocation of the entire slab.
165 __msan_allocated_memory(AlignedPtr, Size);
166 // Similarly, tell ASan about this space.
167 __asan_unpoison_memory_region(AlignedPtr, Size);
168 return AlignedPtr;
169 }
170
171 // If Size is really big, allocate a separate slab for it.
172 size_t PaddedSize = SizeToAllocate + Alignment.value() - 1;
173 if (PaddedSize > SizeThreshold) {
174 void *NewSlab =
175 AllocatorT::Allocate(PaddedSize, alignof(std::max_align_t));
176 // We own the new slab and don't want anyone reading anyting other than
177 // pieces returned from this method. So poison the whole slab.
178 __asan_poison_memory_region(NewSlab, PaddedSize);
179 CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize));
180
181 uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment);
182 assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize)((void)0);
183 char *AlignedPtr = (char*)AlignedAddr;
184 __msan_allocated_memory(AlignedPtr, Size);
185 __asan_unpoison_memory_region(AlignedPtr, Size);
186 return AlignedPtr;
187 }
188
189 // Otherwise, start a new slab and try again.
190 StartNewSlab();
191 uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment);
192 assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End &&((void)0)
193 "Unable to allocate memory!")((void)0);
194 char *AlignedPtr = (char*)AlignedAddr;
195 CurPtr = AlignedPtr + SizeToAllocate;
196 __msan_allocated_memory(AlignedPtr, Size);
197 __asan_unpoison_memory_region(AlignedPtr, Size);
198 return AlignedPtr;
199 }
200
201 inline LLVM_ATTRIBUTE_RETURNS_NONNULL__attribute__((returns_nonnull)) LLVM_ATTRIBUTE_RETURNS_NOALIAS__attribute__((__malloc__)) void *
202 Allocate(size_t Size, size_t Alignment) {
203 assert(Alignment > 0 && "0-byte alignment is not allowed. Use 1 instead.")((void)0);
204 return Allocate(Size, Align(Alignment));
8
Calling 'BumpPtrAllocatorImpl::Allocate'
205 }
206
207 // Pull in base class overloads.
208 using AllocatorBase<BumpPtrAllocatorImpl>::Allocate;
209
210 // Bump pointer allocators are expected to never free their storage; and
211 // clients expect pointers to remain valid for non-dereferencing uses even
212 // after deallocation.
213 void Deallocate(const void *Ptr, size_t Size, size_t /*Alignment*/) {
214 __asan_poison_memory_region(Ptr, Size);
215 }
216
217 // Pull in base class overloads.
218 using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate;
219
220 size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); }
221
222 /// \return An index uniquely and reproducibly identifying
223 /// an input pointer \p Ptr in the given allocator.
224 /// The returned value is negative iff the object is inside a custom-size
225 /// slab.
226 /// Returns an empty optional if the pointer is not found in the allocator.
227 llvm::Optional<int64_t> identifyObject(const void *Ptr) {
228 const char *P = static_cast<const char *>(Ptr);
229 int64_t InSlabIdx = 0;
230 for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) {
231 const char *S = static_cast<const char *>(Slabs[Idx]);
232 if (P >= S && P < S + computeSlabSize(Idx))
233 return InSlabIdx + static_cast<int64_t>(P - S);
234 InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx));
235 }
236
237 // Use negative index to denote custom sized slabs.
238 int64_t InCustomSizedSlabIdx = -1;
239 for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) {
240 const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first);
241 size_t Size = CustomSizedSlabs[Idx].second;
242 if (P >= S && P < S + Size)
243 return InCustomSizedSlabIdx - static_cast<int64_t>(P - S);
244 InCustomSizedSlabIdx -= static_cast<int64_t>(Size);
245 }
246 return None;
247 }
248
249 /// A wrapper around identifyObject that additionally asserts that
250 /// the object is indeed within the allocator.
251 /// \return An index uniquely and reproducibly identifying
252 /// an input pointer \p Ptr in the given allocator.
253 int64_t identifyKnownObject(const void *Ptr) {
254 Optional<int64_t> Out = identifyObject(Ptr);
255 assert(Out && "Wrong allocator used")((void)0);
256 return *Out;
257 }
258
259 /// A wrapper around identifyKnownObject. Accepts type information
260 /// about the object and produces a smaller identifier by relying on
261 /// the alignment information. Note that sub-classes may have different
262 /// alignment, so the most base class should be passed as template parameter
263 /// in order to obtain correct results. For that reason automatic template
264 /// parameter deduction is disabled.
265 /// \return An index uniquely and reproducibly identifying
266 /// an input pointer \p Ptr in the given allocator. This identifier is
267 /// different from the ones produced by identifyObject and
268 /// identifyAlignedObject.
269 template <typename T>
270 int64_t identifyKnownAlignedObject(const void *Ptr) {
271 int64_t Out = identifyKnownObject(Ptr);
272 assert(Out % alignof(T) == 0 && "Wrong alignment information")((void)0);
273 return Out / alignof(T);
274 }
275
276 size_t getTotalMemory() const {
277 size_t TotalMemory = 0;
278 for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I)
279 TotalMemory += computeSlabSize(std::distance(Slabs.begin(), I));
280 for (auto &PtrAndSize : CustomSizedSlabs)
281 TotalMemory += PtrAndSize.second;
282 return TotalMemory;
283 }
284
285 size_t getBytesAllocated() const { return BytesAllocated; }
286
287 void setRedZoneSize(size_t NewSize) {
288 RedZoneSize = NewSize;
289 }
290
291 void PrintStats() const {
292 detail::printBumpPtrAllocatorStats(Slabs.size(), BytesAllocated,
293 getTotalMemory());
294 }
295
296private:
297 /// The current pointer into the current slab.
298 ///
299 /// This points to the next free byte in the slab.
300 char *CurPtr = nullptr;
301
302 /// The end of the current slab.
303 char *End = nullptr;
304
305 /// The slabs allocated so far.
306 SmallVector<void *, 4> Slabs;
307
308 /// Custom-sized slabs allocated for too-large allocation requests.
309 SmallVector<std::pair<void *, size_t>, 0> CustomSizedSlabs;
310
311 /// How many bytes we've allocated.
312 ///
313 /// Used so that we can compute how much space was wasted.
314 size_t BytesAllocated = 0;
315
316 /// The number of bytes to put between allocations when running under
317 /// a sanitizer.
318 size_t RedZoneSize = 1;
319
320 static size_t computeSlabSize(unsigned SlabIdx) {
321 // Scale the actual allocated slab size based on the number of slabs
322 // allocated. Every GrowthDelay slabs allocated, we double
323 // the allocated size to reduce allocation frequency, but saturate at
324 // multiplying the slab size by 2^30.
325 return SlabSize *
326 ((size_t)1 << std::min<size_t>(30, SlabIdx / GrowthDelay));
327 }
328
329 /// Allocate a new slab and move the bump pointers over into the new
330 /// slab, modifying CurPtr and End.
331 void StartNewSlab() {
332 size_t AllocatedSlabSize = computeSlabSize(Slabs.size());
333
334 void *NewSlab =
335 AllocatorT::Allocate(AllocatedSlabSize, alignof(std::max_align_t));
336 // We own the new slab and don't want anyone reading anything other than
337 // pieces returned from this method. So poison the whole slab.
338 __asan_poison_memory_region(NewSlab, AllocatedSlabSize);
339
340 Slabs.push_back(NewSlab);
341 CurPtr = (char *)(NewSlab);
342 End = ((char *)NewSlab) + AllocatedSlabSize;
343 }
344
345 /// Deallocate a sequence of slabs.
346 void DeallocateSlabs(SmallVectorImpl<void *>::iterator I,
347 SmallVectorImpl<void *>::iterator E) {
348 for (; I != E; ++I) {
349 size_t AllocatedSlabSize =
350 computeSlabSize(std::distance(Slabs.begin(), I));
351 AllocatorT::Deallocate(*I, AllocatedSlabSize, alignof(std::max_align_t));
352 }
353 }
354
355 /// Deallocate all memory for custom sized slabs.
356 void DeallocateCustomSizedSlabs() {
357 for (auto &PtrAndSize : CustomSizedSlabs) {
358 void *Ptr = PtrAndSize.first;
359 size_t Size = PtrAndSize.second;
360 AllocatorT::Deallocate(Ptr, Size, alignof(std::max_align_t));
361 }
362 }
363
364 template <typename T> friend class SpecificBumpPtrAllocator;
365};
366
367/// The standard BumpPtrAllocator which just uses the default template
368/// parameters.
369typedef BumpPtrAllocatorImpl<> BumpPtrAllocator;
370
371/// A BumpPtrAllocator that allows only elements of a specific type to be
372/// allocated.
373///
374/// This allows calling the destructor in DestroyAll() and when the allocator is
375/// destroyed.
376template <typename T> class SpecificBumpPtrAllocator {
377 BumpPtrAllocator Allocator;
378
379public:
380 SpecificBumpPtrAllocator() {
381 // Because SpecificBumpPtrAllocator walks the memory to call destructors,
382 // it can't have red zones between allocations.
383 Allocator.setRedZoneSize(0);
384 }
385 SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old)
386 : Allocator(std::move(Old.Allocator)) {}
387 ~SpecificBumpPtrAllocator() { DestroyAll(); }
388
389 SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) {
390 Allocator = std::move(RHS.Allocator);
391 return *this;
392 }
393
394 /// Call the destructor of each allocated object and deallocate all but the
395 /// current slab and reset the current pointer to the beginning of it, freeing
396 /// all memory allocated so far.
397 void DestroyAll() {
398 auto DestroyElements = [](char *Begin, char *End) {
399 assert(Begin == (char *)alignAddr(Begin, Align::Of<T>()))((void)0);
400 for (char *Ptr = Begin; Ptr + sizeof(T) <= End; Ptr += sizeof(T))
401 reinterpret_cast<T *>(Ptr)->~T();
402 };
403
404 for (auto I = Allocator.Slabs.begin(), E = Allocator.Slabs.end(); I != E;
405 ++I) {
406 size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize(
407 std::distance(Allocator.Slabs.begin(), I));
408 char *Begin = (char *)alignAddr(*I, Align::Of<T>());
409 char *End = *I == Allocator.Slabs.back() ? Allocator.CurPtr
410 : (char *)*I + AllocatedSlabSize;
411
412 DestroyElements(Begin, End);
413 }
414
415 for (auto &PtrAndSize : Allocator.CustomSizedSlabs) {
416 void *Ptr = PtrAndSize.first;
417 size_t Size = PtrAndSize.second;
418 DestroyElements((char *)alignAddr(Ptr, Align::Of<T>()),
419 (char *)Ptr + Size);
420 }
421
422 Allocator.Reset();
423 }
424
425 /// Allocate space for an array of objects without constructing them.
426 T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); }
427};
428
429} // end namespace llvm
430
431template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold,
432 size_t GrowthDelay>
433void *
434operator new(size_t Size,
435 llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold,
436 GrowthDelay> &Allocator) {
437 return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size),
7
Calling 'BumpPtrAllocatorImpl::Allocate'
438 alignof(std::max_align_t)));
439}
440
441template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold,
442 size_t GrowthDelay>
443void operator delete(void *,
444 llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize,
445 SizeThreshold, GrowthDelay> &) {
446}
447
448#endif // LLVM_SUPPORT_ALLOCATOR_H

/usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support/Alignment.h

1//===-- llvm/Support/Alignment.h - Useful alignment functions ---*- 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 contains types to represent alignments.
10// They are instrumented to guarantee some invariants are preserved and prevent
11// invalid manipulations.
12//
13// - Align represents an alignment in bytes, it is always set and always a valid
14// power of two, its minimum value is 1 which means no alignment requirements.
15//
16// - MaybeAlign is an optional type, it may be undefined or set. When it's set
17// you can get the underlying Align type by using the getValue() method.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_SUPPORT_ALIGNMENT_H_
22#define LLVM_SUPPORT_ALIGNMENT_H_
23
24#include "llvm/ADT/Optional.h"
25#include "llvm/Support/MathExtras.h"
26#include <cassert>
27#ifndef NDEBUG1
28#include <string>
29#endif // NDEBUG
30
31namespace llvm {
32
33#define ALIGN_CHECK_ISPOSITIVE(decl) \
34 assert(decl > 0 && (#decl " should be defined"))((void)0)
35
36/// This struct is a compact representation of a valid (non-zero power of two)
37/// alignment.
38/// It is suitable for use as static global constants.
39struct Align {
40private:
41 uint8_t ShiftValue = 0; /// The log2 of the required alignment.
42 /// ShiftValue is less than 64 by construction.
43
44 friend struct MaybeAlign;
45 friend unsigned Log2(Align);
46 friend bool operator==(Align Lhs, Align Rhs);
47 friend bool operator!=(Align Lhs, Align Rhs);
48 friend bool operator<=(Align Lhs, Align Rhs);
49 friend bool operator>=(Align Lhs, Align Rhs);
50 friend bool operator<(Align Lhs, Align Rhs);
51 friend bool operator>(Align Lhs, Align Rhs);
52 friend unsigned encode(struct MaybeAlign A);
53 friend struct MaybeAlign decodeMaybeAlign(unsigned Value);
54
55 /// A trivial type to allow construction of constexpr Align.
56 /// This is currently needed to workaround a bug in GCC 5.3 which prevents
57 /// definition of constexpr assign operators.
58 /// https://stackoverflow.com/questions/46756288/explicitly-defaulted-function-cannot-be-declared-as-constexpr-because-the-implic
59 /// FIXME: Remove this, make all assign operators constexpr and introduce user
60 /// defined literals when we don't have to support GCC 5.3 anymore.
61 /// https://llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain
62 struct LogValue {
63 uint8_t Log;
64 };
65
66public:
67 /// Default is byte-aligned.
68 constexpr Align() = default;
69 /// Do not perform checks in case of copy/move construct/assign, because the
70 /// checks have been performed when building `Other`.
71 constexpr Align(const Align &Other) = default;
72 constexpr Align(Align &&Other) = default;
73 Align &operator=(const Align &Other) = default;
74 Align &operator=(Align &&Other) = default;
75
76 explicit Align(uint64_t Value) {
77 assert(Value > 0 && "Value must not be 0")((void)0);
78 assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2")((void)0);
79 ShiftValue = Log2_64(Value);
80 assert(ShiftValue < 64 && "Broken invariant")((void)0);
81 }
82
83 /// This is a hole in the type system and should not be abused.
84 /// Needed to interact with C for instance.
85 uint64_t value() const { return uint64_t(1) << ShiftValue; }
14
The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'uint64_t'
86
87 /// Allow constructions of constexpr Align.
88 template <size_t kValue> constexpr static LogValue Constant() {
89 return LogValue{static_cast<uint8_t>(CTLog2<kValue>())};
90 }
91
92 /// Allow constructions of constexpr Align from types.
93 /// Compile time equivalent to Align(alignof(T)).
94 template <typename T> constexpr static LogValue Of() {
95 return Constant<std::alignment_of<T>::value>();
96 }
97
98 /// Constexpr constructor from LogValue type.
99 constexpr Align(LogValue CA) : ShiftValue(CA.Log) {}
100};
101
102/// Treats the value 0 as a 1, so Align is always at least 1.
103inline Align assumeAligned(uint64_t Value) {
104 return Value ? Align(Value) : Align();
105}
106
107/// This struct is a compact representation of a valid (power of two) or
108/// undefined (0) alignment.
109struct MaybeAlign : public llvm::Optional<Align> {
110private:
111 using UP = llvm::Optional<Align>;
112
113public:
114 /// Default is undefined.
115 MaybeAlign() = default;
116 /// Do not perform checks in case of copy/move construct/assign, because the
117 /// checks have been performed when building `Other`.
118 MaybeAlign(const MaybeAlign &Other) = default;
119 MaybeAlign &operator=(const MaybeAlign &Other) = default;
120 MaybeAlign(MaybeAlign &&Other) = default;
121 MaybeAlign &operator=(MaybeAlign &&Other) = default;
122
123 /// Use llvm::Optional<Align> constructor.
124 using UP::UP;
125
126 explicit MaybeAlign(uint64_t Value) {
127 assert((Value == 0 || llvm::isPowerOf2_64(Value)) &&((void)0)
128 "Alignment is neither 0 nor a power of 2")((void)0);
129 if (Value)
130 emplace(Value);
131 }
132
133 /// For convenience, returns a valid alignment or 1 if undefined.
134 Align valueOrOne() const { return hasValue() ? getValue() : Align(); }
135};
136
137/// Checks that SizeInBytes is a multiple of the alignment.
138inline bool isAligned(Align Lhs, uint64_t SizeInBytes) {
139 return SizeInBytes % Lhs.value() == 0;
140}
141
142/// Checks that Addr is a multiple of the alignment.
143inline bool isAddrAligned(Align Lhs, const void *Addr) {
144 return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr));
145}
146
147/// Returns a multiple of A needed to store `Size` bytes.
148inline uint64_t alignTo(uint64_t Size, Align A) {
149 const uint64_t Value = A.value();
13
Calling 'Align::value'
150 // The following line is equivalent to `(Size + Value - 1) / Value * Value`.
151
152 // The division followed by a multiplication can be thought of as a right
153 // shift followed by a left shift which zeros out the extra bits produced in
154 // the bump; `~(Value - 1)` is a mask where all those bits being zeroed out
155 // are just zero.
156
157 // Most compilers can generate this code but the pattern may be missed when
158 // multiple functions gets inlined.
159 return (Size + Value - 1) & ~(Value - 1U);
160}
161
162/// If non-zero \p Skew is specified, the return value will be a minimal integer
163/// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
164/// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
165/// Skew mod \p A'.
166///
167/// Examples:
168/// \code
169/// alignTo(5, Align(8), 7) = 7
170/// alignTo(17, Align(8), 1) = 17
171/// alignTo(~0LL, Align(8), 3) = 3
172/// \endcode
173inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) {
174 const uint64_t Value = A.value();
175 Skew %= Value;
176 return ((Size + Value - 1 - Skew) & ~(Value - 1U)) + Skew;
177}
178
179/// Returns a multiple of A needed to store `Size` bytes.
180/// Returns `Size` if current alignment is undefined.
181inline uint64_t alignTo(uint64_t Size, MaybeAlign A) {
182 return A ? alignTo(Size, A.getValue()) : Size;
183}
184
185/// Aligns `Addr` to `Alignment` bytes, rounding up.
186inline uintptr_t alignAddr(const void *Addr, Align Alignment) {
187 uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr);
188 assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >=((void)0)
189 ArithAddr &&((void)0)
190 "Overflow")((void)0);
191 return alignTo(ArithAddr, Alignment);
192}
193
194/// Returns the offset to the next integer (mod 2**64) that is greater than
195/// or equal to \p Value and is a multiple of \p Align.
196inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) {
197 return alignTo(Value, Alignment) - Value;
11
The value 255 is assigned to 'A.ShiftValue'
12
Calling 'alignTo'
198}
199
200/// Returns the necessary adjustment for aligning `Addr` to `Alignment`
201/// bytes, rounding up.
202inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) {
203 return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment);
10
Calling 'offsetToAlignment'
204}
205
206/// Returns the log2 of the alignment.
207inline unsigned Log2(Align A) { return A.ShiftValue; }
208
209/// Returns the alignment that satisfies both alignments.
210/// Same semantic as MinAlign.
211inline Align commonAlignment(Align A, Align B) { return std::min(A, B); }
212
213/// Returns the alignment that satisfies both alignments.
214/// Same semantic as MinAlign.
215inline Align commonAlignment(Align A, uint64_t Offset) {
216 return Align(MinAlign(A.value(), Offset));
217}
218
219/// Returns the alignment that satisfies both alignments.
220/// Same semantic as MinAlign.
221inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) {
222 return A && B ? commonAlignment(*A, *B) : A ? A : B;
223}
224
225/// Returns the alignment that satisfies both alignments.
226/// Same semantic as MinAlign.
227inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) {
228 return MaybeAlign(MinAlign((*A).value(), Offset));
229}
230
231/// Returns a representation of the alignment that encodes undefined as 0.
232inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
233
234/// Dual operation of the encode function above.
235inline MaybeAlign decodeMaybeAlign(unsigned Value) {
236 if (Value == 0)
237 return MaybeAlign();
238 Align Out;
239 Out.ShiftValue = Value - 1;
240 return Out;
241}
242
243/// Returns a representation of the alignment, the encoded value is positive by
244/// definition.
245inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
246
247/// Comparisons between Align and scalars. Rhs must be positive.
248inline bool operator==(Align Lhs, uint64_t Rhs) {
249 ALIGN_CHECK_ISPOSITIVE(Rhs);
250 return Lhs.value() == Rhs;
251}
252inline bool operator!=(Align Lhs, uint64_t Rhs) {
253 ALIGN_CHECK_ISPOSITIVE(Rhs);
254 return Lhs.value() != Rhs;
255}
256inline bool operator<=(Align Lhs, uint64_t Rhs) {
257 ALIGN_CHECK_ISPOSITIVE(Rhs);
258 return Lhs.value() <= Rhs;
259}
260inline bool operator>=(Align Lhs, uint64_t Rhs) {
261 ALIGN_CHECK_ISPOSITIVE(Rhs);
262 return Lhs.value() >= Rhs;
263}
264inline bool operator<(Align Lhs, uint64_t Rhs) {
265 ALIGN_CHECK_ISPOSITIVE(Rhs);
266 return Lhs.value() < Rhs;
267}
268inline bool operator>(Align Lhs, uint64_t Rhs) {
269 ALIGN_CHECK_ISPOSITIVE(Rhs);
270 return Lhs.value() > Rhs;
271}
272
273/// Comparisons between MaybeAlign and scalars.
274inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) {
275 return Lhs ? (*Lhs).value() == Rhs : Rhs == 0;
276}
277inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) {
278 return Lhs ? (*Lhs).value() != Rhs : Rhs != 0;
279}
280
281/// Comparisons operators between Align.
282inline bool operator==(Align Lhs, Align Rhs) {
283 return Lhs.ShiftValue == Rhs.ShiftValue;
284}
285inline bool operator!=(Align Lhs, Align Rhs) {
286 return Lhs.ShiftValue != Rhs.ShiftValue;
287}
288inline bool operator<=(Align Lhs, Align Rhs) {
289 return Lhs.ShiftValue <= Rhs.ShiftValue;
290}
291inline bool operator>=(Align Lhs, Align Rhs) {
292 return Lhs.ShiftValue >= Rhs.ShiftValue;
293}
294inline bool operator<(Align Lhs, Align Rhs) {
295 return Lhs.ShiftValue < Rhs.ShiftValue;
296}
297inline bool operator>(Align Lhs, Align Rhs) {
298 return Lhs.ShiftValue > Rhs.ShiftValue;
299}
300
301// Don't allow relational comparisons with MaybeAlign.
302bool operator<=(Align Lhs, MaybeAlign Rhs) = delete;
303bool operator>=(Align Lhs, MaybeAlign Rhs) = delete;
304bool operator<(Align Lhs, MaybeAlign Rhs) = delete;
305bool operator>(Align Lhs, MaybeAlign Rhs) = delete;
306
307bool operator<=(MaybeAlign Lhs, Align Rhs) = delete;
308bool operator>=(MaybeAlign Lhs, Align Rhs) = delete;
309bool operator<(MaybeAlign Lhs, Align Rhs) = delete;
310bool operator>(MaybeAlign Lhs, Align Rhs) = delete;
311
312bool operator<=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
313bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
314bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
315bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
316
317inline Align operator*(Align Lhs, uint64_t Rhs) {
318 assert(Rhs > 0 && "Rhs must be positive")((void)0);
319 return Align(Lhs.value() * Rhs);
320}
321
322inline MaybeAlign operator*(MaybeAlign Lhs, uint64_t Rhs) {
323 assert(Rhs > 0 && "Rhs must be positive")((void)0);
324 return Lhs ? Lhs.getValue() * Rhs : MaybeAlign();
325}
326
327inline Align operator/(Align Lhs, uint64_t Divisor) {
328 assert(llvm::isPowerOf2_64(Divisor) &&((void)0)
329 "Divisor must be positive and a power of 2")((void)0);
330 assert(Lhs != 1 && "Can't halve byte alignment")((void)0);
331 return Align(Lhs.value() / Divisor);
332}
333
334inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) {
335 assert(llvm::isPowerOf2_64(Divisor) &&((void)0)
336 "Divisor must be positive and a power of 2")((void)0);
337 return Lhs ? Lhs.getValue() / Divisor : MaybeAlign();
338}
339
340inline Align max(MaybeAlign Lhs, Align Rhs) {
341 return Lhs && *Lhs > Rhs ? *Lhs : Rhs;
342}
343
344inline Align max(Align Lhs, MaybeAlign Rhs) {
345 return Rhs && *Rhs > Lhs ? *Rhs : Lhs;
346}
347
348#ifndef NDEBUG1
349// For usage in LLVM_DEBUG macros.
350inline std::string DebugStr(const Align &A) {
351 return std::to_string(A.value());
352}
353// For usage in LLVM_DEBUG macros.
354inline std::string DebugStr(const MaybeAlign &MA) {
355 if (MA)
356 return std::to_string(MA->value());
357 return "None";
358}
359#endif // NDEBUG
360
361#undef ALIGN_CHECK_ISPOSITIVE
362
363} // namespace llvm
364
365#endif // LLVM_SUPPORT_ALIGNMENT_H_