blob: da5260bb0c94b67d285482c673835d0ee6c34afc [file] [log] [blame]
Johannes Doerfertaade7822019-06-05 03:02:24 +00001//===- 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 inter procedural pass that deduces and/or propagating
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
Johannes Doerfert368f7ee2019-12-30 16:12:36 -060016#include "llvm/Transforms/IPO/Attributor.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000017
Hideto Ueno11d37102019-07-17 15:15:43 +000018#include "llvm/ADT/DepthFirstIterator.h"
Stefan Stipanovic6058b862019-07-22 23:58:23 +000019#include "llvm/ADT/STLExtras.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/Statistic.h"
Johannes Doerfertb0c77c32019-11-27 00:30:12 -060023#include "llvm/Analysis/CallGraph.h"
24#include "llvm/Analysis/CallGraphSCCPass.h"
Stefan Stipanovic69ebb022019-07-22 19:36:27 +000025#include "llvm/Analysis/CaptureTracking.h"
Johannes Doerfert924d2132019-08-05 21:34:45 +000026#include "llvm/Analysis/EHPersonalities.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000027#include "llvm/Analysis/GlobalsModRef.h"
Hideto Ueno188f9a32020-01-15 15:25:52 +090028#include "llvm/Analysis/LazyValueInfo.h"
Hideto Ueno19c07af2019-07-23 08:16:17 +000029#include "llvm/Analysis/Loads.h"
Stefan Stipanovic431141c2019-09-15 21:47:41 +000030#include "llvm/Analysis/MemoryBuiltins.h"
Hideto Ueno188f9a32020-01-15 15:25:52 +090031#include "llvm/Analysis/ScalarEvolution.h"
Hideto Ueno54869ec2019-07-15 06:49:04 +000032#include "llvm/Analysis/ValueTracking.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000033#include "llvm/IR/Argument.h"
34#include "llvm/IR/Attributes.h"
Hideto Ueno11d37102019-07-17 15:15:43 +000035#include "llvm/IR/CFG.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050036#include "llvm/IR/IRBuilder.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000037#include "llvm/IR/InstIterator.h"
Stefan Stipanovic06263672019-07-11 21:37:40 +000038#include "llvm/IR/IntrinsicInst.h"
Johannes Doerferta4088c72020-01-07 16:01:57 -060039#include "llvm/IR/Verifier.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080040#include "llvm/InitializePasses.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050041#include "llvm/IR/NoFolder.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000042#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/raw_ostream.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050045#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Stefan Stipanovic6058b862019-07-22 23:58:23 +000046#include "llvm/Transforms/Utils/BasicBlockUtils.h"
47#include "llvm/Transforms/Utils/Local.h"
48
Johannes Doerfertaade7822019-06-05 03:02:24 +000049#include <cassert>
50
51using namespace llvm;
52
53#define DEBUG_TYPE "attributor"
54
55STATISTIC(NumFnWithExactDefinition,
56 "Number of function with exact definitions");
57STATISTIC(NumFnWithoutExactDefinition,
58 "Number of function without exact definitions");
59STATISTIC(NumAttributesTimedOut,
60 "Number of abstract attributes timed out before fixpoint");
61STATISTIC(NumAttributesValidFixpoint,
62 "Number of abstract attributes in a valid fixpoint state");
63STATISTIC(NumAttributesManifested,
64 "Number of abstract attributes manifested in IR");
Johannes Doerfert680f6382019-11-02 02:48:05 -050065STATISTIC(NumAttributesFixedDueToRequiredDependences,
66 "Number of abstract attributes fixed due to required dependences");
Johannes Doerfertaade7822019-06-05 03:02:24 +000067
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000068// Some helper macros to deal with statistics tracking.
69//
70// Usage:
71// For simple IR attribute tracking overload trackStatistics in the abstract
Johannes Doerfert17b578b2019-08-14 21:46:25 +000072// attribute and choose the right STATS_DECLTRACK_********* macro,
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000073// e.g.,:
74// void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +000075// STATS_DECLTRACK_ARG_ATTR(returned)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000076// }
77// If there is a single "increment" side one can use the macro
Johannes Doerfert17b578b2019-08-14 21:46:25 +000078// STATS_DECLTRACK with a custom message. If there are multiple increment
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000079// sides, STATS_DECL and STATS_TRACK can also be used separatly.
80//
81#define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \
82 ("Number of " #TYPE " marked '" #NAME "'")
83#define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
Johannes Doerferta7a3b3a2019-09-04 19:01:08 +000084#define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
85#define STATS_DECL(NAME, TYPE, MSG) \
86 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000087#define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
Johannes Doerfert17b578b2019-08-14 21:46:25 +000088#define STATS_DECLTRACK(NAME, TYPE, MSG) \
Johannes Doerfert169af992019-08-20 06:09:56 +000089 { \
90 STATS_DECL(NAME, TYPE, MSG) \
91 STATS_TRACK(NAME, TYPE) \
92 }
Johannes Doerfert17b578b2019-08-14 21:46:25 +000093#define STATS_DECLTRACK_ARG_ATTR(NAME) \
94 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
95#define STATS_DECLTRACK_CSARG_ATTR(NAME) \
96 STATS_DECLTRACK(NAME, CSArguments, \
97 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
98#define STATS_DECLTRACK_FN_ATTR(NAME) \
99 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
100#define STATS_DECLTRACK_CS_ATTR(NAME) \
101 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
102#define STATS_DECLTRACK_FNRET_ATTR(NAME) \
103 STATS_DECLTRACK(NAME, FunctionReturn, \
Johannes Doerfert2db85282019-08-21 20:56:56 +0000104 BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000105#define STATS_DECLTRACK_CSRET_ATTR(NAME) \
106 STATS_DECLTRACK(NAME, CSReturn, \
107 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
108#define STATS_DECLTRACK_FLOATING_ATTR(NAME) \
109 STATS_DECLTRACK(NAME, Floating, \
110 ("Number of floating values known to be '" #NAME "'"))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000111
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600112// Specialization of the operator<< for abstract attributes subclasses. This
113// disambiguates situations where multiple operators are applicable.
114namespace llvm {
115#define PIPE_OPERATOR(CLASS) \
116 raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \
117 return OS << static_cast<const AbstractAttribute &>(AA); \
118 }
119
120PIPE_OPERATOR(AAIsDead)
121PIPE_OPERATOR(AANoUnwind)
122PIPE_OPERATOR(AANoSync)
123PIPE_OPERATOR(AANoRecurse)
124PIPE_OPERATOR(AAWillReturn)
125PIPE_OPERATOR(AANoReturn)
126PIPE_OPERATOR(AAReturnedValues)
127PIPE_OPERATOR(AANonNull)
128PIPE_OPERATOR(AANoAlias)
129PIPE_OPERATOR(AADereferenceable)
130PIPE_OPERATOR(AAAlign)
131PIPE_OPERATOR(AANoCapture)
132PIPE_OPERATOR(AAValueSimplify)
133PIPE_OPERATOR(AANoFree)
134PIPE_OPERATOR(AAHeapToStack)
135PIPE_OPERATOR(AAReachability)
136PIPE_OPERATOR(AAMemoryBehavior)
Hideto Ueno188f9a32020-01-15 15:25:52 +0900137PIPE_OPERATOR(AAValueConstantRange)
Johannes Doerfert89c2e732019-10-30 17:20:20 -0500138PIPE_OPERATOR(AAPrivatizablePtr)
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600139
140#undef PIPE_OPERATOR
141} // namespace llvm
142
Johannes Doerfertaade7822019-06-05 03:02:24 +0000143// TODO: Determine a good default value.
144//
145// In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
146// (when run with the first 5 abstract attributes). The results also indicate
147// that we never reach 32 iterations but always find a fixpoint sooner.
148//
149// This will become more evolved once we perform two interleaved fixpoint
150// iterations: bottom-up and top-down.
151static cl::opt<unsigned>
152 MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
153 cl::desc("Maximal number of fixpoint iterations."),
154 cl::init(32));
Johannes Doerfertb504eb82019-08-26 18:55:47 +0000155static cl::opt<bool> VerifyMaxFixpointIterations(
156 "attributor-max-iterations-verify", cl::Hidden,
157 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
158 cl::init(false));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000159
160static cl::opt<bool> DisableAttributor(
161 "attributor-disable", cl::Hidden,
162 cl::desc("Disable the attributor inter-procedural deduction pass."),
Johannes Doerfert282d34e2019-06-14 14:53:41 +0000163 cl::init(true));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000164
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -0500165static cl::opt<bool> AnnotateDeclarationCallSites(
166 "attributor-annotate-decl-cs", cl::Hidden,
James Hendersond68904f2020-01-06 10:15:44 +0000167 cl::desc("Annotate call sites of function declarations."), cl::init(false));
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -0500168
Johannes Doerfert7516a5e2019-09-03 20:37:24 +0000169static cl::opt<bool> ManifestInternal(
170 "attributor-manifest-internal", cl::Hidden,
171 cl::desc("Manifest Attributor internal string attributes."),
172 cl::init(false));
173
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +0000174static cl::opt<unsigned> DepRecInterval(
175 "attributor-dependence-recompute-interval", cl::Hidden,
176 cl::desc("Number of iterations until dependences are recomputed."),
177 cl::init(4));
178
Stefan Stipanovic431141c2019-09-15 21:47:41 +0000179static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
180 cl::init(true), cl::Hidden);
181
Johannes Doerfert1c2afae2019-10-10 05:34:21 +0000182static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
183 cl::Hidden);
Stefan Stipanovic431141c2019-09-15 21:47:41 +0000184
Johannes Doerfertaade7822019-06-05 03:02:24 +0000185/// Logic operators for the change status enum class.
186///
187///{
188ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
189 return l == ChangeStatus::CHANGED ? l : r;
190}
191ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
192 return l == ChangeStatus::UNCHANGED ? l : r;
193}
194///}
195
Johannes Doerfertb1b441d2019-10-10 01:19:57 -0500196Argument *IRPosition::getAssociatedArgument() const {
197 if (getPositionKind() == IRP_ARGUMENT)
198 return cast<Argument>(&getAnchorValue());
199
200 // Not an Argument and no argument number means this is not a call site
201 // argument, thus we cannot find a callback argument to return.
202 int ArgNo = getArgNo();
203 if (ArgNo < 0)
204 return nullptr;
205
206 // Use abstract call sites to make the connection between the call site
207 // values and the ones in callbacks. If a callback was found that makes use
208 // of the underlying call site operand, we want the corresponding callback
209 // callee argument and not the direct callee argument.
210 Optional<Argument *> CBCandidateArg;
211 SmallVector<const Use *, 4> CBUses;
212 ImmutableCallSite ICS(&getAnchorValue());
213 AbstractCallSite::getCallbackUses(ICS, CBUses);
214 for (const Use *U : CBUses) {
215 AbstractCallSite ACS(U);
216 assert(ACS && ACS.isCallbackCall());
217 if (!ACS.getCalledFunction())
218 continue;
219
220 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
221
222 // Test if the underlying call site operand is argument number u of the
223 // callback callee.
224 if (ACS.getCallArgOperandNo(u) != ArgNo)
225 continue;
226
227 assert(ACS.getCalledFunction()->arg_size() > u &&
228 "ACS mapped into var-args arguments!");
229 if (CBCandidateArg.hasValue()) {
230 CBCandidateArg = nullptr;
231 break;
232 }
233 CBCandidateArg = ACS.getCalledFunction()->getArg(u);
234 }
235 }
236
237 // If we found a unique callback candidate argument, return it.
238 if (CBCandidateArg.hasValue() && CBCandidateArg.getValue())
239 return CBCandidateArg.getValue();
240
241 // If no callbacks were found, or none used the underlying call site operand
242 // exclusively, use the direct callee argument if available.
243 const Function *Callee = ICS.getCalledFunction();
244 if (Callee && Callee->arg_size() > unsigned(ArgNo))
245 return Callee->getArg(ArgNo);
246
247 return nullptr;
248}
249
Johannes Doerfert214ed3f2020-01-11 23:35:45 -0600250static Optional<ConstantInt *>
251getAssumedConstant(Attributor &A, const Value &V, const AbstractAttribute &AA,
252 bool &UsedAssumedInformation) {
253 const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
254 AA, IRPosition::value(V), /* TrackDependence */ false);
255 Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A);
256 bool IsKnown = ValueSimplifyAA.isKnown();
257 UsedAssumedInformation |= !IsKnown;
258 if (!SimplifiedV.hasValue()) {
259 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
260 return llvm::None;
261 }
262 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) {
263 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
264 return llvm::None;
265 }
266 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue());
267 if (CI)
268 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
269 return CI;
270}
271
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -0600272/// Get pointer operand of memory accessing instruction. If \p I is
273/// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
274/// is set to false and the instruction is volatile, return nullptr.
275static const Value *getPointerOperand(const Instruction *I,
276 bool AllowVolatile) {
277 if (auto *LI = dyn_cast<LoadInst>(I)) {
278 if (!AllowVolatile && LI->isVolatile())
279 return nullptr;
280 return LI->getPointerOperand();
281 }
282
283 if (auto *SI = dyn_cast<StoreInst>(I)) {
284 if (!AllowVolatile && SI->isVolatile())
285 return nullptr;
286 return SI->getPointerOperand();
287 }
288
289 if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
290 if (!AllowVolatile && CXI->isVolatile())
291 return nullptr;
292 return CXI->getPointerOperand();
293 }
294
295 if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
296 if (!AllowVolatile && RMWI->isVolatile())
297 return nullptr;
298 return RMWI->getPointerOperand();
299 }
300
301 return nullptr;
302}
303
Johannes Doerfert89c2e732019-10-30 17:20:20 -0500304/// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
305/// advanced by \p Offset bytes. To aid later analysis the method tries to build
306/// getelement pointer instructions that traverse the natural type of \p Ptr if
307/// possible. If that fails, the remaining offset is adjusted byte-wise, hence
308/// through a cast to i8*.
309///
310/// TODO: This could probably live somewhere more prominantly if it doesn't
311/// already exist.
312static Value *constructPointer(Type *ResTy, Value *Ptr, int64_t Offset,
313 IRBuilder<NoFolder> &IRB, const DataLayout &DL) {
314 assert(Offset >= 0 && "Negative offset not supported yet!");
315 LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
316 << "-bytes as " << *ResTy << "\n");
317
318 // The initial type we are trying to traverse to get nice GEPs.
319 Type *Ty = Ptr->getType();
320
321 SmallVector<Value *, 4> Indices;
322 std::string GEPName = Ptr->getName().str();
323 while (Offset) {
324 uint64_t Idx, Rem;
325
326 if (auto *STy = dyn_cast<StructType>(Ty)) {
327 const StructLayout *SL = DL.getStructLayout(STy);
328 if (int64_t(SL->getSizeInBytes()) < Offset)
329 break;
330 Idx = SL->getElementContainingOffset(Offset);
331 assert(Idx < STy->getNumElements() && "Offset calculation error!");
332 Rem = Offset - SL->getElementOffset(Idx);
333 Ty = STy->getElementType(Idx);
334 } else if (auto *PTy = dyn_cast<PointerType>(Ty)) {
335 Ty = PTy->getElementType();
336 if (!Ty->isSized())
337 break;
338 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
339 assert(ElementSize && "Expected type with size!");
340 Idx = Offset / ElementSize;
341 Rem = Offset % ElementSize;
342 } else {
343 // Non-aggregate type, we cast and make byte-wise progress now.
344 break;
345 }
346
347 LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset
348 << " Idx: " << Idx << " Rem: " << Rem << "\n");
349
350 GEPName += "." + std::to_string(Idx);
351 Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx));
352 Offset = Rem;
353 }
354
355 // Create a GEP if we collected indices above.
356 if (Indices.size())
357 Ptr = IRB.CreateGEP(Ptr, Indices, GEPName);
358
359 // If an offset is left we use byte-wise adjustment.
360 if (Offset) {
361 Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
362 Ptr = IRB.CreateGEP(Ptr, IRB.getInt32(Offset),
363 GEPName + ".b" + Twine(Offset));
364 }
365
366 // Ensure the result has the requested type.
367 Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
368
369 LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
370 return Ptr;
371}
372
Johannes Doerfertdef99282019-08-14 21:29:37 +0000373/// Recursively visit all values that might become \p IRP at some point. This
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000374/// will be done by looking through cast instructions, selects, phis, and calls
Johannes Doerfertdef99282019-08-14 21:29:37 +0000375/// with the "returned" attribute. Once we cannot look through the value any
376/// further, the callback \p VisitValueCB is invoked and passed the current
377/// value, the \p State, and a flag to indicate if we stripped anything. To
378/// limit how much effort is invested, we will never visit more values than
379/// specified by \p MaxValues.
380template <typename AAType, typename StateTy>
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +0000381static bool genericValueTraversal(
Johannes Doerfertdef99282019-08-14 21:29:37 +0000382 Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State,
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000383 const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB,
Johannes Doerfertdef99282019-08-14 21:29:37 +0000384 int MaxValues = 8) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000385
Johannes Doerfertdef99282019-08-14 21:29:37 +0000386 const AAIsDead *LivenessAA = nullptr;
387 if (IRP.getAnchorScope())
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000388 LivenessAA = &A.getAAFor<AAIsDead>(
Johannes Doerfert19b00432019-08-26 17:48:05 +0000389 QueryingAA, IRPosition::function(*IRP.getAnchorScope()),
390 /* TrackDependence */ false);
391 bool AnyDead = false;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000392
393 // TODO: Use Positions here to allow context sensitivity in VisitValueCB
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000394 SmallPtrSet<Value *, 16> Visited;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000395 SmallVector<Value *, 16> Worklist;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000396 Worklist.push_back(&IRP.getAssociatedValue());
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000397
398 int Iteration = 0;
399 do {
400 Value *V = Worklist.pop_back_val();
401
402 // Check if we should process the current value. To prevent endless
403 // recursion keep a record of the values we followed!
Johannes Doerfertdef99282019-08-14 21:29:37 +0000404 if (!Visited.insert(V).second)
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000405 continue;
406
407 // Make sure we limit the compile time for complex expressions.
408 if (Iteration++ >= MaxValues)
409 return false;
410
411 // Explicitly look through calls with a "returned" attribute if we do
412 // not have a pointer as stripPointerCasts only works on them.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000413 Value *NewV = nullptr;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000414 if (V->getType()->isPointerTy()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000415 NewV = V->stripPointerCasts();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000416 } else {
417 CallSite CS(V);
418 if (CS && CS.getCalledFunction()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000419 for (Argument &Arg : CS.getCalledFunction()->args())
420 if (Arg.hasReturnedAttr()) {
421 NewV = CS.getArgOperand(Arg.getArgNo());
422 break;
423 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000424 }
425 }
Johannes Doerfertdef99282019-08-14 21:29:37 +0000426 if (NewV && NewV != V) {
427 Worklist.push_back(NewV);
428 continue;
429 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000430
431 // Look through select instructions, visit both potential values.
432 if (auto *SI = dyn_cast<SelectInst>(V)) {
433 Worklist.push_back(SI->getTrueValue());
434 Worklist.push_back(SI->getFalseValue());
435 continue;
436 }
437
Johannes Doerfertdef99282019-08-14 21:29:37 +0000438 // Look through phi nodes, visit all live operands.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000439 if (auto *PHI = dyn_cast<PHINode>(V)) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000440 assert(LivenessAA &&
441 "Expected liveness in the presence of instructions!");
Johannes Doerfertdef99282019-08-14 21:29:37 +0000442 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
443 const BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
Johannes Doerfert19b00432019-08-26 17:48:05 +0000444 if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +0000445 AnyDead = true;
Johannes Doerfert19b00432019-08-26 17:48:05 +0000446 continue;
447 }
448 Worklist.push_back(PHI->getIncomingValue(u));
Johannes Doerfertdef99282019-08-14 21:29:37 +0000449 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000450 continue;
451 }
452
453 // Once a leaf is reached we inform the user through the callback.
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000454 if (!VisitValueCB(*V, State, Iteration > 1))
455 return false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000456 } while (!Worklist.empty());
457
Johannes Doerfert19b00432019-08-26 17:48:05 +0000458 // If we actually used liveness information so we have to record a dependence.
459 if (AnyDead)
Johannes Doerfert680f6382019-11-02 02:48:05 -0500460 A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
Johannes Doerfert19b00432019-08-26 17:48:05 +0000461
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000462 // All values have been visited.
463 return true;
464}
465
Johannes Doerfertaade7822019-06-05 03:02:24 +0000466/// Return true if \p New is equal or worse than \p Old.
467static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
468 if (!Old.isIntAttribute())
469 return true;
470
471 return Old.getValueAsInt() >= New.getValueAsInt();
472}
473
474/// Return true if the information provided by \p Attr was added to the
475/// attribute list \p Attrs. This is only the case if it was not already present
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000476/// in \p Attrs at the position describe by \p PK and \p AttrIdx.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000477static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000478 AttributeList &Attrs, int AttrIdx) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000479
480 if (Attr.isEnumAttribute()) {
481 Attribute::AttrKind Kind = Attr.getKindAsEnum();
482 if (Attrs.hasAttribute(AttrIdx, Kind))
483 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
484 return false;
485 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
486 return true;
487 }
488 if (Attr.isStringAttribute()) {
489 StringRef Kind = Attr.getKindAsString();
490 if (Attrs.hasAttribute(AttrIdx, Kind))
491 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
492 return false;
493 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
494 return true;
495 }
Hideto Ueno19c07af2019-07-23 08:16:17 +0000496 if (Attr.isIntAttribute()) {
497 Attribute::AttrKind Kind = Attr.getKindAsEnum();
498 if (Attrs.hasAttribute(AttrIdx, Kind))
499 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
500 return false;
501 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
502 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
503 return true;
504 }
Johannes Doerfertaade7822019-06-05 03:02:24 +0000505
506 llvm_unreachable("Expected enum or string attribute!");
507}
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000508
Hideto Ueno0f4383f2019-11-27 14:41:12 +0000509static const Value *
510getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset,
511 const DataLayout &DL,
512 bool AllowNonInbounds = false) {
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -0600513 const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000514 if (!Ptr)
515 return nullptr;
516
517 return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL,
Hideto Ueno0f4383f2019-11-27 14:41:12 +0000518 AllowNonInbounds);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000519}
Johannes Doerfertaade7822019-06-05 03:02:24 +0000520
Johannes Doerfertece81902019-08-12 22:05:53 +0000521ChangeStatus AbstractAttribute::update(Attributor &A) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000522 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
523 if (getState().isAtFixpoint())
524 return HasChanged;
525
526 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
527
Johannes Doerfertece81902019-08-12 22:05:53 +0000528 HasChanged = updateImpl(A);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000529
530 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
531 << "\n");
532
533 return HasChanged;
534}
535
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000536ChangeStatus
Johannes Doerfertb2083c52019-10-20 22:46:48 -0500537IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000538 const ArrayRef<Attribute> &DeducedAttrs) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000539 Function *ScopeFn = IRP.getAssociatedFunction();
Kristina Brooks26e60f02019-08-06 19:53:19 +0000540 IRPosition::Kind PK = IRP.getPositionKind();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000541
Johannes Doerfertaade7822019-06-05 03:02:24 +0000542 // In the following some generic code that will manifest attributes in
543 // DeducedAttrs if they improve the current IR. Due to the different
544 // annotation positions we use the underlying AttributeList interface.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000545
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000546 AttributeList Attrs;
547 switch (PK) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000548 case IRPosition::IRP_INVALID:
549 case IRPosition::IRP_FLOAT:
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000550 return ChangeStatus::UNCHANGED;
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000551 case IRPosition::IRP_ARGUMENT:
552 case IRPosition::IRP_FUNCTION:
553 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000554 Attrs = ScopeFn->getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000555 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000556 case IRPosition::IRP_CALL_SITE:
557 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000558 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000559 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000560 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000561 }
562
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000563 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000564 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000565 for (const Attribute &Attr : DeducedAttrs) {
Kristina Brooks26e60f02019-08-06 19:53:19 +0000566 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000567 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000568
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000569 HasChanged = ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000570 }
571
572 if (HasChanged == ChangeStatus::UNCHANGED)
573 return HasChanged;
574
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000575 switch (PK) {
576 case IRPosition::IRP_ARGUMENT:
577 case IRPosition::IRP_FUNCTION:
578 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000579 ScopeFn->setAttributes(Attrs);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000580 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000581 case IRPosition::IRP_CALL_SITE:
582 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000583 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000584 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
Johannes Doerfert4395b312019-08-14 21:46:28 +0000585 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000586 case IRPosition::IRP_INVALID:
Johannes Doerfert4395b312019-08-14 21:46:28 +0000587 case IRPosition::IRP_FLOAT:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000588 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000589 }
590
591 return HasChanged;
592}
593
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000594const IRPosition IRPosition::EmptyKey(255);
595const IRPosition IRPosition::TombstoneKey(256);
596
597SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
598 IRPositions.emplace_back(IRP);
599
600 ImmutableCallSite ICS(&IRP.getAnchorValue());
601 switch (IRP.getPositionKind()) {
602 case IRPosition::IRP_INVALID:
603 case IRPosition::IRP_FLOAT:
604 case IRPosition::IRP_FUNCTION:
605 return;
606 case IRPosition::IRP_ARGUMENT:
607 case IRPosition::IRP_RETURNED:
608 IRPositions.emplace_back(
609 IRPosition::function(*IRP.getAssociatedFunction()));
610 return;
611 case IRPosition::IRP_CALL_SITE:
612 assert(ICS && "Expected call site!");
613 // TODO: We need to look at the operand bundles similar to the redirection
614 // in CallBase.
615 if (!ICS.hasOperandBundles())
616 if (const Function *Callee = ICS.getCalledFunction())
617 IRPositions.emplace_back(IRPosition::function(*Callee));
618 return;
619 case IRPosition::IRP_CALL_SITE_RETURNED:
620 assert(ICS && "Expected call site!");
621 // TODO: We need to look at the operand bundles similar to the redirection
622 // in CallBase.
623 if (!ICS.hasOperandBundles()) {
624 if (const Function *Callee = ICS.getCalledFunction()) {
625 IRPositions.emplace_back(IRPosition::returned(*Callee));
626 IRPositions.emplace_back(IRPosition::function(*Callee));
627 }
628 }
629 IRPositions.emplace_back(
630 IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction())));
631 return;
632 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
633 int ArgNo = IRP.getArgNo();
634 assert(ICS && ArgNo >= 0 && "Expected call site!");
635 // TODO: We need to look at the operand bundles similar to the redirection
636 // in CallBase.
637 if (!ICS.hasOperandBundles()) {
638 const Function *Callee = ICS.getCalledFunction();
639 if (Callee && Callee->arg_size() > unsigned(ArgNo))
640 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
641 if (Callee)
642 IRPositions.emplace_back(IRPosition::function(*Callee));
643 }
644 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
645 return;
646 }
647 }
648}
649
Johannes Doerfert1097fab2019-10-07 21:07:57 +0000650bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
651 bool IgnoreSubsumingPositions) const {
652 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000653 for (Attribute::AttrKind AK : AKs)
654 if (EquivIRP.getAttr(AK).getKindAsEnum() == AK)
655 return true;
Johannes Doerfert1097fab2019-10-07 21:07:57 +0000656 // The first position returned by the SubsumingPositionIterator is
657 // always the position itself. If we ignore subsuming positions we
658 // are done after the first iteration.
659 if (IgnoreSubsumingPositions)
660 break;
661 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000662 return false;
663}
664
665void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
Johannes Doerfert6abd01e2019-12-12 15:02:36 -0600666 SmallVectorImpl<Attribute> &Attrs,
667 bool IgnoreSubsumingPositions) const {
668 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000669 for (Attribute::AttrKind AK : AKs) {
670 const Attribute &Attr = EquivIRP.getAttr(AK);
671 if (Attr.getKindAsEnum() == AK)
672 Attrs.push_back(Attr);
673 }
Johannes Doerfert6abd01e2019-12-12 15:02:36 -0600674 // The first position returned by the SubsumingPositionIterator is
675 // always the position itself. If we ignore subsuming positions we
676 // are done after the first iteration.
677 if (IgnoreSubsumingPositions)
678 break;
679 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000680}
681
682void IRPosition::verify() {
683 switch (KindOrArgNo) {
684 default:
685 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!");
686 assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) &&
687 "Expected call base or argument for positive attribute index!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000688 if (isa<Argument>(AnchorVal)) {
689 assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000690 "Argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000691 assert(cast<Argument>(AnchorVal) == &getAssociatedValue() &&
692 "Associated value mismatch!");
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000693 } else {
Simon Pilgrim920b0402019-08-29 10:08:45 +0000694 assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000695 "Call site argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000696 assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) ==
697 &getAssociatedValue() &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000698 "Associated value mismatch!");
699 }
700 break;
701 case IRP_INVALID:
702 assert(!AnchorVal && "Expected no value for an invalid position!");
703 break;
704 case IRP_FLOAT:
705 assert((!isa<CallBase>(&getAssociatedValue()) &&
706 !isa<Argument>(&getAssociatedValue())) &&
707 "Expected specialized kind for call base and argument values!");
708 break;
709 case IRP_RETURNED:
710 assert(isa<Function>(AnchorVal) &&
711 "Expected function for a 'returned' position!");
712 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
713 break;
714 case IRP_CALL_SITE_RETURNED:
715 assert((isa<CallBase>(AnchorVal)) &&
716 "Expected call base for 'call site returned' position!");
717 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
718 break;
719 case IRP_CALL_SITE:
720 assert((isa<CallBase>(AnchorVal)) &&
721 "Expected call base for 'call site function' position!");
722 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
723 break;
724 case IRP_FUNCTION:
725 assert(isa<Function>(AnchorVal) &&
726 "Expected function for a 'function' position!");
727 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
728 break;
729 }
730}
731
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +0000732namespace {
Johannes Doerfert1a746452019-10-20 22:28:49 -0500733/// Helper function to clamp a state \p S of type \p StateType with the
Johannes Doerfert234eda52019-08-16 19:51:23 +0000734/// information in \p R and indicate/return if \p S did change (as-in update is
735/// required to be run again).
Johannes Doerfert234eda52019-08-16 19:51:23 +0000736template <typename StateType>
Johannes Doerfert1a746452019-10-20 22:28:49 -0500737ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
Johannes Doerfert234eda52019-08-16 19:51:23 +0000738 auto Assumed = S.getAssumed();
739 S ^= R;
740 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
741 : ChangeStatus::CHANGED;
742}
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000743
Johannes Doerfert234eda52019-08-16 19:51:23 +0000744/// Clamp the information known for all returned values of a function
745/// (identified by \p QueryingAA) into \p S.
746template <typename AAType, typename StateType = typename AAType::StateType>
747static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA,
748 StateType &S) {
749 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600750 << QueryingAA << " into " << S << "\n");
Johannes Doerfert234eda52019-08-16 19:51:23 +0000751
752 assert((QueryingAA.getIRPosition().getPositionKind() ==
753 IRPosition::IRP_RETURNED ||
754 QueryingAA.getIRPosition().getPositionKind() ==
755 IRPosition::IRP_CALL_SITE_RETURNED) &&
756 "Can only clamp returned value states for a function returned or call "
757 "site returned position!");
758
759 // Use an optional state as there might not be any return values and we want
760 // to join (IntegerState::operator&) the state of all there are.
761 Optional<StateType> T;
762
763 // Callback for each possibly returned value.
764 auto CheckReturnValue = [&](Value &RV) -> bool {
765 const IRPosition &RVPos = IRPosition::value(RV);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000766 const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos);
767 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
768 << " @ " << RVPos << "\n");
769 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000770 if (T.hasValue())
771 *T &= AAS;
772 else
773 T = AAS;
774 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
775 << "\n");
776 return T->isValidState();
777 };
778
779 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
780 S.indicatePessimisticFixpoint();
781 else if (T.hasValue())
782 S ^= *T;
783}
784
Hideto Ueno08daf8c2019-10-08 15:20:19 +0000785/// Helper class to compose two generic deduction
786template <typename AAType, typename Base, typename StateType,
787 template <typename...> class F, template <typename...> class G>
788struct AAComposeTwoGenericDeduction
789 : public F<AAType, G<AAType, Base, StateType>, StateType> {
790 AAComposeTwoGenericDeduction(const IRPosition &IRP)
791 : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {}
792
793 /// See AbstractAttribute::updateImpl(...).
794 ChangeStatus updateImpl(Attributor &A) override {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +0100795 ChangeStatus ChangedF =
796 F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A);
Johannes Doerfertdb6efb02019-10-13 20:40:10 +0000797 ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A);
798 return ChangedF | ChangedG;
Hideto Ueno08daf8c2019-10-08 15:20:19 +0000799 }
800};
801
Johannes Doerfert234eda52019-08-16 19:51:23 +0000802/// Helper class for generic deduction: return value -> returned position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000803template <typename AAType, typename Base,
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600804 typename StateType = typename Base::StateType>
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000805struct AAReturnedFromReturnedValues : public Base {
806 AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000807
808 /// See AbstractAttribute::updateImpl(...).
809 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600810 StateType S(StateType::getBestState(this->getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000811 clampReturnedValueStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000812 // TODO: If we know we visited all returned values, thus no are assumed
813 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000814 return clampStateAndIndicateChange<StateType>(this->getState(), S);
815 }
816};
817
818/// Clamp the information known at all call sites for a given argument
819/// (identified by \p QueryingAA) into \p S.
820template <typename AAType, typename StateType = typename AAType::StateType>
821static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
822 StateType &S) {
823 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600824 << QueryingAA << " into " << S << "\n");
Johannes Doerfert234eda52019-08-16 19:51:23 +0000825
826 assert(QueryingAA.getIRPosition().getPositionKind() ==
827 IRPosition::IRP_ARGUMENT &&
828 "Can only clamp call site argument states for an argument position!");
829
830 // Use an optional state as there might not be any return values and we want
831 // to join (IntegerState::operator&) the state of all there are.
832 Optional<StateType> T;
833
834 // The argument number which is also the call site argument number.
835 unsigned ArgNo = QueryingAA.getIRPosition().getArgNo();
836
Johannes Doerfert661db042019-10-07 23:14:58 +0000837 auto CallSiteCheck = [&](AbstractCallSite ACS) {
838 const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
839 // Check if a coresponding argument was found or if it is on not associated
840 // (which can happen for callback calls).
841 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
842 return false;
843
844 const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos);
845 LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
846 << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000847 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000848 if (T.hasValue())
849 *T &= AAS;
850 else
851 T = AAS;
852 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
853 << "\n");
854 return T->isValidState();
855 };
856
Johannes Doerfert368f7ee2019-12-30 16:12:36 -0600857 bool AllCallSitesKnown;
858 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
859 AllCallSitesKnown))
Johannes Doerfert234eda52019-08-16 19:51:23 +0000860 S.indicatePessimisticFixpoint();
861 else if (T.hasValue())
862 S ^= *T;
863}
864
865/// Helper class for generic deduction: call site argument -> argument position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000866template <typename AAType, typename Base,
867 typename StateType = typename AAType::StateType>
868struct AAArgumentFromCallSiteArguments : public Base {
869 AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000870
871 /// See AbstractAttribute::updateImpl(...).
872 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertea5fabe2020-01-28 09:46:15 -0600873 StateType S(StateType::getBestState(this->getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000874 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000875 // TODO: If we know we visited all incoming values, thus no are assumed
876 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000877 return clampStateAndIndicateChange<StateType>(this->getState(), S);
878 }
879};
880
881/// Helper class for generic replication: function returned -> cs returned.
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000882template <typename AAType, typename Base,
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600883 typename StateType = typename Base::StateType>
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000884struct AACallSiteReturnedFromReturned : public Base {
885 AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000886
887 /// See AbstractAttribute::updateImpl(...).
888 ChangeStatus updateImpl(Attributor &A) override {
889 assert(this->getIRPosition().getPositionKind() ==
890 IRPosition::IRP_CALL_SITE_RETURNED &&
891 "Can only wrap function returned positions for call site returned "
892 "positions!");
893 auto &S = this->getState();
894
895 const Function *AssociatedFunction =
896 this->getIRPosition().getAssociatedFunction();
897 if (!AssociatedFunction)
898 return S.indicatePessimisticFixpoint();
899
900 IRPosition FnPos = IRPosition::returned(*AssociatedFunction);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000901 const AAType &AA = A.getAAFor<AAType>(*this, FnPos);
Johannes Doerfert234eda52019-08-16 19:51:23 +0000902 return clampStateAndIndicateChange(
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600903 S, static_cast<const StateType &>(AA.getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000904 }
905};
906
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000907/// Helper class for generic deduction using must-be-executed-context
908/// Base class is required to have `followUse` method.
909
910/// bool followUse(Attributor &A, const Use *U, const Instruction *I)
Simon Pilgrimf7aee612019-10-10 15:25:16 +0000911/// U - Underlying use.
912/// I - The user of the \p U.
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000913/// `followUse` returns true if the value should be tracked transitively.
914
915template <typename AAType, typename Base,
916 typename StateType = typename AAType::StateType>
917struct AAFromMustBeExecutedContext : public Base {
918 AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {}
919
920 void initialize(Attributor &A) override {
921 Base::initialize(A);
Johannes Doerfertb2083c52019-10-20 22:46:48 -0500922 const IRPosition &IRP = this->getIRPosition();
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000923 Instruction *CtxI = IRP.getCtxI();
924
925 if (!CtxI)
926 return;
927
928 for (const Use &U : IRP.getAssociatedValue().uses())
929 Uses.insert(&U);
930 }
931
932 /// See AbstractAttribute::updateImpl(...).
933 ChangeStatus updateImpl(Attributor &A) override {
934 auto BeforeState = this->getState();
935 auto &S = this->getState();
936 Instruction *CtxI = this->getIRPosition().getCtxI();
937 if (!CtxI)
938 return ChangeStatus::UNCHANGED;
939
940 MustBeExecutedContextExplorer &Explorer =
941 A.getInfoCache().getMustBeExecutedContextExplorer();
942
Johannes Doerfert0be9cf22019-10-14 17:29:05 -0500943 auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +0100944 for (unsigned u = 0; u < Uses.size(); ++u) {
Johannes Doerferteb4f41d2019-10-30 17:21:53 -0500945 const Use *U = Uses[u];
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000946 if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -0500947 bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000948 if (Found && Base::followUse(A, U, UserI))
949 for (const Use &Us : UserI->uses())
Johannes Doerferteb4f41d2019-10-30 17:21:53 -0500950 Uses.insert(&Us);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000951 }
952 }
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000953
954 return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
955 }
956
957private:
958 /// Container for (transitive) uses of the associated value.
959 SetVector<const Use *> Uses;
960};
961
962template <typename AAType, typename Base,
963 typename StateType = typename AAType::StateType>
964using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext =
965 AAComposeTwoGenericDeduction<AAType, Base, StateType,
966 AAFromMustBeExecutedContext,
967 AAArgumentFromCallSiteArguments>;
968
969template <typename AAType, typename Base,
970 typename StateType = typename AAType::StateType>
971using AACallSiteReturnedFromReturnedAndMustBeExecutedContext =
972 AAComposeTwoGenericDeduction<AAType, Base, StateType,
973 AAFromMustBeExecutedContext,
974 AACallSiteReturnedFromReturned>;
975
Stefan Stipanovic53605892019-06-27 11:27:54 +0000976/// -----------------------NoUnwind Function Attribute--------------------------
977
Johannes Doerfert344d0382019-08-07 22:34:26 +0000978struct AANoUnwindImpl : AANoUnwind {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000979 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
Stefan Stipanovic53605892019-06-27 11:27:54 +0000980
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000981 const std::string getAsStr() const override {
Stefan Stipanovic53605892019-06-27 11:27:54 +0000982 return getAssumed() ? "nounwind" : "may-unwind";
983 }
984
985 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +0000986 ChangeStatus updateImpl(Attributor &A) override {
987 auto Opcodes = {
988 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
989 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
990 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
991
992 auto CheckForNoUnwind = [&](Instruction &I) {
993 if (!I.mayThrow())
994 return true;
995
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000996 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
997 const auto &NoUnwindAA =
998 A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS));
999 return NoUnwindAA.isAssumedNoUnwind();
1000 }
1001 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001002 };
1003
1004 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
1005 return indicatePessimisticFixpoint();
1006
1007 return ChangeStatus::UNCHANGED;
1008 }
Stefan Stipanovic53605892019-06-27 11:27:54 +00001009};
1010
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001011struct AANoUnwindFunction final : public AANoUnwindImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001012 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001013
1014 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001015 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001016};
1017
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001018/// NoUnwind attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001019struct AANoUnwindCallSite final : AANoUnwindImpl {
1020 AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
1021
1022 /// See AbstractAttribute::initialize(...).
1023 void initialize(Attributor &A) override {
1024 AANoUnwindImpl::initialize(A);
1025 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001026 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001027 indicatePessimisticFixpoint();
1028 }
1029
1030 /// See AbstractAttribute::updateImpl(...).
1031 ChangeStatus updateImpl(Attributor &A) override {
1032 // TODO: Once we have call site specific value information we can provide
1033 // call site specific liveness information and then it makes
1034 // sense to specialize attributes for call sites arguments instead of
1035 // redirecting requests to the callee argument.
1036 Function *F = getAssociatedFunction();
1037 const IRPosition &FnPos = IRPosition::function(*F);
1038 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos);
1039 return clampStateAndIndicateChange(
1040 getState(),
1041 static_cast<const AANoUnwind::StateType &>(FnAA.getState()));
1042 }
1043
1044 /// See AbstractAttribute::trackStatistics()
1045 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
1046};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001047
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001048/// --------------------- Function Return Values -------------------------------
1049
1050/// "Attribute" that collects all potential returned values and the return
1051/// instructions that they arise from.
1052///
1053/// If there is a unique returned value R, the manifest method will:
1054/// - mark R with the "returned" attribute, if R is an argument.
Johannes Doerferteccdf082019-08-05 23:35:12 +00001055class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001056
1057 /// Mapping of values potentially returned by the associated function to the
1058 /// return instructions that might return them.
Johannes Doerferta4a308c2019-08-26 17:51:23 +00001059 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001060
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001061 /// Mapping to remember the number of returned values for a call site such
1062 /// that we can avoid updates if nothing changed.
1063 DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
1064
1065 /// Set of unresolved calls returned by the associated function.
Johannes Doerfert695089e2019-08-23 15:23:49 +00001066 SmallSetVector<CallBase *, 4> UnresolvedCalls;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001067
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001068 /// State flags
1069 ///
1070 ///{
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001071 bool IsFixed = false;
1072 bool IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001073 ///}
1074
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001075public:
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001076 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001077
1078 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001079 void initialize(Attributor &A) override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001080 // Reset the state.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001081 IsFixed = false;
1082 IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001083 ReturnedValues.clear();
1084
Johannes Doerfertdef99282019-08-14 21:29:37 +00001085 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001086 if (!F) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001087 indicatePessimisticFixpoint();
1088 return;
1089 }
Johannes Doerferte273ac42020-01-12 00:13:03 -06001090 assert(!F->getReturnType()->isVoidTy() &&
1091 "Did not expect a void return type!");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001092
1093 // The map from instruction opcodes to those instructions in the function.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001094 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001095
1096 // Look through all arguments, if one is marked as returned we are done.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001097 for (Argument &Arg : F->args()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001098 if (Arg.hasReturnedAttr()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001099 auto &ReturnInstSet = ReturnedValues[&Arg];
1100 for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
1101 ReturnInstSet.insert(cast<ReturnInst>(RI));
1102
1103 indicateOptimisticFixpoint();
1104 return;
1105 }
1106 }
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001107
1108 if (!F->hasExactDefinition())
1109 indicatePessimisticFixpoint();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001110 }
1111
1112 /// See AbstractAttribute::manifest(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001113 ChangeStatus manifest(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001114
1115 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001116 AbstractState &getState() override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001117
1118 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001119 const AbstractState &getState() const override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001120
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001121 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00001122 ChangeStatus updateImpl(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001123
Johannes Doerfertdef99282019-08-14 21:29:37 +00001124 llvm::iterator_range<iterator> returned_values() override {
1125 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1126 }
1127
1128 llvm::iterator_range<const_iterator> returned_values() const override {
1129 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1130 }
1131
Johannes Doerfert695089e2019-08-23 15:23:49 +00001132 const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001133 return UnresolvedCalls;
1134 }
1135
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001136 /// Return the number of potential return values, -1 if unknown.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001137 size_t getNumReturnValues() const override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001138 return isValidState() ? ReturnedValues.size() : -1;
1139 }
1140
1141 /// Return an assumed unique return value if a single candidate is found. If
1142 /// there cannot be one, return a nullptr. If it is not clear yet, return the
1143 /// Optional::NoneType.
Johannes Doerfert14a04932019-08-07 22:27:24 +00001144 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001145
Johannes Doerfert14a04932019-08-07 22:27:24 +00001146 /// See AbstractState::checkForAllReturnedValues(...).
1147 bool checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00001148 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00001149 &Pred) const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001150
1151 /// Pretty print the attribute similar to the IR representation.
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001152 const std::string getAsStr() const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001153
1154 /// See AbstractState::isAtFixpoint().
1155 bool isAtFixpoint() const override { return IsFixed; }
1156
1157 /// See AbstractState::isValidState().
1158 bool isValidState() const override { return IsValidState; }
1159
1160 /// See AbstractState::indicateOptimisticFixpoint(...).
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001161 ChangeStatus indicateOptimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001162 IsFixed = true;
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001163 return ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001164 }
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001165
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001166 ChangeStatus indicatePessimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001167 IsFixed = true;
1168 IsValidState = false;
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001169 return ChangeStatus::CHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001170 }
1171};
1172
1173ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
1174 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1175
1176 // Bookkeeping.
1177 assert(isValidState());
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001178 STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
1179 "Number of function with known return values");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001180
1181 // Check if we have an assumed unique return value that we could manifest.
Johannes Doerfert14a04932019-08-07 22:27:24 +00001182 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001183
1184 if (!UniqueRV.hasValue() || !UniqueRV.getValue())
1185 return Changed;
1186
1187 // Bookkeeping.
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001188 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
1189 "Number of function with unique return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001190
Johannes Doerfert23400e612019-08-23 17:41:37 +00001191 // Callback to replace the uses of CB with the constant C.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06001192 auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) {
Johannes Doerfert8fa56c42019-10-11 01:45:32 +00001193 if (CB.getNumUses() == 0 || CB.isMustTailCall())
Johannes Doerfert23400e612019-08-23 17:41:37 +00001194 return ChangeStatus::UNCHANGED;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06001195 A.replaceAllUsesWith(CB, C);
Johannes Doerfert23400e612019-08-23 17:41:37 +00001196 return ChangeStatus::CHANGED;
1197 };
1198
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001199 // If the assumed unique return value is an argument, annotate it.
1200 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
Johannes Doerfertb2083c52019-10-20 22:46:48 -05001201 // TODO: This should be handled differently!
1202 this->AnchorVal = UniqueRVArg;
1203 this->KindOrArgNo = UniqueRVArg->getArgNo();
Johannes Doerfert23400e612019-08-23 17:41:37 +00001204 Changed = IRAttribute::manifest(A);
1205 } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
1206 // We can replace the returned value with the unique returned constant.
1207 Value &AnchorValue = getAnchorValue();
1208 if (Function *F = dyn_cast<Function>(&AnchorValue)) {
1209 for (const Use &U : F->uses())
1210 if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001211 if (CB->isCallee(&U)) {
1212 Constant *RVCCast =
Johannes Doerfert07d16422019-11-01 23:03:35 -05001213 CB->getType() == RVC->getType()
1214 ? RVC
1215 : ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001216 Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
1217 }
Johannes Doerfert23400e612019-08-23 17:41:37 +00001218 } else {
1219 assert(isa<CallBase>(AnchorValue) &&
1220 "Expcected a function or call base anchor!");
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001221 Constant *RVCCast =
Johannes Doerfert07d16422019-11-01 23:03:35 -05001222 AnchorValue.getType() == RVC->getType()
1223 ? RVC
1224 : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001225 Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
Johannes Doerfert23400e612019-08-23 17:41:37 +00001226 }
1227 if (Changed == ChangeStatus::CHANGED)
1228 STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
1229 "Number of function returns replaced by constant return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001230 }
1231
1232 return Changed;
1233}
1234
1235const std::string AAReturnedValuesImpl::getAsStr() const {
1236 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
Johannes Doerfert6471bb62019-08-04 18:39:28 +00001237 (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
Johannes Doerfertdef99282019-08-14 21:29:37 +00001238 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001239}
1240
Johannes Doerfert14a04932019-08-07 22:27:24 +00001241Optional<Value *>
1242AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1243 // If checkForAllReturnedValues provides a unique value, ignoring potential
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001244 // undef values that can also be present, it is assumed to be the actual
1245 // return value and forwarded to the caller of this method. If there are
1246 // multiple, a nullptr is returned indicating there cannot be a unique
1247 // returned value.
1248 Optional<Value *> UniqueRV;
1249
Johannes Doerfert14a04932019-08-07 22:27:24 +00001250 auto Pred = [&](Value &RV) -> bool {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001251 // If we found a second returned value and neither the current nor the saved
1252 // one is an undef, there is no unique returned value. Undefs are special
1253 // since we can pretend they have any value.
1254 if (UniqueRV.hasValue() && UniqueRV != &RV &&
1255 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
1256 UniqueRV = nullptr;
1257 return false;
1258 }
1259
1260 // Do not overwrite a value with an undef.
1261 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
1262 UniqueRV = &RV;
1263
1264 return true;
1265 };
1266
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001267 if (!A.checkForAllReturnedValues(Pred, *this))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001268 UniqueRV = nullptr;
1269
1270 return UniqueRV;
1271}
1272
Johannes Doerfert14a04932019-08-07 22:27:24 +00001273bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00001274 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00001275 &Pred) const {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001276 if (!isValidState())
1277 return false;
1278
1279 // Check all returned values but ignore call sites as long as we have not
1280 // encountered an overdefined one during an update.
1281 for (auto &It : ReturnedValues) {
1282 Value *RV = It.first;
1283
Johannes Doerfertdef99282019-08-14 21:29:37 +00001284 CallBase *CB = dyn_cast<CallBase>(RV);
1285 if (CB && !UnresolvedCalls.count(CB))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001286 continue;
1287
Johannes Doerfert695089e2019-08-23 15:23:49 +00001288 if (!Pred(*RV, It.second))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001289 return false;
1290 }
1291
1292 return true;
1293}
1294
Johannes Doerfertece81902019-08-12 22:05:53 +00001295ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001296 size_t NumUnresolvedCalls = UnresolvedCalls.size();
1297 bool Changed = false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001298
Johannes Doerfertdef99282019-08-14 21:29:37 +00001299 // State used in the value traversals starting in returned values.
1300 struct RVState {
1301 // The map in which we collect return values -> return instrs.
1302 decltype(ReturnedValues) &RetValsMap;
1303 // The flag to indicate a change.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001304 bool &Changed;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001305 // The return instrs we come from.
Johannes Doerfert695089e2019-08-23 15:23:49 +00001306 SmallSetVector<ReturnInst *, 4> RetInsts;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001307 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001308
Johannes Doerfertdef99282019-08-14 21:29:37 +00001309 // Callback for a leaf value returned by the associated function.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001310 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001311 auto Size = RVS.RetValsMap[&Val].size();
1312 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
1313 bool Inserted = RVS.RetValsMap[&Val].size() != Size;
1314 RVS.Changed |= Inserted;
1315 LLVM_DEBUG({
1316 if (Inserted)
1317 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
1318 << " => " << RVS.RetInsts.size() << "\n";
1319 });
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001320 return true;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001321 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001322
Johannes Doerfertdef99282019-08-14 21:29:37 +00001323 // Helper method to invoke the generic value traversal.
1324 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) {
1325 IRPosition RetValPos = IRPosition::value(RV);
1326 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this,
1327 RVS, VisitValueCB);
1328 };
Johannes Doerfertda4d8112019-08-01 16:21:54 +00001329
Johannes Doerfertdef99282019-08-14 21:29:37 +00001330 // Callback for all "return intructions" live in the associated function.
1331 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1332 ReturnInst &Ret = cast<ReturnInst>(I);
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001333 RVState RVS({ReturnedValues, Changed, {}});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001334 RVS.RetInsts.insert(&Ret);
Johannes Doerfertdef99282019-08-14 21:29:37 +00001335 return VisitReturnedValue(*Ret.getReturnValue(), RVS);
1336 };
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001337
Johannes Doerfertdef99282019-08-14 21:29:37 +00001338 // Start by discovering returned values from all live returned instructions in
1339 // the associated function.
1340 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1341 return indicatePessimisticFixpoint();
1342
1343 // Once returned values "directly" present in the code are handled we try to
1344 // resolve returned calls.
1345 decltype(ReturnedValues) NewRVsMap;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001346 for (auto &It : ReturnedValues) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001347 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first
1348 << " by #" << It.second.size() << " RIs\n");
1349 CallBase *CB = dyn_cast<CallBase>(It.first);
1350 if (!CB || UnresolvedCalls.count(CB))
1351 continue;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001352
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001353 if (!CB->getCalledFunction()) {
1354 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1355 << "\n");
1356 UnresolvedCalls.insert(CB);
1357 continue;
1358 }
1359
1360 // TODO: use the function scope once we have call site AAReturnedValues.
1361 const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1362 *this, IRPosition::function(*CB->getCalledFunction()));
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001363 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
Johannes Doerfert3d347e22019-12-13 23:35:45 -06001364 << RetValAA << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001365
1366 // Skip dead ends, thus if we do not know anything about the returned
1367 // call we mark it as unresolved and it will stay that way.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001368 if (!RetValAA.getState().isValidState()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001369 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1370 << "\n");
1371 UnresolvedCalls.insert(CB);
1372 continue;
1373 }
1374
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001375 // Do not try to learn partial information. If the callee has unresolved
1376 // return values we will treat the call as unresolved/opaque.
1377 auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1378 if (!RetValAAUnresolvedCalls.empty()) {
1379 UnresolvedCalls.insert(CB);
1380 continue;
1381 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001382
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001383 // Now check if we can track transitively returned values. If possible, thus
1384 // if all return value can be represented in the current scope, do so.
1385 bool Unresolved = false;
1386 for (auto &RetValAAIt : RetValAA.returned_values()) {
1387 Value *RetVal = RetValAAIt.first;
1388 if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1389 isa<Constant>(RetVal))
1390 continue;
1391 // Anything that did not fit in the above categories cannot be resolved,
1392 // mark the call as unresolved.
1393 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1394 "cannot be translated: "
1395 << *RetVal << "\n");
1396 UnresolvedCalls.insert(CB);
1397 Unresolved = true;
1398 break;
1399 }
1400
1401 if (Unresolved)
1402 continue;
1403
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001404 // Now track transitively returned values.
1405 unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1406 if (NumRetAA == RetValAA.getNumReturnValues()) {
1407 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1408 "changed since it was seen last\n");
1409 continue;
1410 }
1411 NumRetAA = RetValAA.getNumReturnValues();
1412
Johannes Doerfertdef99282019-08-14 21:29:37 +00001413 for (auto &RetValAAIt : RetValAA.returned_values()) {
1414 Value *RetVal = RetValAAIt.first;
1415 if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1416 // Arguments are mapped to call site operands and we begin the traversal
1417 // again.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001418 bool Unused = false;
1419 RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001420 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS);
1421 continue;
1422 } else if (isa<CallBase>(RetVal)) {
1423 // Call sites are resolved by the callee attribute over time, no need to
1424 // do anything for us.
1425 continue;
1426 } else if (isa<Constant>(RetVal)) {
1427 // Constants are valid everywhere, we can simply take them.
1428 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end());
1429 continue;
1430 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00001431 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001432 }
1433
Johannes Doerfertdef99282019-08-14 21:29:37 +00001434 // To avoid modifications to the ReturnedValues map while we iterate over it
1435 // we kept record of potential new entries in a copy map, NewRVsMap.
1436 for (auto &It : NewRVsMap) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001437 assert(!It.second.empty() && "Entry does not add anything.");
1438 auto &ReturnInsts = ReturnedValues[It.first];
1439 for (ReturnInst *RI : It.second)
Johannes Doerfert695089e2019-08-23 15:23:49 +00001440 if (ReturnInsts.insert(RI)) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001441 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1442 << *It.first << " => " << *RI << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001443 Changed = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001444 }
1445 }
1446
Johannes Doerfertdef99282019-08-14 21:29:37 +00001447 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1448 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001449}
1450
Johannes Doerfertdef99282019-08-14 21:29:37 +00001451struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1452 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1453
1454 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001455 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
Johannes Doerfertdef99282019-08-14 21:29:37 +00001456};
1457
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001458/// Returned values information for a call sites.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001459struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1460 AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1461
1462 /// See AbstractAttribute::initialize(...).
1463 void initialize(Attributor &A) override {
1464 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001465 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001466 // sense to specialize attributes for call sites instead of
1467 // redirecting requests to the callee.
1468 llvm_unreachable("Abstract attributes for returned values are not "
1469 "supported for call sites yet!");
1470 }
1471
1472 /// See AbstractAttribute::updateImpl(...).
1473 ChangeStatus updateImpl(Attributor &A) override {
1474 return indicatePessimisticFixpoint();
1475 }
1476
1477 /// See AbstractAttribute::trackStatistics()
1478 void trackStatistics() const override {}
1479};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001480
Stefan Stipanovic06263672019-07-11 21:37:40 +00001481/// ------------------------ NoSync Function Attribute -------------------------
1482
Johannes Doerfert344d0382019-08-07 22:34:26 +00001483struct AANoSyncImpl : AANoSync {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001484 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
Stefan Stipanovic06263672019-07-11 21:37:40 +00001485
Stefan Stipanoviccb5ecae2019-07-12 18:34:06 +00001486 const std::string getAsStr() const override {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001487 return getAssumed() ? "nosync" : "may-sync";
1488 }
1489
1490 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001491 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001492
Stefan Stipanovic06263672019-07-11 21:37:40 +00001493 /// Helper function used to determine whether an instruction is non-relaxed
1494 /// atomic. In other words, if an atomic instruction does not have unordered
1495 /// or monotonic ordering
1496 static bool isNonRelaxedAtomic(Instruction *I);
1497
1498 /// Helper function used to determine whether an instruction is volatile.
1499 static bool isVolatile(Instruction *I);
1500
Johannes Doerfertc7a1db32019-07-13 01:09:27 +00001501 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1502 /// memset).
Stefan Stipanovic06263672019-07-11 21:37:40 +00001503 static bool isNoSyncIntrinsic(Instruction *I);
1504};
1505
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001506bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001507 if (!I->isAtomic())
1508 return false;
1509
1510 AtomicOrdering Ordering;
1511 switch (I->getOpcode()) {
1512 case Instruction::AtomicRMW:
1513 Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1514 break;
1515 case Instruction::Store:
1516 Ordering = cast<StoreInst>(I)->getOrdering();
1517 break;
1518 case Instruction::Load:
1519 Ordering = cast<LoadInst>(I)->getOrdering();
1520 break;
1521 case Instruction::Fence: {
1522 auto *FI = cast<FenceInst>(I);
1523 if (FI->getSyncScopeID() == SyncScope::SingleThread)
1524 return false;
1525 Ordering = FI->getOrdering();
1526 break;
1527 }
1528 case Instruction::AtomicCmpXchg: {
1529 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1530 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1531 // Only if both are relaxed, than it can be treated as relaxed.
1532 // Otherwise it is non-relaxed.
1533 if (Success != AtomicOrdering::Unordered &&
1534 Success != AtomicOrdering::Monotonic)
1535 return true;
1536 if (Failure != AtomicOrdering::Unordered &&
1537 Failure != AtomicOrdering::Monotonic)
1538 return true;
1539 return false;
1540 }
1541 default:
1542 llvm_unreachable(
1543 "New atomic operations need to be known in the attributor.");
1544 }
1545
1546 // Relaxed.
1547 if (Ordering == AtomicOrdering::Unordered ||
1548 Ordering == AtomicOrdering::Monotonic)
1549 return false;
1550 return true;
1551}
1552
1553/// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1554/// FIXME: We should ipmrove the handling of intrinsics.
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001555bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001556 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1557 switch (II->getIntrinsicID()) {
1558 /// Element wise atomic memory intrinsics are can only be unordered,
1559 /// therefore nosync.
1560 case Intrinsic::memset_element_unordered_atomic:
1561 case Intrinsic::memmove_element_unordered_atomic:
1562 case Intrinsic::memcpy_element_unordered_atomic:
1563 return true;
1564 case Intrinsic::memset:
1565 case Intrinsic::memmove:
1566 case Intrinsic::memcpy:
1567 if (!cast<MemIntrinsic>(II)->isVolatile())
1568 return true;
1569 return false;
1570 default:
1571 return false;
1572 }
1573 }
1574 return false;
1575}
1576
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001577bool AANoSyncImpl::isVolatile(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001578 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
1579 "Calls should not be checked here");
1580
1581 switch (I->getOpcode()) {
1582 case Instruction::AtomicRMW:
1583 return cast<AtomicRMWInst>(I)->isVolatile();
1584 case Instruction::Store:
1585 return cast<StoreInst>(I)->isVolatile();
1586 case Instruction::Load:
1587 return cast<LoadInst>(I)->isVolatile();
1588 case Instruction::AtomicCmpXchg:
1589 return cast<AtomicCmpXchgInst>(I)->isVolatile();
1590 default:
1591 return false;
1592 }
1593}
1594
Johannes Doerfertece81902019-08-12 22:05:53 +00001595ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001596
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001597 auto CheckRWInstForNoSync = [&](Instruction &I) {
1598 /// We are looking for volatile instructions or Non-Relaxed atomics.
Pankaj Godeb9a26a82019-11-22 14:46:43 +05301599 /// FIXME: We should improve the handling of intrinsics.
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001600
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001601 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1602 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001603
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001604 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1605 if (ICS.hasFnAttr(Attribute::NoSync))
1606 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001607
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001608 const auto &NoSyncAA =
1609 A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS));
1610 if (NoSyncAA.isAssumedNoSync())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001611 return true;
1612 return false;
1613 }
Stefan Stipanovic06263672019-07-11 21:37:40 +00001614
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001615 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1616 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001617
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001618 return false;
1619 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001620
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001621 auto CheckForNoSync = [&](Instruction &I) {
1622 // At this point we handled all read/write effects and they are all
1623 // nosync, so they can be skipped.
1624 if (I.mayReadOrWriteMemory())
1625 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001626
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001627 // non-convergent and readnone imply nosync.
1628 return !ImmutableCallSite(&I).isConvergent();
1629 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001630
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001631 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1632 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001633 return indicatePessimisticFixpoint();
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001634
Stefan Stipanovic06263672019-07-11 21:37:40 +00001635 return ChangeStatus::UNCHANGED;
1636}
1637
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001638struct AANoSyncFunction final : public AANoSyncImpl {
1639 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1640
1641 /// See AbstractAttribute::trackStatistics()
1642 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1643};
1644
1645/// NoSync attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001646struct AANoSyncCallSite final : AANoSyncImpl {
1647 AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1648
1649 /// See AbstractAttribute::initialize(...).
1650 void initialize(Attributor &A) override {
1651 AANoSyncImpl::initialize(A);
1652 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001653 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001654 indicatePessimisticFixpoint();
1655 }
1656
1657 /// See AbstractAttribute::updateImpl(...).
1658 ChangeStatus updateImpl(Attributor &A) override {
1659 // TODO: Once we have call site specific value information we can provide
1660 // call site specific liveness information and then it makes
1661 // sense to specialize attributes for call sites arguments instead of
1662 // redirecting requests to the callee argument.
1663 Function *F = getAssociatedFunction();
1664 const IRPosition &FnPos = IRPosition::function(*F);
1665 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos);
1666 return clampStateAndIndicateChange(
1667 getState(), static_cast<const AANoSync::StateType &>(FnAA.getState()));
1668 }
1669
1670 /// See AbstractAttribute::trackStatistics()
1671 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1672};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001673
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001674/// ------------------------ No-Free Attributes ----------------------------
1675
Johannes Doerfert344d0382019-08-07 22:34:26 +00001676struct AANoFreeImpl : public AANoFree {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001677 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001678
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001679 /// See AbstractAttribute::updateImpl(...).
1680 ChangeStatus updateImpl(Attributor &A) override {
1681 auto CheckForNoFree = [&](Instruction &I) {
1682 ImmutableCallSite ICS(&I);
1683 if (ICS.hasFnAttr(Attribute::NoFree))
1684 return true;
1685
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001686 const auto &NoFreeAA =
1687 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS));
1688 return NoFreeAA.isAssumedNoFree();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001689 };
1690
1691 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1692 return indicatePessimisticFixpoint();
1693 return ChangeStatus::UNCHANGED;
1694 }
1695
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001696 /// See AbstractAttribute::getAsStr().
1697 const std::string getAsStr() const override {
1698 return getAssumed() ? "nofree" : "may-free";
1699 }
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001700};
1701
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001702struct AANoFreeFunction final : public AANoFreeImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001703 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001704
1705 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001706 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001707};
1708
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001709/// NoFree attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001710struct AANoFreeCallSite final : AANoFreeImpl {
1711 AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1712
1713 /// See AbstractAttribute::initialize(...).
1714 void initialize(Attributor &A) override {
1715 AANoFreeImpl::initialize(A);
1716 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001717 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001718 indicatePessimisticFixpoint();
1719 }
1720
1721 /// See AbstractAttribute::updateImpl(...).
1722 ChangeStatus updateImpl(Attributor &A) override {
1723 // TODO: Once we have call site specific value information we can provide
1724 // call site specific liveness information and then it makes
1725 // sense to specialize attributes for call sites arguments instead of
1726 // redirecting requests to the callee argument.
1727 Function *F = getAssociatedFunction();
1728 const IRPosition &FnPos = IRPosition::function(*F);
1729 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos);
1730 return clampStateAndIndicateChange(
1731 getState(), static_cast<const AANoFree::StateType &>(FnAA.getState()));
1732 }
1733
1734 /// See AbstractAttribute::trackStatistics()
1735 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1736};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001737
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001738/// NoFree attribute for floating values.
1739struct AANoFreeFloating : AANoFreeImpl {
1740 AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1741
1742 /// See AbstractAttribute::trackStatistics()
1743 void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
1744
1745 /// See Abstract Attribute::updateImpl(...).
1746 ChangeStatus updateImpl(Attributor &A) override {
1747 const IRPosition &IRP = getIRPosition();
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001748
1749 const auto &NoFreeAA =
1750 A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP));
1751 if (NoFreeAA.isAssumedNoFree())
1752 return ChangeStatus::UNCHANGED;
1753
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001754 Value &AssociatedValue = getIRPosition().getAssociatedValue();
Hideto Ueno63599bd2019-12-12 12:26:09 +00001755 auto Pred = [&](const Use &U, bool &Follow) -> bool {
1756 Instruction *UserI = cast<Instruction>(U.getUser());
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001757 if (auto *CB = dyn_cast<CallBase>(UserI)) {
Hideto Ueno63599bd2019-12-12 12:26:09 +00001758 if (CB->isBundleOperand(&U))
1759 return false;
1760 if (!CB->isArgOperand(&U))
1761 return true;
1762 unsigned ArgNo = CB->getArgOperandNo(&U);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001763
1764 const auto &NoFreeArg = A.getAAFor<AANoFree>(
1765 *this, IRPosition::callsite_argument(*CB, ArgNo));
Hideto Ueno63599bd2019-12-12 12:26:09 +00001766 return NoFreeArg.isAssumedNoFree();
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001767 }
1768
1769 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
1770 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
Hideto Ueno63599bd2019-12-12 12:26:09 +00001771 Follow = true;
1772 return true;
Hideto Ueno4ecf2552019-12-12 13:42:40 +00001773 }
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001774
1775 // Unknown user.
Hideto Ueno63599bd2019-12-12 12:26:09 +00001776 return false;
1777 };
1778 if (!A.checkForAllUses(Pred, *this, AssociatedValue))
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001779 return indicatePessimisticFixpoint();
Hideto Ueno63599bd2019-12-12 12:26:09 +00001780
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001781 return ChangeStatus::UNCHANGED;
1782 }
1783};
1784
1785/// NoFree attribute for a call site argument.
1786struct AANoFreeArgument final : AANoFreeFloating {
1787 AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1788
1789 /// See AbstractAttribute::trackStatistics()
1790 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
1791};
1792
1793/// NoFree attribute for call site arguments.
1794struct AANoFreeCallSiteArgument final : AANoFreeFloating {
1795 AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1796
1797 /// See AbstractAttribute::updateImpl(...).
1798 ChangeStatus updateImpl(Attributor &A) override {
1799 // TODO: Once we have call site specific value information we can provide
1800 // call site specific liveness information and then it makes
1801 // sense to specialize attributes for call sites arguments instead of
1802 // redirecting requests to the callee argument.
1803 Argument *Arg = getAssociatedArgument();
1804 if (!Arg)
1805 return indicatePessimisticFixpoint();
1806 const IRPosition &ArgPos = IRPosition::argument(*Arg);
1807 auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos);
1808 return clampStateAndIndicateChange(
1809 getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState()));
1810 }
1811
1812 /// See AbstractAttribute::trackStatistics()
1813 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
1814};
1815
1816/// NoFree attribute for function return value.
1817struct AANoFreeReturned final : AANoFreeFloating {
1818 AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {
1819 llvm_unreachable("NoFree is not applicable to function returns!");
1820 }
1821
1822 /// See AbstractAttribute::initialize(...).
1823 void initialize(Attributor &A) override {
1824 llvm_unreachable("NoFree is not applicable to function returns!");
1825 }
1826
1827 /// See AbstractAttribute::updateImpl(...).
1828 ChangeStatus updateImpl(Attributor &A) override {
1829 llvm_unreachable("NoFree is not applicable to function returns!");
1830 }
1831
1832 /// See AbstractAttribute::trackStatistics()
1833 void trackStatistics() const override {}
1834};
1835
1836/// NoFree attribute deduction for a call site return value.
1837struct AANoFreeCallSiteReturned final : AANoFreeFloating {
1838 AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1839
1840 ChangeStatus manifest(Attributor &A) override {
1841 return ChangeStatus::UNCHANGED;
1842 }
1843 /// See AbstractAttribute::trackStatistics()
1844 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
1845};
1846
Hideto Ueno54869ec2019-07-15 06:49:04 +00001847/// ------------------------ NonNull Argument Attribute ------------------------
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001848static int64_t getKnownNonNullAndDerefBytesForUse(
1849 Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue,
1850 const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001851 TrackUse = false;
1852
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001853 const Value *UseV = U->get();
1854 if (!UseV->getType()->isPointerTy())
1855 return 0;
1856
1857 Type *PtrTy = UseV->getType();
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001858 const Function *F = I->getFunction();
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001859 bool NullPointerIsDefined =
1860 F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001861 const DataLayout &DL = A.getInfoCache().getDL();
1862 if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
1863 if (ICS.isBundleOperand(U))
1864 return 0;
1865
1866 if (ICS.isCallee(U)) {
1867 IsNonNull |= !NullPointerIsDefined;
1868 return 0;
1869 }
1870
1871 unsigned ArgNo = ICS.getArgumentNo(U);
1872 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
Johannes Doerfert77a6b352019-11-02 14:47:45 -05001873 // As long as we only use known information there is no need to track
1874 // dependences here.
1875 auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP,
1876 /* TrackDependence */ false);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001877 IsNonNull |= DerefAA.isKnownNonNull();
1878 return DerefAA.getKnownDereferenceableBytes();
1879 }
1880
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001881 // We need to follow common pointer manipulation uses to the accesses they
1882 // feed into. We can try to be smart to avoid looking through things we do not
1883 // like for now, e.g., non-inbounds GEPs.
1884 if (isa<CastInst>(I)) {
1885 TrackUse = true;
1886 return 0;
1887 }
1888 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
Hideto Ueno0f4383f2019-11-27 14:41:12 +00001889 if (GEP->hasAllConstantIndices()) {
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001890 TrackUse = true;
1891 return 0;
1892 }
1893
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001894 int64_t Offset;
1895 if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09001896 if (Base == &AssociatedValue &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06001897 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001898 int64_t DerefBytes =
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001899 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001900
1901 IsNonNull |= !NullPointerIsDefined;
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001902 return std::max(int64_t(0), DerefBytes);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001903 }
1904 }
Hideto Ueno0f4383f2019-11-27 14:41:12 +00001905
1906 /// Corner case when an offset is 0.
1907 if (const Value *Base = getBasePointerOfAccessPointerOperand(
1908 I, Offset, DL, /*AllowNonInbounds*/ true)) {
1909 if (Offset == 0 && Base == &AssociatedValue &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06001910 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno0f4383f2019-11-27 14:41:12 +00001911 int64_t DerefBytes =
1912 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType());
1913 IsNonNull |= !NullPointerIsDefined;
1914 return std::max(int64_t(0), DerefBytes);
1915 }
1916 }
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001917
1918 return 0;
1919}
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001920
Johannes Doerfert344d0382019-08-07 22:34:26 +00001921struct AANonNullImpl : AANonNull {
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001922 AANonNullImpl(const IRPosition &IRP)
1923 : AANonNull(IRP),
1924 NullIsDefined(NullPointerIsDefined(
1925 getAnchorScope(),
1926 getAssociatedValue().getType()->getPointerAddressSpace())) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001927
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001928 /// See AbstractAttribute::initialize(...).
1929 void initialize(Attributor &A) override {
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001930 if (!NullIsDefined &&
1931 hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001932 indicateOptimisticFixpoint();
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001933 else if (isa<ConstantPointerNull>(getAssociatedValue()))
1934 indicatePessimisticFixpoint();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001935 else
1936 AANonNull::initialize(A);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001937 }
1938
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001939 /// See AAFromMustBeExecutedContext
1940 bool followUse(Attributor &A, const Use *U, const Instruction *I) {
1941 bool IsNonNull = false;
1942 bool TrackUse = false;
1943 getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
1944 IsNonNull, TrackUse);
Johannes Doerfert1a746452019-10-20 22:28:49 -05001945 setKnown(IsNonNull);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001946 return TrackUse;
1947 }
1948
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001949 /// See AbstractAttribute::getAsStr().
1950 const std::string getAsStr() const override {
1951 return getAssumed() ? "nonnull" : "may-null";
1952 }
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001953
1954 /// Flag to determine if the underlying value can be null and still allow
1955 /// valid accesses.
1956 const bool NullIsDefined;
Hideto Ueno54869ec2019-07-15 06:49:04 +00001957};
1958
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001959/// NonNull attribute for a floating value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001960struct AANonNullFloating
1961 : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> {
1962 using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>;
1963 AANonNullFloating(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001964
Hideto Ueno54869ec2019-07-15 06:49:04 +00001965 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001966 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001967 ChangeStatus Change = Base::updateImpl(A);
1968 if (isKnownNonNull())
1969 return Change;
1970
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001971 if (!NullIsDefined) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001972 const auto &DerefAA =
1973 A.getAAFor<AADereferenceable>(*this, getIRPosition());
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001974 if (DerefAA.getAssumedDereferenceableBytes())
1975 return Change;
1976 }
1977
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001978 const DataLayout &DL = A.getDataLayout();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001979
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05001980 DominatorTree *DT = nullptr;
1981 InformationCache &InfoCache = A.getInfoCache();
1982 if (const Function *Fn = getAnchorScope())
1983 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
1984
Johannes Doerfert1a746452019-10-20 22:28:49 -05001985 auto VisitValueCB = [&](Value &V, AANonNull::StateType &T,
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001986 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001987 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V));
1988 if (!Stripped && this == &AA) {
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05001989 if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT))
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001990 T.indicatePessimisticFixpoint();
1991 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001992 // Use abstract attribute information.
1993 const AANonNull::StateType &NS =
1994 static_cast<const AANonNull::StateType &>(AA.getState());
1995 T ^= NS;
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001996 }
1997 return T.isValidState();
1998 };
1999
2000 StateType T;
2001 if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this,
2002 T, VisitValueCB))
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002003 return indicatePessimisticFixpoint();
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002004
2005 return clampStateAndIndicateChange(getState(), T);
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002006 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002007
2008 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002009 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002010};
2011
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002012/// NonNull attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002013struct AANonNullReturned final
2014 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002015 AANonNullReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002016 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002017
2018 /// See AbstractAttribute::trackStatistics()
2019 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
2020};
2021
Hideto Ueno54869ec2019-07-15 06:49:04 +00002022/// NonNull attribute for function argument.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002023struct AANonNullArgument final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002024 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2025 AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002026 AANonNullArgument(const IRPosition &IRP)
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002027 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2028 AANonNullImpl>(
2029 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002030
2031 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002032 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002033};
2034
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002035struct AANonNullCallSiteArgument final : AANonNullFloating {
2036 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002037
2038 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert1aac1822019-08-29 01:26:09 +00002039 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002040};
Johannes Doerfert007153e2019-08-05 23:26:06 +00002041
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002042/// NonNull attribute for a call site return position.
2043struct AANonNullCallSiteReturned final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002044 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2045 AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002046 AANonNullCallSiteReturned(const IRPosition &IRP)
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002047 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2048 AANonNullImpl>(
2049 IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002050
2051 /// See AbstractAttribute::trackStatistics()
2052 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
2053};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002054
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002055/// ------------------------ No-Recurse Attributes ----------------------------
2056
2057struct AANoRecurseImpl : public AANoRecurse {
2058 AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {}
2059
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002060 /// See AbstractAttribute::getAsStr()
2061 const std::string getAsStr() const override {
2062 return getAssumed() ? "norecurse" : "may-recurse";
2063 }
2064};
2065
2066struct AANoRecurseFunction final : AANoRecurseImpl {
2067 AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2068
Hideto Ueno63f60662019-09-21 15:13:19 +00002069 /// See AbstractAttribute::initialize(...).
2070 void initialize(Attributor &A) override {
2071 AANoRecurseImpl::initialize(A);
2072 if (const Function *F = getAnchorScope())
Johannes Doerfert26d02b02019-12-30 16:15:38 -06002073 if (A.getInfoCache().getSccSize(*F) != 1)
2074 indicatePessimisticFixpoint();
Hideto Ueno63f60662019-09-21 15:13:19 +00002075 }
2076
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002077 /// See AbstractAttribute::updateImpl(...).
2078 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno63f60662019-09-21 15:13:19 +00002079
Johannes Doerfert26d02b02019-12-30 16:15:38 -06002080 // If all live call sites are known to be no-recurse, we are as well.
2081 auto CallSitePred = [&](AbstractCallSite ACS) {
2082 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2083 *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
2084 /* TrackDependence */ false, DepClassTy::OPTIONAL);
2085 return NoRecurseAA.isKnownNoRecurse();
2086 };
2087 bool AllCallSitesKnown;
2088 if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) {
2089 // If we know all call sites and all are known no-recurse, we are done.
2090 // If all known call sites, which might not be all that exist, are known
2091 // to be no-recurse, we are not done but we can continue to assume
2092 // no-recurse. If one of the call sites we have not visited will become
2093 // live, another update is triggered.
2094 if (AllCallSitesKnown)
2095 indicateOptimisticFixpoint();
2096 return ChangeStatus::UNCHANGED;
2097 }
2098
2099 // If the above check does not hold anymore we look at the calls.
Hideto Ueno63f60662019-09-21 15:13:19 +00002100 auto CheckForNoRecurse = [&](Instruction &I) {
2101 ImmutableCallSite ICS(&I);
2102 if (ICS.hasFnAttr(Attribute::NoRecurse))
2103 return true;
2104
2105 const auto &NoRecurseAA =
2106 A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS));
2107 if (!NoRecurseAA.isAssumedNoRecurse())
2108 return false;
2109
2110 // Recursion to the same function
2111 if (ICS.getCalledFunction() == getAnchorScope())
2112 return false;
2113
2114 return true;
2115 };
2116
2117 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this))
2118 return indicatePessimisticFixpoint();
2119 return ChangeStatus::UNCHANGED;
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002120 }
2121
2122 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
2123};
2124
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002125/// NoRecurse attribute deduction for a call sites.
2126struct AANoRecurseCallSite final : AANoRecurseImpl {
2127 AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2128
2129 /// See AbstractAttribute::initialize(...).
2130 void initialize(Attributor &A) override {
2131 AANoRecurseImpl::initialize(A);
2132 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002133 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002134 indicatePessimisticFixpoint();
2135 }
2136
2137 /// See AbstractAttribute::updateImpl(...).
2138 ChangeStatus updateImpl(Attributor &A) override {
2139 // TODO: Once we have call site specific value information we can provide
2140 // call site specific liveness information and then it makes
2141 // sense to specialize attributes for call sites arguments instead of
2142 // redirecting requests to the callee argument.
2143 Function *F = getAssociatedFunction();
2144 const IRPosition &FnPos = IRPosition::function(*F);
2145 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos);
2146 return clampStateAndIndicateChange(
2147 getState(),
2148 static_cast<const AANoRecurse::StateType &>(FnAA.getState()));
2149 }
2150
2151 /// See AbstractAttribute::trackStatistics()
2152 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
2153};
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002154
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002155/// -------------------- Undefined-Behavior Attributes ------------------------
2156
2157struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
2158 AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {}
2159
2160 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert5732f562019-12-24 19:25:08 -06002161 // through a pointer (i.e. also branches etc.)
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002162 ChangeStatus updateImpl(Attributor &A) override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002163 const size_t UBPrevSize = KnownUBInsts.size();
2164 const size_t NoUBPrevSize = AssumedNoUBInsts.size();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002165
Johannes Doerfert5732f562019-12-24 19:25:08 -06002166 auto InspectMemAccessInstForUB = [&](Instruction &I) {
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002167 // Skip instructions that are already saved.
Hideto Uenoef4febd2019-12-29 17:34:08 +09002168 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002169 return true;
2170
Hideto Uenoef4febd2019-12-29 17:34:08 +09002171 // If we reach here, we know we have an instruction
2172 // that accesses memory through a pointer operand,
2173 // for which getPointerOperand() should give it to us.
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06002174 const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true);
Hideto Uenoef4febd2019-12-29 17:34:08 +09002175 assert(PtrOp &&
2176 "Expected pointer operand of memory accessing instruction");
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002177
Johannes Doerfert5732f562019-12-24 19:25:08 -06002178 // A memory access through a pointer is considered UB
2179 // only if the pointer has constant null value.
2180 // TODO: Expand it to not only check constant values.
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002181 if (!isa<ConstantPointerNull>(PtrOp)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002182 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002183 return true;
2184 }
Johannes Doerfert5732f562019-12-24 19:25:08 -06002185 const Type *PtrTy = PtrOp->getType();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002186
Johannes Doerfert5732f562019-12-24 19:25:08 -06002187 // Because we only consider instructions inside functions,
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002188 // assume that a parent function exists.
2189 const Function *F = I.getFunction();
2190
Johannes Doerfert5732f562019-12-24 19:25:08 -06002191 // A memory access using constant null pointer is only considered UB
2192 // if null pointer is _not_ defined for the target platform.
Hideto Uenoef4febd2019-12-29 17:34:08 +09002193 if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2194 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002195 else
Hideto Uenoef4febd2019-12-29 17:34:08 +09002196 KnownUBInsts.insert(&I);
2197 return true;
2198 };
2199
2200 auto InspectBrInstForUB = [&](Instruction &I) {
2201 // A conditional branch instruction is considered UB if it has `undef`
2202 // condition.
2203
2204 // Skip instructions that are already saved.
2205 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2206 return true;
2207
2208 // We know we have a branch instruction.
2209 auto BrInst = cast<BranchInst>(&I);
2210
2211 // Unconditional branches are never considered UB.
2212 if (BrInst->isUnconditional())
2213 return true;
2214
2215 // Either we stopped and the appropriate action was taken,
2216 // or we got back a simplified value to continue.
2217 Optional<Value *> SimplifiedCond =
2218 stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2219 if (!SimplifiedCond.hasValue())
2220 return true;
2221 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002222 return true;
2223 };
2224
Johannes Doerfert5732f562019-12-24 19:25:08 -06002225 A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2226 {Instruction::Load, Instruction::Store,
2227 Instruction::AtomicCmpXchg,
2228 Instruction::AtomicRMW});
Hideto Uenoef4febd2019-12-29 17:34:08 +09002229 A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br});
2230 if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2231 UBPrevSize != KnownUBInsts.size())
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002232 return ChangeStatus::CHANGED;
2233 return ChangeStatus::UNCHANGED;
2234 }
2235
Hideto Uenoef4febd2019-12-29 17:34:08 +09002236 bool isKnownToCauseUB(Instruction *I) const override {
2237 return KnownUBInsts.count(I);
2238 }
2239
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002240 bool isAssumedToCauseUB(Instruction *I) const override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002241 // In simple words, if an instruction is not in the assumed to _not_
2242 // cause UB, then it is assumed UB (that includes those
2243 // in the KnownUBInsts set). The rest is boilerplate
2244 // is to ensure that it is one of the instructions we test
2245 // for UB.
2246
2247 switch (I->getOpcode()) {
2248 case Instruction::Load:
2249 case Instruction::Store:
2250 case Instruction::AtomicCmpXchg:
2251 case Instruction::AtomicRMW:
2252 return !AssumedNoUBInsts.count(I);
2253 case Instruction::Br: {
2254 auto BrInst = cast<BranchInst>(I);
2255 if (BrInst->isUnconditional())
2256 return false;
2257 return !AssumedNoUBInsts.count(I);
2258 } break;
2259 default:
2260 return false;
2261 }
2262 return false;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002263 }
2264
2265 ChangeStatus manifest(Attributor &A) override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002266 if (KnownUBInsts.empty())
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002267 return ChangeStatus::UNCHANGED;
Hideto Uenoef4febd2019-12-29 17:34:08 +09002268 for (Instruction *I : KnownUBInsts)
Hideto Uenocb5eb132019-12-27 02:39:37 +09002269 A.changeToUnreachableAfterManifest(I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002270 return ChangeStatus::CHANGED;
2271 }
2272
2273 /// See AbstractAttribute::getAsStr()
2274 const std::string getAsStr() const override {
2275 return getAssumed() ? "undefined-behavior" : "no-ub";
2276 }
2277
Hideto Uenoef4febd2019-12-29 17:34:08 +09002278 /// Note: The correctness of this analysis depends on the fact that the
2279 /// following 2 sets will stop changing after some point.
2280 /// "Change" here means that their size changes.
2281 /// The size of each set is monotonically increasing
2282 /// (we only add items to them) and it is upper bounded by the number of
2283 /// instructions in the processed function (we can never save more
2284 /// elements in either set than this number). Hence, at some point,
2285 /// they will stop increasing.
2286 /// Consequently, at some point, both sets will have stopped
2287 /// changing, effectively making the analysis reach a fixpoint.
2288
2289 /// Note: These 2 sets are disjoint and an instruction can be considered
2290 /// one of 3 things:
2291 /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2292 /// the KnownUBInsts set.
2293 /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2294 /// has a reason to assume it).
2295 /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2296 /// could not find a reason to assume or prove that it can cause UB,
2297 /// hence it assumes it doesn't. We have a set for these instructions
2298 /// so that we don't reprocess them in every update.
2299 /// Note however that instructions in this set may cause UB.
2300
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002301protected:
Hideto Uenoef4febd2019-12-29 17:34:08 +09002302 /// A set of all live instructions _known_ to cause UB.
2303 SmallPtrSet<Instruction *, 8> KnownUBInsts;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002304
2305private:
Hideto Uenoef4febd2019-12-29 17:34:08 +09002306 /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2307 SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2308
2309 // Should be called on updates in which if we're processing an instruction
2310 // \p I that depends on a value \p V, one of the following has to happen:
2311 // - If the value is assumed, then stop.
2312 // - If the value is known but undef, then consider it UB.
2313 // - Otherwise, do specific processing with the simplified value.
2314 // We return None in the first 2 cases to signify that an appropriate
2315 // action was taken and the caller should stop.
2316 // Otherwise, we return the simplified value that the caller should
2317 // use for specific processing.
2318 Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V,
2319 Instruction *I) {
2320 const auto &ValueSimplifyAA =
2321 A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V));
2322 Optional<Value *> SimplifiedV =
2323 ValueSimplifyAA.getAssumedSimplifiedValue(A);
2324 if (!ValueSimplifyAA.isKnown()) {
2325 // Don't depend on assumed values.
2326 return llvm::None;
2327 }
2328 if (!SimplifiedV.hasValue()) {
2329 // If it is known (which we tested above) but it doesn't have a value,
2330 // then we can assume `undef` and hence the instruction is UB.
2331 KnownUBInsts.insert(I);
2332 return llvm::None;
2333 }
2334 Value *Val = SimplifiedV.getValue();
2335 if (isa<UndefValue>(Val)) {
2336 KnownUBInsts.insert(I);
2337 return llvm::None;
2338 }
2339 return Val;
2340 }
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002341};
2342
2343struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2344 AAUndefinedBehaviorFunction(const IRPosition &IRP)
2345 : AAUndefinedBehaviorImpl(IRP) {}
2346
2347 /// See AbstractAttribute::trackStatistics()
2348 void trackStatistics() const override {
2349 STATS_DECL(UndefinedBehaviorInstruction, Instruction,
2350 "Number of instructions known to have UB");
2351 BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
Hideto Uenoef4febd2019-12-29 17:34:08 +09002352 KnownUBInsts.size();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002353 }
2354};
2355
Hideto Ueno11d37102019-07-17 15:15:43 +00002356/// ------------------------ Will-Return Attributes ----------------------------
2357
Hideto Ueno11d37102019-07-17 15:15:43 +00002358// Helper function that checks whether a function has any cycle.
2359// TODO: Replace with more efficent code
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002360static bool containsCycle(Function &F) {
Hideto Ueno11d37102019-07-17 15:15:43 +00002361 SmallPtrSet<BasicBlock *, 32> Visited;
2362
2363 // Traverse BB by dfs and check whether successor is already visited.
2364 for (BasicBlock *BB : depth_first(&F)) {
2365 Visited.insert(BB);
2366 for (auto *SuccBB : successors(BB)) {
2367 if (Visited.count(SuccBB))
2368 return true;
2369 }
2370 }
2371 return false;
2372}
2373
2374// Helper function that checks the function have a loop which might become an
2375// endless loop
2376// FIXME: Any cycle is regarded as endless loop for now.
2377// We have to allow some patterns.
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002378static bool containsPossiblyEndlessLoop(Function *F) {
2379 return !F || !F->hasExactDefinition() || containsCycle(*F);
Hideto Ueno11d37102019-07-17 15:15:43 +00002380}
2381
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002382struct AAWillReturnImpl : public AAWillReturn {
2383 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
Hideto Ueno11d37102019-07-17 15:15:43 +00002384
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002385 /// See AbstractAttribute::initialize(...).
2386 void initialize(Attributor &A) override {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002387 AAWillReturn::initialize(A);
Hideto Ueno11d37102019-07-17 15:15:43 +00002388
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002389 Function *F = getAssociatedFunction();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002390 if (containsPossiblyEndlessLoop(F))
2391 indicatePessimisticFixpoint();
2392 }
Hideto Ueno11d37102019-07-17 15:15:43 +00002393
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002394 /// See AbstractAttribute::updateImpl(...).
2395 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002396 auto CheckForWillReturn = [&](Instruction &I) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002397 IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I));
2398 const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos);
2399 if (WillReturnAA.isKnownWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002400 return true;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002401 if (!WillReturnAA.isAssumedWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002402 return false;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002403 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos);
2404 return NoRecurseAA.isAssumedNoRecurse();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002405 };
2406
2407 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
2408 return indicatePessimisticFixpoint();
2409
2410 return ChangeStatus::UNCHANGED;
2411 }
2412
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002413 /// See AbstractAttribute::getAsStr()
2414 const std::string getAsStr() const override {
2415 return getAssumed() ? "willreturn" : "may-noreturn";
2416 }
2417};
2418
2419struct AAWillReturnFunction final : AAWillReturnImpl {
2420 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2421
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002422 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002423 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002424};
Hideto Ueno11d37102019-07-17 15:15:43 +00002425
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002426/// WillReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002427struct AAWillReturnCallSite final : AAWillReturnImpl {
2428 AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2429
2430 /// See AbstractAttribute::initialize(...).
2431 void initialize(Attributor &A) override {
2432 AAWillReturnImpl::initialize(A);
2433 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002434 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002435 indicatePessimisticFixpoint();
2436 }
2437
2438 /// See AbstractAttribute::updateImpl(...).
2439 ChangeStatus updateImpl(Attributor &A) override {
2440 // TODO: Once we have call site specific value information we can provide
2441 // call site specific liveness information and then it makes
2442 // sense to specialize attributes for call sites arguments instead of
2443 // redirecting requests to the callee argument.
2444 Function *F = getAssociatedFunction();
2445 const IRPosition &FnPos = IRPosition::function(*F);
2446 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos);
2447 return clampStateAndIndicateChange(
2448 getState(),
2449 static_cast<const AAWillReturn::StateType &>(FnAA.getState()));
2450 }
2451
2452 /// See AbstractAttribute::trackStatistics()
2453 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
2454};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002455
Pankaj Gode04945c92019-11-22 18:40:47 +05302456/// -------------------AAReachability Attribute--------------------------
2457
2458struct AAReachabilityImpl : AAReachability {
2459 AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {}
2460
2461 const std::string getAsStr() const override {
2462 // TODO: Return the number of reachable queries.
2463 return "reachable";
2464 }
2465
2466 /// See AbstractAttribute::initialize(...).
Johannes Doerfert0bc33362019-12-16 21:03:18 -06002467 void initialize(Attributor &A) override { indicatePessimisticFixpoint(); }
Pankaj Gode04945c92019-11-22 18:40:47 +05302468
2469 /// See AbstractAttribute::updateImpl(...).
2470 ChangeStatus updateImpl(Attributor &A) override {
2471 return indicatePessimisticFixpoint();
2472 }
2473};
2474
2475struct AAReachabilityFunction final : public AAReachabilityImpl {
2476 AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {}
2477
2478 /// See AbstractAttribute::trackStatistics()
2479 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
2480};
2481
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002482/// ------------------------ NoAlias Argument Attribute ------------------------
2483
Johannes Doerfert344d0382019-08-07 22:34:26 +00002484struct AANoAliasImpl : AANoAlias {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002485 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002486
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002487 const std::string getAsStr() const override {
2488 return getAssumed() ? "noalias" : "may-alias";
2489 }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002490};
2491
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002492/// NoAlias attribute for a floating value.
2493struct AANoAliasFloating final : AANoAliasImpl {
2494 AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2495
Hideto Uenocbab3342019-08-29 05:52:00 +00002496 /// See AbstractAttribute::initialize(...).
2497 void initialize(Attributor &A) override {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002498 AANoAliasImpl::initialize(A);
Johannes Doerfert72adda12019-10-10 05:33:21 +00002499 Value &Val = getAssociatedValue();
2500 if (isa<AllocaInst>(Val))
2501 indicateOptimisticFixpoint();
Johannes Doerfert24ae77e2020-01-27 22:19:11 -06002502 else if (isa<ConstantPointerNull>(Val) &&
2503 !NullPointerIsDefined(getAnchorScope(),
2504 Val.getType()->getPointerAddressSpace()))
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002505 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00002506 }
2507
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002508 /// See AbstractAttribute::updateImpl(...).
2509 ChangeStatus updateImpl(Attributor &A) override {
2510 // TODO: Implement this.
2511 return indicatePessimisticFixpoint();
2512 }
2513
2514 /// See AbstractAttribute::trackStatistics()
2515 void trackStatistics() const override {
2516 STATS_DECLTRACK_FLOATING_ATTR(noalias)
2517 }
2518};
2519
2520/// NoAlias attribute for an argument.
Hideto Uenocbab3342019-08-29 05:52:00 +00002521struct AANoAliasArgument final
2522 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002523 using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
2524 AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {}
2525
Johannes Doerfert2baf0002020-01-12 00:18:07 -06002526 /// See AbstractAttribute::initialize(...).
2527 void initialize(Attributor &A) override {
2528 Base::initialize(A);
2529 // See callsite argument attribute and callee argument attribute.
2530 if (hasAttr({Attribute::ByVal}))
2531 indicateOptimisticFixpoint();
2532 }
2533
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002534 /// See AbstractAttribute::update(...).
2535 ChangeStatus updateImpl(Attributor &A) override {
2536 // We have to make sure no-alias on the argument does not break
2537 // synchronization when this is a callback argument, see also [1] below.
2538 // If synchronization cannot be affected, we delegate to the base updateImpl
2539 // function, otherwise we give up for now.
2540
2541 // If the function is no-sync, no-alias cannot break synchronization.
2542 const auto &NoSyncAA = A.getAAFor<AANoSync>(
2543 *this, IRPosition::function_scope(getIRPosition()));
2544 if (NoSyncAA.isAssumedNoSync())
2545 return Base::updateImpl(A);
2546
2547 // If the argument is read-only, no-alias cannot break synchronization.
2548 const auto &MemBehaviorAA =
2549 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
2550 if (MemBehaviorAA.isAssumedReadOnly())
2551 return Base::updateImpl(A);
2552
2553 // If the argument is never passed through callbacks, no-alias cannot break
2554 // synchronization.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002555 bool AllCallSitesKnown;
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002556 if (A.checkForAllCallSites(
2557 [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002558 true, AllCallSitesKnown))
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002559 return Base::updateImpl(A);
2560
2561 // TODO: add no-alias but make sure it doesn't break synchronization by
2562 // introducing fake uses. See:
2563 // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
2564 // International Workshop on OpenMP 2018,
2565 // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
2566
2567 return indicatePessimisticFixpoint();
2568 }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002569
2570 /// See AbstractAttribute::trackStatistics()
2571 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
2572};
2573
2574struct AANoAliasCallSiteArgument final : AANoAliasImpl {
2575 AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2576
Hideto Uenocbab3342019-08-29 05:52:00 +00002577 /// See AbstractAttribute::initialize(...).
2578 void initialize(Attributor &A) override {
Hideto Ueno6381b142019-08-30 10:00:32 +00002579 // See callsite argument attribute and callee argument attribute.
2580 ImmutableCallSite ICS(&getAnchorValue());
2581 if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias))
2582 indicateOptimisticFixpoint();
Johannes Doerfert24ae77e2020-01-27 22:19:11 -06002583 Value &Val = getAssociatedValue();
2584 if (isa<ConstantPointerNull>(Val) &&
2585 !NullPointerIsDefined(getAnchorScope(),
2586 Val.getType()->getPointerAddressSpace()))
2587 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00002588 }
2589
Johannes Doerfert53992c72020-01-27 22:24:32 -06002590 /// Determine if the underlying value may alias with the call site argument
2591 /// \p OtherArgNo of \p ICS (= the underlying call site).
2592 bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
2593 const AAMemoryBehavior &MemBehaviorAA,
2594 ImmutableCallSite ICS, unsigned OtherArgNo) {
2595 // We do not need to worry about aliasing with the underlying IRP.
2596 if (this->getArgNo() == (int)OtherArgNo)
2597 return false;
2598
2599 // If it is not a pointer or pointer vector we do not alias.
2600 const Value *ArgOp = ICS.getArgOperand(OtherArgNo);
2601 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
2602 return false;
2603
2604 auto &ICSArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
2605 *this, IRPosition::callsite_argument(ICS, OtherArgNo),
2606 /* TrackDependence */ false);
2607
2608 // If the argument is readnone, there is no read-write aliasing.
2609 if (ICSArgMemBehaviorAA.isAssumedReadNone()) {
2610 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2611 return false;
2612 }
2613
2614 // If the argument is readonly and the underlying value is readonly, there
2615 // is no read-write aliasing.
2616 bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
2617 if (ICSArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
2618 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2619 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2620 return false;
2621 }
2622
2623 // We have to utilize actual alias analysis queries so we need the object.
2624 if (!AAR)
2625 AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
2626
2627 // Try to rule it out at the call site.
2628 bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
2629 LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
2630 "callsite arguments: "
2631 << getAssociatedValue() << " " << *ArgOp << " => "
2632 << (IsAliasing ? "" : "no-") << "alias \n");
2633
2634 return IsAliasing;
2635 }
2636
2637 bool
2638 isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
2639 const AAMemoryBehavior &MemBehaviorAA,
2640 const AANoAlias &NoAliasAA) {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002641 // We can deduce "noalias" if the following conditions hold.
2642 // (i) Associated value is assumed to be noalias in the definition.
2643 // (ii) Associated value is assumed to be no-capture in all the uses
2644 // possibly executed before this callsite.
2645 // (iii) There is no other pointer argument which could alias with the
2646 // value.
2647
Johannes Doerfert53992c72020-01-27 22:24:32 -06002648 bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
2649 if (!AssociatedValueIsNoAliasAtDef) {
2650 LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
2651 << " is not no-alias at the definition\n");
2652 return false;
2653 }
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002654
Johannes Doerfert53992c72020-01-27 22:24:32 -06002655 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2656 auto &NoCaptureAA =
2657 A.getAAFor<AANoCapture>(*this, VIRP, /* TrackDependence */ false);
2658 // Check whether the value is captured in the scope using AANoCapture.
2659 // FIXME: This is conservative though, it is better to look at CFG and
2660 // check only uses possibly executed before this callsite.
Johannes Doerfert72adda12019-10-10 05:33:21 +00002661 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
2662 LLVM_DEBUG(
Johannes Doerfert53992c72020-01-27 22:24:32 -06002663 dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
Johannes Doerfert72adda12019-10-10 05:33:21 +00002664 << " cannot be noalias as it is potentially captured\n");
Johannes Doerfert53992c72020-01-27 22:24:32 -06002665 return false;
Johannes Doerfert72adda12019-10-10 05:33:21 +00002666 }
Johannes Doerfert53992c72020-01-27 22:24:32 -06002667 A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002668
Johannes Doerfert53992c72020-01-27 22:24:32 -06002669 // Check there is no other pointer argument which could alias with the
2670 // value passed at this call site.
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002671 // TODO: AbstractCallSite
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002672 ImmutableCallSite ICS(&getAnchorValue());
Johannes Doerfert53992c72020-01-27 22:24:32 -06002673 for (unsigned OtherArgNo = 0; OtherArgNo < ICS.getNumArgOperands();
2674 OtherArgNo++)
2675 if (mayAliasWithArgument(A, AAR, MemBehaviorAA, ICS, OtherArgNo))
2676 return false;
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002677
Johannes Doerfert53992c72020-01-27 22:24:32 -06002678 return true;
2679 }
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002680
Johannes Doerfert53992c72020-01-27 22:24:32 -06002681 /// See AbstractAttribute::updateImpl(...).
2682 ChangeStatus updateImpl(Attributor &A) override {
2683 // If the argument is readnone we are done as there are no accesses via the
2684 // argument.
2685 auto &MemBehaviorAA =
2686 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
2687 /* TrackDependence */ false);
2688 if (MemBehaviorAA.isAssumedReadNone()) {
2689 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2690 return ChangeStatus::UNCHANGED;
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002691 }
2692
Johannes Doerfert53992c72020-01-27 22:24:32 -06002693 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2694 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, VIRP,
2695 /* TrackDependence */ false);
2696
2697 AAResults *AAR = nullptr;
2698 if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
2699 NoAliasAA)) {
2700 LLVM_DEBUG(
2701 dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
2702 return ChangeStatus::UNCHANGED;
2703 }
2704
2705 return indicatePessimisticFixpoint();
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002706 }
2707
2708 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00002709 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002710};
2711
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002712/// NoAlias attribute for function return value.
Johannes Doerfertbeb51502019-08-07 22:36:15 +00002713struct AANoAliasReturned final : AANoAliasImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002714 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002715
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002716 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002717 virtual ChangeStatus updateImpl(Attributor &A) override {
2718
2719 auto CheckReturnValue = [&](Value &RV) -> bool {
2720 if (Constant *C = dyn_cast<Constant>(&RV))
2721 if (C->isNullValue() || isa<UndefValue>(C))
2722 return true;
2723
2724 /// For now, we can only deduce noalias if we have call sites.
2725 /// FIXME: add more support.
2726 ImmutableCallSite ICS(&RV);
2727 if (!ICS)
2728 return false;
2729
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002730 const IRPosition &RVPos = IRPosition::value(RV);
2731 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002732 if (!NoAliasAA.isAssumedNoAlias())
2733 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002734
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002735 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos);
2736 return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002737 };
2738
2739 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
2740 return indicatePessimisticFixpoint();
2741
2742 return ChangeStatus::UNCHANGED;
2743 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002744
2745 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002746 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002747};
2748
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002749/// NoAlias attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002750struct AANoAliasCallSiteReturned final : AANoAliasImpl {
2751 AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2752
2753 /// See AbstractAttribute::initialize(...).
2754 void initialize(Attributor &A) override {
2755 AANoAliasImpl::initialize(A);
2756 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002757 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002758 indicatePessimisticFixpoint();
2759 }
2760
2761 /// See AbstractAttribute::updateImpl(...).
2762 ChangeStatus updateImpl(Attributor &A) override {
2763 // TODO: Once we have call site specific value information we can provide
2764 // call site specific liveness information and then it makes
2765 // sense to specialize attributes for call sites arguments instead of
2766 // redirecting requests to the callee argument.
2767 Function *F = getAssociatedFunction();
2768 const IRPosition &FnPos = IRPosition::returned(*F);
2769 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos);
2770 return clampStateAndIndicateChange(
2771 getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState()));
2772 }
2773
2774 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00002775 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002776};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002777
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002778/// -------------------AAIsDead Function Attribute-----------------------
2779
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002780struct AAIsDeadValueImpl : public AAIsDead {
2781 AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002782
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002783 /// See AAIsDead::isAssumedDead().
2784 bool isAssumedDead() const override { return getAssumed(); }
2785
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002786 /// See AAIsDead::isKnownDead().
2787 bool isKnownDead() const override { return getKnown(); }
2788
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002789 /// See AAIsDead::isAssumedDead(BasicBlock *).
2790 bool isAssumedDead(const BasicBlock *BB) const override { return false; }
2791
2792 /// See AAIsDead::isKnownDead(BasicBlock *).
2793 bool isKnownDead(const BasicBlock *BB) const override { return false; }
2794
2795 /// See AAIsDead::isAssumedDead(Instruction *I).
2796 bool isAssumedDead(const Instruction *I) const override {
2797 return I == getCtxI() && isAssumedDead();
2798 }
2799
2800 /// See AAIsDead::isKnownDead(Instruction *I).
2801 bool isKnownDead(const Instruction *I) const override {
2802 return I == getCtxI() && getKnown();
2803 }
2804
2805 /// See AbstractAttribute::getAsStr().
2806 const std::string getAsStr() const override {
2807 return isAssumedDead() ? "assumed-dead" : "assumed-live";
2808 }
2809};
2810
2811struct AAIsDeadFloating : public AAIsDeadValueImpl {
2812 AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
2813
2814 /// See AbstractAttribute::initialize(...).
2815 void initialize(Attributor &A) override {
2816 if (Instruction *I = dyn_cast<Instruction>(&getAssociatedValue()))
2817 if (!wouldInstructionBeTriviallyDead(I))
2818 indicatePessimisticFixpoint();
2819 if (isa<UndefValue>(getAssociatedValue()))
2820 indicatePessimisticFixpoint();
2821 }
2822
2823 /// See AbstractAttribute::updateImpl(...).
2824 ChangeStatus updateImpl(Attributor &A) override {
2825 auto UsePred = [&](const Use &U, bool &Follow) {
2826 Instruction *UserI = cast<Instruction>(U.getUser());
2827 if (CallSite CS = CallSite(UserI)) {
2828 if (!CS.isArgOperand(&U))
2829 return false;
2830 const IRPosition &CSArgPos =
2831 IRPosition::callsite_argument(CS, CS.getArgumentNo(&U));
2832 const auto &CSArgIsDead = A.getAAFor<AAIsDead>(*this, CSArgPos);
2833 return CSArgIsDead.isAssumedDead();
2834 }
2835 if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
2836 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
2837 const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, RetPos);
2838 return RetIsDeadAA.isAssumedDead();
2839 }
2840 Follow = true;
2841 return wouldInstructionBeTriviallyDead(UserI);
2842 };
2843
2844 if (!A.checkForAllUses(UsePred, *this, getAssociatedValue()))
2845 return indicatePessimisticFixpoint();
2846 return ChangeStatus::UNCHANGED;
2847 }
2848
2849 /// See AbstractAttribute::manifest(...).
2850 ChangeStatus manifest(Attributor &A) override {
2851 Value &V = getAssociatedValue();
2852 if (auto *I = dyn_cast<Instruction>(&V))
2853 if (wouldInstructionBeTriviallyDead(I)) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05002854 A.deleteAfterManifest(*I);
2855 return ChangeStatus::CHANGED;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002856 }
2857
2858 if (V.use_empty())
2859 return ChangeStatus::UNCHANGED;
2860
2861 UndefValue &UV = *UndefValue::get(V.getType());
Hideto Ueno34fe8d02019-12-30 17:08:48 +09002862 bool AnyChange = A.changeValueAfterManifest(V, UV);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002863 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
2864 }
2865
2866 /// See AbstractAttribute::trackStatistics()
2867 void trackStatistics() const override {
2868 STATS_DECLTRACK_FLOATING_ATTR(IsDead)
2869 }
2870};
2871
2872struct AAIsDeadArgument : public AAIsDeadFloating {
2873 AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {}
2874
2875 /// See AbstractAttribute::initialize(...).
2876 void initialize(Attributor &A) override {
2877 if (!getAssociatedFunction()->hasExactDefinition())
2878 indicatePessimisticFixpoint();
2879 }
2880
Johannes Doerfert75133632019-10-10 01:39:16 -05002881 /// See AbstractAttribute::manifest(...).
2882 ChangeStatus manifest(Attributor &A) override {
2883 ChangeStatus Changed = AAIsDeadFloating::manifest(A);
2884 Argument &Arg = *getAssociatedArgument();
Johannes Doerfert89c2e732019-10-30 17:20:20 -05002885 if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
Johannes Doerfert75133632019-10-10 01:39:16 -05002886 if (A.registerFunctionSignatureRewrite(
2887 Arg, /* ReplacementTypes */ {},
2888 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
2889 Attributor::ArgumentReplacementInfo::ACSRepairCBTy{}))
2890 return ChangeStatus::CHANGED;
2891 return Changed;
2892 }
2893
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002894 /// See AbstractAttribute::trackStatistics()
2895 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
2896};
2897
2898struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
2899 AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
2900
2901 /// See AbstractAttribute::initialize(...).
2902 void initialize(Attributor &A) override {
2903 if (isa<UndefValue>(getAssociatedValue()))
2904 indicatePessimisticFixpoint();
2905 }
2906
2907 /// See AbstractAttribute::updateImpl(...).
2908 ChangeStatus updateImpl(Attributor &A) override {
2909 // TODO: Once we have call site specific value information we can provide
2910 // call site specific liveness information and then it makes
2911 // sense to specialize attributes for call sites arguments instead of
2912 // redirecting requests to the callee argument.
2913 Argument *Arg = getAssociatedArgument();
2914 if (!Arg)
2915 return indicatePessimisticFixpoint();
2916 const IRPosition &ArgPos = IRPosition::argument(*Arg);
2917 auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos);
2918 return clampStateAndIndicateChange(
2919 getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState()));
2920 }
2921
2922 /// See AbstractAttribute::manifest(...).
2923 ChangeStatus manifest(Attributor &A) override {
2924 CallBase &CB = cast<CallBase>(getAnchorValue());
2925 Use &U = CB.getArgOperandUse(getArgNo());
2926 assert(!isa<UndefValue>(U.get()) &&
2927 "Expected undef values to be filtered out!");
2928 UndefValue &UV = *UndefValue::get(U->getType());
2929 if (A.changeUseAfterManifest(U, UV))
2930 return ChangeStatus::CHANGED;
2931 return ChangeStatus::UNCHANGED;
2932 }
2933
2934 /// See AbstractAttribute::trackStatistics()
2935 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
2936};
2937
2938struct AAIsDeadReturned : public AAIsDeadValueImpl {
2939 AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
2940
2941 /// See AbstractAttribute::updateImpl(...).
2942 ChangeStatus updateImpl(Attributor &A) override {
2943
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002944 bool AllKnownDead = true;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002945 auto PredForCallSite = [&](AbstractCallSite ACS) {
2946 if (ACS.isCallbackCall())
2947 return false;
2948 const IRPosition &CSRetPos =
2949 IRPosition::callsite_returned(ACS.getCallSite());
2950 const auto &RetIsDeadAA = A.getAAFor<AAIsDead>(*this, CSRetPos);
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002951 AllKnownDead &= RetIsDeadAA.isKnownDead();
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002952 return RetIsDeadAA.isAssumedDead();
2953 };
2954
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002955 bool AllCallSitesKnown;
2956 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
2957 AllCallSitesKnown))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002958 return indicatePessimisticFixpoint();
2959
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002960 if (AllCallSitesKnown && AllKnownDead)
2961 indicateOptimisticFixpoint();
2962
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002963 return ChangeStatus::UNCHANGED;
2964 }
2965
2966 /// See AbstractAttribute::manifest(...).
2967 ChangeStatus manifest(Attributor &A) override {
2968 // TODO: Rewrite the signature to return void?
2969 bool AnyChange = false;
2970 UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
2971 auto RetInstPred = [&](Instruction &I) {
2972 ReturnInst &RI = cast<ReturnInst>(I);
2973 if (!isa<UndefValue>(RI.getReturnValue()))
2974 AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
2975 return true;
2976 };
2977 A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret});
2978 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
2979 }
2980
2981 /// See AbstractAttribute::trackStatistics()
2982 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
2983};
2984
2985struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
2986 AAIsDeadCallSiteReturned(const IRPosition &IRP) : AAIsDeadFloating(IRP) {}
2987
2988 /// See AbstractAttribute::initialize(...).
2989 void initialize(Attributor &A) override {}
2990
2991 /// See AbstractAttribute::trackStatistics()
2992 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(IsDead) }
2993};
2994
2995struct AAIsDeadFunction : public AAIsDead {
2996 AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {}
2997
2998 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00002999 void initialize(Attributor &A) override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003000 const Function *F = getAssociatedFunction();
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003001 if (F && !F->isDeclaration()) {
3002 ToBeExploredFrom.insert(&F->getEntryBlock().front());
3003 assumeLive(A, F->getEntryBlock());
3004 }
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003005 }
3006
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003007 /// See AbstractAttribute::getAsStr().
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003008 const std::string getAsStr() const override {
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003009 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003010 std::to_string(getAssociatedFunction()->size()) + "][#TBEP " +
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003011 std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3012 std::to_string(KnownDeadEnds.size()) + "]";
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003013 }
3014
3015 /// See AbstractAttribute::manifest(...).
3016 ChangeStatus manifest(Attributor &A) override {
3017 assert(getState().isValidState() &&
3018 "Attempted to manifest an invalid state!");
3019
3020 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003021 Function &F = *getAssociatedFunction();
3022
3023 if (AssumedLiveBlocks.empty()) {
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003024 A.deleteAfterManifest(F);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003025 return ChangeStatus::CHANGED;
3026 }
Johannes Doerfert924d2132019-08-05 21:34:45 +00003027
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003028 // Flag to determine if we can change an invoke to a call assuming the
3029 // callee is nounwind. This is not possible if the personality of the
3030 // function allows to catch asynchronous exceptions.
Johannes Doerfert924d2132019-08-05 21:34:45 +00003031 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003032
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003033 KnownDeadEnds.set_union(ToBeExploredFrom);
3034 for (const Instruction *DeadEndI : KnownDeadEnds) {
3035 auto *CB = dyn_cast<CallBase>(DeadEndI);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003036 if (!CB)
3037 continue;
3038 const auto &NoReturnAA =
3039 A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB));
Johannes Doerfertc7ab19d2019-11-01 21:59:32 -05003040 bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3041 if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003042 continue;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003043
Johannes Doerferta4088c72020-01-07 16:01:57 -06003044 if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3045 A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3046 else
3047 A.changeToUnreachableAfterManifest(
3048 const_cast<Instruction *>(DeadEndI->getNextNode()));
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003049 HasChanged = ChangeStatus::CHANGED;
3050 }
3051
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003052 for (BasicBlock &BB : F)
3053 if (!AssumedLiveBlocks.count(&BB))
3054 A.deleteAfterManifest(BB);
3055
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003056 return HasChanged;
3057 }
3058
3059 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003060 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003061
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003062 /// See AbstractAttribute::trackStatistics()
3063 void trackStatistics() const override {}
3064
3065 /// Returns true if the function is assumed dead.
3066 bool isAssumedDead() const override { return false; }
3067
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06003068 /// See AAIsDead::isKnownDead().
3069 bool isKnownDead() const override { return false; }
3070
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003071 /// See AAIsDead::isAssumedDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003072 bool isAssumedDead(const BasicBlock *BB) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003073 assert(BB->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003074 "BB must be in the same anchor scope function.");
3075
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003076 if (!getAssumed())
3077 return false;
3078 return !AssumedLiveBlocks.count(BB);
3079 }
3080
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003081 /// See AAIsDead::isKnownDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003082 bool isKnownDead(const BasicBlock *BB) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003083 return getKnown() && isAssumedDead(BB);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003084 }
3085
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003086 /// See AAIsDead::isAssumed(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003087 bool isAssumedDead(const Instruction *I) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003088 assert(I->getParent()->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003089 "Instruction must be in the same anchor scope function.");
3090
Stefan Stipanovic7849e412019-08-03 15:27:41 +00003091 if (!getAssumed())
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003092 return false;
3093
3094 // If it is not in AssumedLiveBlocks then it for sure dead.
3095 // Otherwise, it can still be after noreturn call in a live block.
3096 if (!AssumedLiveBlocks.count(I->getParent()))
3097 return true;
3098
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003099 // If it is not after a liveness barrier it is live.
3100 const Instruction *PrevI = I->getPrevNode();
3101 while (PrevI) {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003102 if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003103 return true;
3104 PrevI = PrevI->getPrevNode();
3105 }
3106 return false;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003107 }
3108
3109 /// See AAIsDead::isKnownDead(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003110 bool isKnownDead(const Instruction *I) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003111 return getKnown() && isAssumedDead(I);
3112 }
3113
Johannes Doerfert924d2132019-08-05 21:34:45 +00003114 /// Determine if \p F might catch asynchronous exceptions.
3115 static bool mayCatchAsynchronousExceptions(const Function &F) {
3116 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
3117 }
3118
Johannes Doerfert2f622062019-09-04 16:35:20 +00003119 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3120 /// that internal function called from \p BB should now be looked at.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003121 bool assumeLive(Attributor &A, const BasicBlock &BB) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00003122 if (!AssumedLiveBlocks.insert(&BB).second)
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003123 return false;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003124
3125 // We assume that all of BB is (probably) live now and if there are calls to
3126 // internal functions we will assume that those are now live as well. This
3127 // is a performance optimization for blocks with calls to a lot of internal
3128 // functions. It can however cause dead functions to be treated as live.
3129 for (const Instruction &I : BB)
3130 if (ImmutableCallSite ICS = ImmutableCallSite(&I))
3131 if (const Function *F = ICS.getCalledFunction())
Johannes Doerfert766f2cc2019-10-07 23:21:52 +00003132 if (F->hasLocalLinkage())
Johannes Doerfert2f622062019-09-04 16:35:20 +00003133 A.markLiveInternalFunction(*F);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003134 return true;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003135 }
3136
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003137 /// Collection of instructions that need to be explored again, e.g., we
3138 /// did assume they do not transfer control to (one of their) successors.
3139 SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003140
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003141 /// Collection of instructions that are known to not transfer control.
3142 SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3143
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003144 /// Collection of all assumed live BasicBlocks.
Johannes Doerfert4361da22019-08-04 18:38:53 +00003145 DenseSet<const BasicBlock *> AssumedLiveBlocks;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003146};
3147
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003148static bool
3149identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3150 AbstractAttribute &AA,
3151 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3152 const IRPosition &IPos = IRPosition::callsite_function(CB);
3153
3154 const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos);
3155 if (NoReturnAA.isAssumedNoReturn())
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003156 return !NoReturnAA.isKnownNoReturn();
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003157 if (CB.isTerminator())
3158 AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3159 else
3160 AliveSuccessors.push_back(CB.getNextNode());
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003161 return false;
3162}
3163
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003164static bool
3165identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3166 AbstractAttribute &AA,
3167 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3168 bool UsedAssumedInformation =
3169 identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
Johannes Doerfert924d2132019-08-05 21:34:45 +00003170
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003171 // First, determine if we can change an invoke to a call assuming the
3172 // callee is nounwind. This is not possible if the personality of the
3173 // function allows to catch asynchronous exceptions.
3174 if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3175 AliveSuccessors.push_back(&II.getUnwindDest()->front());
3176 } else {
3177 const IRPosition &IPos = IRPosition::callsite_function(II);
3178 const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos);
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003179 if (AANoUnw.isAssumedNoUnwind()) {
3180 UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3181 } else {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003182 AliveSuccessors.push_back(&II.getUnwindDest()->front());
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003183 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003184 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003185 return UsedAssumedInformation;
3186}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003187
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003188static bool
3189identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3190 AbstractAttribute &AA,
3191 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3192 bool UsedAssumedInformation = false;
3193 if (BI.getNumSuccessors() == 1) {
3194 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3195 } else {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003196 Optional<ConstantInt *> CI =
3197 getAssumedConstant(A, *BI.getCondition(), AA, UsedAssumedInformation);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003198 if (!CI.hasValue()) {
3199 // No value yet, assume both edges are dead.
3200 } else if (CI.getValue()) {
3201 const BasicBlock *SuccBB =
3202 BI.getSuccessor(1 - CI.getValue()->getZExtValue());
3203 AliveSuccessors.push_back(&SuccBB->front());
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003204 } else {
3205 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3206 AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003207 UsedAssumedInformation = false;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003208 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003209 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003210 return UsedAssumedInformation;
3211}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003212
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003213static bool
3214identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3215 AbstractAttribute &AA,
3216 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003217 bool UsedAssumedInformation = false;
3218 Optional<ConstantInt *> CI =
3219 getAssumedConstant(A, *SI.getCondition(), AA, UsedAssumedInformation);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003220 if (!CI.hasValue()) {
3221 // No value yet, assume all edges are dead.
3222 } else if (CI.getValue()) {
3223 for (auto &CaseIt : SI.cases()) {
3224 if (CaseIt.getCaseValue() == CI.getValue()) {
3225 AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003226 return UsedAssumedInformation;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003227 }
3228 }
Johannes Doerferted47a9c2019-11-01 13:57:49 -05003229 AliveSuccessors.push_back(&SI.getDefaultDest()->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003230 return UsedAssumedInformation;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003231 } else {
3232 for (const BasicBlock *SuccBB : successors(SI.getParent()))
3233 AliveSuccessors.push_back(&SuccBB->front());
3234 }
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003235 return UsedAssumedInformation;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003236}
3237
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003238ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003239 ChangeStatus Change = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003240
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003241 LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
3242 << getAssociatedFunction()->size() << "] BBs and "
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003243 << ToBeExploredFrom.size() << " exploration points and "
3244 << KnownDeadEnds.size() << " known dead ends\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003245
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003246 // Copy and clear the list of instructions we need to explore from. It is
3247 // refilled with instructions the next update has to look at.
3248 SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
3249 ToBeExploredFrom.end());
3250 decltype(ToBeExploredFrom) NewToBeExploredFrom;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003251
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003252 SmallVector<const Instruction *, 8> AliveSuccessors;
3253 while (!Worklist.empty()) {
3254 const Instruction *I = Worklist.pop_back_val();
3255 LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003256
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003257 AliveSuccessors.clear();
3258
3259 bool UsedAssumedInformation = false;
3260 switch (I->getOpcode()) {
3261 // TODO: look for (assumed) UB to backwards propagate "deadness".
3262 default:
3263 if (I->isTerminator()) {
3264 for (const BasicBlock *SuccBB : successors(I->getParent()))
3265 AliveSuccessors.push_back(&SuccBB->front());
3266 } else {
3267 AliveSuccessors.push_back(I->getNextNode());
3268 }
3269 break;
3270 case Instruction::Call:
3271 UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
3272 *this, AliveSuccessors);
3273 break;
3274 case Instruction::Invoke:
3275 UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
3276 *this, AliveSuccessors);
3277 break;
3278 case Instruction::Br:
3279 UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
3280 *this, AliveSuccessors);
3281 break;
3282 case Instruction::Switch:
3283 UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
3284 *this, AliveSuccessors);
3285 break;
Johannes Doerfert4361da22019-08-04 18:38:53 +00003286 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003287
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003288 if (UsedAssumedInformation) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003289 NewToBeExploredFrom.insert(I);
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003290 } else {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003291 Change = ChangeStatus::CHANGED;
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003292 if (AliveSuccessors.empty() ||
3293 (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors()))
3294 KnownDeadEnds.insert(I);
3295 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003296
3297 LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
3298 << AliveSuccessors.size() << " UsedAssumedInformation: "
3299 << UsedAssumedInformation << "\n");
3300
3301 for (const Instruction *AliveSuccessor : AliveSuccessors) {
3302 if (!I->isTerminator()) {
3303 assert(AliveSuccessors.size() == 1 &&
3304 "Non-terminator expected to have a single successor!");
3305 Worklist.push_back(AliveSuccessor);
3306 } else {
3307 if (assumeLive(A, *AliveSuccessor->getParent()))
3308 Worklist.push_back(AliveSuccessor);
3309 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00003310 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003311 }
3312
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003313 ToBeExploredFrom = std::move(NewToBeExploredFrom);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003314
Johannes Doerfertd6207812019-08-07 22:32:38 +00003315 // If we know everything is live there is no need to query for liveness.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003316 // Instead, indicating a pessimistic fixpoint will cause the state to be
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003317 // "invalid" and all queries to be answered conservatively without lookups.
3318 // To be in this state we have to (1) finished the exploration and (3) not
3319 // discovered any non-trivial dead end and (2) not ruled unreachable code
3320 // dead.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003321 if (ToBeExploredFrom.empty() &&
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003322 getAssociatedFunction()->size() == AssumedLiveBlocks.size() &&
3323 llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
3324 return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
3325 }))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003326 return indicatePessimisticFixpoint();
3327 return Change;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003328}
3329
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003330/// Liveness information for a call sites.
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003331struct AAIsDeadCallSite final : AAIsDeadFunction {
3332 AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {}
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003333
3334 /// See AbstractAttribute::initialize(...).
3335 void initialize(Attributor &A) override {
3336 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003337 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003338 // sense to specialize attributes for call sites instead of
3339 // redirecting requests to the callee.
3340 llvm_unreachable("Abstract attributes for liveness are not "
3341 "supported for call sites yet!");
3342 }
3343
3344 /// See AbstractAttribute::updateImpl(...).
3345 ChangeStatus updateImpl(Attributor &A) override {
3346 return indicatePessimisticFixpoint();
3347 }
3348
3349 /// See AbstractAttribute::trackStatistics()
3350 void trackStatistics() const override {}
3351};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003352
Hideto Ueno19c07af2019-07-23 08:16:17 +00003353/// -------------------- Dereferenceable Argument Attribute --------------------
3354
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003355template <>
3356ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
3357 const DerefState &R) {
Johannes Doerfert31784242019-10-29 23:18:49 -05003358 ChangeStatus CS0 =
3359 clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
3360 ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003361 return CS0 | CS1;
3362}
3363
Hideto Ueno70576ca2019-08-22 14:18:29 +00003364struct AADereferenceableImpl : AADereferenceable {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003365 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
Johannes Doerfert344d0382019-08-07 22:34:26 +00003366 using StateType = DerefState;
Hideto Ueno19c07af2019-07-23 08:16:17 +00003367
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00003368 void initialize(Attributor &A) override {
3369 SmallVector<Attribute, 4> Attrs;
3370 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
3371 Attrs);
3372 for (const Attribute &Attr : Attrs)
3373 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
3374
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06003375 NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition(),
3376 /* TrackDependence */ false);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003377
3378 const IRPosition &IRP = this->getIRPosition();
3379 bool IsFnInterface = IRP.isFnInterfaceKind();
3380 const Function *FnScope = IRP.getAnchorScope();
3381 if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition()))
3382 indicatePessimisticFixpoint();
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00003383 }
3384
Hideto Ueno19c07af2019-07-23 08:16:17 +00003385 /// See AbstractAttribute::getState()
3386 /// {
Johannes Doerfert344d0382019-08-07 22:34:26 +00003387 StateType &getState() override { return *this; }
3388 const StateType &getState() const override { return *this; }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003389 /// }
3390
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003391 /// Helper function for collecting accessed bytes in must-be-executed-context
3392 void addAccessedBytesForUse(Attributor &A, const Use *U,
3393 const Instruction *I) {
3394 const Value *UseV = U->get();
3395 if (!UseV->getType()->isPointerTy())
3396 return;
3397
3398 Type *PtrTy = UseV->getType();
3399 const DataLayout &DL = A.getDataLayout();
3400 int64_t Offset;
3401 if (const Value *Base = getBasePointerOfAccessPointerOperand(
3402 I, Offset, DL, /*AllowNonInbounds*/ true)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09003403 if (Base == &getAssociatedValue() &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06003404 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003405 uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType());
3406 addAccessedBytes(Offset, Size);
3407 }
3408 }
3409 return;
3410 }
3411
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003412 /// See AAFromMustBeExecutedContext
3413 bool followUse(Attributor &A, const Use *U, const Instruction *I) {
3414 bool IsNonNull = false;
3415 bool TrackUse = false;
3416 int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
3417 A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003418
3419 addAccessedBytesForUse(A, U, I);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003420 takeKnownDerefBytesMaximum(DerefBytes);
3421 return TrackUse;
3422 }
3423
Hideto Uenodfedae52019-11-29 06:45:07 +00003424 /// See AbstractAttribute::manifest(...).
3425 ChangeStatus manifest(Attributor &A) override {
3426 ChangeStatus Change = AADereferenceable::manifest(A);
3427 if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
3428 removeAttrs({Attribute::DereferenceableOrNull});
3429 return ChangeStatus::CHANGED;
3430 }
3431 return Change;
3432 }
3433
Johannes Doerferteccdf082019-08-05 23:35:12 +00003434 void getDeducedAttributes(LLVMContext &Ctx,
3435 SmallVectorImpl<Attribute> &Attrs) const override {
Hideto Ueno19c07af2019-07-23 08:16:17 +00003436 // TODO: Add *_globally support
3437 if (isAssumedNonNull())
3438 Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
3439 Ctx, getAssumedDereferenceableBytes()));
3440 else
3441 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
3442 Ctx, getAssumedDereferenceableBytes()));
3443 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003444
3445 /// See AbstractAttribute::getAsStr().
3446 const std::string getAsStr() const override {
3447 if (!getAssumedDereferenceableBytes())
3448 return "unknown-dereferenceable";
3449 return std::string("dereferenceable") +
3450 (isAssumedNonNull() ? "" : "_or_null") +
3451 (isAssumedGlobal() ? "_globally" : "") + "<" +
3452 std::to_string(getKnownDereferenceableBytes()) + "-" +
3453 std::to_string(getAssumedDereferenceableBytes()) + ">";
3454 }
3455};
3456
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003457/// Dereferenceable attribute for a floating value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003458struct AADereferenceableFloating
3459 : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> {
3460 using Base =
3461 AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>;
3462 AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno19c07af2019-07-23 08:16:17 +00003463
3464 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003465 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003466 ChangeStatus Change = Base::updateImpl(A);
3467
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003468 const DataLayout &DL = A.getDataLayout();
3469
3470 auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool {
3471 unsigned IdxWidth =
3472 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
3473 APInt Offset(IdxWidth, 0);
3474 const Value *Base =
3475 V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
3476
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003477 const auto &AA =
3478 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003479 int64_t DerefBytes = 0;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003480 if (!Stripped && this == &AA) {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003481 // Use IR information if we did not strip anything.
3482 // TODO: track globally.
3483 bool CanBeNull;
3484 DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull);
3485 T.GlobalState.indicatePessimisticFixpoint();
3486 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003487 const DerefState &DS = static_cast<const DerefState &>(AA.getState());
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003488 DerefBytes = DS.DerefBytesState.getAssumed();
3489 T.GlobalState &= DS.GlobalState;
3490 }
3491
Hideto Ueno188f9a32020-01-15 15:25:52 +09003492 // TODO: Use `AAConstantRange` to infer dereferenceable bytes.
3493
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00003494 // For now we do not try to "increase" dereferenceability due to negative
3495 // indices as we first have to come up with code to deal with loops and
3496 // for overflows of the dereferenceable bytes.
Johannes Doerfert785fad32019-08-23 17:29:23 +00003497 int64_t OffsetSExt = Offset.getSExtValue();
3498 if (OffsetSExt < 0)
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00003499 OffsetSExt = 0;
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00003500
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003501 T.takeAssumedDerefBytesMinimum(
Johannes Doerfert785fad32019-08-23 17:29:23 +00003502 std::max(int64_t(0), DerefBytes - OffsetSExt));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003503
Johannes Doerfert785fad32019-08-23 17:29:23 +00003504 if (this == &AA) {
3505 if (!Stripped) {
3506 // If nothing was stripped IR information is all we got.
3507 T.takeKnownDerefBytesMaximum(
3508 std::max(int64_t(0), DerefBytes - OffsetSExt));
3509 T.indicatePessimisticFixpoint();
3510 } else if (OffsetSExt > 0) {
3511 // If something was stripped but there is circular reasoning we look
3512 // for the offset. If it is positive we basically decrease the
3513 // dereferenceable bytes in a circluar loop now, which will simply
3514 // drive them down to the known value in a very slow way which we
3515 // can accelerate.
3516 T.indicatePessimisticFixpoint();
3517 }
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003518 }
3519
3520 return T.isValidState();
3521 };
3522
3523 DerefState T;
3524 if (!genericValueTraversal<AADereferenceable, DerefState>(
3525 A, getIRPosition(), *this, T, VisitValueCB))
3526 return indicatePessimisticFixpoint();
3527
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003528 return Change | clampStateAndIndicateChange(getState(), T);
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003529 }
3530
3531 /// See AbstractAttribute::trackStatistics()
3532 void trackStatistics() const override {
3533 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
3534 }
3535};
3536
3537/// Dereferenceable attribute for a return value.
3538struct AADereferenceableReturned final
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003539 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003540 AADereferenceableReturned(const IRPosition &IRP)
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003541 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
3542 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003543
3544 /// See AbstractAttribute::trackStatistics()
3545 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003546 STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003547 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003548};
3549
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003550/// Dereferenceable attribute for an argument
3551struct AADereferenceableArgument final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003552 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003553 AADereferenceable, AADereferenceableImpl> {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003554 using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003555 AADereferenceable, AADereferenceableImpl>;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003556 AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003557
3558 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003559 void trackStatistics() const override {
Johannes Doerfert169af992019-08-20 06:09:56 +00003560 STATS_DECLTRACK_ARG_ATTR(dereferenceable)
3561 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003562};
3563
Hideto Ueno19c07af2019-07-23 08:16:17 +00003564/// Dereferenceable attribute for a call site argument.
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003565struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003566 AADereferenceableCallSiteArgument(const IRPosition &IRP)
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003567 : AADereferenceableFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003568
3569 /// See AbstractAttribute::trackStatistics()
3570 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003571 STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003572 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003573};
3574
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003575/// Dereferenceable attribute deduction for a call site return value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003576struct AADereferenceableCallSiteReturned final
3577 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3578 AADereferenceable, AADereferenceableImpl> {
3579 using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3580 AADereferenceable, AADereferenceableImpl>;
3581 AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003582
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003583 /// See AbstractAttribute::trackStatistics()
3584 void trackStatistics() const override {
3585 STATS_DECLTRACK_CS_ATTR(dereferenceable);
3586 }
3587};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003588
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003589// ------------------------ Align Argument Attribute ------------------------
3590
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003591static unsigned int getKnownAlignForUse(Attributor &A,
3592 AbstractAttribute &QueryingAA,
3593 Value &AssociatedValue, const Use *U,
3594 const Instruction *I, bool &TrackUse) {
Hideto Ueno78a75022019-11-26 07:51:59 +00003595 // We need to follow common pointer manipulation uses to the accesses they
3596 // feed into.
3597 if (isa<CastInst>(I)) {
Johannes Doerfertc90681b2020-01-02 16:41:17 -06003598 // Follow all but ptr2int casts.
3599 TrackUse = !isa<PtrToIntInst>(I);
Hideto Ueno78a75022019-11-26 07:51:59 +00003600 return 0;
3601 }
3602 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
3603 if (GEP->hasAllConstantIndices()) {
3604 TrackUse = true;
3605 return 0;
3606 }
3607 }
3608
3609 unsigned Alignment = 0;
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003610 if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
3611 if (ICS.isBundleOperand(U) || ICS.isCallee(U))
3612 return 0;
3613
3614 unsigned ArgNo = ICS.getArgumentNo(U);
3615 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
3616 // As long as we only use known information there is no need to track
3617 // dependences here.
3618 auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP,
3619 /* TrackDependence */ false);
Hideto Ueno78a75022019-11-26 07:51:59 +00003620 Alignment = AlignAA.getKnownAlign();
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003621 }
3622
Hideto Ueno78a75022019-11-26 07:51:59 +00003623 const Value *UseV = U->get();
Johannes Doerfert30ae8592020-01-10 12:13:10 -06003624 if (auto *SI = dyn_cast<StoreInst>(I)) {
3625 if (SI->getPointerOperand() == UseV)
3626 Alignment = SI->getAlignment();
3627 } else if (auto *LI = dyn_cast<LoadInst>(I))
Hideto Ueno78a75022019-11-26 07:51:59 +00003628 Alignment = LI->getAlignment();
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003629
Hideto Ueno78a75022019-11-26 07:51:59 +00003630 if (Alignment <= 1)
3631 return 0;
3632
3633 auto &DL = A.getDataLayout();
3634 int64_t Offset;
3635
3636 if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
3637 if (Base == &AssociatedValue) {
3638 // BasePointerAddr + Offset = Alignment * Q for some integer Q.
3639 // So we can say that the maximum power of two which is a divisor of
3640 // gcd(Offset, Alignment) is an alignment.
3641
3642 uint32_t gcd =
3643 greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
3644 Alignment = llvm::PowerOf2Floor(gcd);
3645 }
3646 }
3647
3648 return Alignment;
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003649}
Johannes Doerfert344d0382019-08-07 22:34:26 +00003650struct AAAlignImpl : AAAlign {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003651 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003652
Johannes Doerfert234eda52019-08-16 19:51:23 +00003653 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003654 void initialize(Attributor &A) override {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003655 SmallVector<Attribute, 4> Attrs;
3656 getAttrs({Attribute::Alignment}, Attrs);
3657 for (const Attribute &Attr : Attrs)
3658 takeKnownMaximum(Attr.getValueAsInt());
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003659
3660 if (getIRPosition().isFnInterfaceKind() &&
3661 (!getAssociatedFunction() ||
3662 !getAssociatedFunction()->hasExactDefinition()))
3663 indicatePessimisticFixpoint();
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003664 }
3665
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003666 /// See AbstractAttribute::manifest(...).
3667 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert30179d72020-01-12 00:25:45 -06003668 ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003669
3670 // Check for users that allow alignment annotations.
3671 Value &AnchorVal = getIRPosition().getAnchorValue();
3672 for (const Use &U : AnchorVal.uses()) {
3673 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
3674 if (SI->getPointerOperand() == &AnchorVal)
3675 if (SI->getAlignment() < getAssumedAlign()) {
3676 STATS_DECLTRACK(AAAlign, Store,
James Hendersond68904f2020-01-06 10:15:44 +00003677 "Number of times alignment added to a store");
Guillaume Chateletd400d452019-10-03 13:17:21 +00003678 SI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert30179d72020-01-12 00:25:45 -06003679 LoadStoreChanged = ChangeStatus::CHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003680 }
3681 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
3682 if (LI->getPointerOperand() == &AnchorVal)
3683 if (LI->getAlignment() < getAssumedAlign()) {
Guillaume Chatelet17380222019-09-30 09:37:05 +00003684 LI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003685 STATS_DECLTRACK(AAAlign, Load,
James Hendersond68904f2020-01-06 10:15:44 +00003686 "Number of times alignment added to a load");
Johannes Doerfert30179d72020-01-12 00:25:45 -06003687 LoadStoreChanged = ChangeStatus::CHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003688 }
3689 }
3690 }
3691
Johannes Doerfert30179d72020-01-12 00:25:45 -06003692 ChangeStatus Changed = AAAlign::manifest(A);
3693
3694 MaybeAlign InheritAlign =
3695 getAssociatedValue().getPointerAlignment(A.getDataLayout());
3696 if (InheritAlign.valueOrOne() >= getAssumedAlign())
3697 return LoadStoreChanged;
3698 return Changed | LoadStoreChanged;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003699 }
3700
Johannes Doerfert81df4522019-08-30 15:22:28 +00003701 // TODO: Provide a helper to determine the implied ABI alignment and check in
3702 // the existing manifest method and a new one for AAAlignImpl that value
3703 // to avoid making the alignment explicit if it did not improve.
3704
3705 /// See AbstractAttribute::getDeducedAttributes
3706 virtual void
3707 getDeducedAttributes(LLVMContext &Ctx,
3708 SmallVectorImpl<Attribute> &Attrs) const override {
3709 if (getAssumedAlign() > 1)
Guillaume Chateletb65fa482019-10-15 12:56:24 +00003710 Attrs.emplace_back(
3711 Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
Johannes Doerfert81df4522019-08-30 15:22:28 +00003712 }
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003713 /// See AAFromMustBeExecutedContext
3714 bool followUse(Attributor &A, const Use *U, const Instruction *I) {
3715 bool TrackUse = false;
3716
Hideto Ueno4ecf2552019-12-12 13:42:40 +00003717 unsigned int KnownAlign =
3718 getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003719 takeKnownMaximum(KnownAlign);
3720
3721 return TrackUse;
3722 }
Johannes Doerfert81df4522019-08-30 15:22:28 +00003723
3724 /// See AbstractAttribute::getAsStr().
3725 const std::string getAsStr() const override {
3726 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
3727 "-" + std::to_string(getAssumedAlign()) + ">")
3728 : "unknown-align";
3729 }
3730};
3731
3732/// Align attribute for a floating value.
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003733struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> {
3734 using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>;
3735 AAAlignFloating(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert81df4522019-08-30 15:22:28 +00003736
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003737 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert234eda52019-08-16 19:51:23 +00003738 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003739 Base::updateImpl(A);
3740
Johannes Doerfert234eda52019-08-16 19:51:23 +00003741 const DataLayout &DL = A.getDataLayout();
3742
Johannes Doerfertb9b87912019-08-20 06:02:39 +00003743 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
3744 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003745 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V));
3746 if (!Stripped && this == &AA) {
Johannes Doerfert234eda52019-08-16 19:51:23 +00003747 // Use only IR information if we did not strip anything.
Guillaume Chateletbae629b2019-10-15 13:58:22 +00003748 const MaybeAlign PA = V.getPointerAlignment(DL);
3749 T.takeKnownMaximum(PA ? PA->value() : 0);
Johannes Doerfert234eda52019-08-16 19:51:23 +00003750 T.indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00003751 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003752 // Use abstract attribute information.
3753 const AAAlign::StateType &DS =
3754 static_cast<const AAAlign::StateType &>(AA.getState());
3755 T ^= DS;
Johannes Doerfert234eda52019-08-16 19:51:23 +00003756 }
Johannes Doerfertb9b87912019-08-20 06:02:39 +00003757 return T.isValidState();
Johannes Doerfert234eda52019-08-16 19:51:23 +00003758 };
3759
3760 StateType T;
3761 if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
3762 VisitValueCB))
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003763 return indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00003764
Johannes Doerfert028b2aa2019-08-20 05:57:01 +00003765 // TODO: If we know we visited all incoming values, thus no are assumed
3766 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +00003767 return clampStateAndIndicateChange(getState(), T);
3768 }
3769
3770 /// See AbstractAttribute::trackStatistics()
3771 void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
3772};
3773
3774/// Align attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003775struct AAAlignReturned final
3776 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00003777 AAAlignReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003778 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003779
3780 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003781 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003782};
3783
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003784/// Align attribute for function argument.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003785struct AAAlignArgument final
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003786 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
3787 AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00003788 AAAlignArgument(const IRPosition &IRP)
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003789 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
3790 AAAlignImpl>(
3791 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003792
3793 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert169af992019-08-20 06:09:56 +00003794 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003795};
3796
Johannes Doerfert234eda52019-08-16 19:51:23 +00003797struct AAAlignCallSiteArgument final : AAAlignFloating {
3798 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003799
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003800 /// See AbstractAttribute::manifest(...).
3801 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert30179d72020-01-12 00:25:45 -06003802 ChangeStatus Changed = AAAlignImpl::manifest(A);
3803 MaybeAlign InheritAlign =
3804 getAssociatedValue().getPointerAlignment(A.getDataLayout());
3805 if (InheritAlign.valueOrOne() >= getAssumedAlign())
3806 Changed = ChangeStatus::UNCHANGED;
3807 return Changed;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003808 }
3809
Johannes Doerfertdada8132019-12-31 01:27:50 -06003810 /// See AbstractAttribute::updateImpl(Attributor &A).
3811 ChangeStatus updateImpl(Attributor &A) override {
3812 ChangeStatus Changed = AAAlignFloating::updateImpl(A);
3813 if (Argument *Arg = getAssociatedArgument()) {
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06003814 // We only take known information from the argument
3815 // so we do not need to track a dependence.
Johannes Doerfertdada8132019-12-31 01:27:50 -06003816 const auto &ArgAlignAA = A.getAAFor<AAAlign>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06003817 *this, IRPosition::argument(*Arg), /* TrackDependence */ false);
Johannes Doerfertdada8132019-12-31 01:27:50 -06003818 takeKnownMaximum(ArgAlignAA.getKnownAlign());
3819 }
3820 return Changed;
3821 }
3822
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003823 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003824 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003825};
3826
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003827/// Align attribute deduction for a call site return value.
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003828struct AAAlignCallSiteReturned final
3829 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
3830 AAAlignImpl> {
3831 using Base =
3832 AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
3833 AAAlignImpl>;
3834 AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003835
3836 /// See AbstractAttribute::initialize(...).
3837 void initialize(Attributor &A) override {
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003838 Base::initialize(A);
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003839 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003840 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003841 indicatePessimisticFixpoint();
3842 }
3843
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003844 /// See AbstractAttribute::trackStatistics()
3845 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
3846};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003847
Johannes Doerferte83f3032019-08-05 23:22:05 +00003848/// ------------------ Function No-Return Attribute ----------------------------
Johannes Doerfert344d0382019-08-07 22:34:26 +00003849struct AANoReturnImpl : public AANoReturn {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003850 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
Johannes Doerferte83f3032019-08-05 23:22:05 +00003851
Johannes Doerfert0cc2b612019-10-13 21:25:53 +00003852 /// See AbstractAttribute::initialize(...).
3853 void initialize(Attributor &A) override {
3854 AANoReturn::initialize(A);
3855 Function *F = getAssociatedFunction();
Johannes Doerfert1b6041a2019-11-01 20:17:48 -05003856 if (!F)
Johannes Doerfert0cc2b612019-10-13 21:25:53 +00003857 indicatePessimisticFixpoint();
3858 }
3859
Johannes Doerferte83f3032019-08-05 23:22:05 +00003860 /// See AbstractAttribute::getAsStr().
3861 const std::string getAsStr() const override {
3862 return getAssumed() ? "noreturn" : "may-return";
3863 }
3864
Johannes Doerferte83f3032019-08-05 23:22:05 +00003865 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00003866 virtual ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003867 auto CheckForNoReturn = [](Instruction &) { return false; };
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003868 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003869 {(unsigned)Instruction::Ret}))
Johannes Doerferte83f3032019-08-05 23:22:05 +00003870 return indicatePessimisticFixpoint();
Johannes Doerferte83f3032019-08-05 23:22:05 +00003871 return ChangeStatus::UNCHANGED;
3872 }
3873};
3874
Johannes Doerfertfb69f762019-08-05 23:32:31 +00003875struct AANoReturnFunction final : AANoReturnImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003876 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003877
3878 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003879 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00003880};
3881
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003882/// NoReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003883struct AANoReturnCallSite final : AANoReturnImpl {
3884 AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
3885
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003886 /// See AbstractAttribute::updateImpl(...).
3887 ChangeStatus updateImpl(Attributor &A) override {
3888 // TODO: Once we have call site specific value information we can provide
3889 // call site specific liveness information and then it makes
3890 // sense to specialize attributes for call sites arguments instead of
3891 // redirecting requests to the callee argument.
3892 Function *F = getAssociatedFunction();
3893 const IRPosition &FnPos = IRPosition::function(*F);
3894 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos);
3895 return clampStateAndIndicateChange(
3896 getState(),
3897 static_cast<const AANoReturn::StateType &>(FnAA.getState()));
3898 }
3899
3900 /// See AbstractAttribute::trackStatistics()
3901 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
3902};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003903
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003904/// ----------------------- Variable Capturing ---------------------------------
3905
3906/// A class to hold the state of for no-capture attributes.
3907struct AANoCaptureImpl : public AANoCapture {
3908 AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {}
3909
3910 /// See AbstractAttribute::initialize(...).
3911 void initialize(Attributor &A) override {
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05003912 if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
3913 indicateOptimisticFixpoint();
3914 return;
3915 }
3916 Function *AnchorScope = getAnchorScope();
3917 if (isFnInterfaceKind() &&
3918 (!AnchorScope || !AnchorScope->hasExactDefinition())) {
3919 indicatePessimisticFixpoint();
3920 return;
3921 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003922
Johannes Doerfert72adda12019-10-10 05:33:21 +00003923 // You cannot "capture" null in the default address space.
3924 if (isa<ConstantPointerNull>(getAssociatedValue()) &&
3925 getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
3926 indicateOptimisticFixpoint();
3927 return;
3928 }
3929
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05003930 const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003931
3932 // Check what state the associated function can actually capture.
3933 if (F)
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05003934 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003935 else
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003936 indicatePessimisticFixpoint();
3937 }
3938
3939 /// See AbstractAttribute::updateImpl(...).
3940 ChangeStatus updateImpl(Attributor &A) override;
3941
3942 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
3943 virtual void
3944 getDeducedAttributes(LLVMContext &Ctx,
3945 SmallVectorImpl<Attribute> &Attrs) const override {
3946 if (!isAssumedNoCaptureMaybeReturned())
3947 return;
3948
Hideto Ueno37367642019-09-11 06:52:11 +00003949 if (getArgNo() >= 0) {
3950 if (isAssumedNoCapture())
3951 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
3952 else if (ManifestInternal)
3953 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
3954 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003955 }
3956
3957 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
3958 /// depending on the ability of the function associated with \p IRP to capture
3959 /// state in memory and through "returning/throwing", respectively.
Johannes Doerfert3839b572019-10-21 00:48:42 +00003960 static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
3961 const Function &F,
Johannes Doerfert1a746452019-10-20 22:28:49 -05003962 BitIntegerState &State) {
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003963 // TODO: Once we have memory behavior attributes we should use them here.
3964
3965 // If we know we cannot communicate or write to memory, we do not care about
3966 // ptr2int anymore.
3967 if (F.onlyReadsMemory() && F.doesNotThrow() &&
3968 F.getReturnType()->isVoidTy()) {
3969 State.addKnownBits(NO_CAPTURE);
3970 return;
3971 }
3972
3973 // A function cannot capture state in memory if it only reads memory, it can
3974 // however return/throw state and the state might be influenced by the
3975 // pointer value, e.g., loading from a returned pointer might reveal a bit.
3976 if (F.onlyReadsMemory())
3977 State.addKnownBits(NOT_CAPTURED_IN_MEM);
3978
3979 // A function cannot communicate state back if it does not through
3980 // exceptions and doesn not return values.
3981 if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
3982 State.addKnownBits(NOT_CAPTURED_IN_RET);
Johannes Doerfert3839b572019-10-21 00:48:42 +00003983
3984 // Check existing "returned" attributes.
3985 int ArgNo = IRP.getArgNo();
3986 if (F.doesNotThrow() && ArgNo >= 0) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01003987 for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
Johannes Doerfert3839b572019-10-21 00:48:42 +00003988 if (F.hasParamAttribute(u, Attribute::Returned)) {
Johannes Doerfert9d5ad5e2019-10-21 01:29:10 +00003989 if (u == unsigned(ArgNo))
Johannes Doerfert3839b572019-10-21 00:48:42 +00003990 State.removeAssumedBits(NOT_CAPTURED_IN_RET);
3991 else if (F.onlyReadsMemory())
3992 State.addKnownBits(NO_CAPTURE);
3993 else
3994 State.addKnownBits(NOT_CAPTURED_IN_RET);
3995 break;
3996 }
3997 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003998 }
3999
4000 /// See AbstractState::getAsStr().
4001 const std::string getAsStr() const override {
4002 if (isKnownNoCapture())
4003 return "known not-captured";
4004 if (isAssumedNoCapture())
4005 return "assumed not-captured";
4006 if (isKnownNoCaptureMaybeReturned())
4007 return "known not-captured-maybe-returned";
4008 if (isAssumedNoCaptureMaybeReturned())
4009 return "assumed not-captured-maybe-returned";
4010 return "assumed-captured";
4011 }
4012};
4013
4014/// Attributor-aware capture tracker.
4015struct AACaptureUseTracker final : public CaptureTracker {
4016
4017 /// Create a capture tracker that can lookup in-flight abstract attributes
4018 /// through the Attributor \p A.
4019 ///
4020 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4021 /// search is stopped. If a use leads to a return instruction,
4022 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4023 /// If a use leads to a ptr2int which may capture the value,
4024 /// \p CapturedInInteger is set. If a use is found that is currently assumed
4025 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4026 /// set. All values in \p PotentialCopies are later tracked as well. For every
4027 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4028 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4029 /// conservatively set to true.
4030 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
Johannes Doerfert31784242019-10-29 23:18:49 -05004031 const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004032 SmallVectorImpl<const Value *> &PotentialCopies,
4033 unsigned &RemainingUsesToExplore)
4034 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4035 PotentialCopies(PotentialCopies),
4036 RemainingUsesToExplore(RemainingUsesToExplore) {}
4037
4038 /// Determine if \p V maybe captured. *Also updates the state!*
4039 bool valueMayBeCaptured(const Value *V) {
4040 if (V->getType()->isPointerTy()) {
4041 PointerMayBeCaptured(V, this);
4042 } else {
4043 State.indicatePessimisticFixpoint();
4044 }
4045 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4046 }
4047
4048 /// See CaptureTracker::tooManyUses().
4049 void tooManyUses() override {
4050 State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4051 }
4052
4053 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4054 if (CaptureTracker::isDereferenceableOrNull(O, DL))
4055 return true;
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004056 const auto &DerefAA = A.getAAFor<AADereferenceable>(
4057 NoCaptureAA, IRPosition::value(*O), /* TrackDependence */ true,
4058 DepClassTy::OPTIONAL);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004059 return DerefAA.getAssumedDereferenceableBytes();
4060 }
4061
4062 /// See CaptureTracker::captured(...).
4063 bool captured(const Use *U) override {
4064 Instruction *UInst = cast<Instruction>(U->getUser());
4065 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
4066 << "\n");
4067
4068 // Because we may reuse the tracker multiple times we keep track of the
4069 // number of explored uses ourselves as well.
4070 if (RemainingUsesToExplore-- == 0) {
4071 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
4072 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4073 /* Return */ true);
4074 }
4075
4076 // Deal with ptr2int by following uses.
4077 if (isa<PtrToIntInst>(UInst)) {
4078 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
4079 return valueMayBeCaptured(UInst);
4080 }
4081
4082 // Explicitly catch return instructions.
4083 if (isa<ReturnInst>(UInst))
4084 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4085 /* Return */ true);
4086
4087 // For now we only use special logic for call sites. However, the tracker
4088 // itself knows about a lot of other non-capturing cases already.
4089 CallSite CS(UInst);
4090 if (!CS || !CS.isArgOperand(U))
4091 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4092 /* Return */ true);
4093
4094 unsigned ArgNo = CS.getArgumentNo(U);
4095 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
4096 // If we have a abstract no-capture attribute for the argument we can use
4097 // it to justify a non-capture attribute here. This allows recursion!
4098 auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos);
4099 if (ArgNoCaptureAA.isAssumedNoCapture())
4100 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4101 /* Return */ false);
4102 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4103 addPotentialCopy(CS);
4104 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4105 /* Return */ false);
4106 }
4107
4108 // Lastly, we could not find a reason no-capture can be assumed so we don't.
4109 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4110 /* Return */ true);
4111 }
4112
4113 /// Register \p CS as potential copy of the value we are checking.
4114 void addPotentialCopy(CallSite CS) {
4115 PotentialCopies.push_back(CS.getInstruction());
4116 }
4117
4118 /// See CaptureTracker::shouldExplore(...).
4119 bool shouldExplore(const Use *U) override {
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004120 // Check liveness, if it is used to stop exploring we need a dependence.
4121 if (IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser()))) {
4122 A.recordDependence(IsDeadAA, NoCaptureAA, DepClassTy::OPTIONAL);
4123 return false;
4124 }
4125 return true;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004126 }
4127
4128 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4129 /// \p CapturedInRet, then return the appropriate value for use in the
4130 /// CaptureTracker::captured() interface.
4131 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
4132 bool CapturedInRet) {
4133 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
4134 << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
4135 if (CapturedInMem)
4136 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
4137 if (CapturedInInt)
4138 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
4139 if (CapturedInRet)
4140 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
4141 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4142 }
4143
4144private:
4145 /// The attributor providing in-flight abstract attributes.
4146 Attributor &A;
4147
4148 /// The abstract attribute currently updated.
4149 AANoCapture &NoCaptureAA;
4150
4151 /// The abstract liveness state.
4152 const AAIsDead &IsDeadAA;
4153
4154 /// The state currently updated.
Johannes Doerfert31784242019-10-29 23:18:49 -05004155 AANoCapture::StateType &State;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004156
4157 /// Set of potential copies of the tracked value.
4158 SmallVectorImpl<const Value *> &PotentialCopies;
4159
4160 /// Global counter to limit the number of explored uses.
4161 unsigned &RemainingUsesToExplore;
4162};
4163
4164ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
4165 const IRPosition &IRP = getIRPosition();
4166 const Value *V =
4167 getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue();
4168 if (!V)
4169 return indicatePessimisticFixpoint();
4170
4171 const Function *F =
4172 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
4173 assert(F && "Expected a function!");
Johannes Doerfert3839b572019-10-21 00:48:42 +00004174 const IRPosition &FnPos = IRPosition::function(*F);
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004175 const auto &IsDeadAA =
4176 A.getAAFor<AAIsDead>(*this, FnPos, /* TrackDependence */ false);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004177
4178 AANoCapture::StateType T;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004179
Johannes Doerfert3839b572019-10-21 00:48:42 +00004180 // Readonly means we cannot capture through memory.
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004181 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
4182 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004183 if (FnMemAA.isAssumedReadOnly()) {
4184 T.addKnownBits(NOT_CAPTURED_IN_MEM);
4185 if (FnMemAA.isKnownReadOnly())
4186 addKnownBits(NOT_CAPTURED_IN_MEM);
4187 }
4188
4189 // Make sure all returned values are different than the underlying value.
4190 // TODO: we could do this in a more sophisticated way inside
4191 // AAReturnedValues, e.g., track all values that escape through returns
4192 // directly somehow.
4193 auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
4194 bool SeenConstant = false;
4195 for (auto &It : RVAA.returned_values()) {
4196 if (isa<Constant>(It.first)) {
4197 if (SeenConstant)
4198 return false;
4199 SeenConstant = true;
4200 } else if (!isa<Argument>(It.first) ||
4201 It.first == getAssociatedArgument())
4202 return false;
4203 }
4204 return true;
4205 };
4206
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004207 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
4208 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004209 if (NoUnwindAA.isAssumedNoUnwind()) {
4210 bool IsVoidTy = F->getReturnType()->isVoidTy();
4211 const AAReturnedValues *RVAA =
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004212 IsVoidTy ? nullptr
4213 : &A.getAAFor<AAReturnedValues>(*this, FnPos,
4214 /* TrackDependence */ true,
4215 DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004216 if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
4217 T.addKnownBits(NOT_CAPTURED_IN_RET);
4218 if (T.isKnown(NOT_CAPTURED_IN_MEM))
4219 return ChangeStatus::UNCHANGED;
4220 if (NoUnwindAA.isKnownNoUnwind() &&
4221 (IsVoidTy || RVAA->getState().isAtFixpoint())) {
4222 addKnownBits(NOT_CAPTURED_IN_RET);
4223 if (isKnown(NOT_CAPTURED_IN_MEM))
4224 return indicateOptimisticFixpoint();
4225 }
4226 }
4227 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004228
4229 // Use the CaptureTracker interface and logic with the specialized tracker,
4230 // defined in AACaptureUseTracker, that can look at in-flight abstract
4231 // attributes and directly updates the assumed state.
4232 SmallVector<const Value *, 4> PotentialCopies;
4233 unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore;
4234 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
4235 RemainingUsesToExplore);
4236
4237 // Check all potential copies of the associated value until we can assume
4238 // none will be captured or we have to assume at least one might be.
4239 unsigned Idx = 0;
4240 PotentialCopies.push_back(V);
4241 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
4242 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
4243
Johannes Doerfert1a746452019-10-20 22:28:49 -05004244 AANoCapture::StateType &S = getState();
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004245 auto Assumed = S.getAssumed();
4246 S.intersectAssumedBits(T.getAssumed());
Johannes Doerfert31784242019-10-29 23:18:49 -05004247 if (!isAssumedNoCaptureMaybeReturned())
4248 return indicatePessimisticFixpoint();
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004249 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
4250 : ChangeStatus::CHANGED;
4251}
4252
4253/// NoCapture attribute for function arguments.
4254struct AANoCaptureArgument final : AANoCaptureImpl {
4255 AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4256
4257 /// See AbstractAttribute::trackStatistics()
4258 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
4259};
4260
4261/// NoCapture attribute for call site arguments.
4262struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
4263 AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4264
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06004265 /// See AbstractAttribute::initialize(...).
4266 void initialize(Attributor &A) override {
4267 if (Argument *Arg = getAssociatedArgument())
4268 if (Arg->hasByValAttr())
4269 indicateOptimisticFixpoint();
4270 AANoCaptureImpl::initialize(A);
4271 }
4272
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004273 /// See AbstractAttribute::updateImpl(...).
4274 ChangeStatus updateImpl(Attributor &A) override {
4275 // TODO: Once we have call site specific value information we can provide
4276 // call site specific liveness information and then it makes
4277 // sense to specialize attributes for call sites arguments instead of
4278 // redirecting requests to the callee argument.
4279 Argument *Arg = getAssociatedArgument();
4280 if (!Arg)
4281 return indicatePessimisticFixpoint();
4282 const IRPosition &ArgPos = IRPosition::argument(*Arg);
4283 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos);
4284 return clampStateAndIndicateChange(
4285 getState(),
4286 static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
4287 }
4288
4289 /// See AbstractAttribute::trackStatistics()
4290 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
4291};
4292
4293/// NoCapture attribute for floating values.
4294struct AANoCaptureFloating final : AANoCaptureImpl {
4295 AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4296
4297 /// See AbstractAttribute::trackStatistics()
4298 void trackStatistics() const override {
4299 STATS_DECLTRACK_FLOATING_ATTR(nocapture)
4300 }
4301};
4302
4303/// NoCapture attribute for function return value.
4304struct AANoCaptureReturned final : AANoCaptureImpl {
4305 AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {
4306 llvm_unreachable("NoCapture is not applicable to function returns!");
4307 }
4308
4309 /// See AbstractAttribute::initialize(...).
4310 void initialize(Attributor &A) override {
4311 llvm_unreachable("NoCapture is not applicable to function returns!");
4312 }
4313
4314 /// See AbstractAttribute::updateImpl(...).
4315 ChangeStatus updateImpl(Attributor &A) override {
4316 llvm_unreachable("NoCapture is not applicable to function returns!");
4317 }
4318
4319 /// See AbstractAttribute::trackStatistics()
4320 void trackStatistics() const override {}
4321};
4322
4323/// NoCapture attribute deduction for a call site return value.
4324struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
4325 AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4326
4327 /// See AbstractAttribute::trackStatistics()
4328 void trackStatistics() const override {
4329 STATS_DECLTRACK_CSRET_ATTR(nocapture)
4330 }
4331};
4332
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004333/// ------------------ Value Simplify Attribute ----------------------------
4334struct AAValueSimplifyImpl : AAValueSimplify {
4335 AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {}
4336
Johannes Doerfert9dcf8892020-01-11 23:59:36 -06004337 /// See AbstractAttribute::initialize(...).
4338 void initialize(Attributor &A) override {
4339 if (getAssociatedValue().getType()->isVoidTy())
4340 indicatePessimisticFixpoint();
4341 }
4342
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004343 /// See AbstractAttribute::getAsStr().
4344 const std::string getAsStr() const override {
4345 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
4346 : "not-simple";
4347 }
4348
4349 /// See AbstractAttribute::trackStatistics()
4350 void trackStatistics() const override {}
4351
4352 /// See AAValueSimplify::getAssumedSimplifiedValue()
4353 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
4354 if (!getAssumed())
4355 return const_cast<Value *>(&getAssociatedValue());
4356 return SimplifiedAssociatedValue;
4357 }
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004358
4359 /// Helper function for querying AAValueSimplify and updating candicate.
4360 /// \param QueryingValue Value trying to unify with SimplifiedValue
4361 /// \param AccumulatedSimplifiedValue Current simplification result.
4362 static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
4363 Value &QueryingValue,
4364 Optional<Value *> &AccumulatedSimplifiedValue) {
4365 // FIXME: Add a typecast support.
4366
Johannes Doerfertd07b5a52020-01-12 00:00:33 -06004367 auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004368 QueryingAA, IRPosition::value(QueryingValue));
4369
4370 Optional<Value *> QueryingValueSimplified =
Johannes Doerfertd07b5a52020-01-12 00:00:33 -06004371 ValueSimplifyAA.getAssumedSimplifiedValue(A);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004372
4373 if (!QueryingValueSimplified.hasValue())
4374 return true;
4375
4376 if (!QueryingValueSimplified.getValue())
4377 return false;
4378
4379 Value &QueryingValueSimplifiedUnwrapped =
4380 *QueryingValueSimplified.getValue();
4381
4382 if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
4383 return true;
4384
4385 if (AccumulatedSimplifiedValue.hasValue())
4386 return AccumulatedSimplifiedValue == QueryingValueSimplified;
4387
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004388 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004389 << " is assumed to be "
4390 << QueryingValueSimplifiedUnwrapped << "\n");
4391
4392 AccumulatedSimplifiedValue = QueryingValueSimplified;
4393 return true;
4394 }
4395
Hideto Ueno188f9a32020-01-15 15:25:52 +09004396 bool askSimplifiedValueForAAValueConstantRange(Attributor &A) {
4397 if (!getAssociatedValue().getType()->isIntegerTy())
4398 return false;
4399
4400 const auto &ValueConstantRangeAA =
4401 A.getAAFor<AAValueConstantRange>(*this, getIRPosition());
4402
4403 Optional<ConstantInt *> COpt =
4404 ValueConstantRangeAA.getAssumedConstantInt(A);
4405 if (COpt.hasValue()) {
4406 if (auto *C = COpt.getValue())
4407 SimplifiedAssociatedValue = C;
4408 else
4409 return false;
4410 } else {
Johannes Doerfert63adbb92020-02-09 20:21:56 -06004411 SimplifiedAssociatedValue = llvm::None;
Hideto Ueno188f9a32020-01-15 15:25:52 +09004412 }
4413 return true;
4414 }
4415
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004416 /// See AbstractAttribute::manifest(...).
4417 ChangeStatus manifest(Attributor &A) override {
4418 ChangeStatus Changed = ChangeStatus::UNCHANGED;
4419
4420 if (!SimplifiedAssociatedValue.hasValue() ||
4421 !SimplifiedAssociatedValue.getValue())
4422 return Changed;
4423
4424 if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) {
4425 // We can replace the AssociatedValue with the constant.
4426 Value &V = getAssociatedValue();
4427 if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004428 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C
4429 << " :: " << *this << "\n");
Hideto Ueno34fe8d02019-12-30 17:08:48 +09004430 A.changeValueAfterManifest(V, *C);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004431 Changed = ChangeStatus::CHANGED;
4432 }
4433 }
4434
4435 return Changed | AAValueSimplify::manifest(A);
4436 }
4437
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004438 /// See AbstractState::indicatePessimisticFixpoint(...).
4439 ChangeStatus indicatePessimisticFixpoint() override {
4440 // NOTE: Associated value will be returned in a pessimistic fixpoint and is
4441 // regarded as known. That's why`indicateOptimisticFixpoint` is called.
4442 SimplifiedAssociatedValue = &getAssociatedValue();
Johannes Doerferta4b35882019-12-31 13:25:47 -06004443 indicateOptimisticFixpoint();
4444 return ChangeStatus::CHANGED;
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004445 }
4446
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004447protected:
4448 // An assumed simplified value. Initially, it is set to Optional::None, which
4449 // means that the value is not clear under current assumption. If in the
4450 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4451 // returns orignal associated value.
4452 Optional<Value *> SimplifiedAssociatedValue;
4453};
4454
4455struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
4456 AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4457
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004458 void initialize(Attributor &A) override {
4459 AAValueSimplifyImpl::initialize(A);
4460 if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration())
4461 indicatePessimisticFixpoint();
4462 if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest},
4463 /* IgnoreSubsumingPositions */ true))
4464 indicatePessimisticFixpoint();
4465 }
4466
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004467 /// See AbstractAttribute::updateImpl(...).
4468 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004469 // Byval is only replacable if it is readonly otherwise we would write into
4470 // the replaced value and not the copy that byval creates implicitly.
4471 Argument *Arg = getAssociatedArgument();
4472 if (Arg->hasByValAttr()) {
4473 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
4474 if (!MemAA.isAssumedReadOnly())
4475 return indicatePessimisticFixpoint();
4476 }
4477
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004478 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4479
Johannes Doerfert661db042019-10-07 23:14:58 +00004480 auto PredForCallSite = [&](AbstractCallSite ACS) {
4481 // Check if we have an associated argument or not (which can happen for
4482 // callback calls).
Johannes Doerferte360ee62019-11-01 18:45:25 -05004483 Value *ArgOp = ACS.getCallArgOperand(getArgNo());
4484 if (!ArgOp)
Hideto Ueno4ecf2552019-12-12 13:42:40 +00004485 return false;
Johannes Doerferte360ee62019-11-01 18:45:25 -05004486 // We can only propagate thread independent values through callbacks.
4487 // This is different to direct/indirect call sites because for them we
4488 // know the thread executing the caller and callee is the same. For
4489 // callbacks this is not guaranteed, thus a thread dependent value could
4490 // be different for the caller and callee, making it invalid to propagate.
4491 if (ACS.isCallbackCall())
Hideto Ueno4ecf2552019-12-12 13:42:40 +00004492 if (auto *C = dyn_cast<Constant>(ArgOp))
Johannes Doerferte360ee62019-11-01 18:45:25 -05004493 if (C->isThreadDependent())
4494 return false;
4495 return checkAndUpdate(A, *this, *ArgOp, SimplifiedAssociatedValue);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004496 };
4497
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06004498 bool AllCallSitesKnown;
4499 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
4500 AllCallSitesKnown))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004501 if (!askSimplifiedValueForAAValueConstantRange(A))
4502 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004503
4504 // If a candicate was found in this update, return CHANGED.
4505 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4506 ? ChangeStatus::UNCHANGED
4507 : ChangeStatus ::CHANGED;
4508 }
4509
4510 /// See AbstractAttribute::trackStatistics()
4511 void trackStatistics() const override {
4512 STATS_DECLTRACK_ARG_ATTR(value_simplify)
4513 }
4514};
4515
4516struct AAValueSimplifyReturned : AAValueSimplifyImpl {
4517 AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4518
4519 /// See AbstractAttribute::updateImpl(...).
4520 ChangeStatus updateImpl(Attributor &A) override {
4521 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4522
4523 auto PredForReturned = [&](Value &V) {
4524 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4525 };
4526
4527 if (!A.checkForAllReturnedValues(PredForReturned, *this))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004528 if (!askSimplifiedValueForAAValueConstantRange(A))
4529 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004530
4531 // If a candicate was found in this update, return CHANGED.
4532 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4533 ? ChangeStatus::UNCHANGED
4534 : ChangeStatus ::CHANGED;
4535 }
4536 /// See AbstractAttribute::trackStatistics()
4537 void trackStatistics() const override {
4538 STATS_DECLTRACK_FNRET_ATTR(value_simplify)
4539 }
4540};
4541
4542struct AAValueSimplifyFloating : AAValueSimplifyImpl {
4543 AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4544
4545 /// See AbstractAttribute::initialize(...).
4546 void initialize(Attributor &A) override {
4547 Value &V = getAnchorValue();
4548
4549 // TODO: add other stuffs
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004550 if (isa<Constant>(V))
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004551 indicatePessimisticFixpoint();
4552 }
4553
4554 /// See AbstractAttribute::updateImpl(...).
4555 ChangeStatus updateImpl(Attributor &A) override {
4556 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4557
Johannes Doerfert76843ba2020-01-28 23:51:25 -06004558 auto VisitValueCB = [&](Value &V, bool &, bool Stripped) -> bool {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004559 auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V));
4560 if (!Stripped && this == &AA) {
4561 // TODO: Look the instruction and check recursively.
Hideto Ueno188f9a32020-01-15 15:25:52 +09004562
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004563 LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
4564 << "\n");
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004565 return false;
4566 }
4567 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4568 };
4569
Johannes Doerfert76843ba2020-01-28 23:51:25 -06004570 bool Dummy = false;
Johannes Doerfert6626d1b2020-01-28 11:49:59 -06004571 if (!genericValueTraversal<AAValueSimplify, bool>(A, getIRPosition(), *this,
4572 Dummy, VisitValueCB))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004573 if (!askSimplifiedValueForAAValueConstantRange(A))
4574 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004575
4576 // If a candicate was found in this update, return CHANGED.
4577
4578 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4579 ? ChangeStatus::UNCHANGED
4580 : ChangeStatus ::CHANGED;
4581 }
4582
4583 /// See AbstractAttribute::trackStatistics()
4584 void trackStatistics() const override {
4585 STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
4586 }
4587};
4588
4589struct AAValueSimplifyFunction : AAValueSimplifyImpl {
4590 AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4591
4592 /// See AbstractAttribute::initialize(...).
4593 void initialize(Attributor &A) override {
4594 SimplifiedAssociatedValue = &getAnchorValue();
4595 indicateOptimisticFixpoint();
4596 }
4597 /// See AbstractAttribute::initialize(...).
4598 ChangeStatus updateImpl(Attributor &A) override {
4599 llvm_unreachable(
4600 "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
4601 }
4602 /// See AbstractAttribute::trackStatistics()
4603 void trackStatistics() const override {
4604 STATS_DECLTRACK_FN_ATTR(value_simplify)
4605 }
4606};
4607
4608struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
4609 AAValueSimplifyCallSite(const IRPosition &IRP)
4610 : AAValueSimplifyFunction(IRP) {}
4611 /// See AbstractAttribute::trackStatistics()
4612 void trackStatistics() const override {
4613 STATS_DECLTRACK_CS_ATTR(value_simplify)
4614 }
4615};
4616
4617struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
4618 AAValueSimplifyCallSiteReturned(const IRPosition &IRP)
4619 : AAValueSimplifyReturned(IRP) {}
4620
4621 void trackStatistics() const override {
4622 STATS_DECLTRACK_CSRET_ATTR(value_simplify)
4623 }
4624};
4625struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
4626 AAValueSimplifyCallSiteArgument(const IRPosition &IRP)
4627 : AAValueSimplifyFloating(IRP) {}
4628
4629 void trackStatistics() const override {
4630 STATS_DECLTRACK_CSARG_ATTR(value_simplify)
4631 }
4632};
4633
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004634/// ----------------------- Heap-To-Stack Conversion ---------------------------
4635struct AAHeapToStackImpl : public AAHeapToStack {
4636 AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {}
4637
4638 const std::string getAsStr() const override {
4639 return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
4640 }
4641
4642 ChangeStatus manifest(Attributor &A) override {
4643 assert(getState().isValidState() &&
4644 "Attempted to manifest an invalid state!");
4645
4646 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
4647 Function *F = getAssociatedFunction();
4648 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
4649
4650 for (Instruction *MallocCall : MallocCalls) {
4651 // This malloc cannot be replaced.
4652 if (BadMallocCalls.count(MallocCall))
4653 continue;
4654
4655 for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
4656 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
4657 A.deleteAfterManifest(*FreeCall);
4658 HasChanged = ChangeStatus::CHANGED;
4659 }
4660
4661 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
4662 << "\n");
4663
4664 Constant *Size;
4665 if (isCallocLikeFn(MallocCall, TLI)) {
4666 auto *Num = cast<ConstantInt>(MallocCall->getOperand(0));
4667 auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1));
4668 APInt TotalSize = SizeT->getValue() * Num->getValue();
4669 Size =
4670 ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize);
4671 } else {
4672 Size = cast<ConstantInt>(MallocCall->getOperand(0));
4673 }
4674
4675 unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
4676 Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
4677 Size, "", MallocCall->getNextNode());
4678
4679 if (AI->getType() != MallocCall->getType())
4680 AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
4681 AI->getNextNode());
4682
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06004683 A.replaceAllUsesWith(*MallocCall, *AI);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004684
4685 if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
4686 auto *NBB = II->getNormalDest();
4687 BranchInst::Create(NBB, MallocCall->getParent());
4688 A.deleteAfterManifest(*MallocCall);
4689 } else {
4690 A.deleteAfterManifest(*MallocCall);
4691 }
4692
4693 if (isCallocLikeFn(MallocCall, TLI)) {
4694 auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
4695 AI->getNextNode());
4696 Value *Ops[] = {
4697 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
4698 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
4699
4700 Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
4701 Module *M = F->getParent();
4702 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
4703 CallInst::Create(Fn, Ops, "", BI->getNextNode());
4704 }
4705 HasChanged = ChangeStatus::CHANGED;
4706 }
4707
4708 return HasChanged;
4709 }
4710
4711 /// Collection of all malloc calls in a function.
4712 SmallSetVector<Instruction *, 4> MallocCalls;
4713
4714 /// Collection of malloc calls that cannot be converted.
4715 DenseSet<const Instruction *> BadMallocCalls;
4716
4717 /// A map for each malloc call to the set of associated free calls.
4718 DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
4719
4720 ChangeStatus updateImpl(Attributor &A) override;
4721};
4722
4723ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
4724 const Function *F = getAssociatedFunction();
4725 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
4726
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004727 MustBeExecutedContextExplorer &Explorer =
4728 A.getInfoCache().getMustBeExecutedContextExplorer();
4729
4730 auto FreeCheck = [&](Instruction &I) {
4731 const auto &Frees = FreesForMalloc.lookup(&I);
4732 if (Frees.size() != 1)
4733 return false;
4734 Instruction *UniqueFree = *Frees.begin();
4735 return Explorer.findInContextOf(UniqueFree, I.getNextNode());
4736 };
4737
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004738 auto UsesCheck = [&](Instruction &I) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004739 bool ValidUsesOnly = true;
4740 bool MustUse = true;
Hideto Ueno827bade2019-12-12 12:26:30 +00004741 auto Pred = [&](const Use &U, bool &Follow) -> bool {
4742 Instruction *UserI = cast<Instruction>(U.getUser());
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004743 if (isa<LoadInst>(UserI))
Hideto Ueno827bade2019-12-12 12:26:30 +00004744 return true;
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004745 if (auto *SI = dyn_cast<StoreInst>(UserI)) {
Hideto Ueno827bade2019-12-12 12:26:30 +00004746 if (SI->getValueOperand() == U.get()) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004747 LLVM_DEBUG(dbgs()
4748 << "[H2S] escaping store to memory: " << *UserI << "\n");
4749 ValidUsesOnly = false;
4750 } else {
4751 // A store into the malloc'ed memory is fine.
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004752 }
Hideto Ueno827bade2019-12-12 12:26:30 +00004753 return true;
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004754 }
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004755 if (auto *CB = dyn_cast<CallBase>(UserI)) {
Hideto Ueno827bade2019-12-12 12:26:30 +00004756 if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
4757 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004758 // Record malloc.
4759 if (isFreeCall(UserI, TLI)) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004760 if (MustUse) {
Hideto Ueno827bade2019-12-12 12:26:30 +00004761 FreesForMalloc[&I].insert(UserI);
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004762 } else {
4763 LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: "
4764 << *UserI << "\n");
4765 ValidUsesOnly = false;
4766 }
Hideto Ueno827bade2019-12-12 12:26:30 +00004767 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004768 }
4769
Hideto Ueno827bade2019-12-12 12:26:30 +00004770 unsigned ArgNo = CB->getArgOperandNo(&U);
Stefan Stipanovica516fba2019-11-17 21:35:04 +01004771
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004772 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
4773 *this, IRPosition::callsite_argument(*CB, ArgNo));
4774
Stefan Stipanovica516fba2019-11-17 21:35:04 +01004775 // If a callsite argument use is nofree, we are fine.
4776 const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
4777 *this, IRPosition::callsite_argument(*CB, ArgNo));
4778
Hideto Ueno827bade2019-12-12 12:26:30 +00004779 if (!NoCaptureAA.isAssumedNoCapture() ||
4780 !ArgNoFreeAA.isAssumedNoFree()) {
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004781 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004782 ValidUsesOnly = false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004783 }
Hideto Ueno827bade2019-12-12 12:26:30 +00004784 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004785 }
4786
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004787 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
4788 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
4789 MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI));
Hideto Ueno827bade2019-12-12 12:26:30 +00004790 Follow = true;
4791 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004792 }
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004793 // Unknown user for which we can not track uses further (in a way that
4794 // makes sense).
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004795 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004796 ValidUsesOnly = false;
Hideto Ueno827bade2019-12-12 12:26:30 +00004797 return true;
4798 };
4799 A.checkForAllUses(Pred, *this, I);
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004800 return ValidUsesOnly;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004801 };
4802
4803 auto MallocCallocCheck = [&](Instruction &I) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004804 if (BadMallocCalls.count(&I))
4805 return true;
4806
4807 bool IsMalloc = isMallocLikeFn(&I, TLI);
4808 bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
4809 if (!IsMalloc && !IsCalloc) {
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004810 BadMallocCalls.insert(&I);
4811 return true;
4812 }
4813
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004814 if (IsMalloc) {
4815 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
Stefan Stipanovicfff8ec92019-12-17 20:41:09 +01004816 if (Size->getValue().ule(MaxHeapToStackSize))
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004817 if (UsesCheck(I) || FreeCheck(I)) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004818 MallocCalls.insert(&I);
4819 return true;
4820 }
4821 } else if (IsCalloc) {
4822 bool Overflow = false;
4823 if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
4824 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
4825 if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
Stefan Stipanovicfff8ec92019-12-17 20:41:09 +01004826 .ule(MaxHeapToStackSize))
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004827 if (!Overflow && (UsesCheck(I) || FreeCheck(I))) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004828 MallocCalls.insert(&I);
4829 return true;
4830 }
4831 }
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004832
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004833 BadMallocCalls.insert(&I);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004834 return true;
4835 };
4836
4837 size_t NumBadMallocs = BadMallocCalls.size();
4838
4839 A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
4840
4841 if (NumBadMallocs != BadMallocCalls.size())
4842 return ChangeStatus::CHANGED;
4843
4844 return ChangeStatus::UNCHANGED;
4845}
4846
4847struct AAHeapToStackFunction final : public AAHeapToStackImpl {
4848 AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {}
4849
4850 /// See AbstractAttribute::trackStatistics()
4851 void trackStatistics() const override {
4852 STATS_DECL(MallocCalls, Function,
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004853 "Number of malloc calls converted to allocas");
4854 for (auto *C : MallocCalls)
4855 if (!BadMallocCalls.count(C))
4856 ++BUILD_STAT_NAME(MallocCalls, Function);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004857 }
4858};
4859
Johannes Doerfert89c2e732019-10-30 17:20:20 -05004860/// ----------------------- Privatizable Pointers ------------------------------
4861struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
4862 AAPrivatizablePtrImpl(const IRPosition &IRP)
4863 : AAPrivatizablePtr(IRP), PrivatizableType(llvm::None) {}
4864
4865 ChangeStatus indicatePessimisticFixpoint() override {
4866 AAPrivatizablePtr::indicatePessimisticFixpoint();
4867 PrivatizableType = nullptr;
4868 return ChangeStatus::CHANGED;
4869 }
4870
4871 /// Identify the type we can chose for a private copy of the underlying
4872 /// argument. None means it is not clear yet, nullptr means there is none.
4873 virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
4874
4875 /// Return a privatizable type that encloses both T0 and T1.
4876 /// TODO: This is merely a stub for now as we should manage a mapping as well.
4877 Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
4878 if (!T0.hasValue())
4879 return T1;
4880 if (!T1.hasValue())
4881 return T0;
4882 if (T0 == T1)
4883 return T0;
4884 return nullptr;
4885 }
4886
4887 Optional<Type *> getPrivatizableType() const override {
4888 return PrivatizableType;
4889 }
4890
4891 const std::string getAsStr() const override {
4892 return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
4893 }
4894
4895protected:
4896 Optional<Type *> PrivatizableType;
4897};
4898
4899// TODO: Do this for call site arguments (probably also other values) as well.
4900
4901struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
4902 AAPrivatizablePtrArgument(const IRPosition &IRP)
4903 : AAPrivatizablePtrImpl(IRP) {}
4904
4905 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
4906 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
4907 // If this is a byval argument and we know all the call sites (so we can
4908 // rewrite them), there is no need to check them explicitly.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06004909 bool AllCallSitesKnown;
Johannes Doerfert89c2e732019-10-30 17:20:20 -05004910 if (getIRPosition().hasAttr(Attribute::ByVal) &&
4911 A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06004912 true, AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05004913 return getAssociatedValue().getType()->getPointerElementType();
4914
4915 Optional<Type *> Ty;
4916 unsigned ArgNo = getIRPosition().getArgNo();
4917
4918 // Make sure the associated call site argument has the same type at all call
4919 // sites and it is an allocation we know is safe to privatize, for now that
4920 // means we only allow alloca instructions.
4921 // TODO: We can additionally analyze the accesses in the callee to create
4922 // the type from that information instead. That is a little more
4923 // involved and will be done in a follow up patch.
4924 auto CallSiteCheck = [&](AbstractCallSite ACS) {
4925 IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
4926 // Check if a coresponding argument was found or if it is one not
4927 // associated (which can happen for callback calls).
4928 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
4929 return false;
4930
4931 // Check that all call sites agree on a type.
4932 auto &PrivCSArgAA = A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos);
4933 Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
4934
4935 LLVM_DEBUG({
4936 dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
4937 if (CSTy.hasValue() && CSTy.getValue())
4938 CSTy.getValue()->print(dbgs());
4939 else if (CSTy.hasValue())
4940 dbgs() << "<nullptr>";
4941 else
4942 dbgs() << "<none>";
4943 });
4944
4945 Ty = combineTypes(Ty, CSTy);
4946
4947 LLVM_DEBUG({
4948 dbgs() << " : New Type: ";
4949 if (Ty.hasValue() && Ty.getValue())
4950 Ty.getValue()->print(dbgs());
4951 else if (Ty.hasValue())
4952 dbgs() << "<nullptr>";
4953 else
4954 dbgs() << "<none>";
4955 dbgs() << "\n";
4956 });
4957
4958 return !Ty.hasValue() || Ty.getValue();
4959 };
4960
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06004961 if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05004962 return nullptr;
4963 return Ty;
4964 }
4965
4966 /// See AbstractAttribute::updateImpl(...).
4967 ChangeStatus updateImpl(Attributor &A) override {
4968 PrivatizableType = identifyPrivatizableType(A);
4969 if (!PrivatizableType.hasValue())
4970 return ChangeStatus::UNCHANGED;
4971 if (!PrivatizableType.getValue())
4972 return indicatePessimisticFixpoint();
4973
4974 // Avoid arguments with padding for now.
4975 if (!getIRPosition().hasAttr(Attribute::ByVal) &&
4976 !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
4977 A.getInfoCache().getDL())) {
4978 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
4979 return indicatePessimisticFixpoint();
4980 }
4981
4982 // Verify callee and caller agree on how the promoted argument would be
4983 // passed.
4984 // TODO: The use of the ArgumentPromotion interface here is ugly, we need a
4985 // specialized form of TargetTransformInfo::areFunctionArgsABICompatible
4986 // which doesn't require the arguments ArgumentPromotion wanted to pass.
4987 Function &Fn = *getIRPosition().getAnchorScope();
4988 SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy;
4989 ArgsToPromote.insert(getAssociatedArgument());
4990 const auto *TTI =
4991 A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
4992 if (!TTI ||
4993 !ArgumentPromotionPass::areFunctionArgsABICompatible(
4994 Fn, *TTI, ArgsToPromote, Dummy) ||
4995 ArgsToPromote.empty()) {
4996 LLVM_DEBUG(
4997 dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
4998 << Fn.getName() << "\n");
4999 return indicatePessimisticFixpoint();
5000 }
5001
5002 // Collect the types that will replace the privatizable type in the function
5003 // signature.
5004 SmallVector<Type *, 16> ReplacementTypes;
5005 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5006
5007 // Register a rewrite of the argument.
5008 Argument *Arg = getAssociatedArgument();
5009 if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
5010 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
5011 return indicatePessimisticFixpoint();
5012 }
5013
5014 unsigned ArgNo = Arg->getArgNo();
5015
5016 // Helper to check if for the given call site the associated argument is
5017 // passed to a callback where the privatization would be different.
5018 auto IsCompatiblePrivArgOfCallback = [&](CallSite CS) {
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005019 SmallVector<const Use *, 4> CBUses;
5020 AbstractCallSite::getCallbackUses(CS, CBUses);
5021 for (const Use *U : CBUses) {
5022 AbstractCallSite CBACS(U);
5023 assert(CBACS && CBACS.isCallbackCall());
5024 for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
5025 int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
5026
5027 LLVM_DEBUG({
5028 dbgs()
5029 << "[AAPrivatizablePtr] Argument " << *Arg
5030 << "check if can be privatized in the context of its parent ("
5031 << Arg->getParent()->getName()
5032 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5033 "callback ("
5034 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5035 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
Michael Forster676c2962020-01-30 10:20:49 +01005036 << CBACS.getCallArgOperand(CBArg) << " vs "
5037 << CS.getArgOperand(ArgNo) << "\n"
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005038 << "[AAPrivatizablePtr] " << CBArg << " : "
5039 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
5040 });
5041
5042 if (CBArgNo != int(ArgNo))
5043 continue;
5044 const auto &CBArgPrivAA =
5045 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(CBArg));
5046 if (CBArgPrivAA.isValidState()) {
5047 auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
5048 if (!CBArgPrivTy.hasValue())
5049 continue;
5050 if (CBArgPrivTy.getValue() == PrivatizableType)
5051 continue;
5052 }
5053
5054 LLVM_DEBUG({
5055 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5056 << " cannot be privatized in the context of its parent ("
5057 << Arg->getParent()->getName()
5058 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5059 "callback ("
5060 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5061 << ").\n[AAPrivatizablePtr] for which the argument "
5062 "privatization is not compatible.\n";
5063 });
5064 return false;
5065 }
5066 }
5067 return true;
5068 };
5069
5070 // Helper to check if for the given call site the associated argument is
5071 // passed to a direct call where the privatization would be different.
5072 auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
5073 CallBase *DC = cast<CallBase>(ACS.getInstruction());
5074 int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
5075 assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() &&
5076 "Expected a direct call operand for callback call operand");
5077
5078 LLVM_DEBUG({
5079 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5080 << " check if be privatized in the context of its parent ("
5081 << Arg->getParent()->getName()
5082 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5083 "direct call of ("
5084 << DCArgNo << "@" << DC->getCalledFunction()->getName()
5085 << ").\n";
5086 });
5087
5088 Function *DCCallee = DC->getCalledFunction();
5089 if (unsigned(DCArgNo) < DCCallee->arg_size()) {
5090 const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
5091 *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)));
5092 if (DCArgPrivAA.isValidState()) {
5093 auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
5094 if (!DCArgPrivTy.hasValue())
5095 return true;
5096 if (DCArgPrivTy.getValue() == PrivatizableType)
5097 return true;
5098 }
5099 }
5100
5101 LLVM_DEBUG({
5102 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5103 << " cannot be privatized in the context of its parent ("
5104 << Arg->getParent()->getName()
5105 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5106 "direct call of ("
5107 << ACS.getCallSite().getCalledFunction()->getName()
5108 << ").\n[AAPrivatizablePtr] for which the argument "
5109 "privatization is not compatible.\n";
5110 });
5111 return false;
5112 };
5113
5114 // Helper to check if the associated argument is used at the given abstract
5115 // call site in a way that is incompatible with the privatization assumed
5116 // here.
5117 auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
5118 if (ACS.isDirectCall())
5119 return IsCompatiblePrivArgOfCallback(ACS.getCallSite());
5120 if (ACS.isCallbackCall())
5121 return IsCompatiblePrivArgOfDirectCS(ACS);
5122 return false;
5123 };
5124
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005125 bool AllCallSitesKnown;
5126 if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
5127 AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005128 return indicatePessimisticFixpoint();
5129
5130 return ChangeStatus::UNCHANGED;
5131 }
5132
5133 /// Given a type to private \p PrivType, collect the constituates (which are
5134 /// used) in \p ReplacementTypes.
5135 static void
5136 identifyReplacementTypes(Type *PrivType,
5137 SmallVectorImpl<Type *> &ReplacementTypes) {
5138 // TODO: For now we expand the privatization type to the fullest which can
5139 // lead to dead arguments that need to be removed later.
5140 assert(PrivType && "Expected privatizable type!");
5141
5142 // Traverse the type, extract constituate types on the outermost level.
5143 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5144 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
5145 ReplacementTypes.push_back(PrivStructType->getElementType(u));
5146 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5147 ReplacementTypes.append(PrivArrayType->getNumElements(),
5148 PrivArrayType->getElementType());
5149 } else {
5150 ReplacementTypes.push_back(PrivType);
5151 }
5152 }
5153
5154 /// Initialize \p Base according to the type \p PrivType at position \p IP.
5155 /// The values needed are taken from the arguments of \p F starting at
5156 /// position \p ArgNo.
5157 static void createInitialization(Type *PrivType, Value &Base, Function &F,
5158 unsigned ArgNo, Instruction &IP) {
5159 assert(PrivType && "Expected privatizable type!");
5160
5161 IRBuilder<NoFolder> IRB(&IP);
5162 const DataLayout &DL = F.getParent()->getDataLayout();
5163
5164 // Traverse the type, build GEPs and stores.
5165 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5166 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5167 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5168 Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
5169 Value *Ptr = constructPointer(
5170 PointeeTy, &Base, PrivStructLayout->getElementOffset(u), IRB, DL);
5171 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5172 }
5173 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5174 Type *PointeePtrTy = PrivArrayType->getElementType()->getPointerTo();
5175 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeePtrTy);
5176 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5177 Value *Ptr =
5178 constructPointer(PointeePtrTy, &Base, u * PointeeTySize, IRB, DL);
5179 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5180 }
5181 } else {
5182 new StoreInst(F.getArg(ArgNo), &Base, &IP);
5183 }
5184 }
5185
5186 /// Extract values from \p Base according to the type \p PrivType at the
5187 /// call position \p ACS. The values are appended to \p ReplacementValues.
5188 void createReplacementValues(Type *PrivType, AbstractCallSite ACS,
5189 Value *Base,
5190 SmallVectorImpl<Value *> &ReplacementValues) {
5191 assert(Base && "Expected base value!");
5192 assert(PrivType && "Expected privatizable type!");
5193 Instruction *IP = ACS.getInstruction();
5194
5195 IRBuilder<NoFolder> IRB(IP);
5196 const DataLayout &DL = IP->getModule()->getDataLayout();
5197
5198 if (Base->getType()->getPointerElementType() != PrivType)
5199 Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(),
5200 "", ACS.getInstruction());
5201
5202 // TODO: Improve the alignment of the loads.
5203 // Traverse the type, build GEPs and loads.
5204 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5205 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5206 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5207 Type *PointeeTy = PrivStructType->getElementType(u);
5208 Value *Ptr =
5209 constructPointer(PointeeTy->getPointerTo(), Base,
5210 PrivStructLayout->getElementOffset(u), IRB, DL);
5211 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
5212 L->setAlignment(MaybeAlign(1));
5213 ReplacementValues.push_back(L);
5214 }
5215 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5216 Type *PointeeTy = PrivArrayType->getElementType();
5217 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
5218 Type *PointeePtrTy = PointeeTy->getPointerTo();
5219 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5220 Value *Ptr =
5221 constructPointer(PointeePtrTy, Base, u * PointeeTySize, IRB, DL);
5222 LoadInst *L = new LoadInst(PointeePtrTy, Ptr, "", IP);
5223 L->setAlignment(MaybeAlign(1));
5224 ReplacementValues.push_back(L);
5225 }
5226 } else {
5227 LoadInst *L = new LoadInst(PrivType, Base, "", IP);
5228 L->setAlignment(MaybeAlign(1));
5229 ReplacementValues.push_back(L);
5230 }
5231 }
5232
5233 /// See AbstractAttribute::manifest(...)
5234 ChangeStatus manifest(Attributor &A) override {
5235 if (!PrivatizableType.hasValue())
5236 return ChangeStatus::UNCHANGED;
5237 assert(PrivatizableType.getValue() && "Expected privatizable type!");
5238
5239 // Collect all tail calls in the function as we cannot allow new allocas to
5240 // escape into tail recursion.
5241 // TODO: Be smarter about new allocas escaping into tail calls.
5242 SmallVector<CallInst *, 16> TailCalls;
5243 if (!A.checkForAllInstructions(
5244 [&](Instruction &I) {
5245 CallInst &CI = cast<CallInst>(I);
5246 if (CI.isTailCall())
5247 TailCalls.push_back(&CI);
5248 return true;
5249 },
5250 *this, {Instruction::Call}))
5251 return ChangeStatus::UNCHANGED;
5252
5253 Argument *Arg = getAssociatedArgument();
5254
5255 // Callback to repair the associated function. A new alloca is placed at the
5256 // beginning and initialized with the values passed through arguments. The
5257 // new alloca replaces the use of the old pointer argument.
5258 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
5259 [=](const Attributor::ArgumentReplacementInfo &ARI,
5260 Function &ReplacementFn, Function::arg_iterator ArgIt) {
5261 BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
5262 Instruction *IP = &*EntryBB.getFirstInsertionPt();
5263 auto *AI = new AllocaInst(PrivatizableType.getValue(), 0,
5264 Arg->getName() + ".priv", IP);
5265 createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
5266 ArgIt->getArgNo(), *IP);
5267 Arg->replaceAllUsesWith(AI);
5268
5269 for (CallInst *CI : TailCalls)
5270 CI->setTailCall(false);
5271 };
5272
5273 // Callback to repair a call site of the associated function. The elements
5274 // of the privatizable type are loaded prior to the call and passed to the
5275 // new function version.
5276 Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
5277 [=](const Attributor::ArgumentReplacementInfo &ARI,
5278 AbstractCallSite ACS, SmallVectorImpl<Value *> &NewArgOperands) {
5279 createReplacementValues(
5280 PrivatizableType.getValue(), ACS,
5281 ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
5282 NewArgOperands);
5283 };
5284
5285 // Collect the types that will replace the privatizable type in the function
5286 // signature.
5287 SmallVector<Type *, 16> ReplacementTypes;
5288 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5289
5290 // Register a rewrite of the argument.
5291 if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
5292 std::move(FnRepairCB),
5293 std::move(ACSRepairCB)))
5294 return ChangeStatus::CHANGED;
5295 return ChangeStatus::UNCHANGED;
5296 }
5297
5298 /// See AbstractAttribute::trackStatistics()
5299 void trackStatistics() const override {
5300 STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
5301 }
5302};
5303
5304struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
5305 AAPrivatizablePtrFloating(const IRPosition &IRP)
5306 : AAPrivatizablePtrImpl(IRP) {}
5307
5308 /// See AbstractAttribute::initialize(...).
5309 virtual void initialize(Attributor &A) override {
5310 // TODO: We can privatize more than arguments.
5311 indicatePessimisticFixpoint();
5312 }
5313
5314 ChangeStatus updateImpl(Attributor &A) override {
5315 llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
5316 "updateImpl will not be called");
5317 }
5318
5319 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5320 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5321 Value *Obj =
5322 GetUnderlyingObject(&getAssociatedValue(), A.getInfoCache().getDL());
5323 if (!Obj) {
5324 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
5325 return nullptr;
5326 }
5327
5328 if (auto *AI = dyn_cast<AllocaInst>(Obj))
5329 if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
5330 if (CI->isOne())
5331 return Obj->getType()->getPointerElementType();
5332 if (auto *Arg = dyn_cast<Argument>(Obj)) {
5333 auto &PrivArgAA =
5334 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(*Arg));
5335 if (PrivArgAA.isAssumedPrivatizablePtr())
5336 return Obj->getType()->getPointerElementType();
5337 }
5338
5339 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
5340 "alloca nor privatizable argument: "
5341 << *Obj << "!\n");
5342 return nullptr;
5343 }
5344
5345 /// See AbstractAttribute::trackStatistics()
5346 void trackStatistics() const override {
5347 STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
5348 }
5349};
5350
5351struct AAPrivatizablePtrCallSiteArgument final
5352 : public AAPrivatizablePtrFloating {
5353 AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP)
5354 : AAPrivatizablePtrFloating(IRP) {}
5355
5356 /// See AbstractAttribute::initialize(...).
5357 void initialize(Attributor &A) override {
5358 if (getIRPosition().hasAttr(Attribute::ByVal))
5359 indicateOptimisticFixpoint();
5360 }
5361
5362 /// See AbstractAttribute::updateImpl(...).
5363 ChangeStatus updateImpl(Attributor &A) override {
5364 PrivatizableType = identifyPrivatizableType(A);
5365 if (!PrivatizableType.hasValue())
5366 return ChangeStatus::UNCHANGED;
5367 if (!PrivatizableType.getValue())
5368 return indicatePessimisticFixpoint();
5369
5370 const IRPosition &IRP = getIRPosition();
5371 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
5372 if (!NoCaptureAA.isAssumedNoCapture()) {
5373 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
5374 return indicatePessimisticFixpoint();
5375 }
5376
5377 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP);
5378 if (!NoAliasAA.isAssumedNoAlias()) {
5379 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
5380 return indicatePessimisticFixpoint();
5381 }
5382
5383 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, IRP);
5384 if (!MemBehaviorAA.isAssumedReadOnly()) {
5385 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
5386 return indicatePessimisticFixpoint();
5387 }
5388
5389 return ChangeStatus::UNCHANGED;
5390 }
5391
5392 /// See AbstractAttribute::trackStatistics()
5393 void trackStatistics() const override {
5394 STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
5395 }
5396};
5397
5398struct AAPrivatizablePtrCallSiteReturned final
5399 : public AAPrivatizablePtrFloating {
5400 AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP)
5401 : AAPrivatizablePtrFloating(IRP) {}
5402
5403 /// See AbstractAttribute::initialize(...).
5404 void initialize(Attributor &A) override {
5405 // TODO: We can privatize more than arguments.
5406 indicatePessimisticFixpoint();
5407 }
5408
5409 /// See AbstractAttribute::trackStatistics()
5410 void trackStatistics() const override {
5411 STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
5412 }
5413};
5414
5415struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
5416 AAPrivatizablePtrReturned(const IRPosition &IRP)
5417 : AAPrivatizablePtrFloating(IRP) {}
5418
5419 /// See AbstractAttribute::initialize(...).
5420 void initialize(Attributor &A) override {
5421 // TODO: We can privatize more than arguments.
5422 indicatePessimisticFixpoint();
5423 }
5424
5425 /// See AbstractAttribute::trackStatistics()
5426 void trackStatistics() const override {
5427 STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
5428 }
5429};
5430
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005431/// -------------------- Memory Behavior Attributes ----------------------------
5432/// Includes read-none, read-only, and write-only.
5433/// ----------------------------------------------------------------------------
5434struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
5435 AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {}
5436
5437 /// See AbstractAttribute::initialize(...).
5438 void initialize(Attributor &A) override {
5439 intersectAssumedBits(BEST_STATE);
5440 getKnownStateFromValue(getIRPosition(), getState());
5441 IRAttribute::initialize(A);
5442 }
5443
5444 /// Return the memory behavior information encoded in the IR for \p IRP.
5445 static void getKnownStateFromValue(const IRPosition &IRP,
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005446 BitIntegerState &State,
5447 bool IgnoreSubsumingPositions = false) {
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005448 SmallVector<Attribute, 2> Attrs;
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005449 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005450 for (const Attribute &Attr : Attrs) {
5451 switch (Attr.getKindAsEnum()) {
5452 case Attribute::ReadNone:
5453 State.addKnownBits(NO_ACCESSES);
5454 break;
5455 case Attribute::ReadOnly:
5456 State.addKnownBits(NO_WRITES);
5457 break;
5458 case Attribute::WriteOnly:
5459 State.addKnownBits(NO_READS);
5460 break;
5461 default:
5462 llvm_unreachable("Unexpcted attribute!");
5463 }
5464 }
5465
5466 if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
5467 if (!I->mayReadFromMemory())
5468 State.addKnownBits(NO_READS);
5469 if (!I->mayWriteToMemory())
5470 State.addKnownBits(NO_WRITES);
5471 }
5472 }
5473
5474 /// See AbstractAttribute::getDeducedAttributes(...).
5475 void getDeducedAttributes(LLVMContext &Ctx,
5476 SmallVectorImpl<Attribute> &Attrs) const override {
5477 assert(Attrs.size() == 0);
5478 if (isAssumedReadNone())
5479 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
5480 else if (isAssumedReadOnly())
5481 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
5482 else if (isAssumedWriteOnly())
5483 Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
5484 assert(Attrs.size() <= 1);
5485 }
5486
5487 /// See AbstractAttribute::manifest(...).
5488 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfertb2083c52019-10-20 22:46:48 -05005489 const IRPosition &IRP = getIRPosition();
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005490
5491 // Check if we would improve the existing attributes first.
5492 SmallVector<Attribute, 4> DeducedAttrs;
5493 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
5494 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
5495 return IRP.hasAttr(Attr.getKindAsEnum(),
5496 /* IgnoreSubsumingPositions */ true);
5497 }))
5498 return ChangeStatus::UNCHANGED;
5499
5500 // Clear existing attributes.
5501 IRP.removeAttrs(AttrKinds);
5502
5503 // Use the generic manifest method.
5504 return IRAttribute::manifest(A);
5505 }
5506
5507 /// See AbstractState::getAsStr().
5508 const std::string getAsStr() const override {
5509 if (isAssumedReadNone())
5510 return "readnone";
5511 if (isAssumedReadOnly())
5512 return "readonly";
5513 if (isAssumedWriteOnly())
5514 return "writeonly";
5515 return "may-read/write";
5516 }
5517
5518 /// The set of IR attributes AAMemoryBehavior deals with.
5519 static const Attribute::AttrKind AttrKinds[3];
5520};
5521
5522const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
5523 Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
5524
5525/// Memory behavior attribute for a floating value.
5526struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
5527 AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5528
5529 /// See AbstractAttribute::initialize(...).
5530 void initialize(Attributor &A) override {
5531 AAMemoryBehaviorImpl::initialize(A);
5532 // Initialize the use vector with all direct uses of the associated value.
5533 for (const Use &U : getAssociatedValue().uses())
5534 Uses.insert(&U);
5535 }
5536
5537 /// See AbstractAttribute::updateImpl(...).
5538 ChangeStatus updateImpl(Attributor &A) override;
5539
5540 /// See AbstractAttribute::trackStatistics()
5541 void trackStatistics() const override {
5542 if (isAssumedReadNone())
5543 STATS_DECLTRACK_FLOATING_ATTR(readnone)
5544 else if (isAssumedReadOnly())
5545 STATS_DECLTRACK_FLOATING_ATTR(readonly)
5546 else if (isAssumedWriteOnly())
5547 STATS_DECLTRACK_FLOATING_ATTR(writeonly)
5548 }
5549
5550private:
5551 /// Return true if users of \p UserI might access the underlying
5552 /// variable/location described by \p U and should therefore be analyzed.
5553 bool followUsersOfUseIn(Attributor &A, const Use *U,
5554 const Instruction *UserI);
5555
5556 /// Update the state according to the effect of use \p U in \p UserI.
5557 void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI);
5558
5559protected:
5560 /// Container for (transitive) uses of the associated argument.
5561 SetVector<const Use *> Uses;
5562};
5563
5564/// Memory behavior attribute for function argument.
5565struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
5566 AAMemoryBehaviorArgument(const IRPosition &IRP)
5567 : AAMemoryBehaviorFloating(IRP) {}
5568
5569 /// See AbstractAttribute::initialize(...).
5570 void initialize(Attributor &A) override {
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005571 intersectAssumedBits(BEST_STATE);
5572 const IRPosition &IRP = getIRPosition();
5573 // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
5574 // can query it when we use has/getAttr. That would allow us to reuse the
5575 // initialize of the base class here.
5576 bool HasByVal =
5577 IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
5578 getKnownStateFromValue(IRP, getState(),
5579 /* IgnoreSubsumingPositions */ HasByVal);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005580
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005581 // Initialize the use vector with all direct uses of the associated value.
5582 Argument *Arg = getAssociatedArgument();
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005583 if (!Arg || !Arg->getParent()->hasExactDefinition()) {
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005584 indicatePessimisticFixpoint();
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005585 } else {
5586 // Initialize the use vector with all direct uses of the associated value.
5587 for (const Use &U : Arg->uses())
5588 Uses.insert(&U);
5589 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005590 }
5591
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005592 ChangeStatus manifest(Attributor &A) override {
5593 // TODO: From readattrs.ll: "inalloca parameters are always
5594 // considered written"
5595 if (hasAttr({Attribute::InAlloca})) {
5596 removeKnownBits(NO_WRITES);
5597 removeAssumedBits(NO_WRITES);
5598 }
5599 return AAMemoryBehaviorFloating::manifest(A);
5600 }
5601
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005602 /// See AbstractAttribute::trackStatistics()
5603 void trackStatistics() const override {
5604 if (isAssumedReadNone())
5605 STATS_DECLTRACK_ARG_ATTR(readnone)
5606 else if (isAssumedReadOnly())
5607 STATS_DECLTRACK_ARG_ATTR(readonly)
5608 else if (isAssumedWriteOnly())
5609 STATS_DECLTRACK_ARG_ATTR(writeonly)
5610 }
5611};
5612
5613struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
5614 AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP)
5615 : AAMemoryBehaviorArgument(IRP) {}
5616
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005617 /// See AbstractAttribute::initialize(...).
5618 void initialize(Attributor &A) override {
5619 if (Argument *Arg = getAssociatedArgument()) {
5620 if (Arg->hasByValAttr()) {
5621 addKnownBits(NO_WRITES);
5622 removeKnownBits(NO_READS);
5623 removeAssumedBits(NO_READS);
5624 }
5625 } else {
5626 }
5627 AAMemoryBehaviorArgument::initialize(A);
5628 }
5629
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005630 /// See AbstractAttribute::updateImpl(...).
5631 ChangeStatus updateImpl(Attributor &A) override {
5632 // TODO: Once we have call site specific value information we can provide
5633 // call site specific liveness liveness information and then it makes
5634 // sense to specialize attributes for call sites arguments instead of
5635 // redirecting requests to the callee argument.
5636 Argument *Arg = getAssociatedArgument();
5637 const IRPosition &ArgPos = IRPosition::argument(*Arg);
5638 auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos);
5639 return clampStateAndIndicateChange(
5640 getState(),
Johannes Doerfert31784242019-10-29 23:18:49 -05005641 static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState()));
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005642 }
5643
5644 /// See AbstractAttribute::trackStatistics()
5645 void trackStatistics() const override {
5646 if (isAssumedReadNone())
5647 STATS_DECLTRACK_CSARG_ATTR(readnone)
5648 else if (isAssumedReadOnly())
5649 STATS_DECLTRACK_CSARG_ATTR(readonly)
5650 else if (isAssumedWriteOnly())
5651 STATS_DECLTRACK_CSARG_ATTR(writeonly)
5652 }
5653};
5654
5655/// Memory behavior attribute for a call site return position.
5656struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
5657 AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP)
5658 : AAMemoryBehaviorFloating(IRP) {}
5659
5660 /// See AbstractAttribute::manifest(...).
5661 ChangeStatus manifest(Attributor &A) override {
5662 // We do not annotate returned values.
5663 return ChangeStatus::UNCHANGED;
5664 }
5665
5666 /// See AbstractAttribute::trackStatistics()
5667 void trackStatistics() const override {}
5668};
5669
5670/// An AA to represent the memory behavior function attributes.
5671struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
5672 AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5673
5674 /// See AbstractAttribute::updateImpl(Attributor &A).
5675 virtual ChangeStatus updateImpl(Attributor &A) override;
5676
5677 /// See AbstractAttribute::manifest(...).
5678 ChangeStatus manifest(Attributor &A) override {
5679 Function &F = cast<Function>(getAnchorValue());
5680 if (isAssumedReadNone()) {
5681 F.removeFnAttr(Attribute::ArgMemOnly);
5682 F.removeFnAttr(Attribute::InaccessibleMemOnly);
5683 F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
5684 }
5685 return AAMemoryBehaviorImpl::manifest(A);
5686 }
5687
5688 /// See AbstractAttribute::trackStatistics()
5689 void trackStatistics() const override {
5690 if (isAssumedReadNone())
5691 STATS_DECLTRACK_FN_ATTR(readnone)
5692 else if (isAssumedReadOnly())
5693 STATS_DECLTRACK_FN_ATTR(readonly)
5694 else if (isAssumedWriteOnly())
5695 STATS_DECLTRACK_FN_ATTR(writeonly)
5696 }
5697};
5698
5699/// AAMemoryBehavior attribute for call sites.
5700struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
5701 AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5702
5703 /// See AbstractAttribute::initialize(...).
5704 void initialize(Attributor &A) override {
5705 AAMemoryBehaviorImpl::initialize(A);
5706 Function *F = getAssociatedFunction();
5707 if (!F || !F->hasExactDefinition())
5708 indicatePessimisticFixpoint();
5709 }
5710
5711 /// See AbstractAttribute::updateImpl(...).
5712 ChangeStatus updateImpl(Attributor &A) override {
5713 // TODO: Once we have call site specific value information we can provide
5714 // call site specific liveness liveness information and then it makes
5715 // sense to specialize attributes for call sites arguments instead of
5716 // redirecting requests to the callee argument.
5717 Function *F = getAssociatedFunction();
5718 const IRPosition &FnPos = IRPosition::function(*F);
5719 auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos);
5720 return clampStateAndIndicateChange(
Johannes Doerfert1a746452019-10-20 22:28:49 -05005721 getState(),
5722 static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState()));
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005723 }
5724
5725 /// See AbstractAttribute::trackStatistics()
5726 void trackStatistics() const override {
5727 if (isAssumedReadNone())
5728 STATS_DECLTRACK_CS_ATTR(readnone)
5729 else if (isAssumedReadOnly())
5730 STATS_DECLTRACK_CS_ATTR(readonly)
5731 else if (isAssumedWriteOnly())
5732 STATS_DECLTRACK_CS_ATTR(writeonly)
5733 }
5734};
Benjamin Kramerc5d1d562019-10-12 11:01:52 +00005735} // namespace
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005736
5737ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
5738
5739 // The current assumed state used to determine a change.
5740 auto AssumedState = getAssumed();
5741
5742 auto CheckRWInst = [&](Instruction &I) {
5743 // If the instruction has an own memory behavior state, use it to restrict
5744 // the local state. No further analysis is required as the other memory
5745 // state is as optimistic as it gets.
5746 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
5747 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
5748 *this, IRPosition::callsite_function(ICS));
5749 intersectAssumedBits(MemBehaviorAA.getAssumed());
5750 return !isAtFixpoint();
5751 }
5752
5753 // Remove access kind modifiers if necessary.
5754 if (I.mayReadFromMemory())
5755 removeAssumedBits(NO_READS);
5756 if (I.mayWriteToMemory())
5757 removeAssumedBits(NO_WRITES);
5758 return !isAtFixpoint();
5759 };
5760
5761 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
5762 return indicatePessimisticFixpoint();
5763
5764 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
5765 : ChangeStatus::UNCHANGED;
5766}
5767
5768ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
5769
5770 const IRPosition &IRP = getIRPosition();
5771 const IRPosition &FnPos = IRPosition::function_scope(IRP);
5772 AAMemoryBehavior::StateType &S = getState();
5773
5774 // First, check the function scope. We take the known information and we avoid
5775 // work if the assumed information implies the current assumed information for
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005776 // this attribute. This is a valid for all but byval arguments.
5777 Argument *Arg = IRP.getAssociatedArgument();
5778 AAMemoryBehavior::base_t FnMemAssumedState =
5779 AAMemoryBehavior::StateType::getWorstState();
5780 if (!Arg || !Arg->hasByValAttr()) {
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005781 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
5782 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005783 FnMemAssumedState = FnMemAA.getAssumed();
5784 S.addKnownBits(FnMemAA.getKnown());
5785 if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
5786 return ChangeStatus::UNCHANGED;
5787 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005788
5789 // Make sure the value is not captured (except through "return"), if
5790 // it is, any information derived would be irrelevant anyway as we cannot
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005791 // check the potential aliases introduced by the capture. However, no need
5792 // to fall back to anythign less optimistic than the function state.
Johannes Doerfert28880192019-12-31 00:57:00 -06005793 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
5794 *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005795 if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005796 S.intersectAssumedBits(FnMemAssumedState);
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005797 return ChangeStatus::CHANGED;
5798 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005799
5800 // The current assumed state used to determine a change.
5801 auto AssumedState = S.getAssumed();
5802
5803 // Liveness information to exclude dead users.
5804 // TODO: Take the FnPos once we have call site specific liveness information.
5805 const auto &LivenessAA = A.getAAFor<AAIsDead>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005806 *this, IRPosition::function(*IRP.getAssociatedFunction()),
5807 /* TrackDependence */ false);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005808
5809 // Visit and expand uses until all are analyzed or a fixpoint is reached.
5810 for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) {
5811 const Use *U = Uses[i];
5812 Instruction *UserI = cast<Instruction>(U->getUser());
5813 LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI
5814 << " [Dead: " << (LivenessAA.isAssumedDead(UserI))
5815 << "]\n");
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005816 if (LivenessAA.isAssumedDead(UserI)) {
5817 A.recordDependence(LivenessAA, *this, DepClassTy::OPTIONAL);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005818 continue;
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005819 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005820
5821 // Check if the users of UserI should also be visited.
5822 if (followUsersOfUseIn(A, U, UserI))
5823 for (const Use &UserIUse : UserI->uses())
5824 Uses.insert(&UserIUse);
5825
5826 // If UserI might touch memory we analyze the use in detail.
5827 if (UserI->mayReadOrWriteMemory())
5828 analyzeUseIn(A, U, UserI);
5829 }
5830
5831 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
5832 : ChangeStatus::UNCHANGED;
5833}
5834
5835bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U,
5836 const Instruction *UserI) {
5837 // The loaded value is unrelated to the pointer argument, no need to
5838 // follow the users of the load.
5839 if (isa<LoadInst>(UserI))
5840 return false;
5841
5842 // By default we follow all uses assuming UserI might leak information on U,
5843 // we have special handling for call sites operands though.
5844 ImmutableCallSite ICS(UserI);
5845 if (!ICS || !ICS.isArgOperand(U))
5846 return true;
5847
5848 // If the use is a call argument known not to be captured, the users of
5849 // the call do not need to be visited because they have to be unrelated to
5850 // the input. Note that this check is not trivial even though we disallow
5851 // general capturing of the underlying argument. The reason is that the
5852 // call might the argument "through return", which we allow and for which we
5853 // need to check call users.
Johannes Doerfertff6254d2020-01-10 12:32:24 -06005854 if (U->get()->getType()->isPointerTy()) {
5855 unsigned ArgNo = ICS.getArgumentNo(U);
5856 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005857 *this, IRPosition::callsite_argument(ICS, ArgNo),
5858 /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfertff6254d2020-01-10 12:32:24 -06005859 return !ArgNoCaptureAA.isAssumedNoCapture();
5860 }
5861
5862 return true;
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005863}
5864
5865void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U,
5866 const Instruction *UserI) {
5867 assert(UserI->mayReadOrWriteMemory());
5868
5869 switch (UserI->getOpcode()) {
5870 default:
5871 // TODO: Handle all atomics and other side-effect operations we know of.
5872 break;
5873 case Instruction::Load:
5874 // Loads cause the NO_READS property to disappear.
5875 removeAssumedBits(NO_READS);
5876 return;
5877
5878 case Instruction::Store:
5879 // Stores cause the NO_WRITES property to disappear if the use is the
5880 // pointer operand. Note that we do assume that capturing was taken care of
5881 // somewhere else.
5882 if (cast<StoreInst>(UserI)->getPointerOperand() == U->get())
5883 removeAssumedBits(NO_WRITES);
5884 return;
5885
5886 case Instruction::Call:
5887 case Instruction::CallBr:
5888 case Instruction::Invoke: {
5889 // For call sites we look at the argument memory behavior attribute (this
5890 // could be recursive!) in order to restrict our own state.
5891 ImmutableCallSite ICS(UserI);
5892
5893 // Give up on operand bundles.
5894 if (ICS.isBundleOperand(U)) {
5895 indicatePessimisticFixpoint();
5896 return;
5897 }
5898
5899 // Calling a function does read the function pointer, maybe write it if the
5900 // function is self-modifying.
5901 if (ICS.isCallee(U)) {
5902 removeAssumedBits(NO_READS);
5903 break;
5904 }
5905
5906 // Adjust the possible access behavior based on the information on the
5907 // argument.
Johannes Doerfertff6254d2020-01-10 12:32:24 -06005908 IRPosition Pos;
5909 if (U->get()->getType()->isPointerTy())
5910 Pos = IRPosition::callsite_argument(ICS, ICS.getArgumentNo(U));
5911 else
5912 Pos = IRPosition::callsite_function(ICS);
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005913 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
5914 *this, Pos,
5915 /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005916 // "assumed" has at most the same bits as the MemBehaviorAA assumed
5917 // and at least "known".
5918 intersectAssumedBits(MemBehaviorAA.getAssumed());
5919 return;
5920 }
5921 };
5922
5923 // Generally, look at the "may-properties" and adjust the assumed state if we
5924 // did not trigger special handling before.
5925 if (UserI->mayReadFromMemory())
5926 removeAssumedBits(NO_READS);
5927 if (UserI->mayWriteToMemory())
5928 removeAssumedBits(NO_WRITES);
5929}
Hideto Ueno188f9a32020-01-15 15:25:52 +09005930/// ------------------ Value Constant Range Attribute -------------------------
Hideto Uenoe9963032020-01-01 15:25:19 +09005931
Hideto Ueno188f9a32020-01-15 15:25:52 +09005932struct AAValueConstantRangeImpl : AAValueConstantRange {
5933 using StateType = IntegerRangeState;
5934 AAValueConstantRangeImpl(const IRPosition &IRP) : AAValueConstantRange(IRP) {}
5935
5936 /// See AbstractAttribute::getAsStr().
5937 const std::string getAsStr() const override {
5938 std::string Str;
5939 llvm::raw_string_ostream OS(Str);
5940 OS << "range(" << getBitWidth() << ")<";
5941 getKnown().print(OS);
5942 OS << " / ";
5943 getAssumed().print(OS);
5944 OS << ">";
5945 return OS.str();
5946 }
5947
5948 /// Helper function to get a SCEV expr for the associated value at program
5949 /// point \p I.
5950 const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
5951 if (!getAnchorScope())
5952 return nullptr;
5953
5954 ScalarEvolution *SE =
5955 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
5956 *getAnchorScope());
5957
5958 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
5959 *getAnchorScope());
5960
5961 if (!SE || !LI)
5962 return nullptr;
5963
5964 const SCEV *S = SE->getSCEV(&getAssociatedValue());
5965 if (!I)
5966 return S;
5967
5968 return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
5969 }
5970
5971 /// Helper function to get a range from SCEV for the associated value at
5972 /// program point \p I.
5973 ConstantRange getConstantRangeFromSCEV(Attributor &A,
5974 const Instruction *I = nullptr) const {
5975 if (!getAnchorScope())
5976 return getWorstState(getBitWidth());
5977
5978 ScalarEvolution *SE =
5979 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
5980 *getAnchorScope());
5981
5982 const SCEV *S = getSCEV(A, I);
5983 if (!SE || !S)
5984 return getWorstState(getBitWidth());
5985
5986 return SE->getUnsignedRange(S);
5987 }
5988
5989 /// Helper function to get a range from LVI for the associated value at
5990 /// program point \p I.
5991 ConstantRange
5992 getConstantRangeFromLVI(Attributor &A,
5993 const Instruction *CtxI = nullptr) const {
5994 if (!getAnchorScope())
5995 return getWorstState(getBitWidth());
5996
5997 LazyValueInfo *LVI =
5998 A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
5999 *getAnchorScope());
6000
6001 if (!LVI || !CtxI)
6002 return getWorstState(getBitWidth());
6003 return LVI->getConstantRange(&getAssociatedValue(),
6004 const_cast<BasicBlock *>(CtxI->getParent()),
6005 const_cast<Instruction *>(CtxI));
6006 }
6007
6008 /// See AAValueConstantRange::getKnownConstantRange(..).
6009 ConstantRange
6010 getKnownConstantRange(Attributor &A,
6011 const Instruction *CtxI = nullptr) const override {
6012 if (!CtxI || CtxI == getCtxI())
6013 return getKnown();
6014
6015 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6016 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6017 return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
6018 }
6019
6020 /// See AAValueConstantRange::getAssumedConstantRange(..).
6021 ConstantRange
6022 getAssumedConstantRange(Attributor &A,
6023 const Instruction *CtxI = nullptr) const override {
6024 // TODO: Make SCEV use Attributor assumption.
6025 // We may be able to bound a variable range via assumptions in
6026 // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
6027 // evolve to x^2 + x, then we can say that y is in [2, 12].
6028
6029 if (!CtxI || CtxI == getCtxI())
6030 return getAssumed();
6031
6032 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6033 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6034 return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
6035 }
6036
6037 /// See AbstractAttribute::initialize(..).
6038 void initialize(Attributor &A) override {
6039 // Intersect a range given by SCEV.
6040 intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
6041
6042 // Intersect a range given by LVI.
6043 intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
6044 }
6045
6046 /// Helper function to create MDNode for range metadata.
6047 static MDNode *
6048 getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
6049 const ConstantRange &AssumedConstantRange) {
6050 Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
6051 Ty, AssumedConstantRange.getLower())),
6052 ConstantAsMetadata::get(ConstantInt::get(
6053 Ty, AssumedConstantRange.getUpper()))};
6054 return MDNode::get(Ctx, LowAndHigh);
6055 }
6056
6057 /// Return true if \p Assumed is included in \p KnownRanges.
6058 static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
6059
6060 if (Assumed.isFullSet())
6061 return false;
6062
6063 if (!KnownRanges)
6064 return true;
6065
6066 // If multiple ranges are annotated in IR, we give up to annotate assumed
6067 // range for now.
6068
6069 // TODO: If there exists a known range which containts assumed range, we
6070 // can say assumed range is better.
6071 if (KnownRanges->getNumOperands() > 2)
6072 return false;
6073
6074 ConstantInt *Lower =
6075 mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
6076 ConstantInt *Upper =
6077 mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
6078
6079 ConstantRange Known(Lower->getValue(), Upper->getValue());
6080 return Known.contains(Assumed) && Known != Assumed;
6081 }
6082
6083 /// Helper function to set range metadata.
6084 static bool
6085 setRangeMetadataIfisBetterRange(Instruction *I,
6086 const ConstantRange &AssumedConstantRange) {
6087 auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
6088 if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
6089 if (!AssumedConstantRange.isEmptySet()) {
6090 I->setMetadata(LLVMContext::MD_range,
6091 getMDNodeForConstantRange(I->getType(), I->getContext(),
6092 AssumedConstantRange));
6093 return true;
6094 }
6095 }
6096 return false;
6097 }
6098
6099 /// See AbstractAttribute::manifest()
6100 ChangeStatus manifest(Attributor &A) override {
6101 ChangeStatus Changed = ChangeStatus::UNCHANGED;
6102 ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
6103 assert(!AssumedConstantRange.isFullSet() && "Invalid state");
6104
6105 auto &V = getAssociatedValue();
6106 if (!AssumedConstantRange.isEmptySet() &&
6107 !AssumedConstantRange.isSingleElement()) {
6108 if (Instruction *I = dyn_cast<Instruction>(&V))
6109 if (isa<CallInst>(I) || isa<LoadInst>(I))
6110 if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
6111 Changed = ChangeStatus::CHANGED;
6112 }
6113
6114 return Changed;
6115 }
6116};
6117
Johannes Doerfertea5fabe2020-01-28 09:46:15 -06006118struct AAValueConstantRangeArgument final
6119 : AAArgumentFromCallSiteArguments<
6120 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState> {
Hideto Ueno188f9a32020-01-15 15:25:52 +09006121 AAValueConstantRangeArgument(const IRPosition &IRP)
Johannes Doerfertea5fabe2020-01-28 09:46:15 -06006122 : AAArgumentFromCallSiteArguments<
6123 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState>(
6124 IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006125
6126 /// See AbstractAttribute::trackStatistics()
6127 void trackStatistics() const override {
6128 STATS_DECLTRACK_ARG_ATTR(value_range)
6129 }
6130};
6131
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006132struct AAValueConstantRangeReturned
6133 : AAReturnedFromReturnedValues<AAValueConstantRange,
6134 AAValueConstantRangeImpl> {
6135 using Base = AAReturnedFromReturnedValues<AAValueConstantRange,
6136 AAValueConstantRangeImpl>;
6137 AAValueConstantRangeReturned(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006138
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006139 /// See AbstractAttribute::initialize(...).
6140 void initialize(Attributor &A) override {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006141
6142 /// See AbstractAttribute::trackStatistics()
6143 void trackStatistics() const override {
6144 STATS_DECLTRACK_FNRET_ATTR(value_range)
6145 }
6146};
6147
6148struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
6149 AAValueConstantRangeFloating(const IRPosition &IRP)
6150 : AAValueConstantRangeImpl(IRP) {}
6151
6152 /// See AbstractAttribute::initialize(...).
6153 void initialize(Attributor &A) override {
Johannes Doerfert028db8c42020-02-09 19:05:15 -06006154 AAValueConstantRangeImpl::initialize(A);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006155 Value &V = getAssociatedValue();
6156
6157 if (auto *C = dyn_cast<ConstantInt>(&V)) {
6158 unionAssumed(ConstantRange(C->getValue()));
6159 indicateOptimisticFixpoint();
6160 return;
6161 }
6162
6163 if (isa<UndefValue>(&V)) {
6164 indicateOptimisticFixpoint();
6165 return;
6166 }
6167
6168 if (auto *I = dyn_cast<Instruction>(&V))
6169 if (isa<BinaryOperator>(I) || isa<CmpInst>(I)) {
6170 Value *LHS = I->getOperand(0);
6171 Value *RHS = I->getOperand(1);
6172
6173 if (LHS->getType()->isIntegerTy() && RHS->getType()->isIntegerTy())
6174 return;
6175 }
6176
6177 // If it is a load instruction with range metadata, use it.
6178 if (LoadInst *LI = dyn_cast<LoadInst>(&V))
6179 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
6180 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
6181 return;
6182 }
6183
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06006184 // We handle casts in the updateImpl.
6185 // TODO: Allow non integers as well.
6186 if (CastInst *CI = dyn_cast<CastInst>(&V))
6187 if (CI->getOperand(0)->getType()->isIntegerTy())
6188 return;
6189
Johannes Doerfert7e7e6592020-02-09 19:08:04 -06006190 // We can work with select instruction as we traverse their operands
6191 // during update.
6192 if (isa<SelectInst>(V))
6193 return;
6194
Hideto Ueno188f9a32020-01-15 15:25:52 +09006195 // Otherwise we give up.
6196 indicatePessimisticFixpoint();
6197
Johannes Doerfert02bd8182020-01-28 11:49:35 -06006198 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
6199 << getAssociatedValue() << "\n");
Hideto Ueno188f9a32020-01-15 15:25:52 +09006200 }
6201
6202 bool calculateBinaryOperator(Attributor &A, BinaryOperator *BinOp,
6203 IntegerRangeState &T, Instruction *CtxI) {
6204 Value *LHS = BinOp->getOperand(0);
6205 Value *RHS = BinOp->getOperand(1);
6206
6207 auto &LHSAA =
6208 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
6209 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
6210
6211 auto &RHSAA =
6212 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
6213 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
6214
6215 auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
6216
6217 T.unionAssumed(AssumedRange);
6218
6219 // TODO: Track a known state too.
6220
6221 return T.isValidState();
6222 }
6223
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06006224 bool calculateCastInst(Attributor &A, CastInst *CastI, IntegerRangeState &T,
6225 Instruction *CtxI) {
6226 assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
6227 // TODO: Allow non integers as well.
6228 Value &OpV = *CastI->getOperand(0);
6229 assert(OpV.getType()->isIntegerTy() && "Expected integer cast");
6230
6231 auto &OpAA =
6232 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(OpV));
6233 T.unionAssumed(
6234 OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
6235 return T.isValidState();
6236 }
6237
Hideto Ueno188f9a32020-01-15 15:25:52 +09006238 bool calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
6239 Instruction *CtxI) {
6240 Value *LHS = CmpI->getOperand(0);
6241 Value *RHS = CmpI->getOperand(1);
6242
6243 auto &LHSAA =
6244 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
6245 auto &RHSAA =
6246 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
6247
6248 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
6249 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
6250
6251 // If one of them is empty set, we can't decide.
6252 if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
6253 return true;
6254
6255 bool MustTrue = false, MustFalse = false;
6256
6257 auto AllowedRegion =
6258 ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
6259
6260 auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion(
6261 CmpI->getPredicate(), RHSAARange);
6262
6263 if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
6264 MustFalse = true;
6265
6266 if (SatisfyingRegion.contains(LHSAARange))
6267 MustTrue = true;
6268
6269 assert((!MustTrue || !MustFalse) &&
6270 "Either MustTrue or MustFalse should be false!");
6271
6272 if (MustTrue)
6273 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
6274 else if (MustFalse)
6275 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
6276 else
6277 T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
6278
6279 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
6280 << " " << RHSAA << "\n");
6281
6282 // TODO: Track a known state too.
6283 return T.isValidState();
6284 }
6285
6286 /// See AbstractAttribute::updateImpl(...).
6287 ChangeStatus updateImpl(Attributor &A) override {
6288 Instruction *CtxI = getCtxI();
6289 auto VisitValueCB = [&](Value &V, IntegerRangeState &T,
6290 bool Stripped) -> bool {
6291 Instruction *I = dyn_cast<Instruction>(&V);
6292 if (!I) {
6293
6294 // If the value is not instruction, we query AA to Attributor.
6295 const auto &AA =
6296 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(V));
6297
6298 // Clamp operator is not used to utilize a program point CtxI.
6299 T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
6300
6301 return T.isValidState();
6302 }
6303
6304 if (auto *BinOp = dyn_cast<BinaryOperator>(I))
6305 return calculateBinaryOperator(A, BinOp, T, CtxI);
6306 else if (auto *CmpI = dyn_cast<CmpInst>(I))
6307 return calculateCmpInst(A, CmpI, T, CtxI);
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06006308 else if (auto *CastI = dyn_cast<CastInst>(I))
6309 return calculateCastInst(A, CastI, T, CtxI);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006310 else {
6311 // Give up with other instructions.
6312 // TODO: Add other instructions
6313
6314 T.indicatePessimisticFixpoint();
6315 return false;
6316 }
6317 };
6318
6319 IntegerRangeState T(getBitWidth());
6320
6321 if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>(
6322 A, getIRPosition(), *this, T, VisitValueCB))
6323 return indicatePessimisticFixpoint();
6324
6325 return clampStateAndIndicateChange(getState(), T);
6326 }
6327
6328 /// See AbstractAttribute::trackStatistics()
6329 void trackStatistics() const override {
6330 STATS_DECLTRACK_FLOATING_ATTR(value_range)
6331 }
6332};
6333
6334struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
6335 AAValueConstantRangeFunction(const IRPosition &IRP)
6336 : AAValueConstantRangeImpl(IRP) {}
6337
6338 /// See AbstractAttribute::initialize(...).
6339 ChangeStatus updateImpl(Attributor &A) override {
6340 llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
6341 "not be called");
6342 }
6343
6344 /// See AbstractAttribute::trackStatistics()
6345 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
6346};
6347
6348struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
6349 AAValueConstantRangeCallSite(const IRPosition &IRP)
6350 : AAValueConstantRangeFunction(IRP) {}
6351
6352 /// See AbstractAttribute::trackStatistics()
6353 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
6354};
6355
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006356struct AAValueConstantRangeCallSiteReturned
6357 : AACallSiteReturnedFromReturned<AAValueConstantRange,
6358 AAValueConstantRangeImpl> {
Hideto Ueno188f9a32020-01-15 15:25:52 +09006359 AAValueConstantRangeCallSiteReturned(const IRPosition &IRP)
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006360 : AACallSiteReturnedFromReturned<AAValueConstantRange,
6361 AAValueConstantRangeImpl>(IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006362
6363 /// See AbstractAttribute::initialize(...).
6364 void initialize(Attributor &A) override {
6365 // If it is a load instruction with range metadata, use the metadata.
6366 if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
6367 if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
6368 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
6369
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006370 AAValueConstantRangeImpl::initialize(A);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006371 }
6372
6373 /// See AbstractAttribute::trackStatistics()
6374 void trackStatistics() const override {
6375 STATS_DECLTRACK_CSRET_ATTR(value_range)
6376 }
6377};
6378struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
6379 AAValueConstantRangeCallSiteArgument(const IRPosition &IRP)
6380 : AAValueConstantRangeFloating(IRP) {}
6381
6382 /// See AbstractAttribute::trackStatistics()
6383 void trackStatistics() const override {
6384 STATS_DECLTRACK_CSARG_ATTR(value_range)
6385 }
6386};
Johannes Doerfertaade7822019-06-05 03:02:24 +00006387/// ----------------------------------------------------------------------------
6388/// Attributor
6389/// ----------------------------------------------------------------------------
6390
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00006391bool Attributor::isAssumedDead(const AbstractAttribute &AA,
6392 const AAIsDead *LivenessAA) {
6393 const Instruction *CtxI = AA.getIRPosition().getCtxI();
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006394 if (!CtxI || !Functions.count(const_cast<Function *>(CtxI->getFunction())))
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00006395 return false;
6396
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006397 // TODO: Find a good way to utilize fine and coarse grained liveness
6398 // information.
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00006399 if (!LivenessAA)
6400 LivenessAA =
Johannes Doerfert19b00432019-08-26 17:48:05 +00006401 &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()),
6402 /* TrackDependence */ false);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00006403
6404 // Don't check liveness for AAIsDead.
6405 if (&AA == LivenessAA)
6406 return false;
6407
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00006408 if (!LivenessAA->isAssumedDead(CtxI))
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00006409 return false;
6410
Johannes Doerfert19b00432019-08-26 17:48:05 +00006411 // We actually used liveness information so we have to record a dependence.
Johannes Doerfert680f6382019-11-02 02:48:05 -05006412 recordDependence(*LivenessAA, AA, DepClassTy::OPTIONAL);
Johannes Doerfert19b00432019-08-26 17:48:05 +00006413
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00006414 return true;
6415}
6416
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006417bool Attributor::checkForAllUses(
6418 const function_ref<bool(const Use &, bool &)> &Pred,
6419 const AbstractAttribute &QueryingAA, const Value &V) {
6420 const IRPosition &IRP = QueryingAA.getIRPosition();
6421 SmallVector<const Use *, 16> Worklist;
6422 SmallPtrSet<const Use *, 16> Visited;
6423
6424 for (const Use &U : V.uses())
6425 Worklist.push_back(&U);
6426
6427 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
6428 << " initial uses to check\n");
6429
6430 if (Worklist.empty())
6431 return true;
6432
6433 bool AnyDead = false;
6434 const Function *ScopeFn = IRP.getAnchorScope();
6435 const auto *LivenessAA =
6436 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
6437 /* TrackDependence */ false)
6438 : nullptr;
6439
6440 while (!Worklist.empty()) {
6441 const Use *U = Worklist.pop_back_val();
6442 if (!Visited.insert(U).second)
6443 continue;
6444 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << "\n");
6445 if (Instruction *UserI = dyn_cast<Instruction>(U->getUser()))
6446 if (LivenessAA && LivenessAA->isAssumedDead(UserI)) {
6447 LLVM_DEBUG(dbgs() << "[Attributor] Dead user: " << *UserI << ": "
Johannes Doerfert3d347e22019-12-13 23:35:45 -06006448 << *LivenessAA << "\n");
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006449 AnyDead = true;
6450 continue;
6451 }
6452
6453 bool Follow = false;
6454 if (!Pred(*U, Follow))
6455 return false;
6456 if (!Follow)
6457 continue;
6458 for (const Use &UU : U->getUser()->uses())
6459 Worklist.push_back(&UU);
6460 }
6461
6462 if (AnyDead)
Johannes Doerfert680f6382019-11-02 02:48:05 -05006463 recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006464
6465 return true;
6466}
6467
Johannes Doerfert661db042019-10-07 23:14:58 +00006468bool Attributor::checkForAllCallSites(
6469 const function_ref<bool(AbstractCallSite)> &Pred,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006470 const AbstractAttribute &QueryingAA, bool RequireAllCallSites,
6471 bool &AllCallSitesKnown) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00006472 // We can try to determine information from
6473 // the call sites. However, this is only possible all call sites are known,
6474 // hence the function has internal linkage.
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006475 const IRPosition &IRP = QueryingAA.getIRPosition();
6476 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfert748538e2019-10-07 23:30:04 +00006477 if (!AssociatedFunction) {
6478 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
6479 << "\n");
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006480 AllCallSitesKnown = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006481 return false;
Johannes Doerfert748538e2019-10-07 23:30:04 +00006482 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006483
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006484 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006485 &QueryingAA, AllCallSitesKnown);
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006486}
6487
6488bool Attributor::checkForAllCallSites(
6489 const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006490 bool RequireAllCallSites, const AbstractAttribute *QueryingAA,
6491 bool &AllCallSitesKnown) {
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006492 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00006493 LLVM_DEBUG(
6494 dbgs()
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006495 << "[Attributor] Function " << Fn.getName()
Hideto Ueno54869ec2019-07-15 06:49:04 +00006496 << " has no internal linkage, hence not all call sites are known\n");
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006497 AllCallSitesKnown = false;
Hideto Ueno54869ec2019-07-15 06:49:04 +00006498 return false;
6499 }
6500
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006501 // If we do not require all call sites we might not see all.
6502 AllCallSitesKnown = RequireAllCallSites;
6503
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006504 for (const Use &U : Fn.uses()) {
Johannes Doerfert661db042019-10-07 23:14:58 +00006505 AbstractCallSite ACS(&U);
6506 if (!ACS) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01006507 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
Johannes Doerfert661db042019-10-07 23:14:58 +00006508 << " has non call site use " << *U.get() << " in "
6509 << *U.getUser() << "\n");
Johannes Doerfert2d77b0c2019-11-01 22:35:18 -05006510 // BlockAddress users are allowed.
6511 if (isa<BlockAddress>(U.getUser()))
6512 continue;
Johannes Doerfertd98f9752019-08-21 21:48:56 +00006513 return false;
Johannes Doerfert661db042019-10-07 23:14:58 +00006514 }
Johannes Doerfertd98f9752019-08-21 21:48:56 +00006515
Johannes Doerfert661db042019-10-07 23:14:58 +00006516 Instruction *I = ACS.getInstruction();
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006517 Function *Caller = I->getFunction();
Stefan Stipanovicd0216172019-08-02 21:31:22 +00006518
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006519 const auto *LivenessAA =
6520 lookupAAFor<AAIsDead>(IRPosition::function(*Caller), QueryingAA,
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01006521 /* TrackDependence */ false);
Stefan Stipanovicd0216172019-08-02 21:31:22 +00006522
6523 // Skip dead calls.
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006524 if (LivenessAA && LivenessAA->isAssumedDead(I)) {
Johannes Doerfert19b00432019-08-26 17:48:05 +00006525 // We actually used liveness information so we have to record a
6526 // dependence.
Johannes Doerfert3753aa72019-10-13 04:16:02 +00006527 if (QueryingAA)
Johannes Doerfert680f6382019-11-02 02:48:05 -05006528 recordDependence(*LivenessAA, *QueryingAA, DepClassTy::OPTIONAL);
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006529 AllCallSitesKnown = false;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00006530 continue;
Johannes Doerfert19b00432019-08-26 17:48:05 +00006531 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00006532
Johannes Doerfert661db042019-10-07 23:14:58 +00006533 const Use *EffectiveUse =
6534 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
6535 if (!ACS.isCallee(EffectiveUse)) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00006536 if (!RequireAllCallSites)
6537 continue;
Johannes Doerfert661db042019-10-07 23:14:58 +00006538 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01006539 << " is an invalid use of " << Fn.getName() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00006540 return false;
6541 }
6542
Johannes Doerfert661db042019-10-07 23:14:58 +00006543 if (Pred(ACS))
Hideto Ueno54869ec2019-07-15 06:49:04 +00006544 continue;
6545
Johannes Doerfert5304b722019-08-14 22:04:28 +00006546 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
Johannes Doerfert661db042019-10-07 23:14:58 +00006547 << *ACS.getInstruction() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00006548 return false;
6549 }
6550
6551 return true;
6552}
6553
Johannes Doerfert14a04932019-08-07 22:27:24 +00006554bool Attributor::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00006555 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00006556 &Pred,
6557 const AbstractAttribute &QueryingAA) {
6558
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006559 const IRPosition &IRP = QueryingAA.getIRPosition();
6560 // Since we need to provide return instructions we have to have an exact
6561 // definition.
6562 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00006563 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00006564 return false;
6565
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006566 // If this is a call site query we use the call site specific return values
6567 // and liveness information.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00006568 // TODO: use the function scope once we have call site AAReturnedValues.
6569 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006570 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00006571 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006572 return false;
6573
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00006574 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
Johannes Doerfert14a04932019-08-07 22:27:24 +00006575}
6576
6577bool Attributor::checkForAllReturnedValues(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006578 const function_ref<bool(Value &)> &Pred,
Johannes Doerfert14a04932019-08-07 22:27:24 +00006579 const AbstractAttribute &QueryingAA) {
6580
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006581 const IRPosition &IRP = QueryingAA.getIRPosition();
6582 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00006583 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00006584 return false;
6585
Johannes Doerfert07a5c122019-08-28 14:09:14 +00006586 // TODO: use the function scope once we have call site AAReturnedValues.
6587 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006588 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00006589 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006590 return false;
6591
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00006592 return AARetVal.checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00006593 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00006594 return Pred(RV);
6595 });
Johannes Doerfert14a04932019-08-07 22:27:24 +00006596}
6597
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00006598static bool
6599checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap,
6600 const function_ref<bool(Instruction &)> &Pred,
6601 const AAIsDead *LivenessAA, bool &AnyDead,
6602 const ArrayRef<unsigned> &Opcodes) {
6603 for (unsigned Opcode : Opcodes) {
6604 for (Instruction *I : OpcodeInstMap[Opcode]) {
6605 // Skip dead instructions.
6606 if (LivenessAA && LivenessAA->isAssumedDead(I)) {
6607 AnyDead = true;
6608 continue;
6609 }
6610
6611 if (!Pred(*I))
6612 return false;
6613 }
6614 }
6615 return true;
6616}
6617
Johannes Doerfertd0f64002019-08-06 00:32:43 +00006618bool Attributor::checkForAllInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006619 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00006620 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00006621
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006622 const IRPosition &IRP = QueryingAA.getIRPosition();
6623 // Since we need to provide instructions we have to have an exact definition.
6624 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00006625 if (!AssociatedFunction)
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006626 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00006627
Johannes Doerfert07a5c122019-08-28 14:09:14 +00006628 // TODO: use the function scope once we have call site AAReturnedValues.
6629 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert19b00432019-08-26 17:48:05 +00006630 const auto &LivenessAA =
6631 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
6632 bool AnyDead = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006633
6634 auto &OpcodeInstMap =
6635 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006636 if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead,
6637 Opcodes))
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00006638 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00006639
Johannes Doerfert19b00432019-08-26 17:48:05 +00006640 // If we actually used liveness information so we have to record a dependence.
6641 if (AnyDead)
Johannes Doerfert680f6382019-11-02 02:48:05 -05006642 recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
Johannes Doerfert19b00432019-08-26 17:48:05 +00006643
Johannes Doerfertd0f64002019-08-06 00:32:43 +00006644 return true;
6645}
6646
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00006647bool Attributor::checkForAllReadWriteInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006648 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00006649 AbstractAttribute &QueryingAA) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00006650
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006651 const Function *AssociatedFunction =
6652 QueryingAA.getIRPosition().getAssociatedFunction();
6653 if (!AssociatedFunction)
6654 return false;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00006655
Johannes Doerfert07a5c122019-08-28 14:09:14 +00006656 // TODO: use the function scope once we have call site AAReturnedValues.
6657 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
6658 const auto &LivenessAA =
6659 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
Johannes Doerfert19b00432019-08-26 17:48:05 +00006660 bool AnyDead = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00006661
6662 for (Instruction *I :
6663 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00006664 // Skip dead instructions.
Johannes Doerfert19b00432019-08-26 17:48:05 +00006665 if (LivenessAA.isAssumedDead(I)) {
6666 AnyDead = true;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00006667 continue;
Johannes Doerfert19b00432019-08-26 17:48:05 +00006668 }
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00006669
6670 if (!Pred(*I))
6671 return false;
6672 }
6673
Johannes Doerfert19b00432019-08-26 17:48:05 +00006674 // If we actually used liveness information so we have to record a dependence.
6675 if (AnyDead)
Johannes Doerfert680f6382019-11-02 02:48:05 -05006676 recordDependence(LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
Johannes Doerfert19b00432019-08-26 17:48:05 +00006677
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00006678 return true;
6679}
6680
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006681ChangeStatus Attributor::run() {
Johannes Doerfertaade7822019-06-05 03:02:24 +00006682 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
6683 << AllAbstractAttributes.size()
6684 << " abstract attributes.\n");
6685
Stefan Stipanovic53605892019-06-27 11:27:54 +00006686 // Now that all abstract attributes are collected and initialized we start
6687 // the abstract analysis.
Johannes Doerfertaade7822019-06-05 03:02:24 +00006688
6689 unsigned IterationCounter = 1;
6690
6691 SmallVector<AbstractAttribute *, 64> ChangedAAs;
Johannes Doerfert680f6382019-11-02 02:48:05 -05006692 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
Johannes Doerfertaade7822019-06-05 03:02:24 +00006693 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
6694
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00006695 bool RecomputeDependences = false;
6696
Johannes Doerfertaade7822019-06-05 03:02:24 +00006697 do {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00006698 // Remember the size to determine new attributes.
6699 size_t NumAAs = AllAbstractAttributes.size();
Johannes Doerfertaade7822019-06-05 03:02:24 +00006700 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
6701 << ", Worklist size: " << Worklist.size() << "\n");
6702
Johannes Doerfert680f6382019-11-02 02:48:05 -05006703 // For invalid AAs we can fix dependent AAs that have a required dependence,
6704 // thereby folding long dependence chains in a single step without the need
6705 // to run updates.
6706 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
6707 AbstractAttribute *InvalidAA = InvalidAAs[u];
6708 auto &QuerriedAAs = QueryMap[InvalidAA];
6709 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "
6710 << QuerriedAAs.RequiredAAs.size() << "/"
6711 << QuerriedAAs.OptionalAAs.size()
6712 << " required/optional dependences\n");
6713 for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) {
6714 AbstractState &DOIAAState = DepOnInvalidAA->getState();
6715 DOIAAState.indicatePessimisticFixpoint();
6716 ++NumAttributesFixedDueToRequiredDependences;
6717 assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!");
6718 if (!DOIAAState.isValidState())
6719 InvalidAAs.insert(DepOnInvalidAA);
Johannes Doerfert22408542020-01-28 09:38:07 -06006720 else
6721 ChangedAAs.push_back(DepOnInvalidAA);
Johannes Doerfert680f6382019-11-02 02:48:05 -05006722 }
6723 if (!RecomputeDependences)
6724 Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
6725 QuerriedAAs.OptionalAAs.end());
6726 }
6727
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00006728 // If dependences (=QueryMap) are recomputed we have to look at all abstract
6729 // attributes again, regardless of what changed in the last iteration.
6730 if (RecomputeDependences) {
6731 LLVM_DEBUG(
6732 dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
6733 QueryMap.clear();
6734 ChangedAAs.clear();
6735 Worklist.insert(AllAbstractAttributes.begin(),
6736 AllAbstractAttributes.end());
6737 }
6738
Johannes Doerfertaade7822019-06-05 03:02:24 +00006739 // Add all abstract attributes that are potentially dependent on one that
6740 // changed to the work list.
6741 for (AbstractAttribute *ChangedAA : ChangedAAs) {
6742 auto &QuerriedAAs = QueryMap[ChangedAA];
Johannes Doerfert680f6382019-11-02 02:48:05 -05006743 Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
6744 QuerriedAAs.OptionalAAs.end());
6745 Worklist.insert(QuerriedAAs.RequiredAAs.begin(),
6746 QuerriedAAs.RequiredAAs.end());
Johannes Doerfertaade7822019-06-05 03:02:24 +00006747 }
6748
Johannes Doerfertb504eb82019-08-26 18:55:47 +00006749 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
6750 << ", Worklist+Dependent size: " << Worklist.size()
6751 << "\n");
6752
Johannes Doerfert680f6382019-11-02 02:48:05 -05006753 // Reset the changed and invalid set.
Johannes Doerfertaade7822019-06-05 03:02:24 +00006754 ChangedAAs.clear();
Johannes Doerfert680f6382019-11-02 02:48:05 -05006755 InvalidAAs.clear();
Johannes Doerfertaade7822019-06-05 03:02:24 +00006756
6757 // Update all abstract attribute in the work list and record the ones that
6758 // changed.
6759 for (AbstractAttribute *AA : Worklist)
Johannes Doerfert2dad7292019-10-13 21:10:31 -05006760 if (!AA->getState().isAtFixpoint() && !isAssumedDead(*AA, nullptr)) {
6761 QueriedNonFixAA = false;
6762 if (AA->update(*this) == ChangeStatus::CHANGED) {
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00006763 ChangedAAs.push_back(AA);
Johannes Doerfert680f6382019-11-02 02:48:05 -05006764 if (!AA->getState().isValidState())
6765 InvalidAAs.insert(AA);
Johannes Doerfert2dad7292019-10-13 21:10:31 -05006766 } else if (!QueriedNonFixAA) {
6767 // If the attribute did not query any non-fix information, the state
6768 // will not change and we can indicate that right away.
6769 AA->getState().indicateOptimisticFixpoint();
6770 }
6771 }
Johannes Doerfertaade7822019-06-05 03:02:24 +00006772
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00006773 // Check if we recompute the dependences in the next iteration.
6774 RecomputeDependences = (DepRecomputeInterval > 0 &&
6775 IterationCounter % DepRecomputeInterval == 0);
6776
Johannes Doerfert9543f142019-08-23 15:24:57 +00006777 // Add attributes to the changed set if they have been created in the last
6778 // iteration.
6779 ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs,
6780 AllAbstractAttributes.end());
6781
Johannes Doerfertaade7822019-06-05 03:02:24 +00006782 // Reset the work list and repopulate with the changed abstract attributes.
6783 // Note that dependent ones are added above.
6784 Worklist.clear();
6785 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
6786
Johannes Doerfertbf112132019-08-29 01:29:44 +00006787 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
6788 VerifyMaxFixpointIterations));
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00006789
Johannes Doerfertaade7822019-06-05 03:02:24 +00006790 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
6791 << IterationCounter << "/" << MaxFixpointIterations
6792 << " iterations\n");
6793
Johannes Doerfertbf112132019-08-29 01:29:44 +00006794 size_t NumFinalAAs = AllAbstractAttributes.size();
Johannes Doerfertb504eb82019-08-26 18:55:47 +00006795
Johannes Doerfertaade7822019-06-05 03:02:24 +00006796 // Reset abstract arguments not settled in a sound fixpoint by now. This
6797 // happens when we stopped the fixpoint iteration early. Note that only the
6798 // ones marked as "changed" *and* the ones transitively depending on them
6799 // need to be reverted to a pessimistic state. Others might not be in a
6800 // fixpoint state but we can use the optimistic results for them anyway.
6801 SmallPtrSet<AbstractAttribute *, 32> Visited;
6802 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
6803 AbstractAttribute *ChangedAA = ChangedAAs[u];
6804 if (!Visited.insert(ChangedAA).second)
6805 continue;
6806
6807 AbstractState &State = ChangedAA->getState();
6808 if (!State.isAtFixpoint()) {
6809 State.indicatePessimisticFixpoint();
6810
6811 NumAttributesTimedOut++;
6812 }
6813
6814 auto &QuerriedAAs = QueryMap[ChangedAA];
Johannes Doerfert680f6382019-11-02 02:48:05 -05006815 ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(),
6816 QuerriedAAs.OptionalAAs.end());
6817 ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(),
6818 QuerriedAAs.RequiredAAs.end());
Johannes Doerfertaade7822019-06-05 03:02:24 +00006819 }
6820
6821 LLVM_DEBUG({
6822 if (!Visited.empty())
6823 dbgs() << "\n[Attributor] Finalized " << Visited.size()
6824 << " abstract attributes.\n";
6825 });
6826
6827 unsigned NumManifested = 0;
6828 unsigned NumAtFixpoint = 0;
6829 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
6830 for (AbstractAttribute *AA : AllAbstractAttributes) {
6831 AbstractState &State = AA->getState();
6832
6833 // If there is not already a fixpoint reached, we can now take the
6834 // optimistic state. This is correct because we enforced a pessimistic one
6835 // on abstract attributes that were transitively dependent on a changed one
6836 // already above.
6837 if (!State.isAtFixpoint())
6838 State.indicateOptimisticFixpoint();
6839
6840 // If the state is invalid, we do not try to manifest it.
6841 if (!State.isValidState())
6842 continue;
6843
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00006844 // Skip dead code.
6845 if (isAssumedDead(*AA, nullptr))
6846 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +00006847 // Manifest the state and record if we changed the IR.
6848 ChangeStatus LocalChange = AA->manifest(*this);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00006849 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
6850 AA->trackStatistics();
6851
Johannes Doerfertaade7822019-06-05 03:02:24 +00006852 ManifestChange = ManifestChange | LocalChange;
6853
6854 NumAtFixpoint++;
6855 NumManifested += (LocalChange == ChangeStatus::CHANGED);
6856 }
6857
6858 (void)NumManifested;
6859 (void)NumAtFixpoint;
6860 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
6861 << " arguments while " << NumAtFixpoint
6862 << " were in a valid fixpoint state\n");
6863
Johannes Doerfertaade7822019-06-05 03:02:24 +00006864 NumAttributesManifested += NumManifested;
6865 NumAttributesValidFixpoint += NumAtFixpoint;
6866
Fangrui Songf1826172019-08-20 07:21:43 +00006867 (void)NumFinalAAs;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006868 assert(NumFinalAAs == AllAbstractAttributes.size() &&
6869 "Expected the final number of abstract attributes to remain "
6870 "unchanged!");
Johannes Doerfert39681e72019-08-27 04:57:54 +00006871
6872 // Delete stuff at the end to avoid invalid references and a nice order.
Johannes Doerfert2f622062019-09-04 16:35:20 +00006873 {
6874 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
6875 << ToBeDeletedFunctions.size() << " functions and "
6876 << ToBeDeletedBlocks.size() << " blocks and "
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006877 << ToBeDeletedInsts.size() << " instructions and "
6878 << ToBeChangedUses.size() << " uses\n");
6879
Alina Sbirlea9e66c4e2020-01-23 14:21:08 -08006880 SmallVector<WeakTrackingVH, 32> DeadInsts;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006881 SmallVector<Instruction *, 32> TerminatorsToFold;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006882
6883 for (auto &It : ToBeChangedUses) {
6884 Use *U = It.first;
6885 Value *NewV = It.second;
6886 Value *OldV = U->get();
6887 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
6888 << " instead of " << *OldV << "\n");
6889 U->set(NewV);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006890 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
6891 CGModifiedFunctions.insert(I->getFunction());
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01006892 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006893 isInstructionTriviallyDead(I))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006894 DeadInsts.push_back(I);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006895 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006896 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
6897 Instruction *UserI = cast<Instruction>(U->getUser());
6898 if (isa<UndefValue>(NewV)) {
Hideto Uenocb5eb132019-12-27 02:39:37 +09006899 ToBeChangedToUnreachableInsts.insert(UserI);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006900 } else {
6901 TerminatorsToFold.push_back(UserI);
6902 }
6903 }
Johannes Doerfert2f622062019-09-04 16:35:20 +00006904 }
Johannes Doerferta4088c72020-01-07 16:01:57 -06006905 for (auto &V : InvokeWithDeadSuccessor)
6906 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
6907 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
6908 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
6909 bool Invoke2CallAllowed =
6910 !AAIsDeadFunction::mayCatchAsynchronousExceptions(
6911 *II->getFunction());
6912 assert((UnwindBBIsDead || NormalBBIsDead) &&
6913 "Invoke does not have dead successors!");
6914 BasicBlock *BB = II->getParent();
6915 BasicBlock *NormalDestBB = II->getNormalDest();
6916 if (UnwindBBIsDead) {
6917 Instruction *NormalNextIP = &NormalDestBB->front();
6918 if (Invoke2CallAllowed) {
6919 changeToCall(II);
6920 NormalNextIP = BB->getTerminator();
6921 }
6922 if (NormalBBIsDead)
6923 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
6924 } else {
6925 assert(NormalBBIsDead && "Broken invariant!");
6926 if (!NormalDestBB->getUniquePredecessor())
6927 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
6928 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
6929 }
6930 }
Johannes Doerfert1e46eb72020-01-07 15:10:30 -06006931 for (auto &V : ToBeChangedToUnreachableInsts)
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006932 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
6933 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfert1e46eb72020-01-07 15:10:30 -06006934 changeToUnreachable(I, /* UseLLVMTrap */ false);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006935 }
6936 for (Instruction *I : TerminatorsToFold) {
6937 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006938 ConstantFoldTerminator(I->getParent());
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006939 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006940
Johannes Doerfert5429c822020-01-11 23:30:36 -06006941 for (auto &V : ToBeDeletedInsts) {
6942 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006943 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfert5429c822020-01-11 23:30:36 -06006944 I->replaceAllUsesWith(UndefValue::get(I->getType()));
6945 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
6946 DeadInsts.push_back(I);
6947 else
6948 I->eraseFromParent();
6949 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006950 }
6951
6952 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
Johannes Doerfertb19cd272019-09-03 20:42:16 +00006953
Johannes Doerfert2f622062019-09-04 16:35:20 +00006954 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
6955 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
6956 ToBeDeletedBBs.reserve(NumDeadBlocks);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006957 for (BasicBlock *BB : ToBeDeletedBlocks) {
6958 CGModifiedFunctions.insert(BB->getParent());
6959 ToBeDeletedBBs.push_back(BB);
6960 }
Johannes Doerfert5e442a52019-10-30 17:34:59 -05006961 // Actually we do not delete the blocks but squash them into a single
6962 // unreachable but untangling branches that jump here is something we need
6963 // to do in a more generic way.
6964 DetatchDeadBlocks(ToBeDeletedBBs, nullptr);
6965 STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
6966 BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size();
Johannes Doerfert2f622062019-09-04 16:35:20 +00006967 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00006968
Johannes Doerfert2f622062019-09-04 16:35:20 +00006969 // Identify dead internal functions and delete them. This happens outside
6970 // the other fixpoint analysis as we might treat potentially dead functions
6971 // as live to lower the number of iterations. If they happen to be dead, the
6972 // below fixpoint loop will identify and eliminate them.
6973 SmallVector<Function *, 8> InternalFns;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06006974 for (Function *F : Functions)
6975 if (F->hasLocalLinkage())
6976 InternalFns.push_back(F);
Johannes Doerfert2f622062019-09-04 16:35:20 +00006977
6978 bool FoundDeadFn = true;
6979 while (FoundDeadFn) {
6980 FoundDeadFn = false;
6981 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
6982 Function *F = InternalFns[u];
6983 if (!F)
6984 continue;
6985
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006986 bool AllCallSitesKnown;
Johannes Doerfert6b9ee2d2020-01-02 16:53:37 -06006987 if (!checkForAllCallSites(
6988 [this](AbstractCallSite ACS) {
6989 return ToBeDeletedFunctions.count(
6990 ACS.getInstruction()->getFunction());
6991 },
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06006992 *F, true, nullptr, AllCallSitesKnown))
Johannes Doerfert2f622062019-09-04 16:35:20 +00006993 continue;
6994
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05006995 ToBeDeletedFunctions.insert(F);
Johannes Doerfert2f622062019-09-04 16:35:20 +00006996 InternalFns[u] = nullptr;
6997 FoundDeadFn = true;
6998 }
6999 }
Johannes Doerfert39681e72019-08-27 04:57:54 +00007000 }
7001
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007002 // Rewrite the functions as requested during manifest.
7003 ManifestChange =
7004 ManifestChange | rewriteFunctionSignatures(CGModifiedFunctions);
7005
7006 for (Function *Fn : CGModifiedFunctions)
7007 CGUpdater.reanalyzeFunction(*Fn);
7008
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007009 STATS_DECL(AAIsDead, Function, "Number of dead functions deleted.");
7010 BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size();
7011
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007012 for (Function *Fn : ToBeDeletedFunctions)
7013 CGUpdater.removeFunction(*Fn);
Johannes Doerfert6b9ee2d2020-01-02 16:53:37 -06007014
Johannes Doerfertbf112132019-08-29 01:29:44 +00007015 if (VerifyMaxFixpointIterations &&
7016 IterationCounter != MaxFixpointIterations) {
7017 errs() << "\n[Attributor] Fixpoint iteration done after: "
7018 << IterationCounter << "/" << MaxFixpointIterations
7019 << " iterations\n";
7020 llvm_unreachable("The fixpoint was not reached with exactly the number of "
7021 "specified iterations!");
7022 }
7023
Johannes Doerfertaade7822019-06-05 03:02:24 +00007024 return ManifestChange;
7025}
7026
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007027bool Attributor::isValidFunctionSignatureRewrite(
7028 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
Johannes Doerfert75133632019-10-10 01:39:16 -05007029
7030 auto CallSiteCanBeChanged = [](AbstractCallSite ACS) {
7031 // Forbid must-tail calls for now.
7032 return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall();
7033 };
7034
7035 Function *Fn = Arg.getParent();
7036 // Avoid var-arg functions for now.
7037 if (Fn->isVarArg()) {
7038 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
7039 return false;
7040 }
7041
7042 // Avoid functions with complicated argument passing semantics.
7043 AttributeList FnAttributeList = Fn->getAttributes();
7044 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
7045 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
7046 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) {
7047 LLVM_DEBUG(
7048 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
7049 return false;
7050 }
7051
7052 // Avoid callbacks for now.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007053 bool AllCallSitesKnown;
7054 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
7055 AllCallSitesKnown)) {
Johannes Doerfert75133632019-10-10 01:39:16 -05007056 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
7057 return false;
7058 }
7059
7060 auto InstPred = [](Instruction &I) {
7061 if (auto *CI = dyn_cast<CallInst>(&I))
7062 return !CI->isMustTailCall();
7063 return true;
7064 };
7065
7066 // Forbid must-tail calls for now.
7067 // TODO:
7068 bool AnyDead;
7069 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
7070 if (!checkForAllInstructionsImpl(OpcodeInstMap, InstPred, nullptr, AnyDead,
7071 {Instruction::Call})) {
7072 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
7073 return false;
7074 }
7075
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007076 return true;
7077}
7078
7079bool Attributor::registerFunctionSignatureRewrite(
7080 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
7081 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
7082 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
7083 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
7084 << Arg.getParent()->getName() << " with "
7085 << ReplacementTypes.size() << " replacements\n");
7086 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
7087 "Cannot register an invalid rewrite");
7088
7089 Function *Fn = Arg.getParent();
Johannes Doerfert75133632019-10-10 01:39:16 -05007090 SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn];
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007091 if (ARIs.empty())
Johannes Doerfert75133632019-10-10 01:39:16 -05007092 ARIs.resize(Fn->arg_size());
7093
7094 // If we have a replacement already with less than or equal new arguments,
7095 // ignore this request.
7096 ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()];
7097 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
7098 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
7099 return false;
7100 }
7101
7102 // If we have a replacement already but we like the new one better, delete
7103 // the old.
7104 if (ARI)
7105 delete ARI;
7106
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007107 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
7108 << Arg.getParent()->getName() << " with "
7109 << ReplacementTypes.size() << " replacements\n");
7110
Johannes Doerfert75133632019-10-10 01:39:16 -05007111 // Remember the replacement.
7112 ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
7113 std::move(CalleeRepairCB),
7114 std::move(ACSRepairCB));
7115
7116 return true;
7117}
7118
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007119ChangeStatus Attributor::rewriteFunctionSignatures(
7120 SmallPtrSetImpl<Function *> &ModifiedFns) {
Johannes Doerfert75133632019-10-10 01:39:16 -05007121 ChangeStatus Changed = ChangeStatus::UNCHANGED;
7122
7123 for (auto &It : ArgumentReplacementMap) {
7124 Function *OldFn = It.getFirst();
7125
7126 // Deleted functions do not require rewrites.
7127 if (ToBeDeletedFunctions.count(OldFn))
7128 continue;
7129
7130 const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond();
7131 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
7132
7133 SmallVector<Type *, 16> NewArgumentTypes;
7134 SmallVector<AttributeSet, 16> NewArgumentAttributes;
7135
7136 // Collect replacement argument types and copy over existing attributes.
7137 AttributeList OldFnAttributeList = OldFn->getAttributes();
7138 for (Argument &Arg : OldFn->args()) {
7139 if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) {
7140 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
7141 ARI->ReplacementTypes.end());
7142 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
7143 AttributeSet());
7144 } else {
7145 NewArgumentTypes.push_back(Arg.getType());
7146 NewArgumentAttributes.push_back(
7147 OldFnAttributeList.getParamAttributes(Arg.getArgNo()));
7148 }
7149 }
7150
7151 FunctionType *OldFnTy = OldFn->getFunctionType();
7152 Type *RetTy = OldFnTy->getReturnType();
7153
7154 // Construct the new function type using the new arguments types.
7155 FunctionType *NewFnTy =
7156 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
7157
7158 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
7159 << "' from " << *OldFn->getFunctionType() << " to "
7160 << *NewFnTy << "\n");
7161
7162 // Create the new function body and insert it into the module.
7163 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
7164 OldFn->getAddressSpace(), "");
7165 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
7166 NewFn->takeName(OldFn);
7167 NewFn->copyAttributesFrom(OldFn);
7168
7169 // Patch the pointer to LLVM function in debug info descriptor.
7170 NewFn->setSubprogram(OldFn->getSubprogram());
7171 OldFn->setSubprogram(nullptr);
7172
7173 // Recompute the parameter attributes list based on the new arguments for
7174 // the function.
7175 LLVMContext &Ctx = OldFn->getContext();
7176 NewFn->setAttributes(AttributeList::get(
7177 Ctx, OldFnAttributeList.getFnAttributes(),
7178 OldFnAttributeList.getRetAttributes(), NewArgumentAttributes));
7179
7180 // Since we have now created the new function, splice the body of the old
7181 // function right into the new function, leaving the old rotting hulk of the
7182 // function empty.
7183 NewFn->getBasicBlockList().splice(NewFn->begin(),
7184 OldFn->getBasicBlockList());
7185
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007186 // Set of all "call-like" instructions that invoke the old function mapped
7187 // to their new replacements.
7188 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
Johannes Doerfert75133632019-10-10 01:39:16 -05007189
7190 // Callback to create a new "call-like" instruction for a given one.
7191 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
7192 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
7193 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
7194
7195 // Collect the new argument operands for the replacement call site.
7196 SmallVector<Value *, 16> NewArgOperands;
7197 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
7198 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
7199 unsigned NewFirstArgNum = NewArgOperands.size();
Ilya Biryukov4f82af82019-12-31 10:21:52 +01007200 (void)NewFirstArgNum; // only used inside assert.
Johannes Doerfert75133632019-10-10 01:39:16 -05007201 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
7202 if (ARI->ACSRepairCB)
7203 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
7204 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
7205 NewArgOperands.size() &&
7206 "ACS repair callback did not provide as many operand as new "
7207 "types were registered!");
7208 // TODO: Exose the attribute set to the ACS repair callback
7209 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
7210 AttributeSet());
7211 } else {
7212 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
7213 NewArgOperandAttributes.push_back(
7214 OldCallAttributeList.getParamAttributes(OldArgNum));
7215 }
7216 }
7217
7218 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
7219 "Mismatch # argument operands vs. # argument operand attributes!");
7220 assert(NewArgOperands.size() == NewFn->arg_size() &&
7221 "Mismatch # argument operands vs. # function arguments!");
7222
7223 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
7224 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
7225
7226 // Create a new call or invoke instruction to replace the old one.
7227 CallBase *NewCB;
7228 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
7229 NewCB =
7230 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
7231 NewArgOperands, OperandBundleDefs, "", OldCB);
7232 } else {
7233 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
7234 "", OldCB);
7235 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
7236 NewCB = NewCI;
7237 }
7238
7239 // Copy over various properties and the new attributes.
Johannes Doerfert75133632019-10-10 01:39:16 -05007240 uint64_t W;
7241 if (OldCB->extractProfTotalWeight(W))
7242 NewCB->setProfWeight(W);
7243 NewCB->setCallingConv(OldCB->getCallingConv());
7244 NewCB->setDebugLoc(OldCB->getDebugLoc());
7245 NewCB->takeName(OldCB);
7246 NewCB->setAttributes(AttributeList::get(
7247 Ctx, OldCallAttributeList.getFnAttributes(),
7248 OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes));
7249
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007250 CallSitePairs.push_back({OldCB, NewCB});
Johannes Doerfert75133632019-10-10 01:39:16 -05007251 return true;
7252 };
7253
7254 // Use the CallSiteReplacementCreator to create replacement call sites.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007255 bool AllCallSitesKnown;
7256 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
7257 true, nullptr, AllCallSitesKnown);
Ilya Biryukov4f82af82019-12-31 10:21:52 +01007258 (void)Success;
Johannes Doerfert75133632019-10-10 01:39:16 -05007259 assert(Success && "Assumed call site replacement to succeed!");
7260
7261 // Rewire the arguments.
7262 auto OldFnArgIt = OldFn->arg_begin();
7263 auto NewFnArgIt = NewFn->arg_begin();
7264 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
7265 ++OldArgNum, ++OldFnArgIt) {
7266 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
7267 if (ARI->CalleeRepairCB)
7268 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
7269 NewFnArgIt += ARI->ReplacementTypes.size();
7270 } else {
7271 NewFnArgIt->takeName(&*OldFnArgIt);
7272 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
7273 ++NewFnArgIt;
7274 }
7275 }
7276
7277 // Eliminate the instructions *after* we visited all of them.
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007278 for (auto &CallSitePair : CallSitePairs) {
7279 CallBase &OldCB = *CallSitePair.first;
7280 CallBase &NewCB = *CallSitePair.second;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007281 // We do not modify the call graph here but simply reanalyze the old
7282 // function. This should be revisited once the old PM is gone.
7283 ModifiedFns.insert(OldCB.getFunction());
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007284 OldCB.replaceAllUsesWith(&NewCB);
7285 OldCB.eraseFromParent();
7286 }
Johannes Doerfert75133632019-10-10 01:39:16 -05007287
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007288 // Replace the function in the call graph (if any).
7289 CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
7290
7291 // If the old function was modified and needed to be reanalyzed, the new one
7292 // does now.
7293 if (ModifiedFns.erase(OldFn))
7294 ModifiedFns.insert(NewFn);
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007295
Johannes Doerfert75133632019-10-10 01:39:16 -05007296 Changed = ChangeStatus::CHANGED;
7297 }
7298
7299 return Changed;
7300}
7301
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007302void Attributor::initializeInformationCache(Function &F) {
7303
7304 // Walk all instructions to find interesting instructions that might be
7305 // queried by abstract attributes during their initialization or update.
7306 // This has to happen before we create attributes.
7307 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
7308 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
7309
7310 for (Instruction &I : instructions(&F)) {
7311 bool IsInterestingOpcode = false;
7312
7313 // To allow easy access to all instructions in a function with a given
7314 // opcode we store them in the InfoCache. As not all opcodes are interesting
7315 // to concrete attributes we only cache the ones that are as identified in
7316 // the following switch.
7317 // Note: There are no concrete attributes now so this is initially empty.
7318 switch (I.getOpcode()) {
7319 default:
7320 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
7321 "New call site/base instruction type needs to be known int the "
7322 "Attributor.");
7323 break;
7324 case Instruction::Load:
7325 // The alignment of a pointer is interesting for loads.
7326 case Instruction::Store:
7327 // The alignment of a pointer is interesting for stores.
7328 case Instruction::Call:
7329 case Instruction::CallBr:
7330 case Instruction::Invoke:
7331 case Instruction::CleanupRet:
7332 case Instruction::CatchSwitch:
Johannes Doerfert5732f562019-12-24 19:25:08 -06007333 case Instruction::AtomicRMW:
7334 case Instruction::AtomicCmpXchg:
Hideto Uenoef4febd2019-12-29 17:34:08 +09007335 case Instruction::Br:
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007336 case Instruction::Resume:
7337 case Instruction::Ret:
7338 IsInterestingOpcode = true;
7339 }
7340 if (IsInterestingOpcode)
7341 InstOpcodeMap[I.getOpcode()].push_back(&I);
7342 if (I.mayReadOrWriteMemory())
7343 ReadOrWriteInsts.push_back(&I);
7344 }
7345}
7346
Johannes Doerfert12173e62019-10-13 20:25:25 -05007347void Attributor::recordDependence(const AbstractAttribute &FromAA,
Johannes Doerfert680f6382019-11-02 02:48:05 -05007348 const AbstractAttribute &ToAA,
7349 DepClassTy DepClass) {
Johannes Doerfert2dad7292019-10-13 21:10:31 -05007350 if (FromAA.getState().isAtFixpoint())
7351 return;
7352
Johannes Doerfert680f6382019-11-02 02:48:05 -05007353 if (DepClass == DepClassTy::REQUIRED)
7354 QueryMap[&FromAA].RequiredAAs.insert(
7355 const_cast<AbstractAttribute *>(&ToAA));
7356 else
7357 QueryMap[&FromAA].OptionalAAs.insert(
7358 const_cast<AbstractAttribute *>(&ToAA));
Johannes Doerfert2dad7292019-10-13 21:10:31 -05007359 QueriedNonFixAA = true;
Johannes Doerfert12173e62019-10-13 20:25:25 -05007360}
7361
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00007362void Attributor::identifyDefaultAbstractAttributes(Function &F) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00007363 if (!VisitedFunctions.insert(&F).second)
7364 return;
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05007365 if (F.isDeclaration())
7366 return;
Johannes Doerfertaade7822019-06-05 03:02:24 +00007367
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007368 IRPosition FPos = IRPosition::function(F);
7369
Johannes Doerfert305b9612019-08-04 18:40:01 +00007370 // Check for dead BasicBlocks in every function.
Johannes Doerfert21fe0a32019-08-06 00:55:11 +00007371 // We need dead instruction detection because we do not want to deal with
7372 // broken IR in which SSA rules do not apply.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007373 getOrCreateAAFor<AAIsDead>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00007374
7375 // Every function might be "will-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007376 getOrCreateAAFor<AAWillReturn>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00007377
Johannes Doerfert58f324a2019-12-24 18:48:50 -06007378 // Every function might contain instructions that cause "undefined behavior".
7379 getOrCreateAAFor<AAUndefinedBehavior>(FPos);
7380
Stefan Stipanovic53605892019-06-27 11:27:54 +00007381 // Every function can be nounwind.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007382 getOrCreateAAFor<AANoUnwind>(FPos);
Stefan Stipanovic53605892019-06-27 11:27:54 +00007383
Stefan Stipanovic06263672019-07-11 21:37:40 +00007384 // Every function might be marked "nosync"
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007385 getOrCreateAAFor<AANoSync>(FPos);
Stefan Stipanovic06263672019-07-11 21:37:40 +00007386
Hideto Ueno65bbaf92019-07-12 17:38:51 +00007387 // Every function might be "no-free".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007388 getOrCreateAAFor<AANoFree>(FPos);
Hideto Ueno65bbaf92019-07-12 17:38:51 +00007389
Johannes Doerferte83f3032019-08-05 23:22:05 +00007390 // Every function might be "no-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007391 getOrCreateAAFor<AANoReturn>(FPos);
Johannes Doerferte83f3032019-08-05 23:22:05 +00007392
Hideto Ueno63f60662019-09-21 15:13:19 +00007393 // Every function might be "no-recurse".
7394 getOrCreateAAFor<AANoRecurse>(FPos);
7395
Johannes Doerfert1097fab2019-10-07 21:07:57 +00007396 // Every function might be "readnone/readonly/writeonly/...".
7397 getOrCreateAAFor<AAMemoryBehavior>(FPos);
7398
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007399 // Every function might be applicable for Heap-To-Stack conversion.
7400 if (EnableHeapToStack)
7401 getOrCreateAAFor<AAHeapToStack>(FPos);
7402
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00007403 // Return attributes are only appropriate if the return type is non void.
7404 Type *ReturnType = F.getReturnType();
7405 if (!ReturnType->isVoidTy()) {
7406 // Argument attribute "returned" --- Create only one per function even
7407 // though it is an argument attribute.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007408 getOrCreateAAFor<AAReturnedValues>(FPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00007409
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007410 IRPosition RetPos = IRPosition::returned(F);
7411
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007412 // Every returned value might be dead.
7413 getOrCreateAAFor<AAIsDead>(RetPos);
7414
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007415 // Every function might be simplified.
7416 getOrCreateAAFor<AAValueSimplify>(RetPos);
7417
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00007418 if (ReturnType->isPointerTy()) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007419
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00007420 // Every function with pointer return type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007421 getOrCreateAAFor<AAAlign>(RetPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00007422
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00007423 // Every function with pointer return type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007424 getOrCreateAAFor<AANonNull>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00007425
7426 // Every function with pointer return type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007427 getOrCreateAAFor<AANoAlias>(RetPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00007428
7429 // Every function with pointer return type might be marked
7430 // dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007431 getOrCreateAAFor<AADereferenceable>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00007432 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00007433 }
7434
Hideto Ueno54869ec2019-07-15 06:49:04 +00007435 for (Argument &Arg : F.args()) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007436 IRPosition ArgPos = IRPosition::argument(Arg);
7437
7438 // Every argument might be simplified.
7439 getOrCreateAAFor<AAValueSimplify>(ArgPos);
7440
Hideto Ueno19c07af2019-07-23 08:16:17 +00007441 if (Arg.getType()->isPointerTy()) {
7442 // Every argument with pointer type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007443 getOrCreateAAFor<AANonNull>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00007444
Hideto Uenocbab3342019-08-29 05:52:00 +00007445 // Every argument with pointer type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007446 getOrCreateAAFor<AANoAlias>(ArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00007447
Hideto Ueno19c07af2019-07-23 08:16:17 +00007448 // Every argument with pointer type might be marked dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007449 getOrCreateAAFor<AADereferenceable>(ArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00007450
7451 // Every argument with pointer type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007452 getOrCreateAAFor<AAAlign>(ArgPos);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00007453
7454 // Every argument with pointer type might be marked nocapture.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007455 getOrCreateAAFor<AANoCapture>(ArgPos);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00007456
7457 // Every argument with pointer type might be marked
7458 // "readnone/readonly/writeonly/..."
7459 getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007460
7461 // Every argument with pointer type might be marked nofree.
7462 getOrCreateAAFor<AANoFree>(ArgPos);
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007463
7464 // Every argument with pointer type might be privatizable (or promotable)
7465 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00007466 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00007467 }
7468
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007469 auto CallSitePred = [&](Instruction &I) -> bool {
Hideto Ueno54869ec2019-07-15 06:49:04 +00007470 CallSite CS(&I);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007471 if (Function *Callee = CS.getCalledFunction()) {
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05007472 // Skip declerations except if annotations on their call sites were
7473 // explicitly requested.
Johannes Doerfert139c9ef2019-12-13 23:41:02 -06007474 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
7475 !Callee->hasMetadata(LLVMContext::MD_callback))
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05007476 return true;
7477
7478 if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) {
Hideto Ueno188f9a32020-01-15 15:25:52 +09007479
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007480 IRPosition CSRetPos = IRPosition::callsite_returned(CS);
7481
7482 // Call site return values might be dead.
7483 getOrCreateAAFor<AAIsDead>(CSRetPos);
Hideto Ueno188f9a32020-01-15 15:25:52 +09007484
7485 // Call site return integer values might be limited by a constant range.
7486 if (Callee->getReturnType()->isIntegerTy()) {
7487 getOrCreateAAFor<AAValueConstantRange>(CSRetPos);
7488 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007489 }
7490
Johannes Doerfert28880192019-12-31 00:57:00 -06007491 for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007492
7493 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
7494
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007495 // Every call site argument might be dead.
7496 getOrCreateAAFor<AAIsDead>(CSArgPos);
7497
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007498 // Call site argument might be simplified.
7499 getOrCreateAAFor<AAValueSimplify>(CSArgPos);
7500
Hideto Ueno54869ec2019-07-15 06:49:04 +00007501 if (!CS.getArgument(i)->getType()->isPointerTy())
7502 continue;
7503
7504 // Call site argument attribute "non-null".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007505 getOrCreateAAFor<AANonNull>(CSArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00007506
Hideto Uenocbab3342019-08-29 05:52:00 +00007507 // Call site argument attribute "no-alias".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007508 getOrCreateAAFor<AANoAlias>(CSArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00007509
Hideto Ueno19c07af2019-07-23 08:16:17 +00007510 // Call site argument attribute "dereferenceable".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007511 getOrCreateAAFor<AADereferenceable>(CSArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00007512
7513 // Call site argument attribute "align".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00007514 getOrCreateAAFor<AAAlign>(CSArgPos);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007515
Johannes Doerfert28880192019-12-31 00:57:00 -06007516 // Call site argument attribute
7517 // "readnone/readonly/writeonly/..."
7518 getOrCreateAAFor<AAMemoryBehavior>(CSArgPos);
7519
Hideto Ueno4ecf2552019-12-12 13:42:40 +00007520 // Call site argument attribute "nofree".
7521 getOrCreateAAFor<AANoFree>(CSArgPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00007522 }
7523 }
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007524 return true;
7525 };
7526
7527 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
7528 bool Success, AnyDead = false;
7529 Success = checkForAllInstructionsImpl(
7530 OpcodeInstMap, CallSitePred, nullptr, AnyDead,
7531 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
7532 (unsigned)Instruction::Call});
7533 (void)Success;
7534 assert(Success && !AnyDead && "Expected the check call to be successful!");
7535
7536 auto LoadStorePred = [&](Instruction &I) -> bool {
7537 if (isa<LoadInst>(I))
7538 getOrCreateAAFor<AAAlign>(
7539 IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
7540 else
7541 getOrCreateAAFor<AAAlign>(
7542 IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
7543 return true;
7544 };
7545 Success = checkForAllInstructionsImpl(
7546 OpcodeInstMap, LoadStorePred, nullptr, AnyDead,
7547 {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
7548 (void)Success;
7549 assert(Success && !AnyDead && "Expected the check call to be successful!");
Johannes Doerfertaade7822019-06-05 03:02:24 +00007550}
7551
7552/// Helpers to ease debugging through output streams and print calls.
7553///
7554///{
7555raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
7556 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
7557}
7558
Johannes Doerfertfb69f762019-08-05 23:32:31 +00007559raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00007560 switch (AP) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007561 case IRPosition::IRP_INVALID:
7562 return OS << "inv";
7563 case IRPosition::IRP_FLOAT:
7564 return OS << "flt";
7565 case IRPosition::IRP_RETURNED:
7566 return OS << "fn_ret";
7567 case IRPosition::IRP_CALL_SITE_RETURNED:
7568 return OS << "cs_ret";
7569 case IRPosition::IRP_FUNCTION:
7570 return OS << "fn";
7571 case IRPosition::IRP_CALL_SITE:
7572 return OS << "cs";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00007573 case IRPosition::IRP_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00007574 return OS << "arg";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00007575 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00007576 return OS << "cs_arg";
Johannes Doerfertaade7822019-06-05 03:02:24 +00007577 }
7578 llvm_unreachable("Unknown attribute position!");
7579}
7580
Johannes Doerfertfb69f762019-08-05 23:32:31 +00007581raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007582 const Value &AV = Pos.getAssociatedValue();
7583 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
Johannes Doerfertfb69f762019-08-05 23:32:31 +00007584 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
7585}
7586
Johannes Doerfert1a746452019-10-20 22:28:49 -05007587template <typename base_ty, base_ty BestState, base_ty WorstState>
Hideto Ueno188f9a32020-01-15 15:25:52 +09007588raw_ostream &
7589llvm::operator<<(raw_ostream &OS,
7590 const IntegerStateBase<base_ty, BestState, WorstState> &S) {
Johannes Doerfertacc80792019-08-12 22:07:34 +00007591 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
7592 << static_cast<const AbstractState &>(S);
7593}
7594
Hideto Ueno188f9a32020-01-15 15:25:52 +09007595raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
7596 OS << "range-state(" << S.getBitWidth() << ")<";
7597 S.getKnown().print(OS);
7598 OS << " / ";
7599 S.getAssumed().print(OS);
7600 OS << ">";
7601
7602 return OS << static_cast<const AbstractState &>(S);
7603}
7604
Johannes Doerfertaade7822019-06-05 03:02:24 +00007605raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
7606 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
7607}
7608
7609raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
7610 AA.print(OS);
7611 return OS;
7612}
7613
7614void AbstractAttribute::print(raw_ostream &OS) const {
Johannes Doerfertfb69f762019-08-05 23:32:31 +00007615 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
7616 << "]";
Johannes Doerfertaade7822019-06-05 03:02:24 +00007617}
7618///}
7619
7620/// ----------------------------------------------------------------------------
7621/// Pass (Manager) Boilerplate
7622/// ----------------------------------------------------------------------------
7623
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007624static bool runAttributorOnFunctions(InformationCache &InfoCache,
7625 SetVector<Function *> &Functions,
7626 AnalysisGetter &AG,
7627 CallGraphUpdater &CGUpdater) {
7628 if (DisableAttributor || Functions.empty())
Johannes Doerfertaade7822019-06-05 03:02:24 +00007629 return false;
7630
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007631 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size()
Johannes Doerfertaade7822019-06-05 03:02:24 +00007632 << " functions.\n");
7633
7634 // Create an Attributor and initially empty information cache that is filled
7635 // while we identify default attribute opportunities.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007636 Attributor A(Functions, InfoCache, CGUpdater, DepRecInterval);
Johannes Doerfertaade7822019-06-05 03:02:24 +00007637
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007638 for (Function *F : Functions)
7639 A.initializeInformationCache(*F);
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007640
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007641 for (Function *F : Functions) {
7642 if (F->hasExactDefinition())
Johannes Doerfertb0412e42019-09-04 16:16:13 +00007643 NumFnWithExactDefinition++;
7644 else
Johannes Doerfertaade7822019-06-05 03:02:24 +00007645 NumFnWithoutExactDefinition++;
Johannes Doerfertaade7822019-06-05 03:02:24 +00007646
Johannes Doerfert2f622062019-09-04 16:35:20 +00007647 // We look at internal functions only on-demand but if any use is not a
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007648 // direct call or outside the current set of analyzed functions, we have to
7649 // do it eagerly.
7650 if (F->hasLocalLinkage()) {
7651 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
7652 ImmutableCallSite ICS(U.getUser());
7653 return ICS && ICS.isCallee(&U) &&
7654 Functions.count(const_cast<Function *>(ICS.getCaller()));
Johannes Doerfert2f622062019-09-04 16:35:20 +00007655 }))
7656 continue;
7657 }
7658
Johannes Doerfertaade7822019-06-05 03:02:24 +00007659 // Populate the Attributor with abstract attribute opportunities in the
7660 // function and the information cache with IR information.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007661 A.identifyDefaultAbstractAttributes(*F);
Johannes Doerfertaade7822019-06-05 03:02:24 +00007662 }
7663
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007664 bool Changed = A.run() == ChangeStatus::CHANGED;
7665 assert(!verifyModule(*Functions.front()->getParent(), &errs()) &&
7666 "Module verification failed!");
Johannes Doerferta4088c72020-01-07 16:01:57 -06007667 return Changed;
Johannes Doerfertaade7822019-06-05 03:02:24 +00007668}
7669
7670PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007671 FunctionAnalysisManager &FAM =
7672 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
7673 AnalysisGetter AG(FAM);
7674
7675 SetVector<Function *> Functions;
7676 for (Function &F : M)
7677 Functions.insert(&F);
7678
7679 CallGraphUpdater CGUpdater;
7680 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
7681 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
7682 // FIXME: Think about passes we will preserve and add them here.
7683 return PreservedAnalyses::none();
7684 }
7685 return PreservedAnalyses::all();
7686}
7687
7688PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
7689 CGSCCAnalysisManager &AM,
7690 LazyCallGraph &CG,
7691 CGSCCUpdateResult &UR) {
7692 FunctionAnalysisManager &FAM =
7693 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
7694 AnalysisGetter AG(FAM);
7695
7696 SetVector<Function *> Functions;
7697 for (LazyCallGraph::Node &N : C)
7698 Functions.insert(&N.getFunction());
7699
7700 if (Functions.empty())
7701 return PreservedAnalyses::all();
7702
7703 Module &M = *Functions.back()->getParent();
7704 CallGraphUpdater CGUpdater;
7705 CGUpdater.initialize(CG, C, AM, UR);
7706 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
7707 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00007708 // FIXME: Think about passes we will preserve and add them here.
7709 return PreservedAnalyses::none();
7710 }
7711 return PreservedAnalyses::all();
7712}
7713
7714namespace {
7715
7716struct AttributorLegacyPass : public ModulePass {
7717 static char ID;
7718
7719 AttributorLegacyPass() : ModulePass(ID) {
7720 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
7721 }
7722
7723 bool runOnModule(Module &M) override {
7724 if (skipModule(M))
7725 return false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007726
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00007727 AnalysisGetter AG;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007728 SetVector<Function *> Functions;
7729 for (Function &F : M)
7730 Functions.insert(&F);
7731
7732 CallGraphUpdater CGUpdater;
7733 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
7734 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
Johannes Doerfertaade7822019-06-05 03:02:24 +00007735 }
7736
7737 void getAnalysisUsage(AnalysisUsage &AU) const override {
7738 // FIXME: Think about passes we will preserve and add them here.
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007739 AU.addRequired<TargetLibraryInfoWrapperPass>();
Johannes Doerfertaade7822019-06-05 03:02:24 +00007740 }
7741};
7742
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007743struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
7744 CallGraphUpdater CGUpdater;
7745 static char ID;
7746
7747 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
7748 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
7749 }
7750
7751 bool runOnSCC(CallGraphSCC &SCC) override {
7752 if (skipSCC(SCC))
7753 return false;
7754
7755 SetVector<Function *> Functions;
7756 for (CallGraphNode *CGN : SCC)
7757 if (Function *Fn = CGN->getFunction())
7758 if (!Fn->isDeclaration())
7759 Functions.insert(Fn);
7760
7761 if (Functions.empty())
7762 return false;
7763
7764 AnalysisGetter AG;
7765 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
7766 CGUpdater.initialize(CG, SCC);
7767 Module &M = *Functions.back()->getParent();
7768 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
7769 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
7770 }
7771
7772 bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); }
7773
7774 void getAnalysisUsage(AnalysisUsage &AU) const override {
7775 // FIXME: Think about passes we will preserve and add them here.
7776 AU.addRequired<TargetLibraryInfoWrapperPass>();
7777 CallGraphSCCPass::getAnalysisUsage(AU);
7778 }
7779};
7780
Johannes Doerfertaade7822019-06-05 03:02:24 +00007781} // end anonymous namespace
7782
7783Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007784Pass *llvm::createAttributorCGSCCLegacyPass() {
7785 return new AttributorCGSCCLegacyPass();
7786}
Johannes Doerfertaade7822019-06-05 03:02:24 +00007787
7788char AttributorLegacyPass::ID = 0;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007789char AttributorCGSCCLegacyPass::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00007790
7791const char AAReturnedValues::ID = 0;
7792const char AANoUnwind::ID = 0;
7793const char AANoSync::ID = 0;
Johannes Doerferteccdf082019-08-05 23:35:12 +00007794const char AANoFree::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00007795const char AANonNull::ID = 0;
7796const char AANoRecurse::ID = 0;
7797const char AAWillReturn::ID = 0;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06007798const char AAUndefinedBehavior::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00007799const char AANoAlias::ID = 0;
Pankaj Gode04945c92019-11-22 18:40:47 +05307800const char AAReachability::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00007801const char AANoReturn::ID = 0;
7802const char AAIsDead::ID = 0;
7803const char AADereferenceable::ID = 0;
7804const char AAAlign::ID = 0;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00007805const char AANoCapture::ID = 0;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007806const char AAValueSimplify::ID = 0;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007807const char AAHeapToStack::ID = 0;
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007808const char AAPrivatizablePtr::ID = 0;
Johannes Doerfert1097fab2019-10-07 21:07:57 +00007809const char AAMemoryBehavior::ID = 0;
Hideto Ueno188f9a32020-01-15 15:25:52 +09007810const char AAValueConstantRange::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00007811
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007812// Macro magic to create the static generator function for attributes that
7813// follow the naming scheme.
7814
7815#define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
7816 case IRPosition::PK: \
7817 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
7818
7819#define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
7820 case IRPosition::PK: \
7821 AA = new CLASS##SUFFIX(IRP); \
7822 break;
7823
7824#define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
7825 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
7826 CLASS *AA = nullptr; \
7827 switch (IRP.getPositionKind()) { \
7828 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
7829 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
7830 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
7831 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
7832 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
7833 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
7834 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
7835 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
7836 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007837 return *AA; \
7838 }
7839
7840#define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
7841 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
7842 CLASS *AA = nullptr; \
7843 switch (IRP.getPositionKind()) { \
7844 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
7845 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
7846 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
7847 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
7848 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
7849 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
7850 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
7851 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
7852 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007853 return *AA; \
7854 }
7855
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007856#define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
7857 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
7858 CLASS *AA = nullptr; \
7859 switch (IRP.getPositionKind()) { \
7860 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
7861 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
7862 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
7863 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
7864 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
7865 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
7866 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
7867 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
7868 } \
7869 return *AA; \
7870 }
7871
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007872#define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
7873 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
7874 CLASS *AA = nullptr; \
7875 switch (IRP.getPositionKind()) { \
7876 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
7877 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
7878 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
7879 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
7880 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
7881 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
7882 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
7883 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
7884 } \
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007885 return *AA; \
7886 }
7887
Johannes Doerfert1097fab2019-10-07 21:07:57 +00007888#define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
7889 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
7890 CLASS *AA = nullptr; \
7891 switch (IRP.getPositionKind()) { \
7892 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
7893 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
7894 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
7895 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
7896 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
7897 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
7898 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
7899 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
7900 } \
Johannes Doerfert1097fab2019-10-07 21:07:57 +00007901 return *AA; \
7902 }
7903
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007904CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
7905CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007906CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
7907CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
7908CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007909CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
7910
7911CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
7912CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007913CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007914CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
7915CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00007916CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
Hideto Ueno188f9a32020-01-15 15:25:52 +09007917CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007918
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007919CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007920CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007921CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007922
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007923CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
Pankaj Gode04945c92019-11-22 18:40:47 +05307924CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
Johannes Doerfert58f324a2019-12-24 18:48:50 -06007925CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007926
Johannes Doerfert1097fab2019-10-07 21:07:57 +00007927CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
7928
Johannes Doerfertd4bea882019-10-07 23:28:54 +00007929#undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007930#undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfertd4bea882019-10-07 23:28:54 +00007931#undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007932#undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
Hideto Uenof2b9dc42019-09-07 07:03:05 +00007933#undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007934#undef SWITCH_PK_CREATE
7935#undef SWITCH_PK_INV
7936
Johannes Doerfertaade7822019-06-05 03:02:24 +00007937INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
7938 "Deduce and propagate attributes", false, false)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00007939INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Johannes Doerfertaade7822019-06-05 03:02:24 +00007940INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
7941 "Deduce and propagate attributes", false, false)
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007942INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
7943 "Deduce and propagate attributes (CGSCC pass)", false,
7944 false)
7945INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
7946INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
7947INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
7948 "Deduce and propagate attributes (CGSCC pass)", false,
7949 false)