blob: fb860bdf7a012778b3d1d9cda04dc4a32274d1fa [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"
omarahmed1111b285b332020-03-13 10:30:36 -050031#include "llvm/Analysis/MustExecute.h"
Hideto Ueno188f9a32020-01-15 15:25:52 +090032#include "llvm/Analysis/ScalarEvolution.h"
Hideto Ueno54869ec2019-07-15 06:49:04 +000033#include "llvm/Analysis/ValueTracking.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000034#include "llvm/IR/Argument.h"
35#include "llvm/IR/Attributes.h"
Hideto Ueno11d37102019-07-17 15:15:43 +000036#include "llvm/IR/CFG.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050037#include "llvm/IR/IRBuilder.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000038#include "llvm/IR/InstIterator.h"
Stefan Stipanovic06263672019-07-11 21:37:40 +000039#include "llvm/IR/IntrinsicInst.h"
omarahmed1111b285b332020-03-13 10:30:36 -050040#include "llvm/IR/NoFolder.h"
Johannes Doerferta4088c72020-01-07 16:01:57 -060041#include "llvm/IR/Verifier.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080042#include "llvm/InitializePasses.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000043#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Support/raw_ostream.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050046#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Stefan Stipanovic6058b862019-07-22 23:58:23 +000047#include "llvm/Transforms/Utils/BasicBlockUtils.h"
48#include "llvm/Transforms/Utils/Local.h"
49
Johannes Doerfertaade7822019-06-05 03:02:24 +000050#include <cassert>
51
52using namespace llvm;
53
54#define DEBUG_TYPE "attributor"
55
56STATISTIC(NumFnWithExactDefinition,
57 "Number of function with exact definitions");
58STATISTIC(NumFnWithoutExactDefinition,
59 "Number of function without exact definitions");
60STATISTIC(NumAttributesTimedOut,
61 "Number of abstract attributes timed out before fixpoint");
62STATISTIC(NumAttributesValidFixpoint,
63 "Number of abstract attributes in a valid fixpoint state");
64STATISTIC(NumAttributesManifested,
65 "Number of abstract attributes manifested in IR");
Johannes Doerfert680f6382019-11-02 02:48:05 -050066STATISTIC(NumAttributesFixedDueToRequiredDependences,
67 "Number of abstract attributes fixed due to required dependences");
Johannes Doerfertaade7822019-06-05 03:02:24 +000068
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000069// Some helper macros to deal with statistics tracking.
70//
71// Usage:
72// For simple IR attribute tracking overload trackStatistics in the abstract
Johannes Doerfert17b578b2019-08-14 21:46:25 +000073// attribute and choose the right STATS_DECLTRACK_********* macro,
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000074// e.g.,:
75// void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +000076// STATS_DECLTRACK_ARG_ATTR(returned)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000077// }
78// If there is a single "increment" side one can use the macro
Johannes Doerfert17b578b2019-08-14 21:46:25 +000079// STATS_DECLTRACK with a custom message. If there are multiple increment
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000080// sides, STATS_DECL and STATS_TRACK can also be used separatly.
81//
82#define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \
83 ("Number of " #TYPE " marked '" #NAME "'")
84#define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
Johannes Doerferta7a3b3a2019-09-04 19:01:08 +000085#define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
86#define STATS_DECL(NAME, TYPE, MSG) \
87 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000088#define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
Johannes Doerfert17b578b2019-08-14 21:46:25 +000089#define STATS_DECLTRACK(NAME, TYPE, MSG) \
Johannes Doerfert169af992019-08-20 06:09:56 +000090 { \
91 STATS_DECL(NAME, TYPE, MSG) \
92 STATS_TRACK(NAME, TYPE) \
93 }
Johannes Doerfert17b578b2019-08-14 21:46:25 +000094#define STATS_DECLTRACK_ARG_ATTR(NAME) \
95 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
96#define STATS_DECLTRACK_CSARG_ATTR(NAME) \
97 STATS_DECLTRACK(NAME, CSArguments, \
98 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
99#define STATS_DECLTRACK_FN_ATTR(NAME) \
100 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
101#define STATS_DECLTRACK_CS_ATTR(NAME) \
102 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
103#define STATS_DECLTRACK_FNRET_ATTR(NAME) \
104 STATS_DECLTRACK(NAME, FunctionReturn, \
Johannes Doerfert2db85282019-08-21 20:56:56 +0000105 BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000106#define STATS_DECLTRACK_CSRET_ATTR(NAME) \
107 STATS_DECLTRACK(NAME, CSReturn, \
108 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
109#define STATS_DECLTRACK_FLOATING_ATTR(NAME) \
110 STATS_DECLTRACK(NAME, Floating, \
111 ("Number of floating values known to be '" #NAME "'"))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000112
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600113// Specialization of the operator<< for abstract attributes subclasses. This
114// disambiguates situations where multiple operators are applicable.
115namespace llvm {
116#define PIPE_OPERATOR(CLASS) \
117 raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \
118 return OS << static_cast<const AbstractAttribute &>(AA); \
119 }
120
121PIPE_OPERATOR(AAIsDead)
122PIPE_OPERATOR(AANoUnwind)
123PIPE_OPERATOR(AANoSync)
124PIPE_OPERATOR(AANoRecurse)
125PIPE_OPERATOR(AAWillReturn)
126PIPE_OPERATOR(AANoReturn)
127PIPE_OPERATOR(AAReturnedValues)
128PIPE_OPERATOR(AANonNull)
129PIPE_OPERATOR(AANoAlias)
130PIPE_OPERATOR(AADereferenceable)
131PIPE_OPERATOR(AAAlign)
132PIPE_OPERATOR(AANoCapture)
133PIPE_OPERATOR(AAValueSimplify)
134PIPE_OPERATOR(AANoFree)
135PIPE_OPERATOR(AAHeapToStack)
136PIPE_OPERATOR(AAReachability)
137PIPE_OPERATOR(AAMemoryBehavior)
Johannes Doerfert282f5d72020-01-26 02:51:57 -0600138PIPE_OPERATOR(AAMemoryLocation)
Hideto Ueno188f9a32020-01-15 15:25:52 +0900139PIPE_OPERATOR(AAValueConstantRange)
Johannes Doerfert89c2e732019-10-30 17:20:20 -0500140PIPE_OPERATOR(AAPrivatizablePtr)
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600141
142#undef PIPE_OPERATOR
143} // namespace llvm
144
Johannes Doerfertaade7822019-06-05 03:02:24 +0000145// TODO: Determine a good default value.
146//
147// In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
148// (when run with the first 5 abstract attributes). The results also indicate
149// that we never reach 32 iterations but always find a fixpoint sooner.
150//
151// This will become more evolved once we perform two interleaved fixpoint
152// iterations: bottom-up and top-down.
153static cl::opt<unsigned>
154 MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
155 cl::desc("Maximal number of fixpoint iterations."),
156 cl::init(32));
Johannes Doerfertb504eb82019-08-26 18:55:47 +0000157static cl::opt<bool> VerifyMaxFixpointIterations(
158 "attributor-max-iterations-verify", cl::Hidden,
159 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
160 cl::init(false));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000161
162static cl::opt<bool> DisableAttributor(
163 "attributor-disable", cl::Hidden,
164 cl::desc("Disable the attributor inter-procedural deduction pass."),
Johannes Doerfert282d34e2019-06-14 14:53:41 +0000165 cl::init(true));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000166
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -0500167static cl::opt<bool> AnnotateDeclarationCallSites(
168 "attributor-annotate-decl-cs", cl::Hidden,
James Hendersond68904f2020-01-06 10:15:44 +0000169 cl::desc("Annotate call sites of function declarations."), cl::init(false));
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -0500170
Johannes Doerfert7516a5e2019-09-03 20:37:24 +0000171static cl::opt<bool> ManifestInternal(
172 "attributor-manifest-internal", cl::Hidden,
173 cl::desc("Manifest Attributor internal string attributes."),
174 cl::init(false));
175
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +0000176static cl::opt<unsigned> DepRecInterval(
177 "attributor-dependence-recompute-interval", cl::Hidden,
178 cl::desc("Number of iterations until dependences are recomputed."),
179 cl::init(4));
180
Stefan Stipanovic431141c2019-09-15 21:47:41 +0000181static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
182 cl::init(true), cl::Hidden);
183
Johannes Doerfert1c2afae2019-10-10 05:34:21 +0000184static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
185 cl::Hidden);
Stefan Stipanovic431141c2019-09-15 21:47:41 +0000186
Johannes Doerfertaade7822019-06-05 03:02:24 +0000187/// Logic operators for the change status enum class.
188///
189///{
190ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
191 return l == ChangeStatus::CHANGED ? l : r;
192}
193ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
194 return l == ChangeStatus::UNCHANGED ? l : r;
195}
196///}
197
Johannes Doerfertb1b441d2019-10-10 01:19:57 -0500198Argument *IRPosition::getAssociatedArgument() const {
199 if (getPositionKind() == IRP_ARGUMENT)
200 return cast<Argument>(&getAnchorValue());
201
202 // Not an Argument and no argument number means this is not a call site
203 // argument, thus we cannot find a callback argument to return.
204 int ArgNo = getArgNo();
205 if (ArgNo < 0)
206 return nullptr;
207
208 // Use abstract call sites to make the connection between the call site
209 // values and the ones in callbacks. If a callback was found that makes use
210 // of the underlying call site operand, we want the corresponding callback
211 // callee argument and not the direct callee argument.
212 Optional<Argument *> CBCandidateArg;
213 SmallVector<const Use *, 4> CBUses;
214 ImmutableCallSite ICS(&getAnchorValue());
215 AbstractCallSite::getCallbackUses(ICS, CBUses);
216 for (const Use *U : CBUses) {
217 AbstractCallSite ACS(U);
218 assert(ACS && ACS.isCallbackCall());
219 if (!ACS.getCalledFunction())
220 continue;
221
222 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
223
224 // Test if the underlying call site operand is argument number u of the
225 // callback callee.
226 if (ACS.getCallArgOperandNo(u) != ArgNo)
227 continue;
228
229 assert(ACS.getCalledFunction()->arg_size() > u &&
230 "ACS mapped into var-args arguments!");
231 if (CBCandidateArg.hasValue()) {
232 CBCandidateArg = nullptr;
233 break;
234 }
235 CBCandidateArg = ACS.getCalledFunction()->getArg(u);
236 }
237 }
238
239 // If we found a unique callback candidate argument, return it.
240 if (CBCandidateArg.hasValue() && CBCandidateArg.getValue())
241 return CBCandidateArg.getValue();
242
243 // If no callbacks were found, or none used the underlying call site operand
244 // exclusively, use the direct callee argument if available.
245 const Function *Callee = ICS.getCalledFunction();
246 if (Callee && Callee->arg_size() > unsigned(ArgNo))
247 return Callee->getArg(ArgNo);
248
249 return nullptr;
250}
251
Johannes Doerferte1eed6c2020-02-16 16:45:28 -0600252static Optional<Constant *> getAssumedConstant(Attributor &A, const Value &V,
253 const AbstractAttribute &AA,
254 bool &UsedAssumedInformation) {
Johannes Doerfert214ed3f2020-01-11 23:35:45 -0600255 const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
256 AA, IRPosition::value(V), /* TrackDependence */ false);
257 Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A);
258 bool IsKnown = ValueSimplifyAA.isKnown();
259 UsedAssumedInformation |= !IsKnown;
260 if (!SimplifiedV.hasValue()) {
261 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
262 return llvm::None;
263 }
264 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) {
265 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
266 return llvm::None;
267 }
Johannes Doerferte1eed6c2020-02-16 16:45:28 -0600268 Constant *CI = dyn_cast_or_null<Constant>(SimplifiedV.getValue());
269 if (CI && CI->getType() != V.getType()) {
270 // TODO: Check for a save conversion.
271 return nullptr;
272 }
Johannes Doerfert214ed3f2020-01-11 23:35:45 -0600273 if (CI)
274 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
275 return CI;
276}
277
Johannes Doerferte1eed6c2020-02-16 16:45:28 -0600278static Optional<ConstantInt *>
279getAssumedConstantInt(Attributor &A, const Value &V,
280 const AbstractAttribute &AA,
281 bool &UsedAssumedInformation) {
282 Optional<Constant *> C = getAssumedConstant(A, V, AA, UsedAssumedInformation);
283 if (C.hasValue())
284 return dyn_cast_or_null<ConstantInt>(C.getValue());
285 return llvm::None;
286}
287
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -0600288/// Get pointer operand of memory accessing instruction. If \p I is
289/// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
290/// is set to false and the instruction is volatile, return nullptr.
291static const Value *getPointerOperand(const Instruction *I,
292 bool AllowVolatile) {
293 if (auto *LI = dyn_cast<LoadInst>(I)) {
294 if (!AllowVolatile && LI->isVolatile())
295 return nullptr;
296 return LI->getPointerOperand();
297 }
298
299 if (auto *SI = dyn_cast<StoreInst>(I)) {
300 if (!AllowVolatile && SI->isVolatile())
301 return nullptr;
302 return SI->getPointerOperand();
303 }
304
305 if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
306 if (!AllowVolatile && CXI->isVolatile())
307 return nullptr;
308 return CXI->getPointerOperand();
309 }
310
311 if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
312 if (!AllowVolatile && RMWI->isVolatile())
313 return nullptr;
314 return RMWI->getPointerOperand();
315 }
316
317 return nullptr;
318}
319
Johannes Doerfert89c2e732019-10-30 17:20:20 -0500320/// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
321/// advanced by \p Offset bytes. To aid later analysis the method tries to build
322/// getelement pointer instructions that traverse the natural type of \p Ptr if
323/// possible. If that fails, the remaining offset is adjusted byte-wise, hence
324/// through a cast to i8*.
325///
326/// TODO: This could probably live somewhere more prominantly if it doesn't
327/// already exist.
328static Value *constructPointer(Type *ResTy, Value *Ptr, int64_t Offset,
329 IRBuilder<NoFolder> &IRB, const DataLayout &DL) {
330 assert(Offset >= 0 && "Negative offset not supported yet!");
331 LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
332 << "-bytes as " << *ResTy << "\n");
333
334 // The initial type we are trying to traverse to get nice GEPs.
335 Type *Ty = Ptr->getType();
336
337 SmallVector<Value *, 4> Indices;
338 std::string GEPName = Ptr->getName().str();
339 while (Offset) {
340 uint64_t Idx, Rem;
341
342 if (auto *STy = dyn_cast<StructType>(Ty)) {
343 const StructLayout *SL = DL.getStructLayout(STy);
344 if (int64_t(SL->getSizeInBytes()) < Offset)
345 break;
346 Idx = SL->getElementContainingOffset(Offset);
347 assert(Idx < STy->getNumElements() && "Offset calculation error!");
348 Rem = Offset - SL->getElementOffset(Idx);
349 Ty = STy->getElementType(Idx);
350 } else if (auto *PTy = dyn_cast<PointerType>(Ty)) {
351 Ty = PTy->getElementType();
352 if (!Ty->isSized())
353 break;
354 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
355 assert(ElementSize && "Expected type with size!");
356 Idx = Offset / ElementSize;
357 Rem = Offset % ElementSize;
358 } else {
359 // Non-aggregate type, we cast and make byte-wise progress now.
360 break;
361 }
362
363 LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset
364 << " Idx: " << Idx << " Rem: " << Rem << "\n");
365
366 GEPName += "." + std::to_string(Idx);
367 Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx));
368 Offset = Rem;
369 }
370
371 // Create a GEP if we collected indices above.
372 if (Indices.size())
373 Ptr = IRB.CreateGEP(Ptr, Indices, GEPName);
374
375 // If an offset is left we use byte-wise adjustment.
376 if (Offset) {
377 Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
378 Ptr = IRB.CreateGEP(Ptr, IRB.getInt32(Offset),
379 GEPName + ".b" + Twine(Offset));
380 }
381
382 // Ensure the result has the requested type.
383 Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
384
385 LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
386 return Ptr;
387}
388
Johannes Doerfertdef99282019-08-14 21:29:37 +0000389/// Recursively visit all values that might become \p IRP at some point. This
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000390/// will be done by looking through cast instructions, selects, phis, and calls
Johannes Doerfertdef99282019-08-14 21:29:37 +0000391/// with the "returned" attribute. Once we cannot look through the value any
392/// further, the callback \p VisitValueCB is invoked and passed the current
Johannes Doerfert52aec322020-02-11 15:11:32 -0600393/// value, the \p State, and a flag to indicate if we stripped anything.
394/// Stripped means that we unpacked the value associated with \p IRP at least
395/// once. Note that the value used for the callback may still be the value
396/// associated with \p IRP (due to PHIs). To limit how much effort is invested,
397/// we will never visit more values than specified by \p MaxValues.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000398template <typename AAType, typename StateTy>
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +0000399static bool genericValueTraversal(
Johannes Doerfertdef99282019-08-14 21:29:37 +0000400 Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State,
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000401 const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB,
Johannes Doerfert282f5d72020-01-26 02:51:57 -0600402 int MaxValues = 8, const function_ref<Value *(Value *)> StripCB = nullptr) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000403
Johannes Doerfertdef99282019-08-14 21:29:37 +0000404 const AAIsDead *LivenessAA = nullptr;
405 if (IRP.getAnchorScope())
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000406 LivenessAA = &A.getAAFor<AAIsDead>(
Johannes Doerfert19b00432019-08-26 17:48:05 +0000407 QueryingAA, IRPosition::function(*IRP.getAnchorScope()),
408 /* TrackDependence */ false);
409 bool AnyDead = false;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000410
411 // TODO: Use Positions here to allow context sensitivity in VisitValueCB
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000412 SmallPtrSet<Value *, 16> Visited;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000413 SmallVector<Value *, 16> Worklist;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000414 Worklist.push_back(&IRP.getAssociatedValue());
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000415
416 int Iteration = 0;
417 do {
418 Value *V = Worklist.pop_back_val();
Johannes Doerfert282f5d72020-01-26 02:51:57 -0600419 if (StripCB)
420 V = StripCB(V);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000421
422 // Check if we should process the current value. To prevent endless
423 // recursion keep a record of the values we followed!
Johannes Doerfertdef99282019-08-14 21:29:37 +0000424 if (!Visited.insert(V).second)
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000425 continue;
426
427 // Make sure we limit the compile time for complex expressions.
428 if (Iteration++ >= MaxValues)
429 return false;
430
431 // Explicitly look through calls with a "returned" attribute if we do
432 // not have a pointer as stripPointerCasts only works on them.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000433 Value *NewV = nullptr;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000434 if (V->getType()->isPointerTy()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000435 NewV = V->stripPointerCasts();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000436 } else {
437 CallSite CS(V);
438 if (CS && CS.getCalledFunction()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000439 for (Argument &Arg : CS.getCalledFunction()->args())
440 if (Arg.hasReturnedAttr()) {
441 NewV = CS.getArgOperand(Arg.getArgNo());
442 break;
443 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000444 }
445 }
Johannes Doerfertdef99282019-08-14 21:29:37 +0000446 if (NewV && NewV != V) {
447 Worklist.push_back(NewV);
448 continue;
449 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000450
451 // Look through select instructions, visit both potential values.
452 if (auto *SI = dyn_cast<SelectInst>(V)) {
453 Worklist.push_back(SI->getTrueValue());
454 Worklist.push_back(SI->getFalseValue());
455 continue;
456 }
457
Johannes Doerfertdef99282019-08-14 21:29:37 +0000458 // Look through phi nodes, visit all live operands.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000459 if (auto *PHI = dyn_cast<PHINode>(V)) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000460 assert(LivenessAA &&
461 "Expected liveness in the presence of instructions!");
Johannes Doerfertdef99282019-08-14 21:29:37 +0000462 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
463 const BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
Johannes Doerfert23f41f12020-01-12 01:09:22 -0600464 if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA,
465 LivenessAA,
466 /* CheckBBLivenessOnly */ true)) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +0000467 AnyDead = true;
Johannes Doerfert19b00432019-08-26 17:48:05 +0000468 continue;
469 }
470 Worklist.push_back(PHI->getIncomingValue(u));
Johannes Doerfertdef99282019-08-14 21:29:37 +0000471 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000472 continue;
473 }
474
475 // Once a leaf is reached we inform the user through the callback.
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000476 if (!VisitValueCB(*V, State, Iteration > 1))
477 return false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000478 } while (!Worklist.empty());
479
Johannes Doerfert19b00432019-08-26 17:48:05 +0000480 // If we actually used liveness information so we have to record a dependence.
481 if (AnyDead)
Johannes Doerfert680f6382019-11-02 02:48:05 -0500482 A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
Johannes Doerfert19b00432019-08-26 17:48:05 +0000483
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000484 // All values have been visited.
485 return true;
486}
487
Johannes Doerfertaade7822019-06-05 03:02:24 +0000488/// Return true if \p New is equal or worse than \p Old.
489static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
490 if (!Old.isIntAttribute())
491 return true;
492
493 return Old.getValueAsInt() >= New.getValueAsInt();
494}
495
496/// Return true if the information provided by \p Attr was added to the
497/// attribute list \p Attrs. This is only the case if it was not already present
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000498/// in \p Attrs at the position describe by \p PK and \p AttrIdx.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000499static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000500 AttributeList &Attrs, int AttrIdx) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000501
502 if (Attr.isEnumAttribute()) {
503 Attribute::AttrKind Kind = Attr.getKindAsEnum();
504 if (Attrs.hasAttribute(AttrIdx, Kind))
505 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
506 return false;
507 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
508 return true;
509 }
510 if (Attr.isStringAttribute()) {
511 StringRef Kind = Attr.getKindAsString();
512 if (Attrs.hasAttribute(AttrIdx, Kind))
513 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
514 return false;
515 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
516 return true;
517 }
Hideto Ueno19c07af2019-07-23 08:16:17 +0000518 if (Attr.isIntAttribute()) {
519 Attribute::AttrKind Kind = Attr.getKindAsEnum();
520 if (Attrs.hasAttribute(AttrIdx, Kind))
521 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
522 return false;
523 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
524 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
525 return true;
526 }
Johannes Doerfertaade7822019-06-05 03:02:24 +0000527
528 llvm_unreachable("Expected enum or string attribute!");
529}
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000530
Hideto Ueno0f4383f2019-11-27 14:41:12 +0000531static const Value *
532getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset,
533 const DataLayout &DL,
534 bool AllowNonInbounds = false) {
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -0600535 const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000536 if (!Ptr)
537 return nullptr;
538
539 return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL,
Hideto Ueno0f4383f2019-11-27 14:41:12 +0000540 AllowNonInbounds);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000541}
Johannes Doerfertaade7822019-06-05 03:02:24 +0000542
Johannes Doerfertece81902019-08-12 22:05:53 +0000543ChangeStatus AbstractAttribute::update(Attributor &A) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000544 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
545 if (getState().isAtFixpoint())
546 return HasChanged;
547
548 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
549
Johannes Doerfertece81902019-08-12 22:05:53 +0000550 HasChanged = updateImpl(A);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000551
552 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
553 << "\n");
554
555 return HasChanged;
556}
557
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000558ChangeStatus
Johannes Doerfertb2083c52019-10-20 22:46:48 -0500559IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000560 const ArrayRef<Attribute> &DeducedAttrs) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000561 Function *ScopeFn = IRP.getAssociatedFunction();
Kristina Brooks26e60f02019-08-06 19:53:19 +0000562 IRPosition::Kind PK = IRP.getPositionKind();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000563
Johannes Doerfertaade7822019-06-05 03:02:24 +0000564 // In the following some generic code that will manifest attributes in
565 // DeducedAttrs if they improve the current IR. Due to the different
566 // annotation positions we use the underlying AttributeList interface.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000567
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000568 AttributeList Attrs;
569 switch (PK) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000570 case IRPosition::IRP_INVALID:
571 case IRPosition::IRP_FLOAT:
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000572 return ChangeStatus::UNCHANGED;
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000573 case IRPosition::IRP_ARGUMENT:
574 case IRPosition::IRP_FUNCTION:
575 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000576 Attrs = ScopeFn->getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000577 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000578 case IRPosition::IRP_CALL_SITE:
579 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000580 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000581 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000582 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000583 }
584
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000585 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000586 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000587 for (const Attribute &Attr : DeducedAttrs) {
Kristina Brooks26e60f02019-08-06 19:53:19 +0000588 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000589 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000590
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000591 HasChanged = ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000592 }
593
594 if (HasChanged == ChangeStatus::UNCHANGED)
595 return HasChanged;
596
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000597 switch (PK) {
598 case IRPosition::IRP_ARGUMENT:
599 case IRPosition::IRP_FUNCTION:
600 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000601 ScopeFn->setAttributes(Attrs);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000602 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000603 case IRPosition::IRP_CALL_SITE:
604 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000605 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000606 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
Johannes Doerfert4395b312019-08-14 21:46:28 +0000607 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000608 case IRPosition::IRP_INVALID:
Johannes Doerfert4395b312019-08-14 21:46:28 +0000609 case IRPosition::IRP_FLOAT:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000610 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000611 }
612
613 return HasChanged;
614}
615
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000616const IRPosition IRPosition::EmptyKey(255);
617const IRPosition IRPosition::TombstoneKey(256);
618
619SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
620 IRPositions.emplace_back(IRP);
621
622 ImmutableCallSite ICS(&IRP.getAnchorValue());
623 switch (IRP.getPositionKind()) {
624 case IRPosition::IRP_INVALID:
625 case IRPosition::IRP_FLOAT:
626 case IRPosition::IRP_FUNCTION:
627 return;
628 case IRPosition::IRP_ARGUMENT:
629 case IRPosition::IRP_RETURNED:
630 IRPositions.emplace_back(
631 IRPosition::function(*IRP.getAssociatedFunction()));
632 return;
633 case IRPosition::IRP_CALL_SITE:
634 assert(ICS && "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 if (const Function *Callee = ICS.getCalledFunction())
639 IRPositions.emplace_back(IRPosition::function(*Callee));
640 return;
641 case IRPosition::IRP_CALL_SITE_RETURNED:
642 assert(ICS && "Expected call site!");
643 // TODO: We need to look at the operand bundles similar to the redirection
644 // in CallBase.
645 if (!ICS.hasOperandBundles()) {
646 if (const Function *Callee = ICS.getCalledFunction()) {
647 IRPositions.emplace_back(IRPosition::returned(*Callee));
648 IRPositions.emplace_back(IRPosition::function(*Callee));
Johannes Doerfertf8ad7352020-02-19 23:39:57 -0600649 for (const Argument &Arg : Callee->args())
650 if (Arg.hasReturnedAttr()) {
651 IRPositions.emplace_back(
652 IRPosition::callsite_argument(ICS, Arg.getArgNo()));
653 IRPositions.emplace_back(
654 IRPosition::value(*ICS.getArgOperand(Arg.getArgNo())));
655 IRPositions.emplace_back(IRPosition::argument(Arg));
656 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000657 }
658 }
659 IRPositions.emplace_back(
660 IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction())));
661 return;
662 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
663 int ArgNo = IRP.getArgNo();
664 assert(ICS && ArgNo >= 0 && "Expected call site!");
665 // TODO: We need to look at the operand bundles similar to the redirection
666 // in CallBase.
667 if (!ICS.hasOperandBundles()) {
668 const Function *Callee = ICS.getCalledFunction();
669 if (Callee && Callee->arg_size() > unsigned(ArgNo))
670 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
671 if (Callee)
672 IRPositions.emplace_back(IRPosition::function(*Callee));
673 }
674 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
675 return;
676 }
677 }
678}
679
Johannes Doerfert1097fab2019-10-07 21:07:57 +0000680bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
681 bool IgnoreSubsumingPositions) const {
Johannes Doerfert6185fb12020-02-20 02:02:57 -0600682 SmallVector<Attribute, 4> Attrs;
Johannes Doerfert1097fab2019-10-07 21:07:57 +0000683 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000684 for (Attribute::AttrKind AK : AKs)
Johannes Doerfert6185fb12020-02-20 02:02:57 -0600685 if (EquivIRP.getAttrsFromIRAttr(AK, Attrs))
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000686 return true;
Johannes Doerfert1097fab2019-10-07 21:07:57 +0000687 // The first position returned by the SubsumingPositionIterator is
688 // always the position itself. If we ignore subsuming positions we
689 // are done after the first iteration.
690 if (IgnoreSubsumingPositions)
691 break;
692 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000693 return false;
694}
695
696void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
Johannes Doerfert6abd01e2019-12-12 15:02:36 -0600697 SmallVectorImpl<Attribute> &Attrs,
698 bool IgnoreSubsumingPositions) const {
699 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
Johannes Doerfert6185fb12020-02-20 02:02:57 -0600700 for (Attribute::AttrKind AK : AKs)
701 EquivIRP.getAttrsFromIRAttr(AK, Attrs);
Johannes Doerfert6abd01e2019-12-12 15:02:36 -0600702 // The first position returned by the SubsumingPositionIterator is
703 // always the position itself. If we ignore subsuming positions we
704 // are done after the first iteration.
705 if (IgnoreSubsumingPositions)
706 break;
707 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000708}
709
Johannes Doerfert6185fb12020-02-20 02:02:57 -0600710bool IRPosition::getAttrsFromIRAttr(Attribute::AttrKind AK,
711 SmallVectorImpl<Attribute> &Attrs) const {
712 if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
713 return false;
714
715 AttributeList AttrList;
716 if (ImmutableCallSite ICS = ImmutableCallSite(&getAnchorValue()))
717 AttrList = ICS.getAttributes();
718 else
719 AttrList = getAssociatedFunction()->getAttributes();
720
721 bool HasAttr = AttrList.hasAttribute(getAttrIdx(), AK);
722 if (HasAttr)
723 Attrs.push_back(AttrList.getAttribute(getAttrIdx(), AK));
724 return HasAttr;
725}
726
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000727void IRPosition::verify() {
728 switch (KindOrArgNo) {
729 default:
730 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!");
731 assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) &&
732 "Expected call base or argument for positive attribute index!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000733 if (isa<Argument>(AnchorVal)) {
734 assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000735 "Argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000736 assert(cast<Argument>(AnchorVal) == &getAssociatedValue() &&
737 "Associated value mismatch!");
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000738 } else {
Simon Pilgrim920b0402019-08-29 10:08:45 +0000739 assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000740 "Call site argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000741 assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) ==
742 &getAssociatedValue() &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000743 "Associated value mismatch!");
744 }
745 break;
746 case IRP_INVALID:
747 assert(!AnchorVal && "Expected no value for an invalid position!");
748 break;
749 case IRP_FLOAT:
750 assert((!isa<CallBase>(&getAssociatedValue()) &&
751 !isa<Argument>(&getAssociatedValue())) &&
752 "Expected specialized kind for call base and argument values!");
753 break;
754 case IRP_RETURNED:
755 assert(isa<Function>(AnchorVal) &&
756 "Expected function for a 'returned' position!");
757 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
758 break;
759 case IRP_CALL_SITE_RETURNED:
760 assert((isa<CallBase>(AnchorVal)) &&
761 "Expected call base for 'call site returned' position!");
762 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
763 break;
764 case IRP_CALL_SITE:
765 assert((isa<CallBase>(AnchorVal)) &&
766 "Expected call base for 'call site function' position!");
767 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
768 break;
769 case IRP_FUNCTION:
770 assert(isa<Function>(AnchorVal) &&
771 "Expected function for a 'function' position!");
772 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
773 break;
774 }
775}
776
Benjamin Kramer564a9de2020-02-17 17:55:03 +0100777namespace {
778
Johannes Doerfert1a746452019-10-20 22:28:49 -0500779/// Helper function to clamp a state \p S of type \p StateType with the
Johannes Doerfert234eda52019-08-16 19:51:23 +0000780/// information in \p R and indicate/return if \p S did change (as-in update is
781/// required to be run again).
Johannes Doerfert234eda52019-08-16 19:51:23 +0000782template <typename StateType>
Johannes Doerfert1a746452019-10-20 22:28:49 -0500783ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
Johannes Doerfert234eda52019-08-16 19:51:23 +0000784 auto Assumed = S.getAssumed();
785 S ^= R;
786 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
787 : ChangeStatus::CHANGED;
788}
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000789
Johannes Doerfert234eda52019-08-16 19:51:23 +0000790/// Clamp the information known for all returned values of a function
791/// (identified by \p QueryingAA) into \p S.
792template <typename AAType, typename StateType = typename AAType::StateType>
793static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA,
794 StateType &S) {
795 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600796 << QueryingAA << " into " << S << "\n");
Johannes Doerfert234eda52019-08-16 19:51:23 +0000797
798 assert((QueryingAA.getIRPosition().getPositionKind() ==
799 IRPosition::IRP_RETURNED ||
800 QueryingAA.getIRPosition().getPositionKind() ==
801 IRPosition::IRP_CALL_SITE_RETURNED) &&
802 "Can only clamp returned value states for a function returned or call "
803 "site returned position!");
804
805 // Use an optional state as there might not be any return values and we want
806 // to join (IntegerState::operator&) the state of all there are.
807 Optional<StateType> T;
808
809 // Callback for each possibly returned value.
810 auto CheckReturnValue = [&](Value &RV) -> bool {
811 const IRPosition &RVPos = IRPosition::value(RV);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000812 const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos);
813 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
814 << " @ " << RVPos << "\n");
815 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000816 if (T.hasValue())
817 *T &= AAS;
818 else
819 T = AAS;
820 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
821 << "\n");
822 return T->isValidState();
823 };
824
825 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
826 S.indicatePessimisticFixpoint();
827 else if (T.hasValue())
828 S ^= *T;
829}
830
Hideto Ueno08daf8c2019-10-08 15:20:19 +0000831/// Helper class to compose two generic deduction
832template <typename AAType, typename Base, typename StateType,
833 template <typename...> class F, template <typename...> class G>
834struct AAComposeTwoGenericDeduction
835 : public F<AAType, G<AAType, Base, StateType>, StateType> {
836 AAComposeTwoGenericDeduction(const IRPosition &IRP)
837 : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {}
838
Johannes Doerfertd95cb562020-02-20 02:03:32 -0600839 void initialize(Attributor &A) override {
840 F<AAType, G<AAType, Base, StateType>, StateType>::initialize(A);
841 G<AAType, Base, StateType>::initialize(A);
842 }
843
Hideto Ueno08daf8c2019-10-08 15:20:19 +0000844 /// See AbstractAttribute::updateImpl(...).
845 ChangeStatus updateImpl(Attributor &A) override {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +0100846 ChangeStatus ChangedF =
847 F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A);
Johannes Doerfertdb6efb02019-10-13 20:40:10 +0000848 ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A);
849 return ChangedF | ChangedG;
Hideto Ueno08daf8c2019-10-08 15:20:19 +0000850 }
851};
852
Johannes Doerfert234eda52019-08-16 19:51:23 +0000853/// Helper class for generic deduction: return value -> returned position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000854template <typename AAType, typename Base,
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600855 typename StateType = typename Base::StateType>
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000856struct AAReturnedFromReturnedValues : public Base {
857 AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000858
859 /// See AbstractAttribute::updateImpl(...).
860 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600861 StateType S(StateType::getBestState(this->getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000862 clampReturnedValueStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000863 // TODO: If we know we visited all returned values, thus no are assumed
864 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000865 return clampStateAndIndicateChange<StateType>(this->getState(), S);
866 }
867};
868
869/// Clamp the information known at all call sites for a given argument
870/// (identified by \p QueryingAA) into \p S.
871template <typename AAType, typename StateType = typename AAType::StateType>
872static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
873 StateType &S) {
874 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600875 << QueryingAA << " into " << S << "\n");
Johannes Doerfert234eda52019-08-16 19:51:23 +0000876
877 assert(QueryingAA.getIRPosition().getPositionKind() ==
878 IRPosition::IRP_ARGUMENT &&
879 "Can only clamp call site argument states for an argument position!");
880
881 // Use an optional state as there might not be any return values and we want
882 // to join (IntegerState::operator&) the state of all there are.
883 Optional<StateType> T;
884
885 // The argument number which is also the call site argument number.
886 unsigned ArgNo = QueryingAA.getIRPosition().getArgNo();
887
Johannes Doerfert661db042019-10-07 23:14:58 +0000888 auto CallSiteCheck = [&](AbstractCallSite ACS) {
889 const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
890 // Check if a coresponding argument was found or if it is on not associated
891 // (which can happen for callback calls).
892 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
893 return false;
894
895 const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos);
896 LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
897 << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000898 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000899 if (T.hasValue())
900 *T &= AAS;
901 else
902 T = AAS;
903 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
904 << "\n");
905 return T->isValidState();
906 };
907
Johannes Doerfert368f7ee2019-12-30 16:12:36 -0600908 bool AllCallSitesKnown;
909 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
910 AllCallSitesKnown))
Johannes Doerfert234eda52019-08-16 19:51:23 +0000911 S.indicatePessimisticFixpoint();
912 else if (T.hasValue())
913 S ^= *T;
914}
915
916/// Helper class for generic deduction: call site argument -> argument position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000917template <typename AAType, typename Base,
918 typename StateType = typename AAType::StateType>
919struct AAArgumentFromCallSiteArguments : public Base {
920 AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000921
922 /// See AbstractAttribute::updateImpl(...).
923 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertea5fabe2020-01-28 09:46:15 -0600924 StateType S(StateType::getBestState(this->getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000925 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000926 // TODO: If we know we visited all incoming values, thus no are assumed
927 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000928 return clampStateAndIndicateChange<StateType>(this->getState(), S);
929 }
930};
931
932/// Helper class for generic replication: function returned -> cs returned.
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000933template <typename AAType, typename Base,
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600934 typename StateType = typename Base::StateType>
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000935struct AACallSiteReturnedFromReturned : public Base {
936 AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000937
938 /// See AbstractAttribute::updateImpl(...).
939 ChangeStatus updateImpl(Attributor &A) override {
940 assert(this->getIRPosition().getPositionKind() ==
941 IRPosition::IRP_CALL_SITE_RETURNED &&
942 "Can only wrap function returned positions for call site returned "
943 "positions!");
944 auto &S = this->getState();
945
946 const Function *AssociatedFunction =
947 this->getIRPosition().getAssociatedFunction();
948 if (!AssociatedFunction)
949 return S.indicatePessimisticFixpoint();
950
951 IRPosition FnPos = IRPosition::returned(*AssociatedFunction);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000952 const AAType &AA = A.getAAFor<AAType>(*this, FnPos);
Johannes Doerfert234eda52019-08-16 19:51:23 +0000953 return clampStateAndIndicateChange(
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600954 S, static_cast<const StateType &>(AA.getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000955 }
956};
957
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000958/// Helper class for generic deduction using must-be-executed-context
959/// Base class is required to have `followUse` method.
960
961/// bool followUse(Attributor &A, const Use *U, const Instruction *I)
Simon Pilgrimf7aee612019-10-10 15:25:16 +0000962/// U - Underlying use.
963/// I - The user of the \p U.
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000964/// `followUse` returns true if the value should be tracked transitively.
965
966template <typename AAType, typename Base,
967 typename StateType = typename AAType::StateType>
968struct AAFromMustBeExecutedContext : public Base {
969 AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {}
970
971 void initialize(Attributor &A) override {
972 Base::initialize(A);
Johannes Doerfertb2083c52019-10-20 22:46:48 -0500973 const IRPosition &IRP = this->getIRPosition();
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000974 Instruction *CtxI = IRP.getCtxI();
975
976 if (!CtxI)
977 return;
978
979 for (const Use &U : IRP.getAssociatedValue().uses())
980 Uses.insert(&U);
981 }
982
Hideto Uenobdcbdb42020-03-09 14:19:32 +0900983 /// Helper function to accumulate uses.
984 void followUsesInContext(Attributor &A,
985 MustBeExecutedContextExplorer &Explorer,
986 const Instruction *CtxI,
987 SetVector<const Use *> &Uses, StateType &State) {
988 auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
989 for (unsigned u = 0; u < Uses.size(); ++u) {
990 const Use *U = Uses[u];
991 if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
992 bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
993 if (Found && Base::followUse(A, U, UserI, State))
994 for (const Use &Us : UserI->uses())
995 Uses.insert(&Us);
996 }
997 }
998 }
999
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001000 /// See AbstractAttribute::updateImpl(...).
1001 ChangeStatus updateImpl(Attributor &A) override {
1002 auto BeforeState = this->getState();
1003 auto &S = this->getState();
1004 Instruction *CtxI = this->getIRPosition().getCtxI();
1005 if (!CtxI)
1006 return ChangeStatus::UNCHANGED;
1007
1008 MustBeExecutedContextExplorer &Explorer =
1009 A.getInfoCache().getMustBeExecutedContextExplorer();
1010
Hideto Uenobdcbdb42020-03-09 14:19:32 +09001011 followUsesInContext(A, Explorer, CtxI, Uses, S);
1012
1013 if (this->isAtFixpoint())
1014 return ChangeStatus::CHANGED;
1015
1016 SmallVector<const BranchInst *, 4> BrInsts;
1017 auto Pred = [&](const Instruction *I) {
1018 if (const BranchInst *Br = dyn_cast<BranchInst>(I))
1019 if (Br->isConditional())
1020 BrInsts.push_back(Br);
1021 return true;
1022 };
1023
1024 // Here, accumulate conditional branch instructions in the context. We
1025 // explore the child paths and collect the known states. The disjunction of
1026 // those states can be merged to its own state. Let ParentState_i be a state
1027 // to indicate the known information for an i-th branch instruction in the
1028 // context. ChildStates are created for its successors respectively.
1029 //
1030 // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1}
1031 // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2}
1032 // ...
1033 // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
1034 //
1035 // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
1036 //
1037 // FIXME: Currently, recursive branches are not handled. For example, we
1038 // can't deduce that ptr must be dereferenced in below function.
1039 //
1040 // void f(int a, int c, int *ptr) {
1041 // if(a)
1042 // if (b) {
1043 // *ptr = 0;
1044 // } else {
1045 // *ptr = 1;
1046 // }
1047 // else {
1048 // if (b) {
1049 // *ptr = 0;
1050 // } else {
1051 // *ptr = 1;
1052 // }
1053 // }
1054 // }
1055
1056 Explorer.checkForAllContext(CtxI, Pred);
1057 for (const BranchInst *Br : BrInsts) {
1058 StateType ParentState;
1059
1060 // The known state of the parent state is a conjunction of children's
1061 // known states so it is initialized with a best state.
1062 ParentState.indicateOptimisticFixpoint();
1063
1064 for (const BasicBlock *BB : Br->successors()) {
1065 StateType ChildState;
1066
1067 size_t BeforeSize = Uses.size();
1068 followUsesInContext(A, Explorer, &BB->front(), Uses, ChildState);
1069
1070 // Erase uses which only appear in the child.
1071 for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
1072 It = Uses.erase(It);
1073
1074 ParentState &= ChildState;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001075 }
Hideto Uenobdcbdb42020-03-09 14:19:32 +09001076
1077 // Use only known state.
1078 S += ParentState;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001079 }
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001080
1081 return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
1082 }
1083
1084private:
1085 /// Container for (transitive) uses of the associated value.
1086 SetVector<const Use *> Uses;
1087};
1088
1089template <typename AAType, typename Base,
1090 typename StateType = typename AAType::StateType>
1091using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext =
1092 AAComposeTwoGenericDeduction<AAType, Base, StateType,
1093 AAFromMustBeExecutedContext,
1094 AAArgumentFromCallSiteArguments>;
1095
1096template <typename AAType, typename Base,
1097 typename StateType = typename AAType::StateType>
1098using AACallSiteReturnedFromReturnedAndMustBeExecutedContext =
1099 AAComposeTwoGenericDeduction<AAType, Base, StateType,
1100 AAFromMustBeExecutedContext,
1101 AACallSiteReturnedFromReturned>;
1102
Stefan Stipanovic53605892019-06-27 11:27:54 +00001103/// -----------------------NoUnwind Function Attribute--------------------------
1104
Johannes Doerfert344d0382019-08-07 22:34:26 +00001105struct AANoUnwindImpl : AANoUnwind {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001106 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
Stefan Stipanovic53605892019-06-27 11:27:54 +00001107
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001108 const std::string getAsStr() const override {
Stefan Stipanovic53605892019-06-27 11:27:54 +00001109 return getAssumed() ? "nounwind" : "may-unwind";
1110 }
1111
1112 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001113 ChangeStatus updateImpl(Attributor &A) override {
1114 auto Opcodes = {
1115 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
1116 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
1117 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
1118
1119 auto CheckForNoUnwind = [&](Instruction &I) {
1120 if (!I.mayThrow())
1121 return true;
1122
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001123 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1124 const auto &NoUnwindAA =
1125 A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS));
1126 return NoUnwindAA.isAssumedNoUnwind();
1127 }
1128 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001129 };
1130
1131 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
1132 return indicatePessimisticFixpoint();
1133
1134 return ChangeStatus::UNCHANGED;
1135 }
Stefan Stipanovic53605892019-06-27 11:27:54 +00001136};
1137
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001138struct AANoUnwindFunction final : public AANoUnwindImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001139 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001140
1141 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001142 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001143};
1144
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001145/// NoUnwind attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001146struct AANoUnwindCallSite final : AANoUnwindImpl {
1147 AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
1148
1149 /// See AbstractAttribute::initialize(...).
1150 void initialize(Attributor &A) override {
1151 AANoUnwindImpl::initialize(A);
1152 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001153 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001154 indicatePessimisticFixpoint();
1155 }
1156
1157 /// See AbstractAttribute::updateImpl(...).
1158 ChangeStatus updateImpl(Attributor &A) override {
1159 // TODO: Once we have call site specific value information we can provide
1160 // call site specific liveness information and then it makes
1161 // sense to specialize attributes for call sites arguments instead of
1162 // redirecting requests to the callee argument.
1163 Function *F = getAssociatedFunction();
1164 const IRPosition &FnPos = IRPosition::function(*F);
1165 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos);
1166 return clampStateAndIndicateChange(
1167 getState(),
1168 static_cast<const AANoUnwind::StateType &>(FnAA.getState()));
1169 }
1170
1171 /// See AbstractAttribute::trackStatistics()
1172 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
1173};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001174
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001175/// --------------------- Function Return Values -------------------------------
1176
1177/// "Attribute" that collects all potential returned values and the return
1178/// instructions that they arise from.
1179///
1180/// If there is a unique returned value R, the manifest method will:
1181/// - mark R with the "returned" attribute, if R is an argument.
Johannes Doerferteccdf082019-08-05 23:35:12 +00001182class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001183
1184 /// Mapping of values potentially returned by the associated function to the
1185 /// return instructions that might return them.
Johannes Doerferta4a308c2019-08-26 17:51:23 +00001186 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001187
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001188 /// Mapping to remember the number of returned values for a call site such
1189 /// that we can avoid updates if nothing changed.
1190 DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
1191
1192 /// Set of unresolved calls returned by the associated function.
Johannes Doerfert695089e2019-08-23 15:23:49 +00001193 SmallSetVector<CallBase *, 4> UnresolvedCalls;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001194
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001195 /// State flags
1196 ///
1197 ///{
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001198 bool IsFixed = false;
1199 bool IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001200 ///}
1201
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001202public:
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001203 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001204
1205 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001206 void initialize(Attributor &A) override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001207 // Reset the state.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001208 IsFixed = false;
1209 IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001210 ReturnedValues.clear();
1211
Johannes Doerfertdef99282019-08-14 21:29:37 +00001212 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001213 if (!F) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001214 indicatePessimisticFixpoint();
1215 return;
1216 }
Johannes Doerferte273ac42020-01-12 00:13:03 -06001217 assert(!F->getReturnType()->isVoidTy() &&
1218 "Did not expect a void return type!");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001219
1220 // The map from instruction opcodes to those instructions in the function.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001221 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001222
1223 // Look through all arguments, if one is marked as returned we are done.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001224 for (Argument &Arg : F->args()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001225 if (Arg.hasReturnedAttr()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001226 auto &ReturnInstSet = ReturnedValues[&Arg];
1227 for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
1228 ReturnInstSet.insert(cast<ReturnInst>(RI));
1229
1230 indicateOptimisticFixpoint();
1231 return;
1232 }
1233 }
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001234
Johannes Doerferta198adb2020-03-13 00:32:38 -05001235 if (!A.isFunctionIPOAmendable(*F))
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001236 indicatePessimisticFixpoint();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001237 }
1238
1239 /// See AbstractAttribute::manifest(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001240 ChangeStatus manifest(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001241
1242 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001243 AbstractState &getState() override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001244
1245 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001246 const AbstractState &getState() const override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001247
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001248 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00001249 ChangeStatus updateImpl(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001250
Johannes Doerfertdef99282019-08-14 21:29:37 +00001251 llvm::iterator_range<iterator> returned_values() override {
1252 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1253 }
1254
1255 llvm::iterator_range<const_iterator> returned_values() const override {
1256 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1257 }
1258
Johannes Doerfert695089e2019-08-23 15:23:49 +00001259 const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001260 return UnresolvedCalls;
1261 }
1262
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001263 /// Return the number of potential return values, -1 if unknown.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001264 size_t getNumReturnValues() const override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001265 return isValidState() ? ReturnedValues.size() : -1;
1266 }
1267
1268 /// Return an assumed unique return value if a single candidate is found. If
1269 /// there cannot be one, return a nullptr. If it is not clear yet, return the
1270 /// Optional::NoneType.
Johannes Doerfert14a04932019-08-07 22:27:24 +00001271 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001272
Johannes Doerfert14a04932019-08-07 22:27:24 +00001273 /// See AbstractState::checkForAllReturnedValues(...).
1274 bool checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00001275 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00001276 &Pred) const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001277
1278 /// Pretty print the attribute similar to the IR representation.
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001279 const std::string getAsStr() const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001280
1281 /// See AbstractState::isAtFixpoint().
1282 bool isAtFixpoint() const override { return IsFixed; }
1283
1284 /// See AbstractState::isValidState().
1285 bool isValidState() const override { return IsValidState; }
1286
1287 /// See AbstractState::indicateOptimisticFixpoint(...).
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001288 ChangeStatus indicateOptimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001289 IsFixed = true;
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001290 return ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001291 }
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001292
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001293 ChangeStatus indicatePessimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001294 IsFixed = true;
1295 IsValidState = false;
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001296 return ChangeStatus::CHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001297 }
1298};
1299
1300ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
1301 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1302
1303 // Bookkeeping.
1304 assert(isValidState());
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001305 STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
1306 "Number of function with known return values");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001307
1308 // Check if we have an assumed unique return value that we could manifest.
Johannes Doerfert14a04932019-08-07 22:27:24 +00001309 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001310
1311 if (!UniqueRV.hasValue() || !UniqueRV.getValue())
1312 return Changed;
1313
1314 // Bookkeeping.
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001315 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
1316 "Number of function with unique return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001317
Johannes Doerfert23400e612019-08-23 17:41:37 +00001318 // Callback to replace the uses of CB with the constant C.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06001319 auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) {
Johannes Doerfert54ec9b52020-03-16 20:23:27 -05001320 if (CB.getNumUses() == 0)
Johannes Doerfert23400e612019-08-23 17:41:37 +00001321 return ChangeStatus::UNCHANGED;
Johannes Doerfert4c62a352020-01-12 00:17:08 -06001322 if (A.changeValueAfterManifest(CB, C))
1323 return ChangeStatus::CHANGED;
1324 return ChangeStatus::UNCHANGED;
Johannes Doerfert23400e612019-08-23 17:41:37 +00001325 };
1326
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001327 // If the assumed unique return value is an argument, annotate it.
1328 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
Johannes Doerfertb2083c52019-10-20 22:46:48 -05001329 // TODO: This should be handled differently!
1330 this->AnchorVal = UniqueRVArg;
1331 this->KindOrArgNo = UniqueRVArg->getArgNo();
Johannes Doerfert23400e612019-08-23 17:41:37 +00001332 Changed = IRAttribute::manifest(A);
1333 } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
1334 // We can replace the returned value with the unique returned constant.
1335 Value &AnchorValue = getAnchorValue();
1336 if (Function *F = dyn_cast<Function>(&AnchorValue)) {
1337 for (const Use &U : F->uses())
1338 if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001339 if (CB->isCallee(&U)) {
1340 Constant *RVCCast =
Johannes Doerfert07d16422019-11-01 23:03:35 -05001341 CB->getType() == RVC->getType()
1342 ? RVC
1343 : ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001344 Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
1345 }
Johannes Doerfert23400e612019-08-23 17:41:37 +00001346 } else {
1347 assert(isa<CallBase>(AnchorValue) &&
1348 "Expcected a function or call base anchor!");
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001349 Constant *RVCCast =
Johannes Doerfert07d16422019-11-01 23:03:35 -05001350 AnchorValue.getType() == RVC->getType()
1351 ? RVC
1352 : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001353 Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
Johannes Doerfert23400e612019-08-23 17:41:37 +00001354 }
1355 if (Changed == ChangeStatus::CHANGED)
1356 STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
1357 "Number of function returns replaced by constant return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001358 }
1359
1360 return Changed;
1361}
1362
1363const std::string AAReturnedValuesImpl::getAsStr() const {
1364 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
Johannes Doerfert6471bb62019-08-04 18:39:28 +00001365 (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
Johannes Doerfertdef99282019-08-14 21:29:37 +00001366 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001367}
1368
Johannes Doerfert14a04932019-08-07 22:27:24 +00001369Optional<Value *>
1370AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1371 // If checkForAllReturnedValues provides a unique value, ignoring potential
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001372 // undef values that can also be present, it is assumed to be the actual
1373 // return value and forwarded to the caller of this method. If there are
1374 // multiple, a nullptr is returned indicating there cannot be a unique
1375 // returned value.
1376 Optional<Value *> UniqueRV;
1377
Johannes Doerfert14a04932019-08-07 22:27:24 +00001378 auto Pred = [&](Value &RV) -> bool {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001379 // If we found a second returned value and neither the current nor the saved
1380 // one is an undef, there is no unique returned value. Undefs are special
1381 // since we can pretend they have any value.
1382 if (UniqueRV.hasValue() && UniqueRV != &RV &&
1383 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
1384 UniqueRV = nullptr;
1385 return false;
1386 }
1387
1388 // Do not overwrite a value with an undef.
1389 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
1390 UniqueRV = &RV;
1391
1392 return true;
1393 };
1394
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001395 if (!A.checkForAllReturnedValues(Pred, *this))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001396 UniqueRV = nullptr;
1397
1398 return UniqueRV;
1399}
1400
Johannes Doerfert14a04932019-08-07 22:27:24 +00001401bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00001402 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00001403 &Pred) const {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001404 if (!isValidState())
1405 return false;
1406
1407 // Check all returned values but ignore call sites as long as we have not
1408 // encountered an overdefined one during an update.
1409 for (auto &It : ReturnedValues) {
1410 Value *RV = It.first;
1411
Johannes Doerfertdef99282019-08-14 21:29:37 +00001412 CallBase *CB = dyn_cast<CallBase>(RV);
1413 if (CB && !UnresolvedCalls.count(CB))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001414 continue;
1415
Johannes Doerfert695089e2019-08-23 15:23:49 +00001416 if (!Pred(*RV, It.second))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001417 return false;
1418 }
1419
1420 return true;
1421}
1422
Johannes Doerfertece81902019-08-12 22:05:53 +00001423ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001424 size_t NumUnresolvedCalls = UnresolvedCalls.size();
1425 bool Changed = false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001426
Johannes Doerfertdef99282019-08-14 21:29:37 +00001427 // State used in the value traversals starting in returned values.
1428 struct RVState {
1429 // The map in which we collect return values -> return instrs.
1430 decltype(ReturnedValues) &RetValsMap;
1431 // The flag to indicate a change.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001432 bool &Changed;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001433 // The return instrs we come from.
Johannes Doerfert695089e2019-08-23 15:23:49 +00001434 SmallSetVector<ReturnInst *, 4> RetInsts;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001435 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001436
Johannes Doerfertdef99282019-08-14 21:29:37 +00001437 // Callback for a leaf value returned by the associated function.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001438 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001439 auto Size = RVS.RetValsMap[&Val].size();
1440 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
1441 bool Inserted = RVS.RetValsMap[&Val].size() != Size;
1442 RVS.Changed |= Inserted;
1443 LLVM_DEBUG({
1444 if (Inserted)
1445 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
1446 << " => " << RVS.RetInsts.size() << "\n";
1447 });
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001448 return true;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001449 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001450
Johannes Doerfertdef99282019-08-14 21:29:37 +00001451 // Helper method to invoke the generic value traversal.
1452 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) {
1453 IRPosition RetValPos = IRPosition::value(RV);
1454 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this,
1455 RVS, VisitValueCB);
1456 };
Johannes Doerfertda4d8112019-08-01 16:21:54 +00001457
Johannes Doerfertdef99282019-08-14 21:29:37 +00001458 // Callback for all "return intructions" live in the associated function.
1459 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1460 ReturnInst &Ret = cast<ReturnInst>(I);
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001461 RVState RVS({ReturnedValues, Changed, {}});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001462 RVS.RetInsts.insert(&Ret);
Johannes Doerfertdef99282019-08-14 21:29:37 +00001463 return VisitReturnedValue(*Ret.getReturnValue(), RVS);
1464 };
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001465
Johannes Doerfertdef99282019-08-14 21:29:37 +00001466 // Start by discovering returned values from all live returned instructions in
1467 // the associated function.
1468 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1469 return indicatePessimisticFixpoint();
1470
1471 // Once returned values "directly" present in the code are handled we try to
1472 // resolve returned calls.
1473 decltype(ReturnedValues) NewRVsMap;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001474 for (auto &It : ReturnedValues) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001475 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first
1476 << " by #" << It.second.size() << " RIs\n");
1477 CallBase *CB = dyn_cast<CallBase>(It.first);
1478 if (!CB || UnresolvedCalls.count(CB))
1479 continue;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001480
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001481 if (!CB->getCalledFunction()) {
1482 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1483 << "\n");
1484 UnresolvedCalls.insert(CB);
1485 continue;
1486 }
1487
1488 // TODO: use the function scope once we have call site AAReturnedValues.
1489 const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1490 *this, IRPosition::function(*CB->getCalledFunction()));
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001491 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
Johannes Doerfert3d347e22019-12-13 23:35:45 -06001492 << RetValAA << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001493
1494 // Skip dead ends, thus if we do not know anything about the returned
1495 // call we mark it as unresolved and it will stay that way.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001496 if (!RetValAA.getState().isValidState()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001497 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1498 << "\n");
1499 UnresolvedCalls.insert(CB);
1500 continue;
1501 }
1502
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001503 // Do not try to learn partial information. If the callee has unresolved
1504 // return values we will treat the call as unresolved/opaque.
1505 auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1506 if (!RetValAAUnresolvedCalls.empty()) {
1507 UnresolvedCalls.insert(CB);
1508 continue;
1509 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001510
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001511 // Now check if we can track transitively returned values. If possible, thus
1512 // if all return value can be represented in the current scope, do so.
1513 bool Unresolved = false;
1514 for (auto &RetValAAIt : RetValAA.returned_values()) {
1515 Value *RetVal = RetValAAIt.first;
1516 if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1517 isa<Constant>(RetVal))
1518 continue;
1519 // Anything that did not fit in the above categories cannot be resolved,
1520 // mark the call as unresolved.
1521 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1522 "cannot be translated: "
1523 << *RetVal << "\n");
1524 UnresolvedCalls.insert(CB);
1525 Unresolved = true;
1526 break;
1527 }
1528
1529 if (Unresolved)
1530 continue;
1531
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001532 // Now track transitively returned values.
1533 unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1534 if (NumRetAA == RetValAA.getNumReturnValues()) {
1535 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1536 "changed since it was seen last\n");
1537 continue;
1538 }
1539 NumRetAA = RetValAA.getNumReturnValues();
1540
Johannes Doerfertdef99282019-08-14 21:29:37 +00001541 for (auto &RetValAAIt : RetValAA.returned_values()) {
1542 Value *RetVal = RetValAAIt.first;
1543 if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1544 // Arguments are mapped to call site operands and we begin the traversal
1545 // again.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001546 bool Unused = false;
1547 RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001548 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS);
1549 continue;
1550 } else if (isa<CallBase>(RetVal)) {
1551 // Call sites are resolved by the callee attribute over time, no need to
1552 // do anything for us.
1553 continue;
1554 } else if (isa<Constant>(RetVal)) {
1555 // Constants are valid everywhere, we can simply take them.
1556 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end());
1557 continue;
1558 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00001559 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001560 }
1561
Johannes Doerfertdef99282019-08-14 21:29:37 +00001562 // To avoid modifications to the ReturnedValues map while we iterate over it
1563 // we kept record of potential new entries in a copy map, NewRVsMap.
1564 for (auto &It : NewRVsMap) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001565 assert(!It.second.empty() && "Entry does not add anything.");
1566 auto &ReturnInsts = ReturnedValues[It.first];
1567 for (ReturnInst *RI : It.second)
Johannes Doerfert695089e2019-08-23 15:23:49 +00001568 if (ReturnInsts.insert(RI)) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001569 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1570 << *It.first << " => " << *RI << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001571 Changed = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001572 }
1573 }
1574
Johannes Doerfertdef99282019-08-14 21:29:37 +00001575 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1576 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001577}
1578
Johannes Doerfertdef99282019-08-14 21:29:37 +00001579struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1580 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1581
1582 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001583 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
Johannes Doerfertdef99282019-08-14 21:29:37 +00001584};
1585
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001586/// Returned values information for a call sites.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001587struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1588 AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1589
1590 /// See AbstractAttribute::initialize(...).
1591 void initialize(Attributor &A) override {
1592 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001593 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001594 // sense to specialize attributes for call sites instead of
1595 // redirecting requests to the callee.
1596 llvm_unreachable("Abstract attributes for returned values are not "
1597 "supported for call sites yet!");
1598 }
1599
1600 /// See AbstractAttribute::updateImpl(...).
1601 ChangeStatus updateImpl(Attributor &A) override {
1602 return indicatePessimisticFixpoint();
1603 }
1604
1605 /// See AbstractAttribute::trackStatistics()
1606 void trackStatistics() const override {}
1607};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001608
Stefan Stipanovic06263672019-07-11 21:37:40 +00001609/// ------------------------ NoSync Function Attribute -------------------------
1610
Johannes Doerfert344d0382019-08-07 22:34:26 +00001611struct AANoSyncImpl : AANoSync {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001612 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
Stefan Stipanovic06263672019-07-11 21:37:40 +00001613
Stefan Stipanoviccb5ecae2019-07-12 18:34:06 +00001614 const std::string getAsStr() const override {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001615 return getAssumed() ? "nosync" : "may-sync";
1616 }
1617
1618 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001619 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001620
Stefan Stipanovic06263672019-07-11 21:37:40 +00001621 /// Helper function used to determine whether an instruction is non-relaxed
1622 /// atomic. In other words, if an atomic instruction does not have unordered
1623 /// or monotonic ordering
1624 static bool isNonRelaxedAtomic(Instruction *I);
1625
1626 /// Helper function used to determine whether an instruction is volatile.
1627 static bool isVolatile(Instruction *I);
1628
Johannes Doerfertc7a1db32019-07-13 01:09:27 +00001629 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1630 /// memset).
Stefan Stipanovic06263672019-07-11 21:37:40 +00001631 static bool isNoSyncIntrinsic(Instruction *I);
1632};
1633
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001634bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001635 if (!I->isAtomic())
1636 return false;
1637
1638 AtomicOrdering Ordering;
1639 switch (I->getOpcode()) {
1640 case Instruction::AtomicRMW:
1641 Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1642 break;
1643 case Instruction::Store:
1644 Ordering = cast<StoreInst>(I)->getOrdering();
1645 break;
1646 case Instruction::Load:
1647 Ordering = cast<LoadInst>(I)->getOrdering();
1648 break;
1649 case Instruction::Fence: {
1650 auto *FI = cast<FenceInst>(I);
1651 if (FI->getSyncScopeID() == SyncScope::SingleThread)
1652 return false;
1653 Ordering = FI->getOrdering();
1654 break;
1655 }
1656 case Instruction::AtomicCmpXchg: {
1657 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1658 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1659 // Only if both are relaxed, than it can be treated as relaxed.
1660 // Otherwise it is non-relaxed.
1661 if (Success != AtomicOrdering::Unordered &&
1662 Success != AtomicOrdering::Monotonic)
1663 return true;
1664 if (Failure != AtomicOrdering::Unordered &&
1665 Failure != AtomicOrdering::Monotonic)
1666 return true;
1667 return false;
1668 }
1669 default:
1670 llvm_unreachable(
1671 "New atomic operations need to be known in the attributor.");
1672 }
1673
1674 // Relaxed.
1675 if (Ordering == AtomicOrdering::Unordered ||
1676 Ordering == AtomicOrdering::Monotonic)
1677 return false;
1678 return true;
1679}
1680
1681/// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1682/// FIXME: We should ipmrove the handling of intrinsics.
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001683bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001684 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1685 switch (II->getIntrinsicID()) {
1686 /// Element wise atomic memory intrinsics are can only be unordered,
1687 /// therefore nosync.
1688 case Intrinsic::memset_element_unordered_atomic:
1689 case Intrinsic::memmove_element_unordered_atomic:
1690 case Intrinsic::memcpy_element_unordered_atomic:
1691 return true;
1692 case Intrinsic::memset:
1693 case Intrinsic::memmove:
1694 case Intrinsic::memcpy:
1695 if (!cast<MemIntrinsic>(II)->isVolatile())
1696 return true;
1697 return false;
1698 default:
1699 return false;
1700 }
1701 }
1702 return false;
1703}
1704
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001705bool AANoSyncImpl::isVolatile(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001706 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
1707 "Calls should not be checked here");
1708
1709 switch (I->getOpcode()) {
1710 case Instruction::AtomicRMW:
1711 return cast<AtomicRMWInst>(I)->isVolatile();
1712 case Instruction::Store:
1713 return cast<StoreInst>(I)->isVolatile();
1714 case Instruction::Load:
1715 return cast<LoadInst>(I)->isVolatile();
1716 case Instruction::AtomicCmpXchg:
1717 return cast<AtomicCmpXchgInst>(I)->isVolatile();
1718 default:
1719 return false;
1720 }
1721}
1722
Johannes Doerfertece81902019-08-12 22:05:53 +00001723ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001724
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001725 auto CheckRWInstForNoSync = [&](Instruction &I) {
1726 /// We are looking for volatile instructions or Non-Relaxed atomics.
Pankaj Godeb9a26a82019-11-22 14:46:43 +05301727 /// FIXME: We should improve the handling of intrinsics.
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001728
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001729 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1730 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001731
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001732 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1733 if (ICS.hasFnAttr(Attribute::NoSync))
1734 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001735
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001736 const auto &NoSyncAA =
1737 A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS));
1738 if (NoSyncAA.isAssumedNoSync())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001739 return true;
1740 return false;
1741 }
Stefan Stipanovic06263672019-07-11 21:37:40 +00001742
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001743 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1744 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001745
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001746 return false;
1747 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001748
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001749 auto CheckForNoSync = [&](Instruction &I) {
1750 // At this point we handled all read/write effects and they are all
1751 // nosync, so they can be skipped.
1752 if (I.mayReadOrWriteMemory())
1753 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001754
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001755 // non-convergent and readnone imply nosync.
1756 return !ImmutableCallSite(&I).isConvergent();
1757 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001758
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001759 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1760 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001761 return indicatePessimisticFixpoint();
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001762
Stefan Stipanovic06263672019-07-11 21:37:40 +00001763 return ChangeStatus::UNCHANGED;
1764}
1765
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001766struct AANoSyncFunction final : public AANoSyncImpl {
1767 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1768
1769 /// See AbstractAttribute::trackStatistics()
1770 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1771};
1772
1773/// NoSync attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001774struct AANoSyncCallSite final : AANoSyncImpl {
1775 AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1776
1777 /// See AbstractAttribute::initialize(...).
1778 void initialize(Attributor &A) override {
1779 AANoSyncImpl::initialize(A);
1780 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001781 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001782 indicatePessimisticFixpoint();
1783 }
1784
1785 /// See AbstractAttribute::updateImpl(...).
1786 ChangeStatus updateImpl(Attributor &A) override {
1787 // TODO: Once we have call site specific value information we can provide
1788 // call site specific liveness information and then it makes
1789 // sense to specialize attributes for call sites arguments instead of
1790 // redirecting requests to the callee argument.
1791 Function *F = getAssociatedFunction();
1792 const IRPosition &FnPos = IRPosition::function(*F);
1793 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos);
1794 return clampStateAndIndicateChange(
1795 getState(), static_cast<const AANoSync::StateType &>(FnAA.getState()));
1796 }
1797
1798 /// See AbstractAttribute::trackStatistics()
1799 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1800};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001801
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001802/// ------------------------ No-Free Attributes ----------------------------
1803
Johannes Doerfert344d0382019-08-07 22:34:26 +00001804struct AANoFreeImpl : public AANoFree {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001805 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001806
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001807 /// See AbstractAttribute::updateImpl(...).
1808 ChangeStatus updateImpl(Attributor &A) override {
1809 auto CheckForNoFree = [&](Instruction &I) {
1810 ImmutableCallSite ICS(&I);
1811 if (ICS.hasFnAttr(Attribute::NoFree))
1812 return true;
1813
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001814 const auto &NoFreeAA =
1815 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS));
1816 return NoFreeAA.isAssumedNoFree();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001817 };
1818
1819 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1820 return indicatePessimisticFixpoint();
1821 return ChangeStatus::UNCHANGED;
1822 }
1823
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001824 /// See AbstractAttribute::getAsStr().
1825 const std::string getAsStr() const override {
1826 return getAssumed() ? "nofree" : "may-free";
1827 }
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001828};
1829
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001830struct AANoFreeFunction final : public AANoFreeImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001831 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001832
1833 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001834 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001835};
1836
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001837/// NoFree attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001838struct AANoFreeCallSite final : AANoFreeImpl {
1839 AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1840
1841 /// See AbstractAttribute::initialize(...).
1842 void initialize(Attributor &A) override {
1843 AANoFreeImpl::initialize(A);
1844 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001845 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001846 indicatePessimisticFixpoint();
1847 }
1848
1849 /// See AbstractAttribute::updateImpl(...).
1850 ChangeStatus updateImpl(Attributor &A) override {
1851 // TODO: Once we have call site specific value information we can provide
1852 // call site specific liveness information and then it makes
1853 // sense to specialize attributes for call sites arguments instead of
1854 // redirecting requests to the callee argument.
1855 Function *F = getAssociatedFunction();
1856 const IRPosition &FnPos = IRPosition::function(*F);
1857 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos);
1858 return clampStateAndIndicateChange(
1859 getState(), static_cast<const AANoFree::StateType &>(FnAA.getState()));
1860 }
1861
1862 /// See AbstractAttribute::trackStatistics()
1863 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1864};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001865
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001866/// NoFree attribute for floating values.
1867struct AANoFreeFloating : AANoFreeImpl {
1868 AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1869
1870 /// See AbstractAttribute::trackStatistics()
1871 void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
1872
1873 /// See Abstract Attribute::updateImpl(...).
1874 ChangeStatus updateImpl(Attributor &A) override {
1875 const IRPosition &IRP = getIRPosition();
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001876
1877 const auto &NoFreeAA =
1878 A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP));
1879 if (NoFreeAA.isAssumedNoFree())
1880 return ChangeStatus::UNCHANGED;
1881
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001882 Value &AssociatedValue = getIRPosition().getAssociatedValue();
Hideto Ueno63599bd2019-12-12 12:26:09 +00001883 auto Pred = [&](const Use &U, bool &Follow) -> bool {
1884 Instruction *UserI = cast<Instruction>(U.getUser());
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001885 if (auto *CB = dyn_cast<CallBase>(UserI)) {
Hideto Ueno63599bd2019-12-12 12:26:09 +00001886 if (CB->isBundleOperand(&U))
1887 return false;
1888 if (!CB->isArgOperand(&U))
1889 return true;
1890 unsigned ArgNo = CB->getArgOperandNo(&U);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001891
1892 const auto &NoFreeArg = A.getAAFor<AANoFree>(
1893 *this, IRPosition::callsite_argument(*CB, ArgNo));
Hideto Ueno63599bd2019-12-12 12:26:09 +00001894 return NoFreeArg.isAssumedNoFree();
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001895 }
1896
1897 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
1898 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
Hideto Ueno63599bd2019-12-12 12:26:09 +00001899 Follow = true;
1900 return true;
Hideto Ueno4ecf2552019-12-12 13:42:40 +00001901 }
Johannes Doerfertf9555392020-01-10 14:49:45 -06001902 if (isa<ReturnInst>(UserI))
1903 return true;
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001904
1905 // Unknown user.
Hideto Ueno63599bd2019-12-12 12:26:09 +00001906 return false;
1907 };
1908 if (!A.checkForAllUses(Pred, *this, AssociatedValue))
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001909 return indicatePessimisticFixpoint();
Hideto Ueno63599bd2019-12-12 12:26:09 +00001910
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001911 return ChangeStatus::UNCHANGED;
1912 }
1913};
1914
1915/// NoFree attribute for a call site argument.
1916struct AANoFreeArgument final : AANoFreeFloating {
1917 AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1918
1919 /// See AbstractAttribute::trackStatistics()
1920 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
1921};
1922
1923/// NoFree attribute for call site arguments.
1924struct AANoFreeCallSiteArgument final : AANoFreeFloating {
1925 AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1926
1927 /// See AbstractAttribute::updateImpl(...).
1928 ChangeStatus updateImpl(Attributor &A) override {
1929 // TODO: Once we have call site specific value information we can provide
1930 // call site specific liveness information and then it makes
1931 // sense to specialize attributes for call sites arguments instead of
1932 // redirecting requests to the callee argument.
1933 Argument *Arg = getAssociatedArgument();
1934 if (!Arg)
1935 return indicatePessimisticFixpoint();
1936 const IRPosition &ArgPos = IRPosition::argument(*Arg);
1937 auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos);
1938 return clampStateAndIndicateChange(
1939 getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState()));
1940 }
1941
1942 /// See AbstractAttribute::trackStatistics()
1943 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
1944};
1945
1946/// NoFree attribute for function return value.
1947struct AANoFreeReturned final : AANoFreeFloating {
1948 AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {
1949 llvm_unreachable("NoFree is not applicable to function returns!");
1950 }
1951
1952 /// See AbstractAttribute::initialize(...).
1953 void initialize(Attributor &A) override {
1954 llvm_unreachable("NoFree is not applicable to function returns!");
1955 }
1956
1957 /// See AbstractAttribute::updateImpl(...).
1958 ChangeStatus updateImpl(Attributor &A) override {
1959 llvm_unreachable("NoFree is not applicable to function returns!");
1960 }
1961
1962 /// See AbstractAttribute::trackStatistics()
1963 void trackStatistics() const override {}
1964};
1965
1966/// NoFree attribute deduction for a call site return value.
1967struct AANoFreeCallSiteReturned final : AANoFreeFloating {
1968 AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1969
1970 ChangeStatus manifest(Attributor &A) override {
1971 return ChangeStatus::UNCHANGED;
1972 }
1973 /// See AbstractAttribute::trackStatistics()
1974 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
1975};
1976
Hideto Ueno54869ec2019-07-15 06:49:04 +00001977/// ------------------------ NonNull Argument Attribute ------------------------
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001978static int64_t getKnownNonNullAndDerefBytesForUse(
Hideto Uenobdcbdb42020-03-09 14:19:32 +09001979 Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue,
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001980 const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001981 TrackUse = false;
1982
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001983 const Value *UseV = U->get();
1984 if (!UseV->getType()->isPointerTy())
1985 return 0;
1986
1987 Type *PtrTy = UseV->getType();
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001988 const Function *F = I->getFunction();
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001989 bool NullPointerIsDefined =
1990 F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001991 const DataLayout &DL = A.getInfoCache().getDL();
1992 if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
1993 if (ICS.isBundleOperand(U))
1994 return 0;
1995
1996 if (ICS.isCallee(U)) {
1997 IsNonNull |= !NullPointerIsDefined;
1998 return 0;
1999 }
2000
2001 unsigned ArgNo = ICS.getArgumentNo(U);
2002 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
Johannes Doerfert77a6b352019-11-02 14:47:45 -05002003 // As long as we only use known information there is no need to track
2004 // dependences here.
2005 auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP,
2006 /* TrackDependence */ false);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002007 IsNonNull |= DerefAA.isKnownNonNull();
2008 return DerefAA.getKnownDereferenceableBytes();
2009 }
2010
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05002011 // We need to follow common pointer manipulation uses to the accesses they
2012 // feed into. We can try to be smart to avoid looking through things we do not
2013 // like for now, e.g., non-inbounds GEPs.
2014 if (isa<CastInst>(I)) {
2015 TrackUse = true;
2016 return 0;
2017 }
2018 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
Hideto Ueno0f4383f2019-11-27 14:41:12 +00002019 if (GEP->hasAllConstantIndices()) {
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05002020 TrackUse = true;
2021 return 0;
2022 }
2023
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002024 int64_t Offset;
2025 if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002026 if (Base == &AssociatedValue &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06002027 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002028 int64_t DerefBytes =
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05002029 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002030
2031 IsNonNull |= !NullPointerIsDefined;
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05002032 return std::max(int64_t(0), DerefBytes);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002033 }
2034 }
Hideto Ueno0f4383f2019-11-27 14:41:12 +00002035
2036 /// Corner case when an offset is 0.
2037 if (const Value *Base = getBasePointerOfAccessPointerOperand(
2038 I, Offset, DL, /*AllowNonInbounds*/ true)) {
2039 if (Offset == 0 && Base == &AssociatedValue &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06002040 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno0f4383f2019-11-27 14:41:12 +00002041 int64_t DerefBytes =
2042 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType());
2043 IsNonNull |= !NullPointerIsDefined;
2044 return std::max(int64_t(0), DerefBytes);
2045 }
2046 }
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002047
2048 return 0;
2049}
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00002050
Johannes Doerfert344d0382019-08-07 22:34:26 +00002051struct AANonNullImpl : AANonNull {
Johannes Doerfertd82385b2019-10-13 20:48:26 +00002052 AANonNullImpl(const IRPosition &IRP)
2053 : AANonNull(IRP),
2054 NullIsDefined(NullPointerIsDefined(
2055 getAnchorScope(),
2056 getAssociatedValue().getType()->getPointerAddressSpace())) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00002057
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002058 /// See AbstractAttribute::initialize(...).
2059 void initialize(Attributor &A) override {
Johannes Doerfertd82385b2019-10-13 20:48:26 +00002060 if (!NullIsDefined &&
2061 hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002062 indicateOptimisticFixpoint();
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05002063 else if (isa<ConstantPointerNull>(getAssociatedValue()))
2064 indicatePessimisticFixpoint();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002065 else
2066 AANonNull::initialize(A);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002067 }
2068
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002069 /// See AAFromMustBeExecutedContext
Hideto Uenobdcbdb42020-03-09 14:19:32 +09002070 bool followUse(Attributor &A, const Use *U, const Instruction *I,
2071 AANonNull::StateType &State) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002072 bool IsNonNull = false;
2073 bool TrackUse = false;
2074 getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
2075 IsNonNull, TrackUse);
Hideto Uenobdcbdb42020-03-09 14:19:32 +09002076 State.setKnown(IsNonNull);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002077 return TrackUse;
2078 }
2079
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002080 /// See AbstractAttribute::getAsStr().
2081 const std::string getAsStr() const override {
2082 return getAssumed() ? "nonnull" : "may-null";
2083 }
Johannes Doerfertd82385b2019-10-13 20:48:26 +00002084
2085 /// Flag to determine if the underlying value can be null and still allow
2086 /// valid accesses.
2087 const bool NullIsDefined;
Hideto Ueno54869ec2019-07-15 06:49:04 +00002088};
2089
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002090/// NonNull attribute for a floating value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002091struct AANonNullFloating
2092 : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> {
2093 using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>;
2094 AANonNullFloating(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00002095
Hideto Ueno54869ec2019-07-15 06:49:04 +00002096 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002097 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002098 ChangeStatus Change = Base::updateImpl(A);
2099 if (isKnownNonNull())
2100 return Change;
2101
Johannes Doerfertd82385b2019-10-13 20:48:26 +00002102 if (!NullIsDefined) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01002103 const auto &DerefAA =
2104 A.getAAFor<AADereferenceable>(*this, getIRPosition());
Johannes Doerfertd82385b2019-10-13 20:48:26 +00002105 if (DerefAA.getAssumedDereferenceableBytes())
2106 return Change;
2107 }
2108
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002109 const DataLayout &DL = A.getDataLayout();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002110
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05002111 DominatorTree *DT = nullptr;
Hideto Ueno2c0edbf2020-02-25 12:32:50 +09002112 AssumptionCache *AC = nullptr;
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05002113 InformationCache &InfoCache = A.getInfoCache();
Hideto Ueno2c0edbf2020-02-25 12:32:50 +09002114 if (const Function *Fn = getAnchorScope()) {
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05002115 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
Hideto Ueno2c0edbf2020-02-25 12:32:50 +09002116 AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn);
2117 }
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05002118
Johannes Doerfert1a746452019-10-20 22:28:49 -05002119 auto VisitValueCB = [&](Value &V, AANonNull::StateType &T,
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002120 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002121 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V));
2122 if (!Stripped && this == &AA) {
Hideto Ueno2c0edbf2020-02-25 12:32:50 +09002123 if (!isKnownNonZero(&V, DL, 0, AC, getCtxI(), DT))
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002124 T.indicatePessimisticFixpoint();
2125 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002126 // Use abstract attribute information.
2127 const AANonNull::StateType &NS =
2128 static_cast<const AANonNull::StateType &>(AA.getState());
2129 T ^= NS;
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002130 }
2131 return T.isValidState();
2132 };
2133
2134 StateType T;
2135 if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this,
2136 T, VisitValueCB))
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002137 return indicatePessimisticFixpoint();
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002138
2139 return clampStateAndIndicateChange(getState(), T);
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002140 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002141
2142 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002143 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002144};
2145
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002146/// NonNull attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002147struct AANonNullReturned final
2148 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002149 AANonNullReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002150 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002151
2152 /// See AbstractAttribute::trackStatistics()
2153 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
2154};
2155
Hideto Ueno54869ec2019-07-15 06:49:04 +00002156/// NonNull attribute for function argument.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002157struct AANonNullArgument final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002158 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2159 AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002160 AANonNullArgument(const IRPosition &IRP)
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002161 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2162 AANonNullImpl>(
2163 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002164
2165 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002166 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002167};
2168
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002169struct AANonNullCallSiteArgument final : AANonNullFloating {
2170 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002171
2172 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert1aac1822019-08-29 01:26:09 +00002173 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002174};
Johannes Doerfert007153e2019-08-05 23:26:06 +00002175
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002176/// NonNull attribute for a call site return position.
2177struct AANonNullCallSiteReturned final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002178 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2179 AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002180 AANonNullCallSiteReturned(const IRPosition &IRP)
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002181 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2182 AANonNullImpl>(
2183 IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002184
2185 /// See AbstractAttribute::trackStatistics()
2186 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
2187};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002188
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002189/// ------------------------ No-Recurse Attributes ----------------------------
2190
2191struct AANoRecurseImpl : public AANoRecurse {
2192 AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {}
2193
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002194 /// See AbstractAttribute::getAsStr()
2195 const std::string getAsStr() const override {
2196 return getAssumed() ? "norecurse" : "may-recurse";
2197 }
2198};
2199
2200struct AANoRecurseFunction final : AANoRecurseImpl {
2201 AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2202
Hideto Ueno63f60662019-09-21 15:13:19 +00002203 /// See AbstractAttribute::initialize(...).
2204 void initialize(Attributor &A) override {
2205 AANoRecurseImpl::initialize(A);
2206 if (const Function *F = getAnchorScope())
Johannes Doerfert26d02b02019-12-30 16:15:38 -06002207 if (A.getInfoCache().getSccSize(*F) != 1)
2208 indicatePessimisticFixpoint();
Hideto Ueno63f60662019-09-21 15:13:19 +00002209 }
2210
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002211 /// See AbstractAttribute::updateImpl(...).
2212 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno63f60662019-09-21 15:13:19 +00002213
Johannes Doerfert26d02b02019-12-30 16:15:38 -06002214 // If all live call sites are known to be no-recurse, we are as well.
2215 auto CallSitePred = [&](AbstractCallSite ACS) {
2216 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2217 *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
2218 /* TrackDependence */ false, DepClassTy::OPTIONAL);
2219 return NoRecurseAA.isKnownNoRecurse();
2220 };
2221 bool AllCallSitesKnown;
2222 if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) {
2223 // If we know all call sites and all are known no-recurse, we are done.
2224 // If all known call sites, which might not be all that exist, are known
2225 // to be no-recurse, we are not done but we can continue to assume
2226 // no-recurse. If one of the call sites we have not visited will become
2227 // live, another update is triggered.
2228 if (AllCallSitesKnown)
2229 indicateOptimisticFixpoint();
2230 return ChangeStatus::UNCHANGED;
2231 }
2232
2233 // If the above check does not hold anymore we look at the calls.
Hideto Ueno63f60662019-09-21 15:13:19 +00002234 auto CheckForNoRecurse = [&](Instruction &I) {
2235 ImmutableCallSite ICS(&I);
2236 if (ICS.hasFnAttr(Attribute::NoRecurse))
2237 return true;
2238
2239 const auto &NoRecurseAA =
2240 A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS));
2241 if (!NoRecurseAA.isAssumedNoRecurse())
2242 return false;
2243
2244 // Recursion to the same function
2245 if (ICS.getCalledFunction() == getAnchorScope())
2246 return false;
2247
2248 return true;
2249 };
2250
2251 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this))
2252 return indicatePessimisticFixpoint();
2253 return ChangeStatus::UNCHANGED;
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002254 }
2255
2256 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
2257};
2258
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002259/// NoRecurse attribute deduction for a call sites.
2260struct AANoRecurseCallSite final : AANoRecurseImpl {
2261 AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2262
2263 /// See AbstractAttribute::initialize(...).
2264 void initialize(Attributor &A) override {
2265 AANoRecurseImpl::initialize(A);
2266 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002267 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002268 indicatePessimisticFixpoint();
2269 }
2270
2271 /// See AbstractAttribute::updateImpl(...).
2272 ChangeStatus updateImpl(Attributor &A) override {
2273 // TODO: Once we have call site specific value information we can provide
2274 // call site specific liveness information and then it makes
2275 // sense to specialize attributes for call sites arguments instead of
2276 // redirecting requests to the callee argument.
2277 Function *F = getAssociatedFunction();
2278 const IRPosition &FnPos = IRPosition::function(*F);
2279 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos);
2280 return clampStateAndIndicateChange(
2281 getState(),
2282 static_cast<const AANoRecurse::StateType &>(FnAA.getState()));
2283 }
2284
2285 /// See AbstractAttribute::trackStatistics()
2286 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
2287};
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002288
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002289/// -------------------- Undefined-Behavior Attributes ------------------------
2290
2291struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
2292 AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {}
2293
2294 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert5732f562019-12-24 19:25:08 -06002295 // through a pointer (i.e. also branches etc.)
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002296 ChangeStatus updateImpl(Attributor &A) override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002297 const size_t UBPrevSize = KnownUBInsts.size();
2298 const size_t NoUBPrevSize = AssumedNoUBInsts.size();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002299
Johannes Doerfert5732f562019-12-24 19:25:08 -06002300 auto InspectMemAccessInstForUB = [&](Instruction &I) {
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002301 // Skip instructions that are already saved.
Hideto Uenoef4febd2019-12-29 17:34:08 +09002302 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002303 return true;
2304
Hideto Uenoef4febd2019-12-29 17:34:08 +09002305 // If we reach here, we know we have an instruction
2306 // that accesses memory through a pointer operand,
2307 // for which getPointerOperand() should give it to us.
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06002308 const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true);
Hideto Uenoef4febd2019-12-29 17:34:08 +09002309 assert(PtrOp &&
2310 "Expected pointer operand of memory accessing instruction");
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002311
Johannes Doerfert5732f562019-12-24 19:25:08 -06002312 // A memory access through a pointer is considered UB
2313 // only if the pointer has constant null value.
2314 // TODO: Expand it to not only check constant values.
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002315 if (!isa<ConstantPointerNull>(PtrOp)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002316 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002317 return true;
2318 }
Johannes Doerfert5732f562019-12-24 19:25:08 -06002319 const Type *PtrTy = PtrOp->getType();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002320
Johannes Doerfert5732f562019-12-24 19:25:08 -06002321 // Because we only consider instructions inside functions,
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002322 // assume that a parent function exists.
2323 const Function *F = I.getFunction();
2324
Johannes Doerfert5732f562019-12-24 19:25:08 -06002325 // A memory access using constant null pointer is only considered UB
2326 // if null pointer is _not_ defined for the target platform.
Hideto Uenoef4febd2019-12-29 17:34:08 +09002327 if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2328 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002329 else
Hideto Uenoef4febd2019-12-29 17:34:08 +09002330 KnownUBInsts.insert(&I);
2331 return true;
2332 };
2333
2334 auto InspectBrInstForUB = [&](Instruction &I) {
2335 // A conditional branch instruction is considered UB if it has `undef`
2336 // condition.
2337
2338 // Skip instructions that are already saved.
2339 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2340 return true;
2341
2342 // We know we have a branch instruction.
2343 auto BrInst = cast<BranchInst>(&I);
2344
2345 // Unconditional branches are never considered UB.
2346 if (BrInst->isUnconditional())
2347 return true;
2348
2349 // Either we stopped and the appropriate action was taken,
2350 // or we got back a simplified value to continue.
2351 Optional<Value *> SimplifiedCond =
2352 stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2353 if (!SimplifiedCond.hasValue())
2354 return true;
2355 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002356 return true;
2357 };
2358
Johannes Doerfert5732f562019-12-24 19:25:08 -06002359 A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2360 {Instruction::Load, Instruction::Store,
2361 Instruction::AtomicCmpXchg,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06002362 Instruction::AtomicRMW},
2363 /* CheckBBLivenessOnly */ true);
2364 A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2365 /* CheckBBLivenessOnly */ true);
Hideto Uenoef4febd2019-12-29 17:34:08 +09002366 if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2367 UBPrevSize != KnownUBInsts.size())
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002368 return ChangeStatus::CHANGED;
2369 return ChangeStatus::UNCHANGED;
2370 }
2371
Hideto Uenoef4febd2019-12-29 17:34:08 +09002372 bool isKnownToCauseUB(Instruction *I) const override {
2373 return KnownUBInsts.count(I);
2374 }
2375
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002376 bool isAssumedToCauseUB(Instruction *I) const override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002377 // In simple words, if an instruction is not in the assumed to _not_
2378 // cause UB, then it is assumed UB (that includes those
2379 // in the KnownUBInsts set). The rest is boilerplate
2380 // is to ensure that it is one of the instructions we test
2381 // for UB.
2382
2383 switch (I->getOpcode()) {
2384 case Instruction::Load:
2385 case Instruction::Store:
2386 case Instruction::AtomicCmpXchg:
2387 case Instruction::AtomicRMW:
2388 return !AssumedNoUBInsts.count(I);
2389 case Instruction::Br: {
2390 auto BrInst = cast<BranchInst>(I);
2391 if (BrInst->isUnconditional())
2392 return false;
2393 return !AssumedNoUBInsts.count(I);
2394 } break;
2395 default:
2396 return false;
2397 }
2398 return false;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002399 }
2400
2401 ChangeStatus manifest(Attributor &A) override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002402 if (KnownUBInsts.empty())
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002403 return ChangeStatus::UNCHANGED;
Hideto Uenoef4febd2019-12-29 17:34:08 +09002404 for (Instruction *I : KnownUBInsts)
Hideto Uenocb5eb132019-12-27 02:39:37 +09002405 A.changeToUnreachableAfterManifest(I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002406 return ChangeStatus::CHANGED;
2407 }
2408
2409 /// See AbstractAttribute::getAsStr()
2410 const std::string getAsStr() const override {
2411 return getAssumed() ? "undefined-behavior" : "no-ub";
2412 }
2413
Hideto Uenoef4febd2019-12-29 17:34:08 +09002414 /// Note: The correctness of this analysis depends on the fact that the
2415 /// following 2 sets will stop changing after some point.
2416 /// "Change" here means that their size changes.
2417 /// The size of each set is monotonically increasing
2418 /// (we only add items to them) and it is upper bounded by the number of
2419 /// instructions in the processed function (we can never save more
2420 /// elements in either set than this number). Hence, at some point,
2421 /// they will stop increasing.
2422 /// Consequently, at some point, both sets will have stopped
2423 /// changing, effectively making the analysis reach a fixpoint.
2424
2425 /// Note: These 2 sets are disjoint and an instruction can be considered
2426 /// one of 3 things:
2427 /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2428 /// the KnownUBInsts set.
2429 /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2430 /// has a reason to assume it).
2431 /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2432 /// could not find a reason to assume or prove that it can cause UB,
2433 /// hence it assumes it doesn't. We have a set for these instructions
2434 /// so that we don't reprocess them in every update.
2435 /// Note however that instructions in this set may cause UB.
2436
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002437protected:
Hideto Uenoef4febd2019-12-29 17:34:08 +09002438 /// A set of all live instructions _known_ to cause UB.
2439 SmallPtrSet<Instruction *, 8> KnownUBInsts;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002440
2441private:
Hideto Uenoef4febd2019-12-29 17:34:08 +09002442 /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2443 SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2444
2445 // Should be called on updates in which if we're processing an instruction
2446 // \p I that depends on a value \p V, one of the following has to happen:
2447 // - If the value is assumed, then stop.
2448 // - If the value is known but undef, then consider it UB.
2449 // - Otherwise, do specific processing with the simplified value.
2450 // We return None in the first 2 cases to signify that an appropriate
2451 // action was taken and the caller should stop.
2452 // Otherwise, we return the simplified value that the caller should
2453 // use for specific processing.
2454 Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V,
2455 Instruction *I) {
2456 const auto &ValueSimplifyAA =
2457 A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V));
2458 Optional<Value *> SimplifiedV =
2459 ValueSimplifyAA.getAssumedSimplifiedValue(A);
2460 if (!ValueSimplifyAA.isKnown()) {
2461 // Don't depend on assumed values.
2462 return llvm::None;
2463 }
2464 if (!SimplifiedV.hasValue()) {
2465 // If it is known (which we tested above) but it doesn't have a value,
2466 // then we can assume `undef` and hence the instruction is UB.
2467 KnownUBInsts.insert(I);
2468 return llvm::None;
2469 }
2470 Value *Val = SimplifiedV.getValue();
2471 if (isa<UndefValue>(Val)) {
2472 KnownUBInsts.insert(I);
2473 return llvm::None;
2474 }
2475 return Val;
2476 }
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002477};
2478
2479struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2480 AAUndefinedBehaviorFunction(const IRPosition &IRP)
2481 : AAUndefinedBehaviorImpl(IRP) {}
2482
2483 /// See AbstractAttribute::trackStatistics()
2484 void trackStatistics() const override {
2485 STATS_DECL(UndefinedBehaviorInstruction, Instruction,
2486 "Number of instructions known to have UB");
2487 BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
Hideto Uenoef4febd2019-12-29 17:34:08 +09002488 KnownUBInsts.size();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002489 }
2490};
2491
Hideto Ueno11d37102019-07-17 15:15:43 +00002492/// ------------------------ Will-Return Attributes ----------------------------
2493
omarahmed1111b285b332020-03-13 10:30:36 -05002494// Helper function that checks whether a function has any cycle which we don't
2495// know if it is bounded or not.
2496// Loops with maximum trip count are considered bounded, any other cycle not.
2497static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
2498 ScalarEvolution *SE =
2499 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
2500 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
2501 // If either SCEV or LoopInfo is not available for the function then we assume
2502 // any cycle to be unbounded cycle.
2503 // We use scc_iterator which uses Tarjan algorithm to find all the maximal
2504 // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
2505 if (!SE || !LI) {
2506 for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
2507 if (SCCI.hasCycle())
Hideto Ueno11d37102019-07-17 15:15:43 +00002508 return true;
omarahmed1111b285b332020-03-13 10:30:36 -05002509 return false;
2510 }
2511
2512 // If there's irreducible control, the function may contain non-loop cycles.
2513 if (mayContainIrreducibleControl(F, LI))
2514 return true;
2515
2516 // Any loop that does not have a max trip count is considered unbounded cycle.
2517 for (auto *L : LI->getLoopsInPreorder()) {
2518 if (!SE->getSmallConstantMaxTripCount(L))
2519 return true;
Hideto Ueno11d37102019-07-17 15:15:43 +00002520 }
2521 return false;
2522}
2523
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002524struct AAWillReturnImpl : public AAWillReturn {
2525 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
Hideto Ueno11d37102019-07-17 15:15:43 +00002526
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002527 /// See AbstractAttribute::initialize(...).
2528 void initialize(Attributor &A) override {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002529 AAWillReturn::initialize(A);
Hideto Ueno11d37102019-07-17 15:15:43 +00002530
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002531 Function *F = getAssociatedFunction();
omarahmed1111b285b332020-03-13 10:30:36 -05002532 if (!F || !A.isFunctionIPOAmendable(*F) || mayContainUnboundedCycle(*F, A))
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002533 indicatePessimisticFixpoint();
2534 }
Hideto Ueno11d37102019-07-17 15:15:43 +00002535
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002536 /// See AbstractAttribute::updateImpl(...).
2537 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002538 auto CheckForWillReturn = [&](Instruction &I) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002539 IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I));
2540 const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos);
2541 if (WillReturnAA.isKnownWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002542 return true;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002543 if (!WillReturnAA.isAssumedWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002544 return false;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002545 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos);
2546 return NoRecurseAA.isAssumedNoRecurse();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002547 };
2548
2549 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
2550 return indicatePessimisticFixpoint();
2551
2552 return ChangeStatus::UNCHANGED;
2553 }
2554
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002555 /// See AbstractAttribute::getAsStr()
2556 const std::string getAsStr() const override {
2557 return getAssumed() ? "willreturn" : "may-noreturn";
2558 }
2559};
2560
2561struct AAWillReturnFunction final : AAWillReturnImpl {
2562 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2563
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002564 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002565 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002566};
Hideto Ueno11d37102019-07-17 15:15:43 +00002567
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002568/// WillReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002569struct AAWillReturnCallSite final : AAWillReturnImpl {
2570 AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2571
2572 /// See AbstractAttribute::initialize(...).
2573 void initialize(Attributor &A) override {
2574 AAWillReturnImpl::initialize(A);
2575 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002576 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002577 indicatePessimisticFixpoint();
2578 }
2579
2580 /// See AbstractAttribute::updateImpl(...).
2581 ChangeStatus updateImpl(Attributor &A) override {
2582 // TODO: Once we have call site specific value information we can provide
2583 // call site specific liveness information and then it makes
2584 // sense to specialize attributes for call sites arguments instead of
2585 // redirecting requests to the callee argument.
2586 Function *F = getAssociatedFunction();
2587 const IRPosition &FnPos = IRPosition::function(*F);
2588 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos);
2589 return clampStateAndIndicateChange(
2590 getState(),
2591 static_cast<const AAWillReturn::StateType &>(FnAA.getState()));
2592 }
2593
2594 /// See AbstractAttribute::trackStatistics()
2595 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
2596};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002597
Pankaj Gode04945c92019-11-22 18:40:47 +05302598/// -------------------AAReachability Attribute--------------------------
2599
2600struct AAReachabilityImpl : AAReachability {
2601 AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {}
2602
2603 const std::string getAsStr() const override {
2604 // TODO: Return the number of reachable queries.
2605 return "reachable";
2606 }
2607
2608 /// See AbstractAttribute::initialize(...).
Johannes Doerfert0bc33362019-12-16 21:03:18 -06002609 void initialize(Attributor &A) override { indicatePessimisticFixpoint(); }
Pankaj Gode04945c92019-11-22 18:40:47 +05302610
2611 /// See AbstractAttribute::updateImpl(...).
2612 ChangeStatus updateImpl(Attributor &A) override {
2613 return indicatePessimisticFixpoint();
2614 }
2615};
2616
2617struct AAReachabilityFunction final : public AAReachabilityImpl {
2618 AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {}
2619
2620 /// See AbstractAttribute::trackStatistics()
2621 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
2622};
2623
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002624/// ------------------------ NoAlias Argument Attribute ------------------------
2625
Johannes Doerfert344d0382019-08-07 22:34:26 +00002626struct AANoAliasImpl : AANoAlias {
Johannes Doerfert32e98a72020-02-14 19:23:21 -06002627 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {
2628 assert(getAssociatedType()->isPointerTy() &&
2629 "Noalias is a pointer attribute");
2630 }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002631
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002632 const std::string getAsStr() const override {
2633 return getAssumed() ? "noalias" : "may-alias";
2634 }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002635};
2636
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002637/// NoAlias attribute for a floating value.
2638struct AANoAliasFloating final : AANoAliasImpl {
2639 AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2640
Hideto Uenocbab3342019-08-29 05:52:00 +00002641 /// See AbstractAttribute::initialize(...).
2642 void initialize(Attributor &A) override {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002643 AANoAliasImpl::initialize(A);
Johannes Doerfert87ddf1f2020-01-25 20:21:41 -06002644 Value *Val = &getAssociatedValue();
2645 do {
2646 CastInst *CI = dyn_cast<CastInst>(Val);
2647 if (!CI)
2648 break;
2649 Value *Base = CI->getOperand(0);
2650 if (Base->getNumUses() != 1)
2651 break;
2652 Val = Base;
2653 } while (true);
2654
Johannes Doerfert32e98a72020-02-14 19:23:21 -06002655 if (!Val->getType()->isPointerTy()) {
2656 indicatePessimisticFixpoint();
2657 return;
2658 }
2659
Johannes Doerfert72adda12019-10-10 05:33:21 +00002660 if (isa<AllocaInst>(Val))
2661 indicateOptimisticFixpoint();
Johannes Doerfert24ae77e2020-01-27 22:19:11 -06002662 else if (isa<ConstantPointerNull>(Val) &&
2663 !NullPointerIsDefined(getAnchorScope(),
Johannes Doerfert87ddf1f2020-01-25 20:21:41 -06002664 Val->getType()->getPointerAddressSpace()))
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002665 indicateOptimisticFixpoint();
Johannes Doerfert87ddf1f2020-01-25 20:21:41 -06002666 else if (Val != &getAssociatedValue()) {
2667 const auto &ValNoAliasAA =
2668 A.getAAFor<AANoAlias>(*this, IRPosition::value(*Val));
2669 if (ValNoAliasAA.isKnownNoAlias())
2670 indicateOptimisticFixpoint();
2671 }
Hideto Uenocbab3342019-08-29 05:52:00 +00002672 }
2673
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002674 /// See AbstractAttribute::updateImpl(...).
2675 ChangeStatus updateImpl(Attributor &A) override {
2676 // TODO: Implement this.
2677 return indicatePessimisticFixpoint();
2678 }
2679
2680 /// See AbstractAttribute::trackStatistics()
2681 void trackStatistics() const override {
2682 STATS_DECLTRACK_FLOATING_ATTR(noalias)
2683 }
2684};
2685
2686/// NoAlias attribute for an argument.
Hideto Uenocbab3342019-08-29 05:52:00 +00002687struct AANoAliasArgument final
2688 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002689 using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
2690 AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {}
2691
Johannes Doerfert2baf0002020-01-12 00:18:07 -06002692 /// See AbstractAttribute::initialize(...).
2693 void initialize(Attributor &A) override {
2694 Base::initialize(A);
2695 // See callsite argument attribute and callee argument attribute.
2696 if (hasAttr({Attribute::ByVal}))
2697 indicateOptimisticFixpoint();
2698 }
2699
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002700 /// See AbstractAttribute::update(...).
2701 ChangeStatus updateImpl(Attributor &A) override {
2702 // We have to make sure no-alias on the argument does not break
2703 // synchronization when this is a callback argument, see also [1] below.
2704 // If synchronization cannot be affected, we delegate to the base updateImpl
2705 // function, otherwise we give up for now.
2706
2707 // If the function is no-sync, no-alias cannot break synchronization.
2708 const auto &NoSyncAA = A.getAAFor<AANoSync>(
2709 *this, IRPosition::function_scope(getIRPosition()));
2710 if (NoSyncAA.isAssumedNoSync())
2711 return Base::updateImpl(A);
2712
2713 // If the argument is read-only, no-alias cannot break synchronization.
2714 const auto &MemBehaviorAA =
2715 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
2716 if (MemBehaviorAA.isAssumedReadOnly())
2717 return Base::updateImpl(A);
2718
2719 // If the argument is never passed through callbacks, no-alias cannot break
2720 // synchronization.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002721 bool AllCallSitesKnown;
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002722 if (A.checkForAllCallSites(
2723 [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002724 true, AllCallSitesKnown))
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002725 return Base::updateImpl(A);
2726
2727 // TODO: add no-alias but make sure it doesn't break synchronization by
2728 // introducing fake uses. See:
2729 // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
2730 // International Workshop on OpenMP 2018,
2731 // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
2732
2733 return indicatePessimisticFixpoint();
2734 }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002735
2736 /// See AbstractAttribute::trackStatistics()
2737 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
2738};
2739
2740struct AANoAliasCallSiteArgument final : AANoAliasImpl {
2741 AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2742
Hideto Uenocbab3342019-08-29 05:52:00 +00002743 /// See AbstractAttribute::initialize(...).
2744 void initialize(Attributor &A) override {
Hideto Ueno6381b142019-08-30 10:00:32 +00002745 // See callsite argument attribute and callee argument attribute.
2746 ImmutableCallSite ICS(&getAnchorValue());
2747 if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias))
2748 indicateOptimisticFixpoint();
Johannes Doerfert24ae77e2020-01-27 22:19:11 -06002749 Value &Val = getAssociatedValue();
2750 if (isa<ConstantPointerNull>(Val) &&
2751 !NullPointerIsDefined(getAnchorScope(),
2752 Val.getType()->getPointerAddressSpace()))
2753 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00002754 }
2755
Johannes Doerfert53992c72020-01-27 22:24:32 -06002756 /// Determine if the underlying value may alias with the call site argument
2757 /// \p OtherArgNo of \p ICS (= the underlying call site).
2758 bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
2759 const AAMemoryBehavior &MemBehaviorAA,
2760 ImmutableCallSite ICS, unsigned OtherArgNo) {
2761 // We do not need to worry about aliasing with the underlying IRP.
2762 if (this->getArgNo() == (int)OtherArgNo)
2763 return false;
2764
2765 // If it is not a pointer or pointer vector we do not alias.
2766 const Value *ArgOp = ICS.getArgOperand(OtherArgNo);
2767 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
2768 return false;
2769
2770 auto &ICSArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
2771 *this, IRPosition::callsite_argument(ICS, OtherArgNo),
2772 /* TrackDependence */ false);
2773
2774 // If the argument is readnone, there is no read-write aliasing.
2775 if (ICSArgMemBehaviorAA.isAssumedReadNone()) {
2776 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2777 return false;
2778 }
2779
2780 // If the argument is readonly and the underlying value is readonly, there
2781 // is no read-write aliasing.
2782 bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
2783 if (ICSArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
2784 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2785 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2786 return false;
2787 }
2788
2789 // We have to utilize actual alias analysis queries so we need the object.
2790 if (!AAR)
2791 AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
2792
2793 // Try to rule it out at the call site.
2794 bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
2795 LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
2796 "callsite arguments: "
2797 << getAssociatedValue() << " " << *ArgOp << " => "
2798 << (IsAliasing ? "" : "no-") << "alias \n");
2799
2800 return IsAliasing;
2801 }
2802
2803 bool
2804 isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
2805 const AAMemoryBehavior &MemBehaviorAA,
2806 const AANoAlias &NoAliasAA) {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002807 // We can deduce "noalias" if the following conditions hold.
2808 // (i) Associated value is assumed to be noalias in the definition.
2809 // (ii) Associated value is assumed to be no-capture in all the uses
2810 // possibly executed before this callsite.
2811 // (iii) There is no other pointer argument which could alias with the
2812 // value.
2813
Johannes Doerfert53992c72020-01-27 22:24:32 -06002814 bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
2815 if (!AssociatedValueIsNoAliasAtDef) {
2816 LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
2817 << " is not no-alias at the definition\n");
2818 return false;
2819 }
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002820
Hideto Uenod9bf79f2020-03-12 15:25:24 +00002821 A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
2822
Johannes Doerfert53992c72020-01-27 22:24:32 -06002823 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2824 auto &NoCaptureAA =
2825 A.getAAFor<AANoCapture>(*this, VIRP, /* TrackDependence */ false);
2826 // Check whether the value is captured in the scope using AANoCapture.
Pankaj Godebf990532020-03-13 21:09:08 +05302827 // Look at CFG and check only uses possibly executed before this
2828 // callsite.
2829 auto UsePred = [&](const Use &U, bool &Follow) -> bool {
2830 Instruction *UserI = cast<Instruction>(U.getUser());
2831
2832 // If user if curr instr and only use.
2833 if ((UserI == getCtxI()) && (UserI->getNumUses() == 1))
2834 return true;
2835
2836 const Function *ScopeFn = VIRP.getAnchorScope();
2837 if (ScopeFn) {
2838 const auto &ReachabilityAA =
2839 A.getAAFor<AAReachability>(*this, IRPosition::function(*ScopeFn));
2840
2841 if (!ReachabilityAA.isAssumedReachable(UserI, getCtxI()))
2842 return true;
2843
2844 if (auto *CB = dyn_cast<CallBase>(UserI)) {
2845 if (CB->isArgOperand(&U)) {
2846
2847 unsigned ArgNo = CB->getArgOperandNo(&U);
2848
2849 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
2850 *this, IRPosition::callsite_argument(*CB, ArgNo));
2851
2852 if (NoCaptureAA.isAssumedNoCapture())
2853 return true;
2854 }
2855 }
2856 }
2857
2858 // For cases which can potentially have more users
2859 if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) ||
2860 isa<SelectInst>(U)) {
2861 Follow = true;
2862 return true;
2863 }
2864
2865 LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n");
Johannes Doerfert53992c72020-01-27 22:24:32 -06002866 return false;
Pankaj Godebf990532020-03-13 21:09:08 +05302867 };
2868
2869 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
2870 if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
2871 LLVM_DEBUG(
2872 dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
2873 << " cannot be noalias as it is potentially captured\n");
2874 return false;
2875 }
Johannes Doerfert72adda12019-10-10 05:33:21 +00002876 }
Johannes Doerfert53992c72020-01-27 22:24:32 -06002877 A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002878
Johannes Doerfert53992c72020-01-27 22:24:32 -06002879 // Check there is no other pointer argument which could alias with the
2880 // value passed at this call site.
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002881 // TODO: AbstractCallSite
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002882 ImmutableCallSite ICS(&getAnchorValue());
Johannes Doerfert53992c72020-01-27 22:24:32 -06002883 for (unsigned OtherArgNo = 0; OtherArgNo < ICS.getNumArgOperands();
2884 OtherArgNo++)
2885 if (mayAliasWithArgument(A, AAR, MemBehaviorAA, ICS, OtherArgNo))
2886 return false;
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002887
Johannes Doerfert53992c72020-01-27 22:24:32 -06002888 return true;
2889 }
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002890
Johannes Doerfert53992c72020-01-27 22:24:32 -06002891 /// See AbstractAttribute::updateImpl(...).
2892 ChangeStatus updateImpl(Attributor &A) override {
2893 // If the argument is readnone we are done as there are no accesses via the
2894 // argument.
2895 auto &MemBehaviorAA =
2896 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
2897 /* TrackDependence */ false);
2898 if (MemBehaviorAA.isAssumedReadNone()) {
2899 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2900 return ChangeStatus::UNCHANGED;
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002901 }
2902
Johannes Doerfert53992c72020-01-27 22:24:32 -06002903 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2904 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, VIRP,
2905 /* TrackDependence */ false);
2906
2907 AAResults *AAR = nullptr;
2908 if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
2909 NoAliasAA)) {
2910 LLVM_DEBUG(
2911 dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
2912 return ChangeStatus::UNCHANGED;
2913 }
2914
2915 return indicatePessimisticFixpoint();
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002916 }
2917
2918 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00002919 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002920};
2921
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002922/// NoAlias attribute for function return value.
Johannes Doerfertbeb51502019-08-07 22:36:15 +00002923struct AANoAliasReturned final : AANoAliasImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002924 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002925
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002926 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002927 virtual ChangeStatus updateImpl(Attributor &A) override {
2928
2929 auto CheckReturnValue = [&](Value &RV) -> bool {
2930 if (Constant *C = dyn_cast<Constant>(&RV))
2931 if (C->isNullValue() || isa<UndefValue>(C))
2932 return true;
2933
2934 /// For now, we can only deduce noalias if we have call sites.
2935 /// FIXME: add more support.
2936 ImmutableCallSite ICS(&RV);
2937 if (!ICS)
2938 return false;
2939
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002940 const IRPosition &RVPos = IRPosition::value(RV);
2941 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002942 if (!NoAliasAA.isAssumedNoAlias())
2943 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002944
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002945 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos);
2946 return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002947 };
2948
2949 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
2950 return indicatePessimisticFixpoint();
2951
2952 return ChangeStatus::UNCHANGED;
2953 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002954
2955 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002956 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002957};
2958
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002959/// NoAlias attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002960struct AANoAliasCallSiteReturned final : AANoAliasImpl {
2961 AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2962
2963 /// See AbstractAttribute::initialize(...).
2964 void initialize(Attributor &A) override {
2965 AANoAliasImpl::initialize(A);
2966 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002967 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002968 indicatePessimisticFixpoint();
2969 }
2970
2971 /// See AbstractAttribute::updateImpl(...).
2972 ChangeStatus updateImpl(Attributor &A) override {
2973 // TODO: Once we have call site specific value information we can provide
2974 // call site specific liveness information and then it makes
2975 // sense to specialize attributes for call sites arguments instead of
2976 // redirecting requests to the callee argument.
2977 Function *F = getAssociatedFunction();
2978 const IRPosition &FnPos = IRPosition::returned(*F);
2979 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos);
2980 return clampStateAndIndicateChange(
2981 getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState()));
2982 }
2983
2984 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00002985 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002986};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002987
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002988/// -------------------AAIsDead Function Attribute-----------------------
2989
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002990struct AAIsDeadValueImpl : public AAIsDead {
2991 AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002992
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002993 /// See AAIsDead::isAssumedDead().
2994 bool isAssumedDead() const override { return getAssumed(); }
2995
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002996 /// See AAIsDead::isKnownDead().
2997 bool isKnownDead() const override { return getKnown(); }
2998
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002999 /// See AAIsDead::isAssumedDead(BasicBlock *).
3000 bool isAssumedDead(const BasicBlock *BB) const override { return false; }
3001
3002 /// See AAIsDead::isKnownDead(BasicBlock *).
3003 bool isKnownDead(const BasicBlock *BB) const override { return false; }
3004
3005 /// See AAIsDead::isAssumedDead(Instruction *I).
3006 bool isAssumedDead(const Instruction *I) const override {
3007 return I == getCtxI() && isAssumedDead();
3008 }
3009
3010 /// See AAIsDead::isKnownDead(Instruction *I).
3011 bool isKnownDead(const Instruction *I) const override {
Johannes Doerfert86509e82020-01-12 00:34:38 -06003012 return isAssumedDead(I) && getKnown();
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003013 }
3014
3015 /// See AbstractAttribute::getAsStr().
3016 const std::string getAsStr() const override {
3017 return isAssumedDead() ? "assumed-dead" : "assumed-live";
3018 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003019
Johannes Doerfert86509e82020-01-12 00:34:38 -06003020 /// Check if all uses are assumed dead.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003021 bool areAllUsesAssumedDead(Attributor &A, Value &V) {
3022 auto UsePred = [&](const Use &U, bool &Follow) { return false; };
3023 // Explicitly set the dependence class to required because we want a long
3024 // chain of N dependent instructions to be considered live as soon as one is
3025 // without going through N update cycles. This is not required for
3026 // correctness.
3027 return A.checkForAllUses(UsePred, *this, V, DepClassTy::REQUIRED);
Johannes Doerfert86509e82020-01-12 00:34:38 -06003028 }
3029
3030 /// Determine if \p I is assumed to be side-effect free.
3031 bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
3032 if (!I || wouldInstructionBeTriviallyDead(I))
3033 return true;
3034
3035 auto *CB = dyn_cast<CallBase>(I);
3036 if (!CB || isa<IntrinsicInst>(CB))
3037 return false;
3038
3039 const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
3040 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, CallIRP);
3041 if (!NoUnwindAA.isAssumedNoUnwind())
3042 return false;
3043
3044 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, CallIRP);
3045 if (!MemBehaviorAA.isAssumedReadOnly())
3046 return false;
3047
3048 return true;
3049 }
3050};
3051
3052struct AAIsDeadFloating : public AAIsDeadValueImpl {
3053 AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
3054
3055 /// See AbstractAttribute::initialize(...).
3056 void initialize(Attributor &A) override {
3057 if (isa<UndefValue>(getAssociatedValue())) {
3058 indicatePessimisticFixpoint();
3059 return;
3060 }
3061
3062 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3063 if (!isAssumedSideEffectFree(A, I))
3064 indicatePessimisticFixpoint();
3065 }
3066
3067 /// See AbstractAttribute::updateImpl(...).
3068 ChangeStatus updateImpl(Attributor &A) override {
3069 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
3070 if (!isAssumedSideEffectFree(A, I))
3071 return indicatePessimisticFixpoint();
3072
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003073 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003074 return indicatePessimisticFixpoint();
3075 return ChangeStatus::UNCHANGED;
3076 }
3077
3078 /// See AbstractAttribute::manifest(...).
3079 ChangeStatus manifest(Attributor &A) override {
3080 Value &V = getAssociatedValue();
Johannes Doerfert86509e82020-01-12 00:34:38 -06003081 if (auto *I = dyn_cast<Instruction>(&V)) {
3082 // If we get here we basically know the users are all dead. We check if
3083 // isAssumedSideEffectFree returns true here again because it might not be
3084 // the case and only the users are dead but the instruction (=call) is
3085 // still needed.
3086 if (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I)) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003087 A.deleteAfterManifest(*I);
3088 return ChangeStatus::CHANGED;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003089 }
Johannes Doerfert86509e82020-01-12 00:34:38 -06003090 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003091 if (V.use_empty())
3092 return ChangeStatus::UNCHANGED;
3093
Johannes Doerfertb2c76002020-01-23 17:12:56 -06003094 bool UsedAssumedInformation = false;
Johannes Doerferte1eed6c2020-02-16 16:45:28 -06003095 Optional<Constant *> C =
Johannes Doerfertb2c76002020-01-23 17:12:56 -06003096 getAssumedConstant(A, V, *this, UsedAssumedInformation);
Johannes Doerferte1eed6c2020-02-16 16:45:28 -06003097 if (C.hasValue() && C.getValue())
Johannes Doerfertb2c76002020-01-23 17:12:56 -06003098 return ChangeStatus::UNCHANGED;
3099
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003100 UndefValue &UV = *UndefValue::get(V.getType());
Hideto Ueno34fe8d02019-12-30 17:08:48 +09003101 bool AnyChange = A.changeValueAfterManifest(V, UV);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003102 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3103 }
3104
3105 /// See AbstractAttribute::trackStatistics()
3106 void trackStatistics() const override {
3107 STATS_DECLTRACK_FLOATING_ATTR(IsDead)
3108 }
3109};
3110
3111struct AAIsDeadArgument : public AAIsDeadFloating {
3112 AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {}
3113
3114 /// See AbstractAttribute::initialize(...).
3115 void initialize(Attributor &A) override {
Johannes Doerferta198adb2020-03-13 00:32:38 -05003116 if (!A.isFunctionIPOAmendable(*getAssociatedFunction()))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003117 indicatePessimisticFixpoint();
3118 }
3119
Johannes Doerfert75133632019-10-10 01:39:16 -05003120 /// See AbstractAttribute::manifest(...).
3121 ChangeStatus manifest(Attributor &A) override {
3122 ChangeStatus Changed = AAIsDeadFloating::manifest(A);
3123 Argument &Arg = *getAssociatedArgument();
Johannes Doerfert89c2e732019-10-30 17:20:20 -05003124 if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
Johannes Doerfert75133632019-10-10 01:39:16 -05003125 if (A.registerFunctionSignatureRewrite(
3126 Arg, /* ReplacementTypes */ {},
3127 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
3128 Attributor::ArgumentReplacementInfo::ACSRepairCBTy{}))
3129 return ChangeStatus::CHANGED;
3130 return Changed;
3131 }
3132
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003133 /// See AbstractAttribute::trackStatistics()
3134 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
3135};
3136
3137struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
3138 AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
3139
3140 /// See AbstractAttribute::initialize(...).
3141 void initialize(Attributor &A) override {
3142 if (isa<UndefValue>(getAssociatedValue()))
3143 indicatePessimisticFixpoint();
3144 }
3145
3146 /// See AbstractAttribute::updateImpl(...).
3147 ChangeStatus updateImpl(Attributor &A) override {
3148 // TODO: Once we have call site specific value information we can provide
3149 // call site specific liveness information and then it makes
3150 // sense to specialize attributes for call sites arguments instead of
3151 // redirecting requests to the callee argument.
3152 Argument *Arg = getAssociatedArgument();
3153 if (!Arg)
3154 return indicatePessimisticFixpoint();
3155 const IRPosition &ArgPos = IRPosition::argument(*Arg);
3156 auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos);
3157 return clampStateAndIndicateChange(
3158 getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState()));
3159 }
3160
3161 /// See AbstractAttribute::manifest(...).
3162 ChangeStatus manifest(Attributor &A) override {
3163 CallBase &CB = cast<CallBase>(getAnchorValue());
3164 Use &U = CB.getArgOperandUse(getArgNo());
3165 assert(!isa<UndefValue>(U.get()) &&
3166 "Expected undef values to be filtered out!");
3167 UndefValue &UV = *UndefValue::get(U->getType());
3168 if (A.changeUseAfterManifest(U, UV))
3169 return ChangeStatus::CHANGED;
3170 return ChangeStatus::UNCHANGED;
3171 }
3172
3173 /// See AbstractAttribute::trackStatistics()
3174 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
3175};
3176
Johannes Doerfert86509e82020-01-12 00:34:38 -06003177struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
3178 AAIsDeadCallSiteReturned(const IRPosition &IRP)
3179 : AAIsDeadFloating(IRP), IsAssumedSideEffectFree(true) {}
3180
3181 /// See AAIsDead::isAssumedDead().
3182 bool isAssumedDead() const override {
3183 return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
3184 }
3185
Johannes Doerfert86509e82020-01-12 00:34:38 -06003186 /// See AbstractAttribute::initialize(...).
3187 void initialize(Attributor &A) override {
3188 if (isa<UndefValue>(getAssociatedValue())) {
3189 indicatePessimisticFixpoint();
3190 return;
3191 }
3192
3193 // We track this separately as a secondary state.
3194 IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3195 }
3196
3197 /// See AbstractAttribute::updateImpl(...).
3198 ChangeStatus updateImpl(Attributor &A) override {
3199 ChangeStatus Changed = ChangeStatus::UNCHANGED;
3200 if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3201 IsAssumedSideEffectFree = false;
3202 Changed = ChangeStatus::CHANGED;
3203 }
3204
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003205 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
Johannes Doerfert86509e82020-01-12 00:34:38 -06003206 return indicatePessimisticFixpoint();
3207 return Changed;
3208 }
3209
Johannes Doerfert86509e82020-01-12 00:34:38 -06003210 /// See AbstractAttribute::trackStatistics()
3211 void trackStatistics() const override {
3212 if (IsAssumedSideEffectFree)
3213 STATS_DECLTRACK_CSRET_ATTR(IsDead)
3214 else
3215 STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
3216 }
3217
3218 /// See AbstractAttribute::getAsStr().
3219 const std::string getAsStr() const override {
3220 return isAssumedDead()
3221 ? "assumed-dead"
3222 : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3223 }
3224
3225private:
3226 bool IsAssumedSideEffectFree;
3227};
3228
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003229struct AAIsDeadReturned : public AAIsDeadValueImpl {
3230 AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
3231
3232 /// See AbstractAttribute::updateImpl(...).
3233 ChangeStatus updateImpl(Attributor &A) override {
3234
Johannes Doerfertb70297a2020-02-14 10:34:31 -06003235 A.checkForAllInstructions([](Instruction &) { return true; }, *this,
3236 {Instruction::Ret});
3237
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003238 auto PredForCallSite = [&](AbstractCallSite ACS) {
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003239 if (ACS.isCallbackCall() || !ACS.getInstruction())
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003240 return false;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003241 return areAllUsesAssumedDead(A, *ACS.getInstruction());
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003242 };
3243
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06003244 bool AllCallSitesKnown;
3245 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3246 AllCallSitesKnown))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003247 return indicatePessimisticFixpoint();
3248
3249 return ChangeStatus::UNCHANGED;
3250 }
3251
3252 /// See AbstractAttribute::manifest(...).
3253 ChangeStatus manifest(Attributor &A) override {
3254 // TODO: Rewrite the signature to return void?
3255 bool AnyChange = false;
3256 UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3257 auto RetInstPred = [&](Instruction &I) {
3258 ReturnInst &RI = cast<ReturnInst>(I);
3259 if (!isa<UndefValue>(RI.getReturnValue()))
3260 AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3261 return true;
3262 };
3263 A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret});
3264 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3265 }
3266
3267 /// See AbstractAttribute::trackStatistics()
3268 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
3269};
3270
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003271struct AAIsDeadFunction : public AAIsDead {
3272 AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {}
3273
3274 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003275 void initialize(Attributor &A) override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003276 const Function *F = getAssociatedFunction();
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003277 if (F && !F->isDeclaration()) {
3278 ToBeExploredFrom.insert(&F->getEntryBlock().front());
3279 assumeLive(A, F->getEntryBlock());
3280 }
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003281 }
3282
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003283 /// See AbstractAttribute::getAsStr().
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003284 const std::string getAsStr() const override {
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003285 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003286 std::to_string(getAssociatedFunction()->size()) + "][#TBEP " +
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003287 std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3288 std::to_string(KnownDeadEnds.size()) + "]";
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003289 }
3290
3291 /// See AbstractAttribute::manifest(...).
3292 ChangeStatus manifest(Attributor &A) override {
3293 assert(getState().isValidState() &&
3294 "Attempted to manifest an invalid state!");
3295
3296 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003297 Function &F = *getAssociatedFunction();
3298
3299 if (AssumedLiveBlocks.empty()) {
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003300 A.deleteAfterManifest(F);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003301 return ChangeStatus::CHANGED;
3302 }
Johannes Doerfert924d2132019-08-05 21:34:45 +00003303
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003304 // Flag to determine if we can change an invoke to a call assuming the
3305 // callee is nounwind. This is not possible if the personality of the
3306 // function allows to catch asynchronous exceptions.
Johannes Doerfert924d2132019-08-05 21:34:45 +00003307 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003308
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003309 KnownDeadEnds.set_union(ToBeExploredFrom);
3310 for (const Instruction *DeadEndI : KnownDeadEnds) {
3311 auto *CB = dyn_cast<CallBase>(DeadEndI);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003312 if (!CB)
3313 continue;
3314 const auto &NoReturnAA =
3315 A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB));
Johannes Doerfertc7ab19d2019-11-01 21:59:32 -05003316 bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3317 if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003318 continue;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003319
Johannes Doerferta4088c72020-01-07 16:01:57 -06003320 if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3321 A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3322 else
3323 A.changeToUnreachableAfterManifest(
3324 const_cast<Instruction *>(DeadEndI->getNextNode()));
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003325 HasChanged = ChangeStatus::CHANGED;
3326 }
3327
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003328 for (BasicBlock &BB : F)
3329 if (!AssumedLiveBlocks.count(&BB))
3330 A.deleteAfterManifest(BB);
3331
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003332 return HasChanged;
3333 }
3334
3335 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003336 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003337
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003338 /// See AbstractAttribute::trackStatistics()
3339 void trackStatistics() const override {}
3340
3341 /// Returns true if the function is assumed dead.
3342 bool isAssumedDead() const override { return false; }
3343
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06003344 /// See AAIsDead::isKnownDead().
3345 bool isKnownDead() const override { return false; }
3346
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003347 /// See AAIsDead::isAssumedDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003348 bool isAssumedDead(const BasicBlock *BB) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003349 assert(BB->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003350 "BB must be in the same anchor scope function.");
3351
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003352 if (!getAssumed())
3353 return false;
3354 return !AssumedLiveBlocks.count(BB);
3355 }
3356
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003357 /// See AAIsDead::isKnownDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003358 bool isKnownDead(const BasicBlock *BB) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003359 return getKnown() && isAssumedDead(BB);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003360 }
3361
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003362 /// See AAIsDead::isAssumed(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003363 bool isAssumedDead(const Instruction *I) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003364 assert(I->getParent()->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003365 "Instruction must be in the same anchor scope function.");
3366
Stefan Stipanovic7849e412019-08-03 15:27:41 +00003367 if (!getAssumed())
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003368 return false;
3369
3370 // If it is not in AssumedLiveBlocks then it for sure dead.
3371 // Otherwise, it can still be after noreturn call in a live block.
3372 if (!AssumedLiveBlocks.count(I->getParent()))
3373 return true;
3374
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003375 // If it is not after a liveness barrier it is live.
3376 const Instruction *PrevI = I->getPrevNode();
3377 while (PrevI) {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003378 if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003379 return true;
3380 PrevI = PrevI->getPrevNode();
3381 }
3382 return false;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003383 }
3384
3385 /// See AAIsDead::isKnownDead(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003386 bool isKnownDead(const Instruction *I) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003387 return getKnown() && isAssumedDead(I);
3388 }
3389
Johannes Doerfert924d2132019-08-05 21:34:45 +00003390 /// Determine if \p F might catch asynchronous exceptions.
3391 static bool mayCatchAsynchronousExceptions(const Function &F) {
3392 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
3393 }
3394
Johannes Doerfert2f622062019-09-04 16:35:20 +00003395 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3396 /// that internal function called from \p BB should now be looked at.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003397 bool assumeLive(Attributor &A, const BasicBlock &BB) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00003398 if (!AssumedLiveBlocks.insert(&BB).second)
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003399 return false;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003400
3401 // We assume that all of BB is (probably) live now and if there are calls to
3402 // internal functions we will assume that those are now live as well. This
3403 // is a performance optimization for blocks with calls to a lot of internal
3404 // functions. It can however cause dead functions to be treated as live.
3405 for (const Instruction &I : BB)
3406 if (ImmutableCallSite ICS = ImmutableCallSite(&I))
3407 if (const Function *F = ICS.getCalledFunction())
Johannes Doerfert766f2cc2019-10-07 23:21:52 +00003408 if (F->hasLocalLinkage())
Johannes Doerfert2f622062019-09-04 16:35:20 +00003409 A.markLiveInternalFunction(*F);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003410 return true;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003411 }
3412
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003413 /// Collection of instructions that need to be explored again, e.g., we
3414 /// did assume they do not transfer control to (one of their) successors.
3415 SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003416
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003417 /// Collection of instructions that are known to not transfer control.
3418 SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3419
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003420 /// Collection of all assumed live BasicBlocks.
Johannes Doerfert4361da22019-08-04 18:38:53 +00003421 DenseSet<const BasicBlock *> AssumedLiveBlocks;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003422};
3423
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003424static bool
3425identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3426 AbstractAttribute &AA,
3427 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3428 const IRPosition &IPos = IRPosition::callsite_function(CB);
3429
3430 const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos);
3431 if (NoReturnAA.isAssumedNoReturn())
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003432 return !NoReturnAA.isKnownNoReturn();
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003433 if (CB.isTerminator())
3434 AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3435 else
3436 AliveSuccessors.push_back(CB.getNextNode());
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003437 return false;
3438}
3439
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003440static bool
3441identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3442 AbstractAttribute &AA,
3443 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3444 bool UsedAssumedInformation =
3445 identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
Johannes Doerfert924d2132019-08-05 21:34:45 +00003446
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003447 // First, determine if we can change an invoke to a call assuming the
3448 // callee is nounwind. This is not possible if the personality of the
3449 // function allows to catch asynchronous exceptions.
3450 if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3451 AliveSuccessors.push_back(&II.getUnwindDest()->front());
3452 } else {
3453 const IRPosition &IPos = IRPosition::callsite_function(II);
3454 const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos);
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003455 if (AANoUnw.isAssumedNoUnwind()) {
3456 UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3457 } else {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003458 AliveSuccessors.push_back(&II.getUnwindDest()->front());
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003459 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003460 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003461 return UsedAssumedInformation;
3462}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003463
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003464static bool
3465identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3466 AbstractAttribute &AA,
3467 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3468 bool UsedAssumedInformation = false;
3469 if (BI.getNumSuccessors() == 1) {
3470 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3471 } else {
Johannes Doerferte1eed6c2020-02-16 16:45:28 -06003472 Optional<ConstantInt *> CI = getAssumedConstantInt(
3473 A, *BI.getCondition(), AA, UsedAssumedInformation);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003474 if (!CI.hasValue()) {
3475 // No value yet, assume both edges are dead.
3476 } else if (CI.getValue()) {
3477 const BasicBlock *SuccBB =
3478 BI.getSuccessor(1 - CI.getValue()->getZExtValue());
3479 AliveSuccessors.push_back(&SuccBB->front());
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003480 } else {
3481 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3482 AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003483 UsedAssumedInformation = false;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003484 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003485 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003486 return UsedAssumedInformation;
3487}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003488
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003489static bool
3490identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3491 AbstractAttribute &AA,
3492 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003493 bool UsedAssumedInformation = false;
3494 Optional<ConstantInt *> CI =
Johannes Doerferte1eed6c2020-02-16 16:45:28 -06003495 getAssumedConstantInt(A, *SI.getCondition(), AA, UsedAssumedInformation);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003496 if (!CI.hasValue()) {
3497 // No value yet, assume all edges are dead.
3498 } else if (CI.getValue()) {
3499 for (auto &CaseIt : SI.cases()) {
3500 if (CaseIt.getCaseValue() == CI.getValue()) {
3501 AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003502 return UsedAssumedInformation;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003503 }
3504 }
Johannes Doerferted47a9c2019-11-01 13:57:49 -05003505 AliveSuccessors.push_back(&SI.getDefaultDest()->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003506 return UsedAssumedInformation;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003507 } else {
3508 for (const BasicBlock *SuccBB : successors(SI.getParent()))
3509 AliveSuccessors.push_back(&SuccBB->front());
3510 }
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003511 return UsedAssumedInformation;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003512}
3513
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003514ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003515 ChangeStatus Change = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003516
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003517 LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
3518 << getAssociatedFunction()->size() << "] BBs and "
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003519 << ToBeExploredFrom.size() << " exploration points and "
3520 << KnownDeadEnds.size() << " known dead ends\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003521
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003522 // Copy and clear the list of instructions we need to explore from. It is
3523 // refilled with instructions the next update has to look at.
3524 SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
3525 ToBeExploredFrom.end());
3526 decltype(ToBeExploredFrom) NewToBeExploredFrom;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003527
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003528 SmallVector<const Instruction *, 8> AliveSuccessors;
3529 while (!Worklist.empty()) {
3530 const Instruction *I = Worklist.pop_back_val();
3531 LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003532
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003533 AliveSuccessors.clear();
3534
3535 bool UsedAssumedInformation = false;
3536 switch (I->getOpcode()) {
3537 // TODO: look for (assumed) UB to backwards propagate "deadness".
3538 default:
3539 if (I->isTerminator()) {
3540 for (const BasicBlock *SuccBB : successors(I->getParent()))
3541 AliveSuccessors.push_back(&SuccBB->front());
3542 } else {
3543 AliveSuccessors.push_back(I->getNextNode());
3544 }
3545 break;
3546 case Instruction::Call:
3547 UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
3548 *this, AliveSuccessors);
3549 break;
3550 case Instruction::Invoke:
3551 UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
3552 *this, AliveSuccessors);
3553 break;
3554 case Instruction::Br:
3555 UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
3556 *this, AliveSuccessors);
3557 break;
3558 case Instruction::Switch:
3559 UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
3560 *this, AliveSuccessors);
3561 break;
Johannes Doerfert4361da22019-08-04 18:38:53 +00003562 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003563
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003564 if (UsedAssumedInformation) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003565 NewToBeExploredFrom.insert(I);
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003566 } else {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003567 Change = ChangeStatus::CHANGED;
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003568 if (AliveSuccessors.empty() ||
3569 (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors()))
3570 KnownDeadEnds.insert(I);
3571 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003572
3573 LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
3574 << AliveSuccessors.size() << " UsedAssumedInformation: "
3575 << UsedAssumedInformation << "\n");
3576
3577 for (const Instruction *AliveSuccessor : AliveSuccessors) {
3578 if (!I->isTerminator()) {
3579 assert(AliveSuccessors.size() == 1 &&
3580 "Non-terminator expected to have a single successor!");
3581 Worklist.push_back(AliveSuccessor);
3582 } else {
3583 if (assumeLive(A, *AliveSuccessor->getParent()))
3584 Worklist.push_back(AliveSuccessor);
3585 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00003586 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003587 }
3588
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003589 ToBeExploredFrom = std::move(NewToBeExploredFrom);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003590
Johannes Doerfertd6207812019-08-07 22:32:38 +00003591 // If we know everything is live there is no need to query for liveness.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003592 // Instead, indicating a pessimistic fixpoint will cause the state to be
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003593 // "invalid" and all queries to be answered conservatively without lookups.
3594 // To be in this state we have to (1) finished the exploration and (3) not
3595 // discovered any non-trivial dead end and (2) not ruled unreachable code
3596 // dead.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003597 if (ToBeExploredFrom.empty() &&
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003598 getAssociatedFunction()->size() == AssumedLiveBlocks.size() &&
3599 llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
3600 return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
3601 }))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003602 return indicatePessimisticFixpoint();
3603 return Change;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003604}
3605
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003606/// Liveness information for a call sites.
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003607struct AAIsDeadCallSite final : AAIsDeadFunction {
3608 AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {}
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003609
3610 /// See AbstractAttribute::initialize(...).
3611 void initialize(Attributor &A) override {
3612 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003613 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003614 // sense to specialize attributes for call sites instead of
3615 // redirecting requests to the callee.
3616 llvm_unreachable("Abstract attributes for liveness are not "
3617 "supported for call sites yet!");
3618 }
3619
3620 /// See AbstractAttribute::updateImpl(...).
3621 ChangeStatus updateImpl(Attributor &A) override {
3622 return indicatePessimisticFixpoint();
3623 }
3624
3625 /// See AbstractAttribute::trackStatistics()
3626 void trackStatistics() const override {}
3627};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003628
Hideto Ueno19c07af2019-07-23 08:16:17 +00003629/// -------------------- Dereferenceable Argument Attribute --------------------
3630
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003631template <>
3632ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
3633 const DerefState &R) {
Johannes Doerfert31784242019-10-29 23:18:49 -05003634 ChangeStatus CS0 =
3635 clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
3636 ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003637 return CS0 | CS1;
3638}
3639
Hideto Ueno70576ca2019-08-22 14:18:29 +00003640struct AADereferenceableImpl : AADereferenceable {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003641 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
Johannes Doerfert344d0382019-08-07 22:34:26 +00003642 using StateType = DerefState;
Hideto Ueno19c07af2019-07-23 08:16:17 +00003643
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00003644 void initialize(Attributor &A) override {
3645 SmallVector<Attribute, 4> Attrs;
3646 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
3647 Attrs);
3648 for (const Attribute &Attr : Attrs)
3649 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
3650
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06003651 NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition(),
3652 /* TrackDependence */ false);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003653
3654 const IRPosition &IRP = this->getIRPosition();
3655 bool IsFnInterface = IRP.isFnInterfaceKind();
Johannes Doerferta198adb2020-03-13 00:32:38 -05003656 Function *FnScope = IRP.getAnchorScope();
3657 if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope)))
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003658 indicatePessimisticFixpoint();
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00003659 }
3660
Hideto Ueno19c07af2019-07-23 08:16:17 +00003661 /// See AbstractAttribute::getState()
3662 /// {
Johannes Doerfert344d0382019-08-07 22:34:26 +00003663 StateType &getState() override { return *this; }
3664 const StateType &getState() const override { return *this; }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003665 /// }
3666
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003667 /// Helper function for collecting accessed bytes in must-be-executed-context
Hideto Uenobdcbdb42020-03-09 14:19:32 +09003668 void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
3669 DerefState &State) {
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003670 const Value *UseV = U->get();
3671 if (!UseV->getType()->isPointerTy())
3672 return;
3673
3674 Type *PtrTy = UseV->getType();
3675 const DataLayout &DL = A.getDataLayout();
3676 int64_t Offset;
3677 if (const Value *Base = getBasePointerOfAccessPointerOperand(
3678 I, Offset, DL, /*AllowNonInbounds*/ true)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09003679 if (Base == &getAssociatedValue() &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06003680 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003681 uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType());
Hideto Uenobdcbdb42020-03-09 14:19:32 +09003682 State.addAccessedBytes(Offset, Size);
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003683 }
3684 }
3685 return;
3686 }
3687
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003688 /// See AAFromMustBeExecutedContext
Hideto Uenobdcbdb42020-03-09 14:19:32 +09003689 bool followUse(Attributor &A, const Use *U, const Instruction *I,
3690 AADereferenceable::StateType &State) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003691 bool IsNonNull = false;
3692 bool TrackUse = false;
3693 int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
3694 A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003695
Hideto Uenobdcbdb42020-03-09 14:19:32 +09003696 addAccessedBytesForUse(A, U, I, State);
3697 State.takeKnownDerefBytesMaximum(DerefBytes);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003698 return TrackUse;
3699 }
3700
Hideto Uenodfedae52019-11-29 06:45:07 +00003701 /// See AbstractAttribute::manifest(...).
3702 ChangeStatus manifest(Attributor &A) override {
3703 ChangeStatus Change = AADereferenceable::manifest(A);
3704 if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
3705 removeAttrs({Attribute::DereferenceableOrNull});
3706 return ChangeStatus::CHANGED;
3707 }
3708 return Change;
3709 }
3710
Johannes Doerferteccdf082019-08-05 23:35:12 +00003711 void getDeducedAttributes(LLVMContext &Ctx,
3712 SmallVectorImpl<Attribute> &Attrs) const override {
Hideto Ueno19c07af2019-07-23 08:16:17 +00003713 // TODO: Add *_globally support
3714 if (isAssumedNonNull())
3715 Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
3716 Ctx, getAssumedDereferenceableBytes()));
3717 else
3718 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
3719 Ctx, getAssumedDereferenceableBytes()));
3720 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003721
3722 /// See AbstractAttribute::getAsStr().
3723 const std::string getAsStr() const override {
3724 if (!getAssumedDereferenceableBytes())
3725 return "unknown-dereferenceable";
3726 return std::string("dereferenceable") +
3727 (isAssumedNonNull() ? "" : "_or_null") +
3728 (isAssumedGlobal() ? "_globally" : "") + "<" +
3729 std::to_string(getKnownDereferenceableBytes()) + "-" +
3730 std::to_string(getAssumedDereferenceableBytes()) + ">";
3731 }
3732};
3733
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003734/// Dereferenceable attribute for a floating value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003735struct AADereferenceableFloating
3736 : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> {
3737 using Base =
3738 AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>;
3739 AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno19c07af2019-07-23 08:16:17 +00003740
3741 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003742 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003743 ChangeStatus Change = Base::updateImpl(A);
3744
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003745 const DataLayout &DL = A.getDataLayout();
3746
3747 auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool {
3748 unsigned IdxWidth =
3749 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
3750 APInt Offset(IdxWidth, 0);
3751 const Value *Base =
3752 V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
3753
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003754 const auto &AA =
3755 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003756 int64_t DerefBytes = 0;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003757 if (!Stripped && this == &AA) {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003758 // Use IR information if we did not strip anything.
3759 // TODO: track globally.
3760 bool CanBeNull;
3761 DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull);
3762 T.GlobalState.indicatePessimisticFixpoint();
3763 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003764 const DerefState &DS = static_cast<const DerefState &>(AA.getState());
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003765 DerefBytes = DS.DerefBytesState.getAssumed();
3766 T.GlobalState &= DS.GlobalState;
3767 }
3768
Hideto Ueno188f9a32020-01-15 15:25:52 +09003769 // TODO: Use `AAConstantRange` to infer dereferenceable bytes.
3770
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00003771 // For now we do not try to "increase" dereferenceability due to negative
3772 // indices as we first have to come up with code to deal with loops and
3773 // for overflows of the dereferenceable bytes.
Johannes Doerfert785fad32019-08-23 17:29:23 +00003774 int64_t OffsetSExt = Offset.getSExtValue();
3775 if (OffsetSExt < 0)
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00003776 OffsetSExt = 0;
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00003777
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003778 T.takeAssumedDerefBytesMinimum(
Johannes Doerfert785fad32019-08-23 17:29:23 +00003779 std::max(int64_t(0), DerefBytes - OffsetSExt));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003780
Johannes Doerfert785fad32019-08-23 17:29:23 +00003781 if (this == &AA) {
3782 if (!Stripped) {
3783 // If nothing was stripped IR information is all we got.
3784 T.takeKnownDerefBytesMaximum(
3785 std::max(int64_t(0), DerefBytes - OffsetSExt));
3786 T.indicatePessimisticFixpoint();
3787 } else if (OffsetSExt > 0) {
3788 // If something was stripped but there is circular reasoning we look
3789 // for the offset. If it is positive we basically decrease the
3790 // dereferenceable bytes in a circluar loop now, which will simply
3791 // drive them down to the known value in a very slow way which we
3792 // can accelerate.
3793 T.indicatePessimisticFixpoint();
3794 }
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003795 }
3796
3797 return T.isValidState();
3798 };
3799
3800 DerefState T;
3801 if (!genericValueTraversal<AADereferenceable, DerefState>(
3802 A, getIRPosition(), *this, T, VisitValueCB))
3803 return indicatePessimisticFixpoint();
3804
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003805 return Change | clampStateAndIndicateChange(getState(), T);
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003806 }
3807
3808 /// See AbstractAttribute::trackStatistics()
3809 void trackStatistics() const override {
3810 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
3811 }
3812};
3813
3814/// Dereferenceable attribute for a return value.
3815struct AADereferenceableReturned final
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003816 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003817 AADereferenceableReturned(const IRPosition &IRP)
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003818 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
3819 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003820
3821 /// See AbstractAttribute::trackStatistics()
3822 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003823 STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003824 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003825};
3826
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003827/// Dereferenceable attribute for an argument
3828struct AADereferenceableArgument final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003829 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003830 AADereferenceable, AADereferenceableImpl> {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003831 using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003832 AADereferenceable, AADereferenceableImpl>;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003833 AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003834
3835 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003836 void trackStatistics() const override {
Johannes Doerfert169af992019-08-20 06:09:56 +00003837 STATS_DECLTRACK_ARG_ATTR(dereferenceable)
3838 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003839};
3840
Hideto Ueno19c07af2019-07-23 08:16:17 +00003841/// Dereferenceable attribute for a call site argument.
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003842struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003843 AADereferenceableCallSiteArgument(const IRPosition &IRP)
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003844 : AADereferenceableFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003845
3846 /// See AbstractAttribute::trackStatistics()
3847 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003848 STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003849 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003850};
3851
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003852/// Dereferenceable attribute deduction for a call site return value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003853struct AADereferenceableCallSiteReturned final
3854 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3855 AADereferenceable, AADereferenceableImpl> {
3856 using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3857 AADereferenceable, AADereferenceableImpl>;
3858 AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003859
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003860 /// See AbstractAttribute::trackStatistics()
3861 void trackStatistics() const override {
3862 STATS_DECLTRACK_CS_ATTR(dereferenceable);
3863 }
3864};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003865
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003866// ------------------------ Align Argument Attribute ------------------------
3867
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003868static unsigned int getKnownAlignForUse(Attributor &A,
3869 AbstractAttribute &QueryingAA,
3870 Value &AssociatedValue, const Use *U,
3871 const Instruction *I, bool &TrackUse) {
Hideto Ueno78a75022019-11-26 07:51:59 +00003872 // We need to follow common pointer manipulation uses to the accesses they
3873 // feed into.
3874 if (isa<CastInst>(I)) {
Johannes Doerfertc90681b2020-01-02 16:41:17 -06003875 // Follow all but ptr2int casts.
3876 TrackUse = !isa<PtrToIntInst>(I);
Hideto Ueno78a75022019-11-26 07:51:59 +00003877 return 0;
3878 }
3879 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
3880 if (GEP->hasAllConstantIndices()) {
3881 TrackUse = true;
3882 return 0;
3883 }
3884 }
3885
3886 unsigned Alignment = 0;
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003887 if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
3888 if (ICS.isBundleOperand(U) || ICS.isCallee(U))
3889 return 0;
3890
3891 unsigned ArgNo = ICS.getArgumentNo(U);
3892 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
3893 // As long as we only use known information there is no need to track
3894 // dependences here.
3895 auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP,
3896 /* TrackDependence */ false);
Hideto Ueno78a75022019-11-26 07:51:59 +00003897 Alignment = AlignAA.getKnownAlign();
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003898 }
3899
Hideto Ueno78a75022019-11-26 07:51:59 +00003900 const Value *UseV = U->get();
Johannes Doerfert30ae8592020-01-10 12:13:10 -06003901 if (auto *SI = dyn_cast<StoreInst>(I)) {
3902 if (SI->getPointerOperand() == UseV)
3903 Alignment = SI->getAlignment();
3904 } else if (auto *LI = dyn_cast<LoadInst>(I))
Hideto Ueno78a75022019-11-26 07:51:59 +00003905 Alignment = LI->getAlignment();
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003906
Hideto Ueno78a75022019-11-26 07:51:59 +00003907 if (Alignment <= 1)
3908 return 0;
3909
3910 auto &DL = A.getDataLayout();
3911 int64_t Offset;
3912
3913 if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
3914 if (Base == &AssociatedValue) {
3915 // BasePointerAddr + Offset = Alignment * Q for some integer Q.
3916 // So we can say that the maximum power of two which is a divisor of
3917 // gcd(Offset, Alignment) is an alignment.
3918
3919 uint32_t gcd =
3920 greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
3921 Alignment = llvm::PowerOf2Floor(gcd);
3922 }
3923 }
3924
3925 return Alignment;
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003926}
Johannes Doerfert344d0382019-08-07 22:34:26 +00003927struct AAAlignImpl : AAAlign {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003928 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003929
Johannes Doerfert234eda52019-08-16 19:51:23 +00003930 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003931 void initialize(Attributor &A) override {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003932 SmallVector<Attribute, 4> Attrs;
3933 getAttrs({Attribute::Alignment}, Attrs);
3934 for (const Attribute &Attr : Attrs)
3935 takeKnownMaximum(Attr.getValueAsInt());
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003936
3937 if (getIRPosition().isFnInterfaceKind() &&
3938 (!getAssociatedFunction() ||
Johannes Doerferta198adb2020-03-13 00:32:38 -05003939 !A.isFunctionIPOAmendable(*getAssociatedFunction())))
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003940 indicatePessimisticFixpoint();
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003941 }
3942
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003943 /// See AbstractAttribute::manifest(...).
3944 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert30179d72020-01-12 00:25:45 -06003945 ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003946
3947 // Check for users that allow alignment annotations.
Johannes Doerferta801ee82020-02-19 16:52:16 -06003948 Value &AssociatedValue = getAssociatedValue();
3949 for (const Use &U : AssociatedValue.uses()) {
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003950 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
Johannes Doerferta801ee82020-02-19 16:52:16 -06003951 if (SI->getPointerOperand() == &AssociatedValue)
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003952 if (SI->getAlignment() < getAssumedAlign()) {
3953 STATS_DECLTRACK(AAAlign, Store,
James Hendersond68904f2020-01-06 10:15:44 +00003954 "Number of times alignment added to a store");
Guillaume Chateletd400d452019-10-03 13:17:21 +00003955 SI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert30179d72020-01-12 00:25:45 -06003956 LoadStoreChanged = ChangeStatus::CHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003957 }
3958 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
Johannes Doerferta801ee82020-02-19 16:52:16 -06003959 if (LI->getPointerOperand() == &AssociatedValue)
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003960 if (LI->getAlignment() < getAssumedAlign()) {
Guillaume Chatelet17380222019-09-30 09:37:05 +00003961 LI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003962 STATS_DECLTRACK(AAAlign, Load,
James Hendersond68904f2020-01-06 10:15:44 +00003963 "Number of times alignment added to a load");
Johannes Doerfert30179d72020-01-12 00:25:45 -06003964 LoadStoreChanged = ChangeStatus::CHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003965 }
3966 }
3967 }
3968
Johannes Doerfert30179d72020-01-12 00:25:45 -06003969 ChangeStatus Changed = AAAlign::manifest(A);
3970
3971 MaybeAlign InheritAlign =
3972 getAssociatedValue().getPointerAlignment(A.getDataLayout());
3973 if (InheritAlign.valueOrOne() >= getAssumedAlign())
3974 return LoadStoreChanged;
3975 return Changed | LoadStoreChanged;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003976 }
3977
Johannes Doerfert81df4522019-08-30 15:22:28 +00003978 // TODO: Provide a helper to determine the implied ABI alignment and check in
3979 // the existing manifest method and a new one for AAAlignImpl that value
3980 // to avoid making the alignment explicit if it did not improve.
3981
3982 /// See AbstractAttribute::getDeducedAttributes
3983 virtual void
3984 getDeducedAttributes(LLVMContext &Ctx,
3985 SmallVectorImpl<Attribute> &Attrs) const override {
3986 if (getAssumedAlign() > 1)
Guillaume Chateletb65fa482019-10-15 12:56:24 +00003987 Attrs.emplace_back(
3988 Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
Johannes Doerfert81df4522019-08-30 15:22:28 +00003989 }
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003990 /// See AAFromMustBeExecutedContext
Hideto Uenobdcbdb42020-03-09 14:19:32 +09003991 bool followUse(Attributor &A, const Use *U, const Instruction *I,
3992 AAAlign::StateType &State) {
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003993 bool TrackUse = false;
3994
Hideto Ueno4ecf2552019-12-12 13:42:40 +00003995 unsigned int KnownAlign =
3996 getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
Hideto Uenobdcbdb42020-03-09 14:19:32 +09003997 State.takeKnownMaximum(KnownAlign);
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003998
3999 return TrackUse;
4000 }
Johannes Doerfert81df4522019-08-30 15:22:28 +00004001
4002 /// See AbstractAttribute::getAsStr().
4003 const std::string getAsStr() const override {
4004 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
4005 "-" + std::to_string(getAssumedAlign()) + ">")
4006 : "unknown-align";
4007 }
4008};
4009
4010/// Align attribute for a floating value.
Hideto Ueno88b04ef2019-11-12 06:36:49 +00004011struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> {
4012 using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>;
4013 AAAlignFloating(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert81df4522019-08-30 15:22:28 +00004014
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00004015 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert234eda52019-08-16 19:51:23 +00004016 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno88b04ef2019-11-12 06:36:49 +00004017 Base::updateImpl(A);
4018
Johannes Doerfert234eda52019-08-16 19:51:23 +00004019 const DataLayout &DL = A.getDataLayout();
4020
Johannes Doerfertb9b87912019-08-20 06:02:39 +00004021 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
4022 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004023 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V));
4024 if (!Stripped && this == &AA) {
Johannes Doerfert234eda52019-08-16 19:51:23 +00004025 // Use only IR information if we did not strip anything.
Guillaume Chateletbae629b2019-10-15 13:58:22 +00004026 const MaybeAlign PA = V.getPointerAlignment(DL);
4027 T.takeKnownMaximum(PA ? PA->value() : 0);
Johannes Doerfert234eda52019-08-16 19:51:23 +00004028 T.indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00004029 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004030 // Use abstract attribute information.
4031 const AAAlign::StateType &DS =
4032 static_cast<const AAAlign::StateType &>(AA.getState());
4033 T ^= DS;
Johannes Doerfert234eda52019-08-16 19:51:23 +00004034 }
Johannes Doerfertb9b87912019-08-20 06:02:39 +00004035 return T.isValidState();
Johannes Doerfert234eda52019-08-16 19:51:23 +00004036 };
4037
4038 StateType T;
4039 if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
4040 VisitValueCB))
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00004041 return indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00004042
Johannes Doerfert028b2aa2019-08-20 05:57:01 +00004043 // TODO: If we know we visited all incoming values, thus no are assumed
4044 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +00004045 return clampStateAndIndicateChange(getState(), T);
4046 }
4047
4048 /// See AbstractAttribute::trackStatistics()
4049 void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
4050};
4051
4052/// Align attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004053struct AAAlignReturned final
4054 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00004055 AAAlignReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004056 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00004057
4058 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00004059 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00004060};
4061
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00004062/// Align attribute for function argument.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004063struct AAAlignArgument final
Hideto Ueno88b04ef2019-11-12 06:36:49 +00004064 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
4065 AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00004066 AAAlignArgument(const IRPosition &IRP)
Hideto Ueno88b04ef2019-11-12 06:36:49 +00004067 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
4068 AAAlignImpl>(
4069 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00004070
4071 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert169af992019-08-20 06:09:56 +00004072 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00004073};
4074
Johannes Doerfert234eda52019-08-16 19:51:23 +00004075struct AAAlignCallSiteArgument final : AAAlignFloating {
4076 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00004077
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00004078 /// See AbstractAttribute::manifest(...).
4079 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert30179d72020-01-12 00:25:45 -06004080 ChangeStatus Changed = AAAlignImpl::manifest(A);
4081 MaybeAlign InheritAlign =
4082 getAssociatedValue().getPointerAlignment(A.getDataLayout());
4083 if (InheritAlign.valueOrOne() >= getAssumedAlign())
4084 Changed = ChangeStatus::UNCHANGED;
4085 return Changed;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00004086 }
4087
Johannes Doerfertdada8132019-12-31 01:27:50 -06004088 /// See AbstractAttribute::updateImpl(Attributor &A).
4089 ChangeStatus updateImpl(Attributor &A) override {
4090 ChangeStatus Changed = AAAlignFloating::updateImpl(A);
4091 if (Argument *Arg = getAssociatedArgument()) {
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004092 // We only take known information from the argument
4093 // so we do not need to track a dependence.
Johannes Doerfertdada8132019-12-31 01:27:50 -06004094 const auto &ArgAlignAA = A.getAAFor<AAAlign>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004095 *this, IRPosition::argument(*Arg), /* TrackDependence */ false);
Johannes Doerfertdada8132019-12-31 01:27:50 -06004096 takeKnownMaximum(ArgAlignAA.getKnownAlign());
4097 }
4098 return Changed;
4099 }
4100
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00004101 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00004102 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00004103};
4104
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00004105/// Align attribute deduction for a call site return value.
Hideto Ueno88b04ef2019-11-12 06:36:49 +00004106struct AAAlignCallSiteReturned final
4107 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
4108 AAAlignImpl> {
4109 using Base =
4110 AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
4111 AAAlignImpl>;
4112 AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert3fac6682019-08-30 15:24:52 +00004113
4114 /// See AbstractAttribute::initialize(...).
4115 void initialize(Attributor &A) override {
Hideto Ueno88b04ef2019-11-12 06:36:49 +00004116 Base::initialize(A);
Johannes Doerfert3fac6682019-08-30 15:24:52 +00004117 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00004118 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00004119 indicatePessimisticFixpoint();
4120 }
4121
Johannes Doerfert3fac6682019-08-30 15:24:52 +00004122 /// See AbstractAttribute::trackStatistics()
4123 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
4124};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00004125
Johannes Doerferte83f3032019-08-05 23:22:05 +00004126/// ------------------ Function No-Return Attribute ----------------------------
Johannes Doerfert344d0382019-08-07 22:34:26 +00004127struct AANoReturnImpl : public AANoReturn {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00004128 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
Johannes Doerferte83f3032019-08-05 23:22:05 +00004129
Johannes Doerfert0cc2b612019-10-13 21:25:53 +00004130 /// See AbstractAttribute::initialize(...).
4131 void initialize(Attributor &A) override {
4132 AANoReturn::initialize(A);
4133 Function *F = getAssociatedFunction();
Johannes Doerfert1b6041a2019-11-01 20:17:48 -05004134 if (!F)
Johannes Doerfert0cc2b612019-10-13 21:25:53 +00004135 indicatePessimisticFixpoint();
4136 }
4137
Johannes Doerferte83f3032019-08-05 23:22:05 +00004138 /// See AbstractAttribute::getAsStr().
4139 const std::string getAsStr() const override {
4140 return getAssumed() ? "noreturn" : "may-return";
4141 }
4142
Johannes Doerferte83f3032019-08-05 23:22:05 +00004143 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00004144 virtual ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00004145 auto CheckForNoReturn = [](Instruction &) { return false; };
Johannes Doerfert710ebb02019-08-14 21:18:01 +00004146 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
Johannes Doerfertd0f64002019-08-06 00:32:43 +00004147 {(unsigned)Instruction::Ret}))
Johannes Doerferte83f3032019-08-05 23:22:05 +00004148 return indicatePessimisticFixpoint();
Johannes Doerferte83f3032019-08-05 23:22:05 +00004149 return ChangeStatus::UNCHANGED;
4150 }
4151};
4152
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004153struct AANoReturnFunction final : AANoReturnImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00004154 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00004155
4156 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00004157 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004158};
4159
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00004160/// NoReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00004161struct AANoReturnCallSite final : AANoReturnImpl {
4162 AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
4163
Johannes Doerfert3fac6682019-08-30 15:24:52 +00004164 /// See AbstractAttribute::updateImpl(...).
4165 ChangeStatus updateImpl(Attributor &A) override {
4166 // TODO: Once we have call site specific value information we can provide
4167 // call site specific liveness information and then it makes
4168 // sense to specialize attributes for call sites arguments instead of
4169 // redirecting requests to the callee argument.
4170 Function *F = getAssociatedFunction();
4171 const IRPosition &FnPos = IRPosition::function(*F);
4172 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos);
4173 return clampStateAndIndicateChange(
4174 getState(),
4175 static_cast<const AANoReturn::StateType &>(FnAA.getState()));
4176 }
4177
4178 /// See AbstractAttribute::trackStatistics()
4179 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
4180};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00004181
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004182/// ----------------------- Variable Capturing ---------------------------------
4183
4184/// A class to hold the state of for no-capture attributes.
4185struct AANoCaptureImpl : public AANoCapture {
4186 AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {}
4187
4188 /// See AbstractAttribute::initialize(...).
4189 void initialize(Attributor &A) override {
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05004190 if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4191 indicateOptimisticFixpoint();
4192 return;
4193 }
4194 Function *AnchorScope = getAnchorScope();
4195 if (isFnInterfaceKind() &&
Johannes Doerferta198adb2020-03-13 00:32:38 -05004196 (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05004197 indicatePessimisticFixpoint();
4198 return;
4199 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004200
Johannes Doerfert72adda12019-10-10 05:33:21 +00004201 // You cannot "capture" null in the default address space.
4202 if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4203 getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4204 indicateOptimisticFixpoint();
4205 return;
4206 }
4207
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05004208 const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004209
4210 // Check what state the associated function can actually capture.
4211 if (F)
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05004212 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00004213 else
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004214 indicatePessimisticFixpoint();
4215 }
4216
4217 /// See AbstractAttribute::updateImpl(...).
4218 ChangeStatus updateImpl(Attributor &A) override;
4219
4220 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4221 virtual void
4222 getDeducedAttributes(LLVMContext &Ctx,
4223 SmallVectorImpl<Attribute> &Attrs) const override {
4224 if (!isAssumedNoCaptureMaybeReturned())
4225 return;
4226
Hideto Ueno37367642019-09-11 06:52:11 +00004227 if (getArgNo() >= 0) {
4228 if (isAssumedNoCapture())
4229 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4230 else if (ManifestInternal)
4231 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4232 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004233 }
4234
4235 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4236 /// depending on the ability of the function associated with \p IRP to capture
4237 /// state in memory and through "returning/throwing", respectively.
Johannes Doerfert3839b572019-10-21 00:48:42 +00004238 static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4239 const Function &F,
Johannes Doerfert1a746452019-10-20 22:28:49 -05004240 BitIntegerState &State) {
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004241 // TODO: Once we have memory behavior attributes we should use them here.
4242
4243 // If we know we cannot communicate or write to memory, we do not care about
4244 // ptr2int anymore.
4245 if (F.onlyReadsMemory() && F.doesNotThrow() &&
4246 F.getReturnType()->isVoidTy()) {
4247 State.addKnownBits(NO_CAPTURE);
4248 return;
4249 }
4250
4251 // A function cannot capture state in memory if it only reads memory, it can
4252 // however return/throw state and the state might be influenced by the
4253 // pointer value, e.g., loading from a returned pointer might reveal a bit.
4254 if (F.onlyReadsMemory())
4255 State.addKnownBits(NOT_CAPTURED_IN_MEM);
4256
4257 // A function cannot communicate state back if it does not through
4258 // exceptions and doesn not return values.
4259 if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4260 State.addKnownBits(NOT_CAPTURED_IN_RET);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004261
4262 // Check existing "returned" attributes.
4263 int ArgNo = IRP.getArgNo();
4264 if (F.doesNotThrow() && ArgNo >= 0) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01004265 for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
Johannes Doerfert3839b572019-10-21 00:48:42 +00004266 if (F.hasParamAttribute(u, Attribute::Returned)) {
Johannes Doerfert9d5ad5e2019-10-21 01:29:10 +00004267 if (u == unsigned(ArgNo))
Johannes Doerfert3839b572019-10-21 00:48:42 +00004268 State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4269 else if (F.onlyReadsMemory())
4270 State.addKnownBits(NO_CAPTURE);
4271 else
4272 State.addKnownBits(NOT_CAPTURED_IN_RET);
4273 break;
4274 }
4275 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004276 }
4277
4278 /// See AbstractState::getAsStr().
4279 const std::string getAsStr() const override {
4280 if (isKnownNoCapture())
4281 return "known not-captured";
4282 if (isAssumedNoCapture())
4283 return "assumed not-captured";
4284 if (isKnownNoCaptureMaybeReturned())
4285 return "known not-captured-maybe-returned";
4286 if (isAssumedNoCaptureMaybeReturned())
4287 return "assumed not-captured-maybe-returned";
4288 return "assumed-captured";
4289 }
4290};
4291
4292/// Attributor-aware capture tracker.
4293struct AACaptureUseTracker final : public CaptureTracker {
4294
4295 /// Create a capture tracker that can lookup in-flight abstract attributes
4296 /// through the Attributor \p A.
4297 ///
4298 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4299 /// search is stopped. If a use leads to a return instruction,
4300 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4301 /// If a use leads to a ptr2int which may capture the value,
4302 /// \p CapturedInInteger is set. If a use is found that is currently assumed
4303 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4304 /// set. All values in \p PotentialCopies are later tracked as well. For every
4305 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4306 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4307 /// conservatively set to true.
4308 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
Johannes Doerfert31784242019-10-29 23:18:49 -05004309 const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004310 SmallVectorImpl<const Value *> &PotentialCopies,
4311 unsigned &RemainingUsesToExplore)
4312 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4313 PotentialCopies(PotentialCopies),
4314 RemainingUsesToExplore(RemainingUsesToExplore) {}
4315
4316 /// Determine if \p V maybe captured. *Also updates the state!*
4317 bool valueMayBeCaptured(const Value *V) {
4318 if (V->getType()->isPointerTy()) {
4319 PointerMayBeCaptured(V, this);
4320 } else {
4321 State.indicatePessimisticFixpoint();
4322 }
4323 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4324 }
4325
4326 /// See CaptureTracker::tooManyUses().
4327 void tooManyUses() override {
4328 State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4329 }
4330
4331 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4332 if (CaptureTracker::isDereferenceableOrNull(O, DL))
4333 return true;
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004334 const auto &DerefAA = A.getAAFor<AADereferenceable>(
4335 NoCaptureAA, IRPosition::value(*O), /* TrackDependence */ true,
4336 DepClassTy::OPTIONAL);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004337 return DerefAA.getAssumedDereferenceableBytes();
4338 }
4339
4340 /// See CaptureTracker::captured(...).
4341 bool captured(const Use *U) override {
4342 Instruction *UInst = cast<Instruction>(U->getUser());
4343 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
4344 << "\n");
4345
4346 // Because we may reuse the tracker multiple times we keep track of the
4347 // number of explored uses ourselves as well.
4348 if (RemainingUsesToExplore-- == 0) {
4349 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
4350 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4351 /* Return */ true);
4352 }
4353
4354 // Deal with ptr2int by following uses.
4355 if (isa<PtrToIntInst>(UInst)) {
4356 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
4357 return valueMayBeCaptured(UInst);
4358 }
4359
4360 // Explicitly catch return instructions.
4361 if (isa<ReturnInst>(UInst))
4362 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4363 /* Return */ true);
4364
4365 // For now we only use special logic for call sites. However, the tracker
4366 // itself knows about a lot of other non-capturing cases already.
4367 CallSite CS(UInst);
4368 if (!CS || !CS.isArgOperand(U))
4369 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4370 /* Return */ true);
4371
4372 unsigned ArgNo = CS.getArgumentNo(U);
4373 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
4374 // If we have a abstract no-capture attribute for the argument we can use
4375 // it to justify a non-capture attribute here. This allows recursion!
4376 auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos);
4377 if (ArgNoCaptureAA.isAssumedNoCapture())
4378 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4379 /* Return */ false);
4380 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4381 addPotentialCopy(CS);
4382 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4383 /* Return */ false);
4384 }
4385
4386 // Lastly, we could not find a reason no-capture can be assumed so we don't.
4387 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4388 /* Return */ true);
4389 }
4390
4391 /// Register \p CS as potential copy of the value we are checking.
4392 void addPotentialCopy(CallSite CS) {
4393 PotentialCopies.push_back(CS.getInstruction());
4394 }
4395
4396 /// See CaptureTracker::shouldExplore(...).
4397 bool shouldExplore(const Use *U) override {
Johannes Doerfert23f41f12020-01-12 01:09:22 -06004398 // Check liveness.
4399 return !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004400 }
4401
4402 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4403 /// \p CapturedInRet, then return the appropriate value for use in the
4404 /// CaptureTracker::captured() interface.
4405 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
4406 bool CapturedInRet) {
4407 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
4408 << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
4409 if (CapturedInMem)
4410 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
4411 if (CapturedInInt)
4412 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
4413 if (CapturedInRet)
4414 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
4415 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4416 }
4417
4418private:
4419 /// The attributor providing in-flight abstract attributes.
4420 Attributor &A;
4421
4422 /// The abstract attribute currently updated.
4423 AANoCapture &NoCaptureAA;
4424
4425 /// The abstract liveness state.
4426 const AAIsDead &IsDeadAA;
4427
4428 /// The state currently updated.
Johannes Doerfert31784242019-10-29 23:18:49 -05004429 AANoCapture::StateType &State;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004430
4431 /// Set of potential copies of the tracked value.
4432 SmallVectorImpl<const Value *> &PotentialCopies;
4433
4434 /// Global counter to limit the number of explored uses.
4435 unsigned &RemainingUsesToExplore;
4436};
4437
4438ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
4439 const IRPosition &IRP = getIRPosition();
4440 const Value *V =
4441 getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue();
4442 if (!V)
4443 return indicatePessimisticFixpoint();
4444
4445 const Function *F =
4446 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
4447 assert(F && "Expected a function!");
Johannes Doerfert3839b572019-10-21 00:48:42 +00004448 const IRPosition &FnPos = IRPosition::function(*F);
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004449 const auto &IsDeadAA =
4450 A.getAAFor<AAIsDead>(*this, FnPos, /* TrackDependence */ false);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004451
4452 AANoCapture::StateType T;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004453
Johannes Doerfert3839b572019-10-21 00:48:42 +00004454 // Readonly means we cannot capture through memory.
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004455 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
4456 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004457 if (FnMemAA.isAssumedReadOnly()) {
4458 T.addKnownBits(NOT_CAPTURED_IN_MEM);
4459 if (FnMemAA.isKnownReadOnly())
4460 addKnownBits(NOT_CAPTURED_IN_MEM);
4461 }
4462
4463 // Make sure all returned values are different than the underlying value.
4464 // TODO: we could do this in a more sophisticated way inside
4465 // AAReturnedValues, e.g., track all values that escape through returns
4466 // directly somehow.
4467 auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
4468 bool SeenConstant = false;
4469 for (auto &It : RVAA.returned_values()) {
4470 if (isa<Constant>(It.first)) {
4471 if (SeenConstant)
4472 return false;
4473 SeenConstant = true;
4474 } else if (!isa<Argument>(It.first) ||
4475 It.first == getAssociatedArgument())
4476 return false;
4477 }
4478 return true;
4479 };
4480
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004481 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
4482 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004483 if (NoUnwindAA.isAssumedNoUnwind()) {
4484 bool IsVoidTy = F->getReturnType()->isVoidTy();
4485 const AAReturnedValues *RVAA =
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004486 IsVoidTy ? nullptr
4487 : &A.getAAFor<AAReturnedValues>(*this, FnPos,
4488 /* TrackDependence */ true,
4489 DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004490 if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
4491 T.addKnownBits(NOT_CAPTURED_IN_RET);
4492 if (T.isKnown(NOT_CAPTURED_IN_MEM))
4493 return ChangeStatus::UNCHANGED;
4494 if (NoUnwindAA.isKnownNoUnwind() &&
4495 (IsVoidTy || RVAA->getState().isAtFixpoint())) {
4496 addKnownBits(NOT_CAPTURED_IN_RET);
4497 if (isKnown(NOT_CAPTURED_IN_MEM))
4498 return indicateOptimisticFixpoint();
4499 }
4500 }
4501 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004502
4503 // Use the CaptureTracker interface and logic with the specialized tracker,
4504 // defined in AACaptureUseTracker, that can look at in-flight abstract
4505 // attributes and directly updates the assumed state.
4506 SmallVector<const Value *, 4> PotentialCopies;
4507 unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore;
4508 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
4509 RemainingUsesToExplore);
4510
4511 // Check all potential copies of the associated value until we can assume
4512 // none will be captured or we have to assume at least one might be.
4513 unsigned Idx = 0;
4514 PotentialCopies.push_back(V);
4515 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
4516 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
4517
Johannes Doerfert1a746452019-10-20 22:28:49 -05004518 AANoCapture::StateType &S = getState();
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004519 auto Assumed = S.getAssumed();
4520 S.intersectAssumedBits(T.getAssumed());
Johannes Doerfert31784242019-10-29 23:18:49 -05004521 if (!isAssumedNoCaptureMaybeReturned())
4522 return indicatePessimisticFixpoint();
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004523 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
4524 : ChangeStatus::CHANGED;
4525}
4526
4527/// NoCapture attribute for function arguments.
4528struct AANoCaptureArgument final : AANoCaptureImpl {
4529 AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4530
4531 /// See AbstractAttribute::trackStatistics()
4532 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
4533};
4534
4535/// NoCapture attribute for call site arguments.
4536struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
4537 AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4538
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06004539 /// See AbstractAttribute::initialize(...).
4540 void initialize(Attributor &A) override {
4541 if (Argument *Arg = getAssociatedArgument())
4542 if (Arg->hasByValAttr())
4543 indicateOptimisticFixpoint();
4544 AANoCaptureImpl::initialize(A);
4545 }
4546
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004547 /// See AbstractAttribute::updateImpl(...).
4548 ChangeStatus updateImpl(Attributor &A) override {
4549 // TODO: Once we have call site specific value information we can provide
4550 // call site specific liveness information and then it makes
4551 // sense to specialize attributes for call sites arguments instead of
4552 // redirecting requests to the callee argument.
4553 Argument *Arg = getAssociatedArgument();
4554 if (!Arg)
4555 return indicatePessimisticFixpoint();
4556 const IRPosition &ArgPos = IRPosition::argument(*Arg);
4557 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos);
4558 return clampStateAndIndicateChange(
4559 getState(),
4560 static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
4561 }
4562
4563 /// See AbstractAttribute::trackStatistics()
4564 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
4565};
4566
4567/// NoCapture attribute for floating values.
4568struct AANoCaptureFloating final : AANoCaptureImpl {
4569 AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4570
4571 /// See AbstractAttribute::trackStatistics()
4572 void trackStatistics() const override {
4573 STATS_DECLTRACK_FLOATING_ATTR(nocapture)
4574 }
4575};
4576
4577/// NoCapture attribute for function return value.
4578struct AANoCaptureReturned final : AANoCaptureImpl {
4579 AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {
4580 llvm_unreachable("NoCapture is not applicable to function returns!");
4581 }
4582
4583 /// See AbstractAttribute::initialize(...).
4584 void initialize(Attributor &A) override {
4585 llvm_unreachable("NoCapture is not applicable to function returns!");
4586 }
4587
4588 /// See AbstractAttribute::updateImpl(...).
4589 ChangeStatus updateImpl(Attributor &A) override {
4590 llvm_unreachable("NoCapture is not applicable to function returns!");
4591 }
4592
4593 /// See AbstractAttribute::trackStatistics()
4594 void trackStatistics() const override {}
4595};
4596
4597/// NoCapture attribute deduction for a call site return value.
4598struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
4599 AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4600
4601 /// See AbstractAttribute::trackStatistics()
4602 void trackStatistics() const override {
4603 STATS_DECLTRACK_CSRET_ATTR(nocapture)
4604 }
4605};
4606
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004607/// ------------------ Value Simplify Attribute ----------------------------
4608struct AAValueSimplifyImpl : AAValueSimplify {
4609 AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {}
4610
Johannes Doerfert9dcf8892020-01-11 23:59:36 -06004611 /// See AbstractAttribute::initialize(...).
4612 void initialize(Attributor &A) override {
4613 if (getAssociatedValue().getType()->isVoidTy())
4614 indicatePessimisticFixpoint();
4615 }
4616
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004617 /// See AbstractAttribute::getAsStr().
4618 const std::string getAsStr() const override {
4619 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
4620 : "not-simple";
4621 }
4622
4623 /// See AbstractAttribute::trackStatistics()
4624 void trackStatistics() const override {}
4625
4626 /// See AAValueSimplify::getAssumedSimplifiedValue()
4627 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
4628 if (!getAssumed())
4629 return const_cast<Value *>(&getAssociatedValue());
4630 return SimplifiedAssociatedValue;
4631 }
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004632
4633 /// Helper function for querying AAValueSimplify and updating candicate.
4634 /// \param QueryingValue Value trying to unify with SimplifiedValue
4635 /// \param AccumulatedSimplifiedValue Current simplification result.
4636 static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
4637 Value &QueryingValue,
4638 Optional<Value *> &AccumulatedSimplifiedValue) {
4639 // FIXME: Add a typecast support.
4640
Johannes Doerfertd07b5a52020-01-12 00:00:33 -06004641 auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004642 QueryingAA, IRPosition::value(QueryingValue));
4643
4644 Optional<Value *> QueryingValueSimplified =
Johannes Doerfertd07b5a52020-01-12 00:00:33 -06004645 ValueSimplifyAA.getAssumedSimplifiedValue(A);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004646
4647 if (!QueryingValueSimplified.hasValue())
4648 return true;
4649
4650 if (!QueryingValueSimplified.getValue())
4651 return false;
4652
4653 Value &QueryingValueSimplifiedUnwrapped =
4654 *QueryingValueSimplified.getValue();
4655
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004656 if (AccumulatedSimplifiedValue.hasValue() &&
4657 !isa<UndefValue>(AccumulatedSimplifiedValue.getValue()) &&
4658 !isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004659 return AccumulatedSimplifiedValue == QueryingValueSimplified;
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004660 if (AccumulatedSimplifiedValue.hasValue() &&
4661 isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
4662 return true;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004663
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004664 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004665 << " is assumed to be "
4666 << QueryingValueSimplifiedUnwrapped << "\n");
4667
4668 AccumulatedSimplifiedValue = QueryingValueSimplified;
4669 return true;
4670 }
4671
Hideto Ueno188f9a32020-01-15 15:25:52 +09004672 bool askSimplifiedValueForAAValueConstantRange(Attributor &A) {
4673 if (!getAssociatedValue().getType()->isIntegerTy())
4674 return false;
4675
4676 const auto &ValueConstantRangeAA =
4677 A.getAAFor<AAValueConstantRange>(*this, getIRPosition());
4678
4679 Optional<ConstantInt *> COpt =
4680 ValueConstantRangeAA.getAssumedConstantInt(A);
4681 if (COpt.hasValue()) {
4682 if (auto *C = COpt.getValue())
4683 SimplifiedAssociatedValue = C;
4684 else
4685 return false;
4686 } else {
Johannes Doerfert63adbb92020-02-09 20:21:56 -06004687 SimplifiedAssociatedValue = llvm::None;
Hideto Ueno188f9a32020-01-15 15:25:52 +09004688 }
4689 return true;
4690 }
4691
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004692 /// See AbstractAttribute::manifest(...).
4693 ChangeStatus manifest(Attributor &A) override {
4694 ChangeStatus Changed = ChangeStatus::UNCHANGED;
4695
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004696 if (SimplifiedAssociatedValue.hasValue() &&
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004697 !SimplifiedAssociatedValue.getValue())
4698 return Changed;
4699
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004700 Value &V = getAssociatedValue();
4701 auto *C = SimplifiedAssociatedValue.hasValue()
4702 ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())
4703 : UndefValue::get(V.getType());
4704 if (C) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004705 // We can replace the AssociatedValue with the constant.
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004706 if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004707 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C
4708 << " :: " << *this << "\n");
Johannes Doerfert4c62a352020-01-12 00:17:08 -06004709 if (A.changeValueAfterManifest(V, *C))
4710 Changed = ChangeStatus::CHANGED;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004711 }
4712 }
4713
4714 return Changed | AAValueSimplify::manifest(A);
4715 }
4716
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004717 /// See AbstractState::indicatePessimisticFixpoint(...).
4718 ChangeStatus indicatePessimisticFixpoint() override {
4719 // NOTE: Associated value will be returned in a pessimistic fixpoint and is
4720 // regarded as known. That's why`indicateOptimisticFixpoint` is called.
4721 SimplifiedAssociatedValue = &getAssociatedValue();
Johannes Doerferta4b35882019-12-31 13:25:47 -06004722 indicateOptimisticFixpoint();
4723 return ChangeStatus::CHANGED;
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004724 }
4725
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004726protected:
4727 // An assumed simplified value. Initially, it is set to Optional::None, which
4728 // means that the value is not clear under current assumption. If in the
4729 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4730 // returns orignal associated value.
4731 Optional<Value *> SimplifiedAssociatedValue;
4732};
4733
4734struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
4735 AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4736
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004737 void initialize(Attributor &A) override {
4738 AAValueSimplifyImpl::initialize(A);
4739 if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration())
4740 indicatePessimisticFixpoint();
4741 if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest},
4742 /* IgnoreSubsumingPositions */ true))
4743 indicatePessimisticFixpoint();
Johannes Doerfert16188f92020-02-16 23:04:25 -06004744
4745 // FIXME: This is a hack to prevent us from propagating function poiner in
4746 // the new pass manager CGSCC pass as it creates call edges the
4747 // CallGraphUpdater cannot handle yet.
4748 Value &V = getAssociatedValue();
4749 if (V.getType()->isPointerTy() &&
4750 V.getType()->getPointerElementType()->isFunctionTy() &&
Johannes Doerfert97082792020-02-21 14:18:30 -08004751 !A.isModulePass())
Johannes Doerfert16188f92020-02-16 23:04:25 -06004752 indicatePessimisticFixpoint();
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004753 }
4754
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004755 /// See AbstractAttribute::updateImpl(...).
4756 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004757 // Byval is only replacable if it is readonly otherwise we would write into
4758 // the replaced value and not the copy that byval creates implicitly.
4759 Argument *Arg = getAssociatedArgument();
4760 if (Arg->hasByValAttr()) {
Johannes Doerfert8e76fec2020-02-16 17:37:50 -06004761 // TODO: We probably need to verify synchronization is not an issue, e.g.,
4762 // there is no race by not copying a constant byval.
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004763 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
4764 if (!MemAA.isAssumedReadOnly())
4765 return indicatePessimisticFixpoint();
4766 }
4767
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004768 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4769
Johannes Doerfert661db042019-10-07 23:14:58 +00004770 auto PredForCallSite = [&](AbstractCallSite ACS) {
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004771 const IRPosition &ACSArgPos =
4772 IRPosition::callsite_argument(ACS, getArgNo());
4773 // Check if a coresponding argument was found or if it is on not
4774 // associated (which can happen for callback calls).
4775 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
Hideto Ueno4ecf2552019-12-12 13:42:40 +00004776 return false;
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004777
Johannes Doerferte360ee62019-11-01 18:45:25 -05004778 // We can only propagate thread independent values through callbacks.
4779 // This is different to direct/indirect call sites because for them we
4780 // know the thread executing the caller and callee is the same. For
4781 // callbacks this is not guaranteed, thus a thread dependent value could
4782 // be different for the caller and callee, making it invalid to propagate.
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004783 Value &ArgOp = ACSArgPos.getAssociatedValue();
Johannes Doerferte360ee62019-11-01 18:45:25 -05004784 if (ACS.isCallbackCall())
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004785 if (auto *C = dyn_cast<Constant>(&ArgOp))
Johannes Doerferte360ee62019-11-01 18:45:25 -05004786 if (C->isThreadDependent())
4787 return false;
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004788 return checkAndUpdate(A, *this, ArgOp, SimplifiedAssociatedValue);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004789 };
4790
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06004791 bool AllCallSitesKnown;
4792 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
4793 AllCallSitesKnown))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004794 if (!askSimplifiedValueForAAValueConstantRange(A))
4795 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004796
4797 // If a candicate was found in this update, return CHANGED.
4798 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4799 ? ChangeStatus::UNCHANGED
4800 : ChangeStatus ::CHANGED;
4801 }
4802
4803 /// See AbstractAttribute::trackStatistics()
4804 void trackStatistics() const override {
4805 STATS_DECLTRACK_ARG_ATTR(value_simplify)
4806 }
4807};
4808
4809struct AAValueSimplifyReturned : AAValueSimplifyImpl {
4810 AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4811
4812 /// See AbstractAttribute::updateImpl(...).
4813 ChangeStatus updateImpl(Attributor &A) override {
4814 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4815
4816 auto PredForReturned = [&](Value &V) {
4817 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4818 };
4819
4820 if (!A.checkForAllReturnedValues(PredForReturned, *this))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004821 if (!askSimplifiedValueForAAValueConstantRange(A))
4822 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004823
4824 // If a candicate was found in this update, return CHANGED.
4825 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4826 ? ChangeStatus::UNCHANGED
4827 : ChangeStatus ::CHANGED;
4828 }
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004829
4830 ChangeStatus manifest(Attributor &A) override {
4831 ChangeStatus Changed = ChangeStatus::UNCHANGED;
4832
4833 if (SimplifiedAssociatedValue.hasValue() &&
4834 !SimplifiedAssociatedValue.getValue())
4835 return Changed;
4836
4837 Value &V = getAssociatedValue();
4838 auto *C = SimplifiedAssociatedValue.hasValue()
4839 ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())
4840 : UndefValue::get(V.getType());
4841 if (C) {
4842 auto PredForReturned =
4843 [&](Value &V, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
4844 // We can replace the AssociatedValue with the constant.
4845 if (&V == C || V.getType() != C->getType() || isa<UndefValue>(V))
4846 return true;
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004847
4848 for (ReturnInst *RI : RetInsts) {
4849 if (RI->getFunction() != getAnchorScope())
4850 continue;
4851 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C
4852 << " in " << *RI << " :: " << *this << "\n");
4853 if (A.changeUseAfterManifest(RI->getOperandUse(0), *C))
4854 Changed = ChangeStatus::CHANGED;
4855 }
4856 return true;
4857 };
4858 A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
4859 }
4860
4861 return Changed | AAValueSimplify::manifest(A);
4862 }
4863
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004864 /// See AbstractAttribute::trackStatistics()
4865 void trackStatistics() const override {
4866 STATS_DECLTRACK_FNRET_ATTR(value_simplify)
4867 }
4868};
4869
4870struct AAValueSimplifyFloating : AAValueSimplifyImpl {
4871 AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4872
4873 /// See AbstractAttribute::initialize(...).
4874 void initialize(Attributor &A) override {
Johannes Doerfert97082792020-02-21 14:18:30 -08004875 // FIXME: This might have exposed a SCC iterator update bug in the old PM.
4876 // Needs investigation.
4877 // AAValueSimplifyImpl::initialize(A);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004878 Value &V = getAnchorValue();
4879
4880 // TODO: add other stuffs
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004881 if (isa<Constant>(V))
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004882 indicatePessimisticFixpoint();
4883 }
4884
4885 /// See AbstractAttribute::updateImpl(...).
4886 ChangeStatus updateImpl(Attributor &A) override {
4887 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4888
Johannes Doerfert76843ba2020-01-28 23:51:25 -06004889 auto VisitValueCB = [&](Value &V, bool &, bool Stripped) -> bool {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004890 auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V));
4891 if (!Stripped && this == &AA) {
4892 // TODO: Look the instruction and check recursively.
Hideto Ueno188f9a32020-01-15 15:25:52 +09004893
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004894 LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
4895 << "\n");
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004896 return false;
4897 }
4898 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4899 };
4900
Johannes Doerfert76843ba2020-01-28 23:51:25 -06004901 bool Dummy = false;
Johannes Doerfert6626d1b2020-01-28 11:49:59 -06004902 if (!genericValueTraversal<AAValueSimplify, bool>(A, getIRPosition(), *this,
4903 Dummy, VisitValueCB))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004904 if (!askSimplifiedValueForAAValueConstantRange(A))
4905 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004906
4907 // If a candicate was found in this update, return CHANGED.
4908
4909 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4910 ? ChangeStatus::UNCHANGED
4911 : ChangeStatus ::CHANGED;
4912 }
4913
4914 /// See AbstractAttribute::trackStatistics()
4915 void trackStatistics() const override {
4916 STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
4917 }
4918};
4919
4920struct AAValueSimplifyFunction : AAValueSimplifyImpl {
4921 AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4922
4923 /// See AbstractAttribute::initialize(...).
4924 void initialize(Attributor &A) override {
4925 SimplifiedAssociatedValue = &getAnchorValue();
4926 indicateOptimisticFixpoint();
4927 }
4928 /// See AbstractAttribute::initialize(...).
4929 ChangeStatus updateImpl(Attributor &A) override {
4930 llvm_unreachable(
4931 "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
4932 }
4933 /// See AbstractAttribute::trackStatistics()
4934 void trackStatistics() const override {
4935 STATS_DECLTRACK_FN_ATTR(value_simplify)
4936 }
4937};
4938
4939struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
4940 AAValueSimplifyCallSite(const IRPosition &IRP)
4941 : AAValueSimplifyFunction(IRP) {}
4942 /// See AbstractAttribute::trackStatistics()
4943 void trackStatistics() const override {
4944 STATS_DECLTRACK_CS_ATTR(value_simplify)
4945 }
4946};
4947
4948struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
4949 AAValueSimplifyCallSiteReturned(const IRPosition &IRP)
4950 : AAValueSimplifyReturned(IRP) {}
4951
Johannes Doerfertb2c76002020-01-23 17:12:56 -06004952 /// See AbstractAttribute::manifest(...).
4953 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004954 return AAValueSimplifyImpl::manifest(A);
Johannes Doerfertb2c76002020-01-23 17:12:56 -06004955 }
4956
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004957 void trackStatistics() const override {
4958 STATS_DECLTRACK_CSRET_ATTR(value_simplify)
4959 }
4960};
4961struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
4962 AAValueSimplifyCallSiteArgument(const IRPosition &IRP)
4963 : AAValueSimplifyFloating(IRP) {}
4964
4965 void trackStatistics() const override {
4966 STATS_DECLTRACK_CSARG_ATTR(value_simplify)
4967 }
4968};
4969
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004970/// ----------------------- Heap-To-Stack Conversion ---------------------------
4971struct AAHeapToStackImpl : public AAHeapToStack {
4972 AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {}
4973
4974 const std::string getAsStr() const override {
4975 return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
4976 }
4977
4978 ChangeStatus manifest(Attributor &A) override {
4979 assert(getState().isValidState() &&
4980 "Attempted to manifest an invalid state!");
4981
4982 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
4983 Function *F = getAssociatedFunction();
4984 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
4985
4986 for (Instruction *MallocCall : MallocCalls) {
4987 // This malloc cannot be replaced.
4988 if (BadMallocCalls.count(MallocCall))
4989 continue;
4990
4991 for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
4992 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
4993 A.deleteAfterManifest(*FreeCall);
4994 HasChanged = ChangeStatus::CHANGED;
4995 }
4996
4997 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
4998 << "\n");
4999
5000 Constant *Size;
5001 if (isCallocLikeFn(MallocCall, TLI)) {
5002 auto *Num = cast<ConstantInt>(MallocCall->getOperand(0));
5003 auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1));
5004 APInt TotalSize = SizeT->getValue() * Num->getValue();
5005 Size =
5006 ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize);
5007 } else {
5008 Size = cast<ConstantInt>(MallocCall->getOperand(0));
5009 }
5010
5011 unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
5012 Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
5013 Size, "", MallocCall->getNextNode());
5014
5015 if (AI->getType() != MallocCall->getType())
5016 AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
5017 AI->getNextNode());
5018
Johannes Doerfert4c62a352020-01-12 00:17:08 -06005019 A.changeValueAfterManifest(*MallocCall, *AI);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005020
5021 if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
5022 auto *NBB = II->getNormalDest();
5023 BranchInst::Create(NBB, MallocCall->getParent());
5024 A.deleteAfterManifest(*MallocCall);
5025 } else {
5026 A.deleteAfterManifest(*MallocCall);
5027 }
5028
5029 if (isCallocLikeFn(MallocCall, TLI)) {
5030 auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
5031 AI->getNextNode());
5032 Value *Ops[] = {
5033 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
5034 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
5035
5036 Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
5037 Module *M = F->getParent();
5038 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
5039 CallInst::Create(Fn, Ops, "", BI->getNextNode());
5040 }
5041 HasChanged = ChangeStatus::CHANGED;
5042 }
5043
5044 return HasChanged;
5045 }
5046
5047 /// Collection of all malloc calls in a function.
5048 SmallSetVector<Instruction *, 4> MallocCalls;
5049
5050 /// Collection of malloc calls that cannot be converted.
5051 DenseSet<const Instruction *> BadMallocCalls;
5052
5053 /// A map for each malloc call to the set of associated free calls.
5054 DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
5055
5056 ChangeStatus updateImpl(Attributor &A) override;
5057};
5058
5059ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
5060 const Function *F = getAssociatedFunction();
5061 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
5062
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005063 MustBeExecutedContextExplorer &Explorer =
5064 A.getInfoCache().getMustBeExecutedContextExplorer();
5065
5066 auto FreeCheck = [&](Instruction &I) {
5067 const auto &Frees = FreesForMalloc.lookup(&I);
5068 if (Frees.size() != 1)
5069 return false;
5070 Instruction *UniqueFree = *Frees.begin();
5071 return Explorer.findInContextOf(UniqueFree, I.getNextNode());
5072 };
5073
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005074 auto UsesCheck = [&](Instruction &I) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005075 bool ValidUsesOnly = true;
5076 bool MustUse = true;
Hideto Ueno827bade2019-12-12 12:26:30 +00005077 auto Pred = [&](const Use &U, bool &Follow) -> bool {
5078 Instruction *UserI = cast<Instruction>(U.getUser());
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00005079 if (isa<LoadInst>(UserI))
Hideto Ueno827bade2019-12-12 12:26:30 +00005080 return true;
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00005081 if (auto *SI = dyn_cast<StoreInst>(UserI)) {
Hideto Ueno827bade2019-12-12 12:26:30 +00005082 if (SI->getValueOperand() == U.get()) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005083 LLVM_DEBUG(dbgs()
5084 << "[H2S] escaping store to memory: " << *UserI << "\n");
5085 ValidUsesOnly = false;
5086 } else {
5087 // A store into the malloc'ed memory is fine.
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00005088 }
Hideto Ueno827bade2019-12-12 12:26:30 +00005089 return true;
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00005090 }
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005091 if (auto *CB = dyn_cast<CallBase>(UserI)) {
Hideto Ueno827bade2019-12-12 12:26:30 +00005092 if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
5093 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005094 // Record malloc.
5095 if (isFreeCall(UserI, TLI)) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005096 if (MustUse) {
Hideto Ueno827bade2019-12-12 12:26:30 +00005097 FreesForMalloc[&I].insert(UserI);
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005098 } else {
5099 LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: "
5100 << *UserI << "\n");
5101 ValidUsesOnly = false;
5102 }
Hideto Ueno827bade2019-12-12 12:26:30 +00005103 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005104 }
5105
Hideto Ueno827bade2019-12-12 12:26:30 +00005106 unsigned ArgNo = CB->getArgOperandNo(&U);
Stefan Stipanovica516fba2019-11-17 21:35:04 +01005107
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005108 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
5109 *this, IRPosition::callsite_argument(*CB, ArgNo));
5110
Stefan Stipanovica516fba2019-11-17 21:35:04 +01005111 // If a callsite argument use is nofree, we are fine.
5112 const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
5113 *this, IRPosition::callsite_argument(*CB, ArgNo));
5114
Hideto Ueno827bade2019-12-12 12:26:30 +00005115 if (!NoCaptureAA.isAssumedNoCapture() ||
5116 !ArgNoFreeAA.isAssumedNoFree()) {
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005117 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005118 ValidUsesOnly = false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005119 }
Hideto Ueno827bade2019-12-12 12:26:30 +00005120 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005121 }
5122
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005123 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
5124 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
5125 MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI));
Hideto Ueno827bade2019-12-12 12:26:30 +00005126 Follow = true;
5127 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005128 }
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005129 // Unknown user for which we can not track uses further (in a way that
5130 // makes sense).
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005131 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005132 ValidUsesOnly = false;
Hideto Ueno827bade2019-12-12 12:26:30 +00005133 return true;
5134 };
5135 A.checkForAllUses(Pred, *this, I);
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005136 return ValidUsesOnly;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005137 };
5138
5139 auto MallocCallocCheck = [&](Instruction &I) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00005140 if (BadMallocCalls.count(&I))
5141 return true;
5142
5143 bool IsMalloc = isMallocLikeFn(&I, TLI);
5144 bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
5145 if (!IsMalloc && !IsCalloc) {
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005146 BadMallocCalls.insert(&I);
5147 return true;
5148 }
5149
Johannes Doerfertd20f8072019-10-13 03:54:08 +00005150 if (IsMalloc) {
5151 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
Stefan Stipanovicfff8ec92019-12-17 20:41:09 +01005152 if (Size->getValue().ule(MaxHeapToStackSize))
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005153 if (UsesCheck(I) || FreeCheck(I)) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00005154 MallocCalls.insert(&I);
5155 return true;
5156 }
5157 } else if (IsCalloc) {
5158 bool Overflow = false;
5159 if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
5160 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
5161 if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
Stefan Stipanovicfff8ec92019-12-17 20:41:09 +01005162 .ule(MaxHeapToStackSize))
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005163 if (!Overflow && (UsesCheck(I) || FreeCheck(I))) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00005164 MallocCalls.insert(&I);
5165 return true;
5166 }
5167 }
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005168
Johannes Doerfertd20f8072019-10-13 03:54:08 +00005169 BadMallocCalls.insert(&I);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005170 return true;
5171 };
5172
5173 size_t NumBadMallocs = BadMallocCalls.size();
5174
5175 A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
5176
5177 if (NumBadMallocs != BadMallocCalls.size())
5178 return ChangeStatus::CHANGED;
5179
5180 return ChangeStatus::UNCHANGED;
5181}
5182
5183struct AAHeapToStackFunction final : public AAHeapToStackImpl {
5184 AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {}
5185
5186 /// See AbstractAttribute::trackStatistics()
5187 void trackStatistics() const override {
5188 STATS_DECL(MallocCalls, Function,
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005189 "Number of malloc calls converted to allocas");
5190 for (auto *C : MallocCalls)
5191 if (!BadMallocCalls.count(C))
5192 ++BUILD_STAT_NAME(MallocCalls, Function);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005193 }
5194};
5195
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005196/// ----------------------- Privatizable Pointers ------------------------------
5197struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
5198 AAPrivatizablePtrImpl(const IRPosition &IRP)
5199 : AAPrivatizablePtr(IRP), PrivatizableType(llvm::None) {}
5200
5201 ChangeStatus indicatePessimisticFixpoint() override {
5202 AAPrivatizablePtr::indicatePessimisticFixpoint();
5203 PrivatizableType = nullptr;
5204 return ChangeStatus::CHANGED;
5205 }
5206
5207 /// Identify the type we can chose for a private copy of the underlying
5208 /// argument. None means it is not clear yet, nullptr means there is none.
5209 virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
5210
5211 /// Return a privatizable type that encloses both T0 and T1.
5212 /// TODO: This is merely a stub for now as we should manage a mapping as well.
5213 Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
5214 if (!T0.hasValue())
5215 return T1;
5216 if (!T1.hasValue())
5217 return T0;
5218 if (T0 == T1)
5219 return T0;
5220 return nullptr;
5221 }
5222
5223 Optional<Type *> getPrivatizableType() const override {
5224 return PrivatizableType;
5225 }
5226
5227 const std::string getAsStr() const override {
5228 return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
5229 }
5230
5231protected:
5232 Optional<Type *> PrivatizableType;
5233};
5234
5235// TODO: Do this for call site arguments (probably also other values) as well.
5236
5237struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
5238 AAPrivatizablePtrArgument(const IRPosition &IRP)
5239 : AAPrivatizablePtrImpl(IRP) {}
5240
5241 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5242 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5243 // If this is a byval argument and we know all the call sites (so we can
5244 // rewrite them), there is no need to check them explicitly.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005245 bool AllCallSitesKnown;
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005246 if (getIRPosition().hasAttr(Attribute::ByVal) &&
5247 A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005248 true, AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005249 return getAssociatedValue().getType()->getPointerElementType();
5250
5251 Optional<Type *> Ty;
5252 unsigned ArgNo = getIRPosition().getArgNo();
5253
5254 // Make sure the associated call site argument has the same type at all call
5255 // sites and it is an allocation we know is safe to privatize, for now that
5256 // means we only allow alloca instructions.
5257 // TODO: We can additionally analyze the accesses in the callee to create
5258 // the type from that information instead. That is a little more
5259 // involved and will be done in a follow up patch.
5260 auto CallSiteCheck = [&](AbstractCallSite ACS) {
5261 IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
5262 // Check if a coresponding argument was found or if it is one not
5263 // associated (which can happen for callback calls).
5264 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5265 return false;
5266
5267 // Check that all call sites agree on a type.
5268 auto &PrivCSArgAA = A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos);
5269 Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
5270
5271 LLVM_DEBUG({
5272 dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
5273 if (CSTy.hasValue() && CSTy.getValue())
5274 CSTy.getValue()->print(dbgs());
5275 else if (CSTy.hasValue())
5276 dbgs() << "<nullptr>";
5277 else
5278 dbgs() << "<none>";
5279 });
5280
5281 Ty = combineTypes(Ty, CSTy);
5282
5283 LLVM_DEBUG({
5284 dbgs() << " : New Type: ";
5285 if (Ty.hasValue() && Ty.getValue())
5286 Ty.getValue()->print(dbgs());
5287 else if (Ty.hasValue())
5288 dbgs() << "<nullptr>";
5289 else
5290 dbgs() << "<none>";
5291 dbgs() << "\n";
5292 });
5293
5294 return !Ty.hasValue() || Ty.getValue();
5295 };
5296
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005297 if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005298 return nullptr;
5299 return Ty;
5300 }
5301
5302 /// See AbstractAttribute::updateImpl(...).
5303 ChangeStatus updateImpl(Attributor &A) override {
5304 PrivatizableType = identifyPrivatizableType(A);
5305 if (!PrivatizableType.hasValue())
5306 return ChangeStatus::UNCHANGED;
5307 if (!PrivatizableType.getValue())
5308 return indicatePessimisticFixpoint();
5309
5310 // Avoid arguments with padding for now.
5311 if (!getIRPosition().hasAttr(Attribute::ByVal) &&
5312 !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
5313 A.getInfoCache().getDL())) {
5314 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
5315 return indicatePessimisticFixpoint();
5316 }
5317
5318 // Verify callee and caller agree on how the promoted argument would be
5319 // passed.
5320 // TODO: The use of the ArgumentPromotion interface here is ugly, we need a
5321 // specialized form of TargetTransformInfo::areFunctionArgsABICompatible
5322 // which doesn't require the arguments ArgumentPromotion wanted to pass.
5323 Function &Fn = *getIRPosition().getAnchorScope();
5324 SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy;
5325 ArgsToPromote.insert(getAssociatedArgument());
5326 const auto *TTI =
5327 A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
5328 if (!TTI ||
5329 !ArgumentPromotionPass::areFunctionArgsABICompatible(
5330 Fn, *TTI, ArgsToPromote, Dummy) ||
5331 ArgsToPromote.empty()) {
5332 LLVM_DEBUG(
5333 dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
5334 << Fn.getName() << "\n");
5335 return indicatePessimisticFixpoint();
5336 }
5337
5338 // Collect the types that will replace the privatizable type in the function
5339 // signature.
5340 SmallVector<Type *, 16> ReplacementTypes;
5341 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5342
5343 // Register a rewrite of the argument.
5344 Argument *Arg = getAssociatedArgument();
5345 if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
5346 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
5347 return indicatePessimisticFixpoint();
5348 }
5349
5350 unsigned ArgNo = Arg->getArgNo();
5351
5352 // Helper to check if for the given call site the associated argument is
5353 // passed to a callback where the privatization would be different.
5354 auto IsCompatiblePrivArgOfCallback = [&](CallSite CS) {
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005355 SmallVector<const Use *, 4> CBUses;
5356 AbstractCallSite::getCallbackUses(CS, CBUses);
5357 for (const Use *U : CBUses) {
5358 AbstractCallSite CBACS(U);
5359 assert(CBACS && CBACS.isCallbackCall());
5360 for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
5361 int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
5362
5363 LLVM_DEBUG({
5364 dbgs()
5365 << "[AAPrivatizablePtr] Argument " << *Arg
5366 << "check if can be privatized in the context of its parent ("
5367 << Arg->getParent()->getName()
5368 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5369 "callback ("
5370 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5371 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
Michael Forster676c2962020-01-30 10:20:49 +01005372 << CBACS.getCallArgOperand(CBArg) << " vs "
5373 << CS.getArgOperand(ArgNo) << "\n"
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005374 << "[AAPrivatizablePtr] " << CBArg << " : "
5375 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
5376 });
5377
5378 if (CBArgNo != int(ArgNo))
5379 continue;
5380 const auto &CBArgPrivAA =
5381 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(CBArg));
5382 if (CBArgPrivAA.isValidState()) {
5383 auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
5384 if (!CBArgPrivTy.hasValue())
5385 continue;
5386 if (CBArgPrivTy.getValue() == PrivatizableType)
5387 continue;
5388 }
5389
5390 LLVM_DEBUG({
5391 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5392 << " cannot be privatized in the context of its parent ("
5393 << Arg->getParent()->getName()
5394 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5395 "callback ("
5396 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5397 << ").\n[AAPrivatizablePtr] for which the argument "
5398 "privatization is not compatible.\n";
5399 });
5400 return false;
5401 }
5402 }
5403 return true;
5404 };
5405
5406 // Helper to check if for the given call site the associated argument is
5407 // passed to a direct call where the privatization would be different.
5408 auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
5409 CallBase *DC = cast<CallBase>(ACS.getInstruction());
5410 int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
5411 assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() &&
5412 "Expected a direct call operand for callback call operand");
5413
5414 LLVM_DEBUG({
5415 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5416 << " check if be privatized in the context of its parent ("
5417 << Arg->getParent()->getName()
5418 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5419 "direct call of ("
5420 << DCArgNo << "@" << DC->getCalledFunction()->getName()
5421 << ").\n";
5422 });
5423
5424 Function *DCCallee = DC->getCalledFunction();
5425 if (unsigned(DCArgNo) < DCCallee->arg_size()) {
5426 const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
5427 *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)));
5428 if (DCArgPrivAA.isValidState()) {
5429 auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
5430 if (!DCArgPrivTy.hasValue())
5431 return true;
5432 if (DCArgPrivTy.getValue() == PrivatizableType)
5433 return true;
5434 }
5435 }
5436
5437 LLVM_DEBUG({
5438 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5439 << " cannot be privatized in the context of its parent ("
5440 << Arg->getParent()->getName()
5441 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5442 "direct call of ("
5443 << ACS.getCallSite().getCalledFunction()->getName()
5444 << ").\n[AAPrivatizablePtr] for which the argument "
5445 "privatization is not compatible.\n";
5446 });
5447 return false;
5448 };
5449
5450 // Helper to check if the associated argument is used at the given abstract
5451 // call site in a way that is incompatible with the privatization assumed
5452 // here.
5453 auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
5454 if (ACS.isDirectCall())
5455 return IsCompatiblePrivArgOfCallback(ACS.getCallSite());
5456 if (ACS.isCallbackCall())
5457 return IsCompatiblePrivArgOfDirectCS(ACS);
5458 return false;
5459 };
5460
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005461 bool AllCallSitesKnown;
5462 if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
5463 AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005464 return indicatePessimisticFixpoint();
5465
5466 return ChangeStatus::UNCHANGED;
5467 }
5468
5469 /// Given a type to private \p PrivType, collect the constituates (which are
5470 /// used) in \p ReplacementTypes.
5471 static void
5472 identifyReplacementTypes(Type *PrivType,
5473 SmallVectorImpl<Type *> &ReplacementTypes) {
5474 // TODO: For now we expand the privatization type to the fullest which can
5475 // lead to dead arguments that need to be removed later.
5476 assert(PrivType && "Expected privatizable type!");
5477
5478 // Traverse the type, extract constituate types on the outermost level.
5479 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5480 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
5481 ReplacementTypes.push_back(PrivStructType->getElementType(u));
5482 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5483 ReplacementTypes.append(PrivArrayType->getNumElements(),
5484 PrivArrayType->getElementType());
5485 } else {
5486 ReplacementTypes.push_back(PrivType);
5487 }
5488 }
5489
5490 /// Initialize \p Base according to the type \p PrivType at position \p IP.
5491 /// The values needed are taken from the arguments of \p F starting at
5492 /// position \p ArgNo.
5493 static void createInitialization(Type *PrivType, Value &Base, Function &F,
5494 unsigned ArgNo, Instruction &IP) {
5495 assert(PrivType && "Expected privatizable type!");
5496
5497 IRBuilder<NoFolder> IRB(&IP);
5498 const DataLayout &DL = F.getParent()->getDataLayout();
5499
5500 // Traverse the type, build GEPs and stores.
5501 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5502 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5503 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5504 Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
5505 Value *Ptr = constructPointer(
5506 PointeeTy, &Base, PrivStructLayout->getElementOffset(u), IRB, DL);
5507 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5508 }
5509 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5510 Type *PointeePtrTy = PrivArrayType->getElementType()->getPointerTo();
5511 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeePtrTy);
5512 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5513 Value *Ptr =
5514 constructPointer(PointeePtrTy, &Base, u * PointeeTySize, IRB, DL);
5515 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5516 }
5517 } else {
5518 new StoreInst(F.getArg(ArgNo), &Base, &IP);
5519 }
5520 }
5521
5522 /// Extract values from \p Base according to the type \p PrivType at the
5523 /// call position \p ACS. The values are appended to \p ReplacementValues.
5524 void createReplacementValues(Type *PrivType, AbstractCallSite ACS,
5525 Value *Base,
5526 SmallVectorImpl<Value *> &ReplacementValues) {
5527 assert(Base && "Expected base value!");
5528 assert(PrivType && "Expected privatizable type!");
5529 Instruction *IP = ACS.getInstruction();
5530
5531 IRBuilder<NoFolder> IRB(IP);
5532 const DataLayout &DL = IP->getModule()->getDataLayout();
5533
5534 if (Base->getType()->getPointerElementType() != PrivType)
5535 Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(),
5536 "", ACS.getInstruction());
5537
5538 // TODO: Improve the alignment of the loads.
5539 // Traverse the type, build GEPs and loads.
5540 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5541 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5542 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5543 Type *PointeeTy = PrivStructType->getElementType(u);
5544 Value *Ptr =
5545 constructPointer(PointeeTy->getPointerTo(), Base,
5546 PrivStructLayout->getElementOffset(u), IRB, DL);
5547 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
5548 L->setAlignment(MaybeAlign(1));
5549 ReplacementValues.push_back(L);
5550 }
5551 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5552 Type *PointeeTy = PrivArrayType->getElementType();
5553 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
5554 Type *PointeePtrTy = PointeeTy->getPointerTo();
5555 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5556 Value *Ptr =
5557 constructPointer(PointeePtrTy, Base, u * PointeeTySize, IRB, DL);
5558 LoadInst *L = new LoadInst(PointeePtrTy, Ptr, "", IP);
5559 L->setAlignment(MaybeAlign(1));
5560 ReplacementValues.push_back(L);
5561 }
5562 } else {
5563 LoadInst *L = new LoadInst(PrivType, Base, "", IP);
5564 L->setAlignment(MaybeAlign(1));
5565 ReplacementValues.push_back(L);
5566 }
5567 }
5568
5569 /// See AbstractAttribute::manifest(...)
5570 ChangeStatus manifest(Attributor &A) override {
5571 if (!PrivatizableType.hasValue())
5572 return ChangeStatus::UNCHANGED;
5573 assert(PrivatizableType.getValue() && "Expected privatizable type!");
5574
5575 // Collect all tail calls in the function as we cannot allow new allocas to
5576 // escape into tail recursion.
5577 // TODO: Be smarter about new allocas escaping into tail calls.
5578 SmallVector<CallInst *, 16> TailCalls;
5579 if (!A.checkForAllInstructions(
5580 [&](Instruction &I) {
5581 CallInst &CI = cast<CallInst>(I);
5582 if (CI.isTailCall())
5583 TailCalls.push_back(&CI);
5584 return true;
5585 },
5586 *this, {Instruction::Call}))
5587 return ChangeStatus::UNCHANGED;
5588
5589 Argument *Arg = getAssociatedArgument();
5590
5591 // Callback to repair the associated function. A new alloca is placed at the
5592 // beginning and initialized with the values passed through arguments. The
5593 // new alloca replaces the use of the old pointer argument.
5594 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
5595 [=](const Attributor::ArgumentReplacementInfo &ARI,
5596 Function &ReplacementFn, Function::arg_iterator ArgIt) {
5597 BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
5598 Instruction *IP = &*EntryBB.getFirstInsertionPt();
5599 auto *AI = new AllocaInst(PrivatizableType.getValue(), 0,
5600 Arg->getName() + ".priv", IP);
5601 createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
5602 ArgIt->getArgNo(), *IP);
5603 Arg->replaceAllUsesWith(AI);
5604
5605 for (CallInst *CI : TailCalls)
5606 CI->setTailCall(false);
5607 };
5608
5609 // Callback to repair a call site of the associated function. The elements
5610 // of the privatizable type are loaded prior to the call and passed to the
5611 // new function version.
5612 Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
5613 [=](const Attributor::ArgumentReplacementInfo &ARI,
5614 AbstractCallSite ACS, SmallVectorImpl<Value *> &NewArgOperands) {
5615 createReplacementValues(
5616 PrivatizableType.getValue(), ACS,
5617 ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
Johannes Doerfertf3bf4b02020-03-23 02:03:23 -05005618 NewArgOperands);
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005619 };
5620
5621 // Collect the types that will replace the privatizable type in the function
5622 // signature.
5623 SmallVector<Type *, 16> ReplacementTypes;
5624 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5625
5626 // Register a rewrite of the argument.
5627 if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
5628 std::move(FnRepairCB),
5629 std::move(ACSRepairCB)))
5630 return ChangeStatus::CHANGED;
5631 return ChangeStatus::UNCHANGED;
5632 }
5633
5634 /// See AbstractAttribute::trackStatistics()
5635 void trackStatistics() const override {
5636 STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
5637 }
5638};
5639
5640struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
5641 AAPrivatizablePtrFloating(const IRPosition &IRP)
5642 : AAPrivatizablePtrImpl(IRP) {}
5643
5644 /// See AbstractAttribute::initialize(...).
5645 virtual void initialize(Attributor &A) override {
5646 // TODO: We can privatize more than arguments.
5647 indicatePessimisticFixpoint();
5648 }
5649
5650 ChangeStatus updateImpl(Attributor &A) override {
5651 llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
5652 "updateImpl will not be called");
5653 }
5654
5655 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5656 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5657 Value *Obj =
5658 GetUnderlyingObject(&getAssociatedValue(), A.getInfoCache().getDL());
5659 if (!Obj) {
5660 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
5661 return nullptr;
5662 }
5663
5664 if (auto *AI = dyn_cast<AllocaInst>(Obj))
5665 if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
5666 if (CI->isOne())
5667 return Obj->getType()->getPointerElementType();
5668 if (auto *Arg = dyn_cast<Argument>(Obj)) {
5669 auto &PrivArgAA =
5670 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(*Arg));
5671 if (PrivArgAA.isAssumedPrivatizablePtr())
5672 return Obj->getType()->getPointerElementType();
5673 }
5674
5675 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
5676 "alloca nor privatizable argument: "
5677 << *Obj << "!\n");
5678 return nullptr;
5679 }
5680
5681 /// See AbstractAttribute::trackStatistics()
5682 void trackStatistics() const override {
5683 STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
5684 }
5685};
5686
5687struct AAPrivatizablePtrCallSiteArgument final
5688 : public AAPrivatizablePtrFloating {
5689 AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP)
5690 : AAPrivatizablePtrFloating(IRP) {}
5691
5692 /// See AbstractAttribute::initialize(...).
5693 void initialize(Attributor &A) override {
5694 if (getIRPosition().hasAttr(Attribute::ByVal))
5695 indicateOptimisticFixpoint();
5696 }
5697
5698 /// See AbstractAttribute::updateImpl(...).
5699 ChangeStatus updateImpl(Attributor &A) override {
5700 PrivatizableType = identifyPrivatizableType(A);
5701 if (!PrivatizableType.hasValue())
5702 return ChangeStatus::UNCHANGED;
5703 if (!PrivatizableType.getValue())
5704 return indicatePessimisticFixpoint();
5705
5706 const IRPosition &IRP = getIRPosition();
5707 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
5708 if (!NoCaptureAA.isAssumedNoCapture()) {
5709 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
5710 return indicatePessimisticFixpoint();
5711 }
5712
5713 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP);
5714 if (!NoAliasAA.isAssumedNoAlias()) {
5715 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
5716 return indicatePessimisticFixpoint();
5717 }
5718
5719 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, IRP);
5720 if (!MemBehaviorAA.isAssumedReadOnly()) {
5721 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
5722 return indicatePessimisticFixpoint();
5723 }
5724
5725 return ChangeStatus::UNCHANGED;
5726 }
5727
5728 /// See AbstractAttribute::trackStatistics()
5729 void trackStatistics() const override {
5730 STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
5731 }
5732};
5733
5734struct AAPrivatizablePtrCallSiteReturned final
5735 : public AAPrivatizablePtrFloating {
5736 AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP)
5737 : AAPrivatizablePtrFloating(IRP) {}
5738
5739 /// See AbstractAttribute::initialize(...).
5740 void initialize(Attributor &A) override {
5741 // TODO: We can privatize more than arguments.
5742 indicatePessimisticFixpoint();
5743 }
5744
5745 /// See AbstractAttribute::trackStatistics()
5746 void trackStatistics() const override {
5747 STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
5748 }
5749};
5750
5751struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
5752 AAPrivatizablePtrReturned(const IRPosition &IRP)
5753 : AAPrivatizablePtrFloating(IRP) {}
5754
5755 /// See AbstractAttribute::initialize(...).
5756 void initialize(Attributor &A) override {
5757 // TODO: We can privatize more than arguments.
5758 indicatePessimisticFixpoint();
5759 }
5760
5761 /// See AbstractAttribute::trackStatistics()
5762 void trackStatistics() const override {
5763 STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
5764 }
5765};
5766
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005767/// -------------------- Memory Behavior Attributes ----------------------------
5768/// Includes read-none, read-only, and write-only.
5769/// ----------------------------------------------------------------------------
5770struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
5771 AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {}
5772
5773 /// See AbstractAttribute::initialize(...).
5774 void initialize(Attributor &A) override {
5775 intersectAssumedBits(BEST_STATE);
5776 getKnownStateFromValue(getIRPosition(), getState());
5777 IRAttribute::initialize(A);
5778 }
5779
5780 /// Return the memory behavior information encoded in the IR for \p IRP.
5781 static void getKnownStateFromValue(const IRPosition &IRP,
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005782 BitIntegerState &State,
5783 bool IgnoreSubsumingPositions = false) {
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005784 SmallVector<Attribute, 2> Attrs;
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005785 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005786 for (const Attribute &Attr : Attrs) {
5787 switch (Attr.getKindAsEnum()) {
5788 case Attribute::ReadNone:
5789 State.addKnownBits(NO_ACCESSES);
5790 break;
5791 case Attribute::ReadOnly:
5792 State.addKnownBits(NO_WRITES);
5793 break;
5794 case Attribute::WriteOnly:
5795 State.addKnownBits(NO_READS);
5796 break;
5797 default:
Johannes Doerfert282f5d72020-01-26 02:51:57 -06005798 llvm_unreachable("Unexpected attribute!");
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005799 }
5800 }
5801
5802 if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
5803 if (!I->mayReadFromMemory())
5804 State.addKnownBits(NO_READS);
5805 if (!I->mayWriteToMemory())
5806 State.addKnownBits(NO_WRITES);
5807 }
5808 }
5809
5810 /// See AbstractAttribute::getDeducedAttributes(...).
5811 void getDeducedAttributes(LLVMContext &Ctx,
5812 SmallVectorImpl<Attribute> &Attrs) const override {
5813 assert(Attrs.size() == 0);
5814 if (isAssumedReadNone())
5815 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
5816 else if (isAssumedReadOnly())
5817 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
5818 else if (isAssumedWriteOnly())
5819 Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
5820 assert(Attrs.size() <= 1);
5821 }
5822
5823 /// See AbstractAttribute::manifest(...).
5824 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert282f5d72020-01-26 02:51:57 -06005825 if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
5826 return ChangeStatus::UNCHANGED;
5827
Johannes Doerfertb2083c52019-10-20 22:46:48 -05005828 const IRPosition &IRP = getIRPosition();
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005829
5830 // Check if we would improve the existing attributes first.
5831 SmallVector<Attribute, 4> DeducedAttrs;
5832 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
5833 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
5834 return IRP.hasAttr(Attr.getKindAsEnum(),
5835 /* IgnoreSubsumingPositions */ true);
5836 }))
5837 return ChangeStatus::UNCHANGED;
5838
5839 // Clear existing attributes.
5840 IRP.removeAttrs(AttrKinds);
5841
5842 // Use the generic manifest method.
5843 return IRAttribute::manifest(A);
5844 }
5845
5846 /// See AbstractState::getAsStr().
5847 const std::string getAsStr() const override {
5848 if (isAssumedReadNone())
5849 return "readnone";
5850 if (isAssumedReadOnly())
5851 return "readonly";
5852 if (isAssumedWriteOnly())
5853 return "writeonly";
5854 return "may-read/write";
5855 }
5856
5857 /// The set of IR attributes AAMemoryBehavior deals with.
5858 static const Attribute::AttrKind AttrKinds[3];
5859};
5860
5861const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
5862 Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
5863
5864/// Memory behavior attribute for a floating value.
5865struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
5866 AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5867
5868 /// See AbstractAttribute::initialize(...).
5869 void initialize(Attributor &A) override {
5870 AAMemoryBehaviorImpl::initialize(A);
5871 // Initialize the use vector with all direct uses of the associated value.
5872 for (const Use &U : getAssociatedValue().uses())
5873 Uses.insert(&U);
5874 }
5875
5876 /// See AbstractAttribute::updateImpl(...).
5877 ChangeStatus updateImpl(Attributor &A) override;
5878
5879 /// See AbstractAttribute::trackStatistics()
5880 void trackStatistics() const override {
5881 if (isAssumedReadNone())
5882 STATS_DECLTRACK_FLOATING_ATTR(readnone)
5883 else if (isAssumedReadOnly())
5884 STATS_DECLTRACK_FLOATING_ATTR(readonly)
5885 else if (isAssumedWriteOnly())
5886 STATS_DECLTRACK_FLOATING_ATTR(writeonly)
5887 }
5888
5889private:
5890 /// Return true if users of \p UserI might access the underlying
5891 /// variable/location described by \p U and should therefore be analyzed.
5892 bool followUsersOfUseIn(Attributor &A, const Use *U,
5893 const Instruction *UserI);
5894
5895 /// Update the state according to the effect of use \p U in \p UserI.
5896 void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI);
5897
5898protected:
5899 /// Container for (transitive) uses of the associated argument.
5900 SetVector<const Use *> Uses;
5901};
5902
5903/// Memory behavior attribute for function argument.
5904struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
5905 AAMemoryBehaviorArgument(const IRPosition &IRP)
5906 : AAMemoryBehaviorFloating(IRP) {}
5907
5908 /// See AbstractAttribute::initialize(...).
5909 void initialize(Attributor &A) override {
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005910 intersectAssumedBits(BEST_STATE);
5911 const IRPosition &IRP = getIRPosition();
5912 // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
5913 // can query it when we use has/getAttr. That would allow us to reuse the
5914 // initialize of the base class here.
5915 bool HasByVal =
5916 IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
5917 getKnownStateFromValue(IRP, getState(),
5918 /* IgnoreSubsumingPositions */ HasByVal);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005919
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005920 // Initialize the use vector with all direct uses of the associated value.
5921 Argument *Arg = getAssociatedArgument();
Johannes Doerferta198adb2020-03-13 00:32:38 -05005922 if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent()))) {
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005923 indicatePessimisticFixpoint();
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005924 } else {
5925 // Initialize the use vector with all direct uses of the associated value.
5926 for (const Use &U : Arg->uses())
5927 Uses.insert(&U);
5928 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005929 }
5930
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005931 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert282f5d72020-01-26 02:51:57 -06005932 // TODO: Pointer arguments are not supported on vectors of pointers yet.
5933 if (!getAssociatedValue().getType()->isPointerTy())
5934 return ChangeStatus::UNCHANGED;
5935
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005936 // TODO: From readattrs.ll: "inalloca parameters are always
5937 // considered written"
5938 if (hasAttr({Attribute::InAlloca})) {
5939 removeKnownBits(NO_WRITES);
5940 removeAssumedBits(NO_WRITES);
5941 }
5942 return AAMemoryBehaviorFloating::manifest(A);
5943 }
5944
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005945 /// See AbstractAttribute::trackStatistics()
5946 void trackStatistics() const override {
5947 if (isAssumedReadNone())
5948 STATS_DECLTRACK_ARG_ATTR(readnone)
5949 else if (isAssumedReadOnly())
5950 STATS_DECLTRACK_ARG_ATTR(readonly)
5951 else if (isAssumedWriteOnly())
5952 STATS_DECLTRACK_ARG_ATTR(writeonly)
5953 }
5954};
5955
5956struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
5957 AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP)
5958 : AAMemoryBehaviorArgument(IRP) {}
5959
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005960 /// See AbstractAttribute::initialize(...).
5961 void initialize(Attributor &A) override {
5962 if (Argument *Arg = getAssociatedArgument()) {
5963 if (Arg->hasByValAttr()) {
5964 addKnownBits(NO_WRITES);
5965 removeKnownBits(NO_READS);
5966 removeAssumedBits(NO_READS);
5967 }
5968 } else {
5969 }
5970 AAMemoryBehaviorArgument::initialize(A);
5971 }
5972
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005973 /// See AbstractAttribute::updateImpl(...).
5974 ChangeStatus updateImpl(Attributor &A) override {
5975 // TODO: Once we have call site specific value information we can provide
5976 // call site specific liveness liveness information and then it makes
5977 // sense to specialize attributes for call sites arguments instead of
5978 // redirecting requests to the callee argument.
5979 Argument *Arg = getAssociatedArgument();
5980 const IRPosition &ArgPos = IRPosition::argument(*Arg);
5981 auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos);
5982 return clampStateAndIndicateChange(
5983 getState(),
Johannes Doerfert31784242019-10-29 23:18:49 -05005984 static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState()));
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005985 }
5986
5987 /// See AbstractAttribute::trackStatistics()
5988 void trackStatistics() const override {
5989 if (isAssumedReadNone())
5990 STATS_DECLTRACK_CSARG_ATTR(readnone)
5991 else if (isAssumedReadOnly())
5992 STATS_DECLTRACK_CSARG_ATTR(readonly)
5993 else if (isAssumedWriteOnly())
5994 STATS_DECLTRACK_CSARG_ATTR(writeonly)
5995 }
5996};
5997
5998/// Memory behavior attribute for a call site return position.
5999struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
6000 AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP)
6001 : AAMemoryBehaviorFloating(IRP) {}
6002
6003 /// See AbstractAttribute::manifest(...).
6004 ChangeStatus manifest(Attributor &A) override {
6005 // We do not annotate returned values.
6006 return ChangeStatus::UNCHANGED;
6007 }
6008
6009 /// See AbstractAttribute::trackStatistics()
6010 void trackStatistics() const override {}
6011};
6012
6013/// An AA to represent the memory behavior function attributes.
6014struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
6015 AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
6016
6017 /// See AbstractAttribute::updateImpl(Attributor &A).
6018 virtual ChangeStatus updateImpl(Attributor &A) override;
6019
6020 /// See AbstractAttribute::manifest(...).
6021 ChangeStatus manifest(Attributor &A) override {
6022 Function &F = cast<Function>(getAnchorValue());
6023 if (isAssumedReadNone()) {
6024 F.removeFnAttr(Attribute::ArgMemOnly);
6025 F.removeFnAttr(Attribute::InaccessibleMemOnly);
6026 F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
6027 }
6028 return AAMemoryBehaviorImpl::manifest(A);
6029 }
6030
6031 /// See AbstractAttribute::trackStatistics()
6032 void trackStatistics() const override {
6033 if (isAssumedReadNone())
6034 STATS_DECLTRACK_FN_ATTR(readnone)
6035 else if (isAssumedReadOnly())
6036 STATS_DECLTRACK_FN_ATTR(readonly)
6037 else if (isAssumedWriteOnly())
6038 STATS_DECLTRACK_FN_ATTR(writeonly)
6039 }
6040};
6041
6042/// AAMemoryBehavior attribute for call sites.
6043struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
6044 AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
6045
6046 /// See AbstractAttribute::initialize(...).
6047 void initialize(Attributor &A) override {
6048 AAMemoryBehaviorImpl::initialize(A);
6049 Function *F = getAssociatedFunction();
Johannes Doerferta198adb2020-03-13 00:32:38 -05006050 if (!F || !A.isFunctionIPOAmendable(*F))
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006051 indicatePessimisticFixpoint();
6052 }
6053
6054 /// See AbstractAttribute::updateImpl(...).
6055 ChangeStatus updateImpl(Attributor &A) override {
6056 // TODO: Once we have call site specific value information we can provide
6057 // call site specific liveness liveness information and then it makes
6058 // sense to specialize attributes for call sites arguments instead of
6059 // redirecting requests to the callee argument.
6060 Function *F = getAssociatedFunction();
6061 const IRPosition &FnPos = IRPosition::function(*F);
6062 auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos);
6063 return clampStateAndIndicateChange(
Johannes Doerfert1a746452019-10-20 22:28:49 -05006064 getState(),
6065 static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState()));
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006066 }
6067
6068 /// See AbstractAttribute::trackStatistics()
6069 void trackStatistics() const override {
6070 if (isAssumedReadNone())
6071 STATS_DECLTRACK_CS_ATTR(readnone)
6072 else if (isAssumedReadOnly())
6073 STATS_DECLTRACK_CS_ATTR(readonly)
6074 else if (isAssumedWriteOnly())
6075 STATS_DECLTRACK_CS_ATTR(writeonly)
6076 }
6077};
6078
6079ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
6080
6081 // The current assumed state used to determine a change.
6082 auto AssumedState = getAssumed();
6083
6084 auto CheckRWInst = [&](Instruction &I) {
6085 // If the instruction has an own memory behavior state, use it to restrict
6086 // the local state. No further analysis is required as the other memory
6087 // state is as optimistic as it gets.
6088 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
6089 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
6090 *this, IRPosition::callsite_function(ICS));
6091 intersectAssumedBits(MemBehaviorAA.getAssumed());
6092 return !isAtFixpoint();
6093 }
6094
6095 // Remove access kind modifiers if necessary.
6096 if (I.mayReadFromMemory())
6097 removeAssumedBits(NO_READS);
6098 if (I.mayWriteToMemory())
6099 removeAssumedBits(NO_WRITES);
6100 return !isAtFixpoint();
6101 };
6102
6103 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
6104 return indicatePessimisticFixpoint();
6105
6106 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
6107 : ChangeStatus::UNCHANGED;
6108}
6109
6110ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
6111
6112 const IRPosition &IRP = getIRPosition();
6113 const IRPosition &FnPos = IRPosition::function_scope(IRP);
6114 AAMemoryBehavior::StateType &S = getState();
6115
6116 // First, check the function scope. We take the known information and we avoid
6117 // work if the assumed information implies the current assumed information for
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06006118 // this attribute. This is a valid for all but byval arguments.
6119 Argument *Arg = IRP.getAssociatedArgument();
6120 AAMemoryBehavior::base_t FnMemAssumedState =
6121 AAMemoryBehavior::StateType::getWorstState();
6122 if (!Arg || !Arg->hasByValAttr()) {
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06006123 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
6124 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06006125 FnMemAssumedState = FnMemAA.getAssumed();
6126 S.addKnownBits(FnMemAA.getKnown());
6127 if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
6128 return ChangeStatus::UNCHANGED;
6129 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006130
6131 // Make sure the value is not captured (except through "return"), if
6132 // it is, any information derived would be irrelevant anyway as we cannot
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00006133 // check the potential aliases introduced by the capture. However, no need
6134 // to fall back to anythign less optimistic than the function state.
Johannes Doerfert28880192019-12-31 00:57:00 -06006135 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
6136 *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00006137 if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06006138 S.intersectAssumedBits(FnMemAssumedState);
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00006139 return ChangeStatus::CHANGED;
6140 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006141
6142 // The current assumed state used to determine a change.
6143 auto AssumedState = S.getAssumed();
6144
6145 // Liveness information to exclude dead users.
6146 // TODO: Take the FnPos once we have call site specific liveness information.
6147 const auto &LivenessAA = A.getAAFor<AAIsDead>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06006148 *this, IRPosition::function(*IRP.getAssociatedFunction()),
6149 /* TrackDependence */ false);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006150
6151 // Visit and expand uses until all are analyzed or a fixpoint is reached.
6152 for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) {
6153 const Use *U = Uses[i];
6154 Instruction *UserI = cast<Instruction>(U->getUser());
6155 LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI
Johannes Doerfert23f41f12020-01-12 01:09:22 -06006156 << " [Dead: " << (A.isAssumedDead(*U, this, &LivenessAA))
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006157 << "]\n");
Johannes Doerfert23f41f12020-01-12 01:09:22 -06006158 if (A.isAssumedDead(*U, this, &LivenessAA))
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006159 continue;
6160
6161 // Check if the users of UserI should also be visited.
6162 if (followUsersOfUseIn(A, U, UserI))
6163 for (const Use &UserIUse : UserI->uses())
6164 Uses.insert(&UserIUse);
6165
6166 // If UserI might touch memory we analyze the use in detail.
6167 if (UserI->mayReadOrWriteMemory())
6168 analyzeUseIn(A, U, UserI);
6169 }
6170
6171 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
6172 : ChangeStatus::UNCHANGED;
6173}
6174
6175bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U,
6176 const Instruction *UserI) {
6177 // The loaded value is unrelated to the pointer argument, no need to
6178 // follow the users of the load.
6179 if (isa<LoadInst>(UserI))
6180 return false;
6181
6182 // By default we follow all uses assuming UserI might leak information on U,
6183 // we have special handling for call sites operands though.
6184 ImmutableCallSite ICS(UserI);
6185 if (!ICS || !ICS.isArgOperand(U))
6186 return true;
6187
6188 // If the use is a call argument known not to be captured, the users of
6189 // the call do not need to be visited because they have to be unrelated to
6190 // the input. Note that this check is not trivial even though we disallow
6191 // general capturing of the underlying argument. The reason is that the
6192 // call might the argument "through return", which we allow and for which we
6193 // need to check call users.
Johannes Doerfertff6254d2020-01-10 12:32:24 -06006194 if (U->get()->getType()->isPointerTy()) {
6195 unsigned ArgNo = ICS.getArgumentNo(U);
6196 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06006197 *this, IRPosition::callsite_argument(ICS, ArgNo),
6198 /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfertff6254d2020-01-10 12:32:24 -06006199 return !ArgNoCaptureAA.isAssumedNoCapture();
6200 }
6201
6202 return true;
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006203}
6204
6205void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U,
6206 const Instruction *UserI) {
6207 assert(UserI->mayReadOrWriteMemory());
6208
6209 switch (UserI->getOpcode()) {
6210 default:
6211 // TODO: Handle all atomics and other side-effect operations we know of.
6212 break;
6213 case Instruction::Load:
6214 // Loads cause the NO_READS property to disappear.
6215 removeAssumedBits(NO_READS);
6216 return;
6217
6218 case Instruction::Store:
6219 // Stores cause the NO_WRITES property to disappear if the use is the
6220 // pointer operand. Note that we do assume that capturing was taken care of
6221 // somewhere else.
6222 if (cast<StoreInst>(UserI)->getPointerOperand() == U->get())
6223 removeAssumedBits(NO_WRITES);
6224 return;
6225
6226 case Instruction::Call:
6227 case Instruction::CallBr:
6228 case Instruction::Invoke: {
6229 // For call sites we look at the argument memory behavior attribute (this
6230 // could be recursive!) in order to restrict our own state.
6231 ImmutableCallSite ICS(UserI);
6232
6233 // Give up on operand bundles.
6234 if (ICS.isBundleOperand(U)) {
6235 indicatePessimisticFixpoint();
6236 return;
6237 }
6238
6239 // Calling a function does read the function pointer, maybe write it if the
6240 // function is self-modifying.
6241 if (ICS.isCallee(U)) {
6242 removeAssumedBits(NO_READS);
6243 break;
6244 }
6245
6246 // Adjust the possible access behavior based on the information on the
6247 // argument.
Johannes Doerfertff6254d2020-01-10 12:32:24 -06006248 IRPosition Pos;
6249 if (U->get()->getType()->isPointerTy())
6250 Pos = IRPosition::callsite_argument(ICS, ICS.getArgumentNo(U));
6251 else
6252 Pos = IRPosition::callsite_function(ICS);
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06006253 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
6254 *this, Pos,
6255 /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006256 // "assumed" has at most the same bits as the MemBehaviorAA assumed
6257 // and at least "known".
6258 intersectAssumedBits(MemBehaviorAA.getAssumed());
6259 return;
6260 }
6261 };
6262
6263 // Generally, look at the "may-properties" and adjust the assumed state if we
6264 // did not trigger special handling before.
6265 if (UserI->mayReadFromMemory())
6266 removeAssumedBits(NO_READS);
6267 if (UserI->mayWriteToMemory())
6268 removeAssumedBits(NO_WRITES);
6269}
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006270
Benjamin Kramer564a9de2020-02-17 17:55:03 +01006271} // namespace
6272
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006273/// -------------------- Memory Locations Attributes ---------------------------
6274/// Includes read-none, argmemonly, inaccessiblememonly,
6275/// inaccessiblememorargmemonly
6276/// ----------------------------------------------------------------------------
6277
6278std::string AAMemoryLocation::getMemoryLocationsAsStr(
6279 AAMemoryLocation::MemoryLocationsKind MLK) {
6280 if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
6281 return "all memory";
6282 if (MLK == AAMemoryLocation::NO_LOCATIONS)
6283 return "no memory";
6284 std::string S = "memory:";
6285 if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
6286 S += "stack,";
6287 if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
6288 S += "constant,";
6289 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
6290 S += "internal global,";
6291 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
6292 S += "external global,";
6293 if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
6294 S += "argument,";
6295 if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
6296 S += "inaccessible,";
6297 if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
6298 S += "malloced,";
6299 if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
6300 S += "unknown,";
6301 S.pop_back();
6302 return S;
6303}
6304
Benjamin Kramer564a9de2020-02-17 17:55:03 +01006305namespace {
6306
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006307struct AAMemoryLocationImpl : public AAMemoryLocation {
6308
6309 AAMemoryLocationImpl(const IRPosition &IRP) : AAMemoryLocation(IRP) {}
6310
6311 /// See AbstractAttribute::initialize(...).
6312 void initialize(Attributor &A) override {
6313 intersectAssumedBits(BEST_STATE);
6314 getKnownStateFromValue(getIRPosition(), getState());
6315 IRAttribute::initialize(A);
6316 }
6317
6318 /// Return the memory behavior information encoded in the IR for \p IRP.
6319 static void getKnownStateFromValue(const IRPosition &IRP,
6320 BitIntegerState &State,
6321 bool IgnoreSubsumingPositions = false) {
6322 SmallVector<Attribute, 2> Attrs;
6323 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
6324 for (const Attribute &Attr : Attrs) {
6325 switch (Attr.getKindAsEnum()) {
6326 case Attribute::ReadNone:
6327 State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
6328 break;
6329 case Attribute::InaccessibleMemOnly:
6330 State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
6331 break;
6332 case Attribute::ArgMemOnly:
6333 State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
6334 break;
6335 case Attribute::InaccessibleMemOrArgMemOnly:
6336 State.addKnownBits(
6337 inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
6338 break;
6339 default:
6340 llvm_unreachable("Unexpected attribute!");
6341 }
6342 }
6343 }
6344
6345 /// See AbstractAttribute::getDeducedAttributes(...).
6346 void getDeducedAttributes(LLVMContext &Ctx,
6347 SmallVectorImpl<Attribute> &Attrs) const override {
6348 assert(Attrs.size() == 0);
6349 if (isAssumedReadNone()) {
6350 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
6351 } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
6352 if (isAssumedInaccessibleMemOnly())
6353 Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
6354 else if (isAssumedArgMemOnly())
6355 Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
6356 else if (isAssumedInaccessibleOrArgMemOnly())
6357 Attrs.push_back(
6358 Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
6359 }
6360 assert(Attrs.size() <= 1);
6361 }
6362
6363 /// See AbstractAttribute::manifest(...).
6364 ChangeStatus manifest(Attributor &A) override {
6365 const IRPosition &IRP = getIRPosition();
6366
6367 // Check if we would improve the existing attributes first.
6368 SmallVector<Attribute, 4> DeducedAttrs;
6369 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
6370 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
6371 return IRP.hasAttr(Attr.getKindAsEnum(),
6372 /* IgnoreSubsumingPositions */ true);
6373 }))
6374 return ChangeStatus::UNCHANGED;
6375
6376 // Clear existing attributes.
6377 IRP.removeAttrs(AttrKinds);
6378 if (isAssumedReadNone())
6379 IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
6380
6381 // Use the generic manifest method.
6382 return IRAttribute::manifest(A);
6383 }
6384
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006385 /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
6386 bool checkForAllAccessesToMemoryKind(
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006387 const function_ref<bool(const Instruction *, const Value *, AccessKind,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006388 MemoryLocationsKind)> &Pred,
6389 MemoryLocationsKind RequestedMLK) const override {
6390 if (!isValidState())
6391 return false;
6392
6393 MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
6394 if (AssumedMLK == NO_LOCATIONS)
6395 return true;
6396
6397 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
6398 if (CurMLK & RequestedMLK)
6399 continue;
6400
6401 const auto &Accesses = AccessKindAccessesMap.lookup(CurMLK);
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006402 for (const AccessInfo &AI : Accesses) {
6403 if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006404 return false;
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006405 }
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006406 }
6407
6408 return true;
6409 }
6410
6411 ChangeStatus indicatePessimisticFixpoint() override {
6412 // If we give up and indicate a pessimistic fixpoint this instruction will
6413 // become an access for all potential access kinds:
6414 // TODO: Add pointers for argmemonly and globals to improve the results of
6415 // checkForAllAccessesToMemoryKind.
6416 bool Changed = false;
6417 MemoryLocationsKind KnownMLK = getKnown();
6418 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
6419 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
6420 if (!(CurMLK & KnownMLK))
6421 updateStateAndAccessesMap(getState(), AccessKindAccessesMap, CurMLK, I,
6422 nullptr, Changed);
6423 return AAMemoryLocation::indicatePessimisticFixpoint();
6424 }
6425
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006426protected:
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006427 /// Helper struct to tie together an instruction that has a read or write
6428 /// effect with the pointer it accesses (if any).
6429 struct AccessInfo {
6430
6431 /// The instruction that caused the access.
6432 const Instruction *I;
6433
6434 /// The base pointer that is accessed, or null if unknown.
6435 const Value *Ptr;
6436
6437 /// The kind of access (read/write/read+write).
6438 AccessKind Kind;
6439
6440 bool operator==(const AccessInfo &RHS) const {
Simon Pilgrim8a48c4a2020-02-15 13:53:18 +00006441 return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006442 }
6443 bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
6444 if (LHS.I != RHS.I)
6445 return LHS.I < RHS.I;
6446 if (LHS.Ptr != RHS.Ptr)
6447 return LHS.Ptr < RHS.Ptr;
6448 if (LHS.Kind != RHS.Kind)
6449 return LHS.Kind < RHS.Kind;
6450 return false;
6451 }
6452 };
6453
6454 /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
6455 /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
6456 using AccessKindAccessesMapTy =
6457 DenseMap<unsigned, SmallSet<AccessInfo, 8, AccessInfo>>;
6458 AccessKindAccessesMapTy AccessKindAccessesMap;
6459
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006460 /// Return the kind(s) of location that may be accessed by \p V.
6461 AAMemoryLocation::MemoryLocationsKind
6462 categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
6463
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006464 /// Update the state \p State and the AccessKindAccessesMap given that \p I is
6465 /// an access to a \p MLK memory location with the access pointer \p Ptr.
6466 static void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
6467 AccessKindAccessesMapTy &AccessMap,
6468 MemoryLocationsKind MLK,
6469 const Instruction *I, const Value *Ptr,
6470 bool &Changed) {
6471 // TODO: The kind should be determined at the call sites based on the
6472 // information we have there.
6473 AccessKind Kind = READ_WRITE;
6474 if (I) {
6475 Kind = I->mayReadFromMemory() ? READ : NONE;
6476 Kind = AccessKind(Kind | (I->mayWriteToMemory() ? WRITE : NONE));
6477 }
6478
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006479 assert(isPowerOf2_32(MLK) && "Expected a single location set!");
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006480 Changed |= AccessMap[MLK].insert(AccessInfo{I, Ptr, Kind}).second;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006481 State.removeAssumedBits(MLK);
6482 }
6483
6484 /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
6485 /// arguments, and update the state and access map accordingly.
6486 void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
6487 AAMemoryLocation::StateType &State, bool &Changed);
6488
6489 /// The set of IR attributes AAMemoryLocation deals with.
6490 static const Attribute::AttrKind AttrKinds[4];
6491};
6492
6493const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
6494 Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
6495 Attribute::InaccessibleMemOrArgMemOnly};
6496
6497void AAMemoryLocationImpl::categorizePtrValue(
6498 Attributor &A, const Instruction &I, const Value &Ptr,
6499 AAMemoryLocation::StateType &State, bool &Changed) {
6500 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
6501 << Ptr << " ["
6502 << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
6503
6504 auto StripGEPCB = [](Value *V) -> Value * {
6505 auto *GEP = dyn_cast<GEPOperator>(V);
6506 while (GEP) {
6507 V = GEP->getPointerOperand();
6508 GEP = dyn_cast<GEPOperator>(V);
6509 }
6510 return V;
6511 };
6512
6513 auto VisitValueCB = [&](Value &V, AAMemoryLocation::StateType &T,
6514 bool Stripped) -> bool {
6515 assert(!isa<GEPOperator>(V) && "GEPs should have been stripped.");
6516 if (isa<UndefValue>(V))
6517 return true;
6518 if (auto *Arg = dyn_cast<Argument>(&V)) {
6519 if (Arg->hasByValAttr())
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006520 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I,
6521 &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006522 else
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006523 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_ARGUMENT_MEM, &I,
6524 &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006525 return true;
6526 }
6527 if (auto *GV = dyn_cast<GlobalValue>(&V)) {
6528 if (GV->hasLocalLinkage())
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006529 updateStateAndAccessesMap(T, AccessKindAccessesMap,
6530 NO_GLOBAL_INTERNAL_MEM, &I, &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006531 else
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006532 updateStateAndAccessesMap(T, AccessKindAccessesMap,
6533 NO_GLOBAL_EXTERNAL_MEM, &I, &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006534 return true;
6535 }
6536 if (isa<AllocaInst>(V)) {
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006537 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I, &V,
6538 Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006539 return true;
6540 }
6541 if (ImmutableCallSite ICS = ImmutableCallSite(&V)) {
6542 const auto &NoAliasAA =
6543 A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(ICS));
6544 if (NoAliasAA.isAssumedNoAlias()) {
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006545 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_MALLOCED_MEM, &I,
6546 &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006547 return true;
6548 }
6549 }
6550
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006551 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_UNKOWN_MEM, &I, &V,
6552 Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006553 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value cannot be categorized: "
6554 << V << " -> " << getMemoryLocationsAsStr(T.getAssumed())
6555 << "\n");
6556 return true;
6557 };
6558
6559 if (!genericValueTraversal<AAMemoryLocation, AAMemoryLocation::StateType>(
6560 A, IRPosition::value(Ptr), *this, State, VisitValueCB,
6561 /* MaxValues */ 32, StripGEPCB)) {
6562 LLVM_DEBUG(
6563 dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006564 updateStateAndAccessesMap(State, AccessKindAccessesMap, NO_UNKOWN_MEM, &I,
6565 nullptr, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006566 } else {
6567 LLVM_DEBUG(
6568 dbgs()
6569 << "[AAMemoryLocation] Accessed locations with pointer locations: "
6570 << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
6571 }
6572}
6573
6574AAMemoryLocation::MemoryLocationsKind
6575AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
6576 bool &Changed) {
6577 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
6578 << I << "\n");
6579
6580 AAMemoryLocation::StateType AccessedLocs;
6581 AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
6582
6583 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
6584
6585 // First check if we assume any memory is access is visible.
6586 const auto &ICSMemLocationAA =
6587 A.getAAFor<AAMemoryLocation>(*this, IRPosition::callsite_function(ICS));
6588 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
6589 << " [" << ICSMemLocationAA << "]\n");
6590
6591 if (ICSMemLocationAA.isAssumedReadNone())
6592 return NO_LOCATIONS;
6593
6594 if (ICSMemLocationAA.isAssumedInaccessibleMemOnly()) {
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006595 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap,
6596 NO_INACCESSIBLE_MEM, &I, nullptr, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006597 return AccessedLocs.getAssumed();
6598 }
6599
6600 uint32_t ICSAssumedNotAccessedLocs =
6601 ICSMemLocationAA.getAssumedNotAccessedLocation();
6602
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006603 // Set the argmemonly and global bit as we handle them separately below.
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006604 uint32_t ICSAssumedNotAccessedLocsNoArgMem =
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006605 ICSAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006606
6607 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
6608 if (ICSAssumedNotAccessedLocsNoArgMem & CurMLK)
6609 continue;
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006610 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, CurMLK, &I,
6611 nullptr, Changed);
6612 }
6613
6614 // Now handle global memory if it might be accessed.
6615 bool HasGlobalAccesses = !(ICSAssumedNotAccessedLocs & NO_GLOBAL_MEM);
6616 if (HasGlobalAccesses) {
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006617 auto AccessPred = [&](const Instruction *, const Value *Ptr,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006618 AccessKind Kind, MemoryLocationsKind MLK) {
6619 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, MLK, &I,
6620 Ptr, Changed);
6621 return true;
6622 };
6623 if (!ICSMemLocationAA.checkForAllAccessesToMemoryKind(
6624 AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
6625 return AccessedLocs.getWorstState();
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006626 }
6627
6628 LLVM_DEBUG(
6629 dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
6630 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
6631
6632 // Now handle argument memory if it might be accessed.
6633 bool HasArgAccesses = !(ICSAssumedNotAccessedLocs & NO_ARGUMENT_MEM);
6634 if (HasArgAccesses) {
6635 for (unsigned ArgNo = 0, e = ICS.getNumArgOperands(); ArgNo < e;
6636 ++ArgNo) {
6637
6638 // Skip non-pointer arguments.
6639 const Value *ArgOp = ICS.getArgOperand(ArgNo);
6640 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
6641 continue;
6642
6643 // Skip readnone arguments.
6644 const IRPosition &ArgOpIRP = IRPosition::callsite_argument(ICS, ArgNo);
6645 const auto &ArgOpMemLocationAA = A.getAAFor<AAMemoryBehavior>(
6646 *this, ArgOpIRP, /* TrackDependence */ true, DepClassTy::OPTIONAL);
6647
6648 if (ArgOpMemLocationAA.isAssumedReadNone())
6649 continue;
6650
6651 // Categorize potentially accessed pointer arguments as if there was an
6652 // access instruction with them as pointer.
6653 categorizePtrValue(A, I, *ArgOp, AccessedLocs, Changed);
6654 }
6655 }
6656
6657 LLVM_DEBUG(
6658 dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
6659 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
6660
6661 return AccessedLocs.getAssumed();
6662 }
6663
6664 if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
6665 LLVM_DEBUG(
6666 dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
6667 << I << " [" << *Ptr << "]\n");
6668 categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
6669 return AccessedLocs.getAssumed();
6670 }
6671
6672 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
6673 << I << "\n");
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006674 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, NO_UNKOWN_MEM,
6675 &I, nullptr, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006676 return AccessedLocs.getAssumed();
6677}
6678
6679/// An AA to represent the memory behavior function attributes.
6680struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
6681 AAMemoryLocationFunction(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {}
6682
6683 /// See AbstractAttribute::updateImpl(Attributor &A).
6684 virtual ChangeStatus updateImpl(Attributor &A) override {
6685
6686 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
6687 *this, getIRPosition(), /* TrackDependence */ false);
6688 if (MemBehaviorAA.isAssumedReadNone()) {
6689 if (MemBehaviorAA.isKnownReadNone())
6690 return indicateOptimisticFixpoint();
6691 assert(isAssumedReadNone() &&
6692 "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
6693 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
6694 return ChangeStatus::UNCHANGED;
6695 }
6696
6697 // The current assumed state used to determine a change.
6698 auto AssumedState = getAssumed();
6699 bool Changed = false;
6700
6701 auto CheckRWInst = [&](Instruction &I) {
6702 MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
6703 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
6704 << ": " << getMemoryLocationsAsStr(MLK) << "\n");
6705 removeAssumedBits(inverseLocation(MLK, false, false));
6706 return true;
6707 };
6708
6709 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
6710 return indicatePessimisticFixpoint();
6711
6712 Changed |= AssumedState != getAssumed();
6713 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
6714 }
6715
6716 /// See AbstractAttribute::trackStatistics()
6717 void trackStatistics() const override {
6718 if (isAssumedReadNone())
6719 STATS_DECLTRACK_FN_ATTR(readnone)
6720 else if (isAssumedArgMemOnly())
6721 STATS_DECLTRACK_FN_ATTR(argmemonly)
6722 else if (isAssumedInaccessibleMemOnly())
6723 STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
6724 else if (isAssumedInaccessibleOrArgMemOnly())
6725 STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
6726 }
6727};
6728
6729/// AAMemoryLocation attribute for call sites.
6730struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
6731 AAMemoryLocationCallSite(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {}
6732
6733 /// See AbstractAttribute::initialize(...).
6734 void initialize(Attributor &A) override {
6735 AAMemoryLocationImpl::initialize(A);
6736 Function *F = getAssociatedFunction();
Johannes Doerferta198adb2020-03-13 00:32:38 -05006737 if (!F || !A.isFunctionIPOAmendable(*F))
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006738 indicatePessimisticFixpoint();
6739 }
6740
6741 /// See AbstractAttribute::updateImpl(...).
6742 ChangeStatus updateImpl(Attributor &A) override {
6743 // TODO: Once we have call site specific value information we can provide
6744 // call site specific liveness liveness information and then it makes
6745 // sense to specialize attributes for call sites arguments instead of
6746 // redirecting requests to the callee argument.
6747 Function *F = getAssociatedFunction();
6748 const IRPosition &FnPos = IRPosition::function(*F);
6749 auto &FnAA = A.getAAFor<AAMemoryLocation>(*this, FnPos);
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006750 bool Changed = false;
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006751 auto AccessPred = [&](const Instruction *I, const Value *Ptr,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006752 AccessKind Kind, MemoryLocationsKind MLK) {
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006753 updateStateAndAccessesMap(getState(), AccessKindAccessesMap, MLK, I, Ptr,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006754 Changed);
6755 return true;
6756 };
6757 if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
6758 return indicatePessimisticFixpoint();
6759 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006760 }
6761
6762 /// See AbstractAttribute::trackStatistics()
6763 void trackStatistics() const override {
6764 if (isAssumedReadNone())
6765 STATS_DECLTRACK_CS_ATTR(readnone)
6766 }
6767};
6768
Hideto Ueno188f9a32020-01-15 15:25:52 +09006769/// ------------------ Value Constant Range Attribute -------------------------
Hideto Uenoe9963032020-01-01 15:25:19 +09006770
Hideto Ueno188f9a32020-01-15 15:25:52 +09006771struct AAValueConstantRangeImpl : AAValueConstantRange {
6772 using StateType = IntegerRangeState;
6773 AAValueConstantRangeImpl(const IRPosition &IRP) : AAValueConstantRange(IRP) {}
6774
6775 /// See AbstractAttribute::getAsStr().
6776 const std::string getAsStr() const override {
6777 std::string Str;
6778 llvm::raw_string_ostream OS(Str);
6779 OS << "range(" << getBitWidth() << ")<";
6780 getKnown().print(OS);
6781 OS << " / ";
6782 getAssumed().print(OS);
6783 OS << ">";
6784 return OS.str();
6785 }
6786
6787 /// Helper function to get a SCEV expr for the associated value at program
6788 /// point \p I.
6789 const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
6790 if (!getAnchorScope())
6791 return nullptr;
6792
6793 ScalarEvolution *SE =
6794 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
6795 *getAnchorScope());
6796
6797 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
6798 *getAnchorScope());
6799
6800 if (!SE || !LI)
6801 return nullptr;
6802
6803 const SCEV *S = SE->getSCEV(&getAssociatedValue());
6804 if (!I)
6805 return S;
6806
6807 return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
6808 }
6809
6810 /// Helper function to get a range from SCEV for the associated value at
6811 /// program point \p I.
6812 ConstantRange getConstantRangeFromSCEV(Attributor &A,
6813 const Instruction *I = nullptr) const {
6814 if (!getAnchorScope())
6815 return getWorstState(getBitWidth());
6816
6817 ScalarEvolution *SE =
6818 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
6819 *getAnchorScope());
6820
6821 const SCEV *S = getSCEV(A, I);
6822 if (!SE || !S)
6823 return getWorstState(getBitWidth());
6824
6825 return SE->getUnsignedRange(S);
6826 }
6827
6828 /// Helper function to get a range from LVI for the associated value at
6829 /// program point \p I.
6830 ConstantRange
6831 getConstantRangeFromLVI(Attributor &A,
6832 const Instruction *CtxI = nullptr) const {
6833 if (!getAnchorScope())
6834 return getWorstState(getBitWidth());
6835
6836 LazyValueInfo *LVI =
6837 A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
6838 *getAnchorScope());
6839
6840 if (!LVI || !CtxI)
6841 return getWorstState(getBitWidth());
6842 return LVI->getConstantRange(&getAssociatedValue(),
6843 const_cast<BasicBlock *>(CtxI->getParent()),
6844 const_cast<Instruction *>(CtxI));
6845 }
6846
6847 /// See AAValueConstantRange::getKnownConstantRange(..).
6848 ConstantRange
6849 getKnownConstantRange(Attributor &A,
6850 const Instruction *CtxI = nullptr) const override {
6851 if (!CtxI || CtxI == getCtxI())
6852 return getKnown();
6853
6854 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6855 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6856 return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
6857 }
6858
6859 /// See AAValueConstantRange::getAssumedConstantRange(..).
6860 ConstantRange
6861 getAssumedConstantRange(Attributor &A,
6862 const Instruction *CtxI = nullptr) const override {
6863 // TODO: Make SCEV use Attributor assumption.
6864 // We may be able to bound a variable range via assumptions in
6865 // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
6866 // evolve to x^2 + x, then we can say that y is in [2, 12].
6867
6868 if (!CtxI || CtxI == getCtxI())
6869 return getAssumed();
6870
6871 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6872 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6873 return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
6874 }
6875
6876 /// See AbstractAttribute::initialize(..).
6877 void initialize(Attributor &A) override {
6878 // Intersect a range given by SCEV.
6879 intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
6880
6881 // Intersect a range given by LVI.
6882 intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
6883 }
6884
6885 /// Helper function to create MDNode for range metadata.
6886 static MDNode *
6887 getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
6888 const ConstantRange &AssumedConstantRange) {
6889 Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
6890 Ty, AssumedConstantRange.getLower())),
6891 ConstantAsMetadata::get(ConstantInt::get(
6892 Ty, AssumedConstantRange.getUpper()))};
6893 return MDNode::get(Ctx, LowAndHigh);
6894 }
6895
6896 /// Return true if \p Assumed is included in \p KnownRanges.
6897 static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
6898
6899 if (Assumed.isFullSet())
6900 return false;
6901
6902 if (!KnownRanges)
6903 return true;
6904
6905 // If multiple ranges are annotated in IR, we give up to annotate assumed
6906 // range for now.
6907
6908 // TODO: If there exists a known range which containts assumed range, we
6909 // can say assumed range is better.
6910 if (KnownRanges->getNumOperands() > 2)
6911 return false;
6912
6913 ConstantInt *Lower =
6914 mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
6915 ConstantInt *Upper =
6916 mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
6917
6918 ConstantRange Known(Lower->getValue(), Upper->getValue());
6919 return Known.contains(Assumed) && Known != Assumed;
6920 }
6921
6922 /// Helper function to set range metadata.
6923 static bool
6924 setRangeMetadataIfisBetterRange(Instruction *I,
6925 const ConstantRange &AssumedConstantRange) {
6926 auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
6927 if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
6928 if (!AssumedConstantRange.isEmptySet()) {
6929 I->setMetadata(LLVMContext::MD_range,
6930 getMDNodeForConstantRange(I->getType(), I->getContext(),
6931 AssumedConstantRange));
6932 return true;
6933 }
6934 }
6935 return false;
6936 }
6937
6938 /// See AbstractAttribute::manifest()
6939 ChangeStatus manifest(Attributor &A) override {
6940 ChangeStatus Changed = ChangeStatus::UNCHANGED;
6941 ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
6942 assert(!AssumedConstantRange.isFullSet() && "Invalid state");
6943
6944 auto &V = getAssociatedValue();
6945 if (!AssumedConstantRange.isEmptySet() &&
6946 !AssumedConstantRange.isSingleElement()) {
6947 if (Instruction *I = dyn_cast<Instruction>(&V))
6948 if (isa<CallInst>(I) || isa<LoadInst>(I))
6949 if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
6950 Changed = ChangeStatus::CHANGED;
6951 }
6952
6953 return Changed;
6954 }
6955};
6956
Johannes Doerfertea5fabe2020-01-28 09:46:15 -06006957struct AAValueConstantRangeArgument final
6958 : AAArgumentFromCallSiteArguments<
6959 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState> {
Hideto Ueno188f9a32020-01-15 15:25:52 +09006960 AAValueConstantRangeArgument(const IRPosition &IRP)
Johannes Doerfertea5fabe2020-01-28 09:46:15 -06006961 : AAArgumentFromCallSiteArguments<
6962 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState>(
6963 IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006964
6965 /// See AbstractAttribute::trackStatistics()
6966 void trackStatistics() const override {
6967 STATS_DECLTRACK_ARG_ATTR(value_range)
6968 }
6969};
6970
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006971struct AAValueConstantRangeReturned
6972 : AAReturnedFromReturnedValues<AAValueConstantRange,
6973 AAValueConstantRangeImpl> {
6974 using Base = AAReturnedFromReturnedValues<AAValueConstantRange,
6975 AAValueConstantRangeImpl>;
6976 AAValueConstantRangeReturned(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006977
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006978 /// See AbstractAttribute::initialize(...).
6979 void initialize(Attributor &A) override {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006980
6981 /// See AbstractAttribute::trackStatistics()
6982 void trackStatistics() const override {
6983 STATS_DECLTRACK_FNRET_ATTR(value_range)
6984 }
6985};
6986
6987struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
6988 AAValueConstantRangeFloating(const IRPosition &IRP)
6989 : AAValueConstantRangeImpl(IRP) {}
6990
6991 /// See AbstractAttribute::initialize(...).
6992 void initialize(Attributor &A) override {
Johannes Doerfert028db8c42020-02-09 19:05:15 -06006993 AAValueConstantRangeImpl::initialize(A);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006994 Value &V = getAssociatedValue();
6995
6996 if (auto *C = dyn_cast<ConstantInt>(&V)) {
6997 unionAssumed(ConstantRange(C->getValue()));
6998 indicateOptimisticFixpoint();
6999 return;
7000 }
7001
7002 if (isa<UndefValue>(&V)) {
Johannes Doerfertb53af0e2020-02-14 20:08:20 -06007003 // Collapse the undef state to 0.
7004 unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
Hideto Ueno188f9a32020-01-15 15:25:52 +09007005 indicateOptimisticFixpoint();
7006 return;
7007 }
7008
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06007009 if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
7010 return;
Hideto Ueno188f9a32020-01-15 15:25:52 +09007011 // If it is a load instruction with range metadata, use it.
7012 if (LoadInst *LI = dyn_cast<LoadInst>(&V))
7013 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
7014 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
7015 return;
7016 }
7017
Johannes Doerfert81554392020-02-09 20:14:35 -06007018 // We can work with PHI and select instruction as we traverse their operands
Johannes Doerfert7e7e6592020-02-09 19:08:04 -06007019 // during update.
Johannes Doerfert81554392020-02-09 20:14:35 -06007020 if (isa<SelectInst>(V) || isa<PHINode>(V))
Johannes Doerfert7e7e6592020-02-09 19:08:04 -06007021 return;
7022
Hideto Ueno188f9a32020-01-15 15:25:52 +09007023 // Otherwise we give up.
7024 indicatePessimisticFixpoint();
7025
Johannes Doerfert02bd8182020-01-28 11:49:35 -06007026 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
7027 << getAssociatedValue() << "\n");
Hideto Ueno188f9a32020-01-15 15:25:52 +09007028 }
7029
Johannes Doerfert81554392020-02-09 20:14:35 -06007030 bool calculateBinaryOperator(
7031 Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
7032 Instruction *CtxI,
7033 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
Hideto Ueno188f9a32020-01-15 15:25:52 +09007034 Value *LHS = BinOp->getOperand(0);
7035 Value *RHS = BinOp->getOperand(1);
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06007036 // TODO: Allow non integers as well.
7037 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
7038 return false;
Hideto Ueno188f9a32020-01-15 15:25:52 +09007039
7040 auto &LHSAA =
7041 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06007042 QuerriedAAs.push_back(&LHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09007043 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
7044
7045 auto &RHSAA =
7046 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06007047 QuerriedAAs.push_back(&RHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09007048 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
7049
7050 auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
7051
7052 T.unionAssumed(AssumedRange);
7053
7054 // TODO: Track a known state too.
7055
7056 return T.isValidState();
7057 }
7058
Johannes Doerfert81554392020-02-09 20:14:35 -06007059 bool calculateCastInst(
7060 Attributor &A, CastInst *CastI, IntegerRangeState &T, Instruction *CtxI,
7061 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06007062 assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
7063 // TODO: Allow non integers as well.
7064 Value &OpV = *CastI->getOperand(0);
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06007065 if (!OpV.getType()->isIntegerTy())
7066 return false;
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06007067
7068 auto &OpAA =
7069 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(OpV));
Johannes Doerfert81554392020-02-09 20:14:35 -06007070 QuerriedAAs.push_back(&OpAA);
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06007071 T.unionAssumed(
7072 OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
7073 return T.isValidState();
7074 }
7075
Johannes Doerfert81554392020-02-09 20:14:35 -06007076 bool
7077 calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
7078 Instruction *CtxI,
7079 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
Hideto Ueno188f9a32020-01-15 15:25:52 +09007080 Value *LHS = CmpI->getOperand(0);
7081 Value *RHS = CmpI->getOperand(1);
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06007082 // TODO: Allow non integers as well.
7083 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
7084 return false;
Hideto Ueno188f9a32020-01-15 15:25:52 +09007085
7086 auto &LHSAA =
7087 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06007088 QuerriedAAs.push_back(&LHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09007089 auto &RHSAA =
7090 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06007091 QuerriedAAs.push_back(&RHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09007092
7093 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
7094 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
7095
7096 // If one of them is empty set, we can't decide.
7097 if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
7098 return true;
7099
7100 bool MustTrue = false, MustFalse = false;
7101
7102 auto AllowedRegion =
7103 ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
7104
7105 auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion(
7106 CmpI->getPredicate(), RHSAARange);
7107
7108 if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
7109 MustFalse = true;
7110
7111 if (SatisfyingRegion.contains(LHSAARange))
7112 MustTrue = true;
7113
7114 assert((!MustTrue || !MustFalse) &&
7115 "Either MustTrue or MustFalse should be false!");
7116
7117 if (MustTrue)
7118 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
7119 else if (MustFalse)
7120 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
7121 else
7122 T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
7123
7124 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
7125 << " " << RHSAA << "\n");
7126
7127 // TODO: Track a known state too.
7128 return T.isValidState();
7129 }
7130
7131 /// See AbstractAttribute::updateImpl(...).
7132 ChangeStatus updateImpl(Attributor &A) override {
7133 Instruction *CtxI = getCtxI();
7134 auto VisitValueCB = [&](Value &V, IntegerRangeState &T,
7135 bool Stripped) -> bool {
7136 Instruction *I = dyn_cast<Instruction>(&V);
7137 if (!I) {
7138
7139 // If the value is not instruction, we query AA to Attributor.
7140 const auto &AA =
7141 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(V));
7142
7143 // Clamp operator is not used to utilize a program point CtxI.
7144 T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
7145
7146 return T.isValidState();
7147 }
7148
Johannes Doerfert81554392020-02-09 20:14:35 -06007149 SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
7150 if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
7151 if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
7152 return false;
7153 } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
7154 if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
7155 return false;
7156 } else if (auto *CastI = dyn_cast<CastInst>(I)) {
7157 if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
7158 return false;
7159 } else {
Hideto Ueno188f9a32020-01-15 15:25:52 +09007160 // Give up with other instructions.
7161 // TODO: Add other instructions
7162
7163 T.indicatePessimisticFixpoint();
7164 return false;
7165 }
Johannes Doerfert81554392020-02-09 20:14:35 -06007166
7167 // Catch circular reasoning in a pessimistic way for now.
7168 // TODO: Check how the range evolves and if we stripped anything, see also
7169 // AADereferenceable or AAAlign for similar situations.
7170 for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
7171 if (QueriedAA != this)
7172 continue;
7173 // If we are in a stady state we do not need to worry.
7174 if (T.getAssumed() == getState().getAssumed())
7175 continue;
7176 T.indicatePessimisticFixpoint();
7177 }
7178
7179 return T.isValidState();
Hideto Ueno188f9a32020-01-15 15:25:52 +09007180 };
7181
7182 IntegerRangeState T(getBitWidth());
7183
7184 if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>(
7185 A, getIRPosition(), *this, T, VisitValueCB))
7186 return indicatePessimisticFixpoint();
7187
7188 return clampStateAndIndicateChange(getState(), T);
7189 }
7190
7191 /// See AbstractAttribute::trackStatistics()
7192 void trackStatistics() const override {
7193 STATS_DECLTRACK_FLOATING_ATTR(value_range)
7194 }
7195};
7196
7197struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
7198 AAValueConstantRangeFunction(const IRPosition &IRP)
7199 : AAValueConstantRangeImpl(IRP) {}
7200
7201 /// See AbstractAttribute::initialize(...).
7202 ChangeStatus updateImpl(Attributor &A) override {
7203 llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
7204 "not be called");
7205 }
7206
7207 /// See AbstractAttribute::trackStatistics()
7208 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
7209};
7210
7211struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
7212 AAValueConstantRangeCallSite(const IRPosition &IRP)
7213 : AAValueConstantRangeFunction(IRP) {}
7214
7215 /// See AbstractAttribute::trackStatistics()
7216 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
7217};
7218
Johannes Doerfert791c9f12020-01-29 18:02:42 -06007219struct AAValueConstantRangeCallSiteReturned
7220 : AACallSiteReturnedFromReturned<AAValueConstantRange,
7221 AAValueConstantRangeImpl> {
Hideto Ueno188f9a32020-01-15 15:25:52 +09007222 AAValueConstantRangeCallSiteReturned(const IRPosition &IRP)
Johannes Doerfert791c9f12020-01-29 18:02:42 -06007223 : AACallSiteReturnedFromReturned<AAValueConstantRange,
7224 AAValueConstantRangeImpl>(IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09007225
7226 /// See AbstractAttribute::initialize(...).
7227 void initialize(Attributor &A) override {
7228 // If it is a load instruction with range metadata, use the metadata.
7229 if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
7230 if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
7231 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
7232
Johannes Doerfert791c9f12020-01-29 18:02:42 -06007233 AAValueConstantRangeImpl::initialize(A);
Hideto Ueno188f9a32020-01-15 15:25:52 +09007234 }
7235
7236 /// See AbstractAttribute::trackStatistics()
7237 void trackStatistics() const override {
7238 STATS_DECLTRACK_CSRET_ATTR(value_range)
7239 }
7240};
7241struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
7242 AAValueConstantRangeCallSiteArgument(const IRPosition &IRP)
7243 : AAValueConstantRangeFloating(IRP) {}
7244
7245 /// See AbstractAttribute::trackStatistics()
7246 void trackStatistics() const override {
7247 STATS_DECLTRACK_CSARG_ATTR(value_range)
7248 }
7249};
Benjamin Kramer564a9de2020-02-17 17:55:03 +01007250
7251} // namespace
Johannes Doerfertaade7822019-06-05 03:02:24 +00007252/// ----------------------------------------------------------------------------
7253/// Attributor
7254/// ----------------------------------------------------------------------------
7255
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007256bool Attributor::isAssumedDead(const AbstractAttribute &AA,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007257 const AAIsDead *FnLivenessAA,
7258 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7259 const IRPosition &IRP = AA.getIRPosition();
7260 if (!Functions.count(IRP.getAnchorScope()))
7261 return false;
7262 return isAssumedDead(IRP, &AA, FnLivenessAA, CheckBBLivenessOnly, DepClass);
7263}
7264
7265bool Attributor::isAssumedDead(const Use &U,
7266 const AbstractAttribute *QueryingAA,
7267 const AAIsDead *FnLivenessAA,
7268 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7269 Instruction *UserI = dyn_cast<Instruction>(U.getUser());
7270 if (!UserI)
7271 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
7272 CheckBBLivenessOnly, DepClass);
7273
7274 if (CallSite CS = CallSite(UserI)) {
7275 // For call site argument uses we can check if the argument is
7276 // unused/dead.
7277 if (CS.isArgOperand(&U)) {
7278 const IRPosition &CSArgPos =
7279 IRPosition::callsite_argument(CS, CS.getArgumentNo(&U));
7280 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
7281 CheckBBLivenessOnly, DepClass);
7282 }
7283 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
7284 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
7285 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA, CheckBBLivenessOnly,
7286 DepClass);
7287 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
7288 BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
7289 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
7290 CheckBBLivenessOnly, DepClass);
7291 }
7292
7293 return isAssumedDead(IRPosition::value(*UserI), QueryingAA, FnLivenessAA,
7294 CheckBBLivenessOnly, DepClass);
7295}
7296
7297bool Attributor::isAssumedDead(const Instruction &I,
7298 const AbstractAttribute *QueryingAA,
7299 const AAIsDead *FnLivenessAA,
7300 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7301 if (!FnLivenessAA)
7302 FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction()),
7303 QueryingAA,
7304 /* TrackDependence */ false);
7305
7306 // If we have a context instruction and a liveness AA we use it.
7307 if (FnLivenessAA &&
7308 FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
7309 FnLivenessAA->isAssumedDead(&I)) {
7310 if (QueryingAA)
7311 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
7312 return true;
7313 }
7314
7315 if (CheckBBLivenessOnly)
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007316 return false;
7317
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007318 const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>(
7319 IRPosition::value(I), QueryingAA, /* TrackDependence */ false);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00007320 // Don't check liveness for AAIsDead.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007321 if (QueryingAA == &IsDeadAA)
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00007322 return false;
7323
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007324 if (IsDeadAA.isAssumedDead()) {
7325 if (QueryingAA)
7326 recordDependence(IsDeadAA, *QueryingAA, DepClass);
7327 return true;
7328 }
7329
7330 return false;
7331}
7332
7333bool Attributor::isAssumedDead(const IRPosition &IRP,
7334 const AbstractAttribute *QueryingAA,
7335 const AAIsDead *FnLivenessAA,
7336 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7337 Instruction *CtxI = IRP.getCtxI();
7338 if (CtxI &&
7339 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA,
7340 /* CheckBBLivenessOnly */ true,
7341 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
7342 return true;
7343
7344 if (CheckBBLivenessOnly)
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007345 return false;
7346
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007347 // If we haven't succeeded we query the specific liveness info for the IRP.
7348 const AAIsDead *IsDeadAA;
7349 if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
7350 IsDeadAA = &getOrCreateAAFor<AAIsDead>(
7351 IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
7352 QueryingAA, /* TrackDependence */ false);
7353 else
7354 IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA,
7355 /* TrackDependence */ false);
7356 // Don't check liveness for AAIsDead.
7357 if (QueryingAA == IsDeadAA)
7358 return false;
Johannes Doerfert19b00432019-08-26 17:48:05 +00007359
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007360 if (IsDeadAA->isAssumedDead()) {
7361 if (QueryingAA)
7362 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
7363 return true;
7364 }
7365
7366 return false;
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007367}
7368
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007369bool Attributor::checkForAllUses(
7370 const function_ref<bool(const Use &, bool &)> &Pred,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007371 const AbstractAttribute &QueryingAA, const Value &V,
7372 DepClassTy LivenessDepClass) {
Johannes Doerfertb2c76002020-01-23 17:12:56 -06007373
7374 // Check the trivial case first as it catches void values.
7375 if (V.use_empty())
7376 return true;
7377
7378 // If the value is replaced by another one, for now a constant, we do not have
7379 // uses. Note that this requires users of `checkForAllUses` to not recurse but
7380 // instead use the `follow` callback argument to look at transitive users,
7381 // however, that should be clear from the presence of the argument.
7382 bool UsedAssumedInformation = false;
Johannes Doerferte1eed6c2020-02-16 16:45:28 -06007383 Optional<Constant *> C =
Johannes Doerfertb2c76002020-01-23 17:12:56 -06007384 getAssumedConstant(*this, V, QueryingAA, UsedAssumedInformation);
Johannes Doerferte1eed6c2020-02-16 16:45:28 -06007385 if (C.hasValue() && C.getValue()) {
Johannes Doerfertb2c76002020-01-23 17:12:56 -06007386 LLVM_DEBUG(dbgs() << "[Attributor] Value is simplified, uses skipped: " << V
Johannes Doerferte1eed6c2020-02-16 16:45:28 -06007387 << " -> " << *C.getValue() << "\n");
Johannes Doerfertb2c76002020-01-23 17:12:56 -06007388 return true;
7389 }
7390
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007391 const IRPosition &IRP = QueryingAA.getIRPosition();
7392 SmallVector<const Use *, 16> Worklist;
7393 SmallPtrSet<const Use *, 16> Visited;
7394
7395 for (const Use &U : V.uses())
7396 Worklist.push_back(&U);
7397
7398 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
7399 << " initial uses to check\n");
7400
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007401 const Function *ScopeFn = IRP.getAnchorScope();
7402 const auto *LivenessAA =
7403 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
7404 /* TrackDependence */ false)
7405 : nullptr;
7406
7407 while (!Worklist.empty()) {
7408 const Use *U = Worklist.pop_back_val();
7409 if (!Visited.insert(U).second)
7410 continue;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007411 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in "
7412 << *U->getUser() << "\n");
7413 if (isAssumedDead(*U, &QueryingAA, LivenessAA,
7414 /* CheckBBLivenessOnly */ false, LivenessDepClass)) {
7415 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
7416 continue;
Johannes Doerfert8e629682020-02-11 00:10:35 -06007417 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007418
7419 bool Follow = false;
7420 if (!Pred(*U, Follow))
7421 return false;
7422 if (!Follow)
7423 continue;
7424 for (const Use &UU : U->getUser()->uses())
7425 Worklist.push_back(&UU);
7426 }
7427
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007428 return true;
7429}
7430
Johannes Doerfert661db042019-10-07 23:14:58 +00007431bool Attributor::checkForAllCallSites(
7432 const function_ref<bool(AbstractCallSite)> &Pred,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007433 const AbstractAttribute &QueryingAA, bool RequireAllCallSites,
7434 bool &AllCallSitesKnown) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00007435 // We can try to determine information from
7436 // the call sites. However, this is only possible all call sites are known,
7437 // hence the function has internal linkage.
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007438 const IRPosition &IRP = QueryingAA.getIRPosition();
7439 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfert748538e2019-10-07 23:30:04 +00007440 if (!AssociatedFunction) {
7441 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
7442 << "\n");
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007443 AllCallSitesKnown = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007444 return false;
Johannes Doerfert748538e2019-10-07 23:30:04 +00007445 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007446
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007447 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007448 &QueryingAA, AllCallSitesKnown);
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007449}
7450
7451bool Attributor::checkForAllCallSites(
7452 const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007453 bool RequireAllCallSites, const AbstractAttribute *QueryingAA,
7454 bool &AllCallSitesKnown) {
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007455 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00007456 LLVM_DEBUG(
7457 dbgs()
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007458 << "[Attributor] Function " << Fn.getName()
Hideto Ueno54869ec2019-07-15 06:49:04 +00007459 << " has no internal linkage, hence not all call sites are known\n");
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007460 AllCallSitesKnown = false;
Hideto Ueno54869ec2019-07-15 06:49:04 +00007461 return false;
7462 }
7463
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007464 // If we do not require all call sites we might not see all.
7465 AllCallSitesKnown = RequireAllCallSites;
7466
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06007467 SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
7468 for (unsigned u = 0; u < Uses.size(); ++u) {
7469 const Use &U = *Uses[u];
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007470 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in "
7471 << *U.getUser() << "\n");
7472 if (isAssumedDead(U, QueryingAA, nullptr, /* CheckBBLivenessOnly */ true)) {
7473 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
7474 continue;
7475 }
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06007476 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
7477 if (CE->isCast() && CE->getType()->isPointerTy() &&
7478 CE->getType()->getPointerElementType()->isFunctionTy()) {
7479 for (const Use &CEU : CE->uses())
7480 Uses.push_back(&CEU);
7481 continue;
7482 }
7483 }
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007484
Johannes Doerfert661db042019-10-07 23:14:58 +00007485 AbstractCallSite ACS(&U);
7486 if (!ACS) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007487 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
Johannes Doerfert661db042019-10-07 23:14:58 +00007488 << " has non call site use " << *U.get() << " in "
7489 << *U.getUser() << "\n");
Johannes Doerfert2d77b0c2019-11-01 22:35:18 -05007490 // BlockAddress users are allowed.
7491 if (isa<BlockAddress>(U.getUser()))
7492 continue;
Johannes Doerfertd98f9752019-08-21 21:48:56 +00007493 return false;
Johannes Doerfert661db042019-10-07 23:14:58 +00007494 }
Johannes Doerfertd98f9752019-08-21 21:48:56 +00007495
Johannes Doerfert661db042019-10-07 23:14:58 +00007496 const Use *EffectiveUse =
7497 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
7498 if (!ACS.isCallee(EffectiveUse)) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00007499 if (!RequireAllCallSites)
7500 continue;
Johannes Doerfert661db042019-10-07 23:14:58 +00007501 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007502 << " is an invalid use of " << Fn.getName() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00007503 return false;
7504 }
7505
Johannes Doerfert1e99fc92020-02-16 16:42:47 -06007506 // Make sure the arguments that can be matched between the call site and the
7507 // callee argee on their type. It is unlikely they do not and it doesn't
7508 // make sense for all attributes to know/care about this.
7509 assert(&Fn == ACS.getCalledFunction() && "Expected known callee");
7510 unsigned MinArgsParams =
7511 std::min(size_t(ACS.getNumArgOperands()), Fn.arg_size());
7512 for (unsigned u = 0; u < MinArgsParams; ++u) {
7513 Value *CSArgOp = ACS.getCallArgOperand(u);
7514 if (CSArgOp && Fn.getArg(u)->getType() != CSArgOp->getType()) {
7515 LLVM_DEBUG(
7516 dbgs() << "[Attributor] Call site / callee argument type mismatch ["
7517 << u << "@" << Fn.getName() << ": "
7518 << *Fn.getArg(u)->getType() << " vs. "
7519 << *ACS.getCallArgOperand(u)->getType() << "\n");
7520 return false;
7521 }
7522 }
7523
Johannes Doerfert661db042019-10-07 23:14:58 +00007524 if (Pred(ACS))
Hideto Ueno54869ec2019-07-15 06:49:04 +00007525 continue;
7526
Johannes Doerfert5304b722019-08-14 22:04:28 +00007527 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
Johannes Doerfert661db042019-10-07 23:14:58 +00007528 << *ACS.getInstruction() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00007529 return false;
7530 }
7531
7532 return true;
7533}
7534
Johannes Doerfert14a04932019-08-07 22:27:24 +00007535bool Attributor::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00007536 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00007537 &Pred,
7538 const AbstractAttribute &QueryingAA) {
7539
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007540 const IRPosition &IRP = QueryingAA.getIRPosition();
7541 // Since we need to provide return instructions we have to have an exact
7542 // definition.
7543 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00007544 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00007545 return false;
7546
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007547 // If this is a call site query we use the call site specific return values
7548 // and liveness information.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007549 // TODO: use the function scope once we have call site AAReturnedValues.
7550 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007551 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007552 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007553 return false;
7554
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007555 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
Johannes Doerfert14a04932019-08-07 22:27:24 +00007556}
7557
7558bool Attributor::checkForAllReturnedValues(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007559 const function_ref<bool(Value &)> &Pred,
Johannes Doerfert14a04932019-08-07 22:27:24 +00007560 const AbstractAttribute &QueryingAA) {
7561
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007562 const IRPosition &IRP = QueryingAA.getIRPosition();
7563 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00007564 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00007565 return false;
7566
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007567 // TODO: use the function scope once we have call site AAReturnedValues.
7568 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007569 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007570 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007571 return false;
7572
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007573 return AARetVal.checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00007574 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00007575 return Pred(RV);
7576 });
Johannes Doerfert14a04932019-08-07 22:27:24 +00007577}
7578
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007579static bool checkForAllInstructionsImpl(
7580 Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap,
7581 const function_ref<bool(Instruction &)> &Pred,
7582 const AbstractAttribute *QueryingAA, const AAIsDead *LivenessAA,
7583 const ArrayRef<unsigned> &Opcodes, bool CheckBBLivenessOnly = false) {
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007584 for (unsigned Opcode : Opcodes) {
7585 for (Instruction *I : OpcodeInstMap[Opcode]) {
7586 // Skip dead instructions.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007587 if (A && A->isAssumedDead(IRPosition::value(*I), QueryingAA, LivenessAA,
7588 CheckBBLivenessOnly))
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007589 continue;
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007590
7591 if (!Pred(*I))
7592 return false;
7593 }
7594 }
7595 return true;
7596}
7597
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007598bool Attributor::checkForAllInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007599 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007600 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes,
7601 bool CheckBBLivenessOnly) {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007602
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007603 const IRPosition &IRP = QueryingAA.getIRPosition();
7604 // Since we need to provide instructions we have to have an exact definition.
7605 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00007606 if (!AssociatedFunction)
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007607 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007608
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007609 // TODO: use the function scope once we have call site AAReturnedValues.
7610 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert19b00432019-08-26 17:48:05 +00007611 const auto &LivenessAA =
7612 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007613
7614 auto &OpcodeInstMap =
7615 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007616 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA,
7617 &LivenessAA, Opcodes, CheckBBLivenessOnly))
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007618 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007619
7620 return true;
7621}
7622
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007623bool Attributor::checkForAllReadWriteInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007624 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00007625 AbstractAttribute &QueryingAA) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007626
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007627 const Function *AssociatedFunction =
7628 QueryingAA.getIRPosition().getAssociatedFunction();
7629 if (!AssociatedFunction)
7630 return false;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007631
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007632 // TODO: use the function scope once we have call site AAReturnedValues.
7633 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
7634 const auto &LivenessAA =
7635 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007636
7637 for (Instruction *I :
7638 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007639 // Skip dead instructions.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007640 if (isAssumedDead(IRPosition::value(*I), &QueryingAA, &LivenessAA))
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007641 continue;
7642
7643 if (!Pred(*I))
7644 return false;
7645 }
7646
7647 return true;
7648}
7649
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007650ChangeStatus Attributor::run() {
Johannes Doerfertaade7822019-06-05 03:02:24 +00007651 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
7652 << AllAbstractAttributes.size()
7653 << " abstract attributes.\n");
7654
Stefan Stipanovic53605892019-06-27 11:27:54 +00007655 // Now that all abstract attributes are collected and initialized we start
7656 // the abstract analysis.
Johannes Doerfertaade7822019-06-05 03:02:24 +00007657
7658 unsigned IterationCounter = 1;
7659
7660 SmallVector<AbstractAttribute *, 64> ChangedAAs;
Johannes Doerfert680f6382019-11-02 02:48:05 -05007661 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
Johannes Doerfertaade7822019-06-05 03:02:24 +00007662 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
7663
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007664 bool RecomputeDependences = false;
7665
Johannes Doerfertaade7822019-06-05 03:02:24 +00007666 do {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007667 // Remember the size to determine new attributes.
7668 size_t NumAAs = AllAbstractAttributes.size();
Johannes Doerfertaade7822019-06-05 03:02:24 +00007669 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
7670 << ", Worklist size: " << Worklist.size() << "\n");
7671
Johannes Doerfert680f6382019-11-02 02:48:05 -05007672 // For invalid AAs we can fix dependent AAs that have a required dependence,
7673 // thereby folding long dependence chains in a single step without the need
7674 // to run updates.
7675 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
7676 AbstractAttribute *InvalidAA = InvalidAAs[u];
7677 auto &QuerriedAAs = QueryMap[InvalidAA];
7678 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "
7679 << QuerriedAAs.RequiredAAs.size() << "/"
7680 << QuerriedAAs.OptionalAAs.size()
7681 << " required/optional dependences\n");
7682 for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) {
7683 AbstractState &DOIAAState = DepOnInvalidAA->getState();
7684 DOIAAState.indicatePessimisticFixpoint();
7685 ++NumAttributesFixedDueToRequiredDependences;
7686 assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!");
7687 if (!DOIAAState.isValidState())
7688 InvalidAAs.insert(DepOnInvalidAA);
Johannes Doerfert22408542020-01-28 09:38:07 -06007689 else
7690 ChangedAAs.push_back(DepOnInvalidAA);
Johannes Doerfert680f6382019-11-02 02:48:05 -05007691 }
7692 if (!RecomputeDependences)
7693 Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
7694 QuerriedAAs.OptionalAAs.end());
7695 }
7696
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007697 // If dependences (=QueryMap) are recomputed we have to look at all abstract
7698 // attributes again, regardless of what changed in the last iteration.
7699 if (RecomputeDependences) {
7700 LLVM_DEBUG(
7701 dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
7702 QueryMap.clear();
7703 ChangedAAs.clear();
7704 Worklist.insert(AllAbstractAttributes.begin(),
7705 AllAbstractAttributes.end());
7706 }
7707
Johannes Doerfertaade7822019-06-05 03:02:24 +00007708 // Add all abstract attributes that are potentially dependent on one that
7709 // changed to the work list.
7710 for (AbstractAttribute *ChangedAA : ChangedAAs) {
7711 auto &QuerriedAAs = QueryMap[ChangedAA];
Johannes Doerfert680f6382019-11-02 02:48:05 -05007712 Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
7713 QuerriedAAs.OptionalAAs.end());
7714 Worklist.insert(QuerriedAAs.RequiredAAs.begin(),
7715 QuerriedAAs.RequiredAAs.end());
Johannes Doerfertaade7822019-06-05 03:02:24 +00007716 }
7717
Johannes Doerfertb504eb82019-08-26 18:55:47 +00007718 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
7719 << ", Worklist+Dependent size: " << Worklist.size()
7720 << "\n");
7721
Johannes Doerfert680f6382019-11-02 02:48:05 -05007722 // Reset the changed and invalid set.
Johannes Doerfertaade7822019-06-05 03:02:24 +00007723 ChangedAAs.clear();
Johannes Doerfert680f6382019-11-02 02:48:05 -05007724 InvalidAAs.clear();
Johannes Doerfertaade7822019-06-05 03:02:24 +00007725
7726 // Update all abstract attribute in the work list and record the ones that
7727 // changed.
7728 for (AbstractAttribute *AA : Worklist)
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007729 if (!AA->getState().isAtFixpoint() &&
7730 !isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true)) {
Johannes Doerfert2dad7292019-10-13 21:10:31 -05007731 QueriedNonFixAA = false;
7732 if (AA->update(*this) == ChangeStatus::CHANGED) {
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007733 ChangedAAs.push_back(AA);
Johannes Doerfert680f6382019-11-02 02:48:05 -05007734 if (!AA->getState().isValidState())
7735 InvalidAAs.insert(AA);
Johannes Doerfert2dad7292019-10-13 21:10:31 -05007736 } else if (!QueriedNonFixAA) {
7737 // If the attribute did not query any non-fix information, the state
7738 // will not change and we can indicate that right away.
7739 AA->getState().indicateOptimisticFixpoint();
7740 }
7741 }
Johannes Doerfertaade7822019-06-05 03:02:24 +00007742
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007743 // Check if we recompute the dependences in the next iteration.
7744 RecomputeDependences = (DepRecomputeInterval > 0 &&
7745 IterationCounter % DepRecomputeInterval == 0);
7746
Johannes Doerfert9543f142019-08-23 15:24:57 +00007747 // Add attributes to the changed set if they have been created in the last
7748 // iteration.
7749 ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs,
7750 AllAbstractAttributes.end());
7751
Johannes Doerfertaade7822019-06-05 03:02:24 +00007752 // Reset the work list and repopulate with the changed abstract attributes.
7753 // Note that dependent ones are added above.
7754 Worklist.clear();
7755 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
7756
Johannes Doerfertbf112132019-08-29 01:29:44 +00007757 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
7758 VerifyMaxFixpointIterations));
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007759
Johannes Doerfertaade7822019-06-05 03:02:24 +00007760 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
7761 << IterationCounter << "/" << MaxFixpointIterations
7762 << " iterations\n");
7763
Johannes Doerfertbf112132019-08-29 01:29:44 +00007764 size_t NumFinalAAs = AllAbstractAttributes.size();
Johannes Doerfertb504eb82019-08-26 18:55:47 +00007765
Johannes Doerfertaade7822019-06-05 03:02:24 +00007766 // Reset abstract arguments not settled in a sound fixpoint by now. This
7767 // happens when we stopped the fixpoint iteration early. Note that only the
7768 // ones marked as "changed" *and* the ones transitively depending on them
7769 // need to be reverted to a pessimistic state. Others might not be in a
7770 // fixpoint state but we can use the optimistic results for them anyway.
7771 SmallPtrSet<AbstractAttribute *, 32> Visited;
7772 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
7773 AbstractAttribute *ChangedAA = ChangedAAs[u];
7774 if (!Visited.insert(ChangedAA).second)
7775 continue;
7776
7777 AbstractState &State = ChangedAA->getState();
7778 if (!State.isAtFixpoint()) {
7779 State.indicatePessimisticFixpoint();
7780
7781 NumAttributesTimedOut++;
7782 }
7783
7784 auto &QuerriedAAs = QueryMap[ChangedAA];
Johannes Doerfert680f6382019-11-02 02:48:05 -05007785 ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(),
7786 QuerriedAAs.OptionalAAs.end());
7787 ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(),
7788 QuerriedAAs.RequiredAAs.end());
Johannes Doerfertaade7822019-06-05 03:02:24 +00007789 }
7790
7791 LLVM_DEBUG({
7792 if (!Visited.empty())
7793 dbgs() << "\n[Attributor] Finalized " << Visited.size()
7794 << " abstract attributes.\n";
7795 });
7796
7797 unsigned NumManifested = 0;
7798 unsigned NumAtFixpoint = 0;
7799 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
7800 for (AbstractAttribute *AA : AllAbstractAttributes) {
7801 AbstractState &State = AA->getState();
7802
7803 // If there is not already a fixpoint reached, we can now take the
7804 // optimistic state. This is correct because we enforced a pessimistic one
7805 // on abstract attributes that were transitively dependent on a changed one
7806 // already above.
7807 if (!State.isAtFixpoint())
7808 State.indicateOptimisticFixpoint();
7809
7810 // If the state is invalid, we do not try to manifest it.
7811 if (!State.isValidState())
7812 continue;
7813
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007814 // Skip dead code.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007815 if (isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true))
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007816 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +00007817 // Manifest the state and record if we changed the IR.
7818 ChangeStatus LocalChange = AA->manifest(*this);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00007819 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
7820 AA->trackStatistics();
Johannes Doerfert8e76fec2020-02-16 17:37:50 -06007821 LLVM_DEBUG(dbgs() << "[Attributor] Manifest " << LocalChange << " : " << *AA
7822 << "\n");
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00007823
Johannes Doerfertaade7822019-06-05 03:02:24 +00007824 ManifestChange = ManifestChange | LocalChange;
7825
7826 NumAtFixpoint++;
7827 NumManifested += (LocalChange == ChangeStatus::CHANGED);
7828 }
7829
7830 (void)NumManifested;
7831 (void)NumAtFixpoint;
7832 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
7833 << " arguments while " << NumAtFixpoint
7834 << " were in a valid fixpoint state\n");
7835
Johannes Doerfertaade7822019-06-05 03:02:24 +00007836 NumAttributesManifested += NumManifested;
7837 NumAttributesValidFixpoint += NumAtFixpoint;
7838
Fangrui Songf1826172019-08-20 07:21:43 +00007839 (void)NumFinalAAs;
Johannes Doerfertb70297a2020-02-14 10:34:31 -06007840 if (NumFinalAAs != AllAbstractAttributes.size()) {
7841 for (unsigned u = NumFinalAAs; u < AllAbstractAttributes.size(); ++u)
7842 errs() << "Unexpected abstract attribute: " << *AllAbstractAttributes[u]
7843 << " :: "
7844 << AllAbstractAttributes[u]->getIRPosition().getAssociatedValue()
7845 << "\n";
7846 llvm_unreachable("Expected the final number of abstract attributes to "
7847 "remain unchanged!");
7848 }
Johannes Doerfert39681e72019-08-27 04:57:54 +00007849
7850 // Delete stuff at the end to avoid invalid references and a nice order.
Johannes Doerfert2f622062019-09-04 16:35:20 +00007851 {
7852 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
7853 << ToBeDeletedFunctions.size() << " functions and "
7854 << ToBeDeletedBlocks.size() << " blocks and "
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007855 << ToBeDeletedInsts.size() << " instructions and "
7856 << ToBeChangedUses.size() << " uses\n");
7857
Alina Sbirlea9e66c4e2020-01-23 14:21:08 -08007858 SmallVector<WeakTrackingVH, 32> DeadInsts;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007859 SmallVector<Instruction *, 32> TerminatorsToFold;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007860
7861 for (auto &It : ToBeChangedUses) {
7862 Use *U = It.first;
7863 Value *NewV = It.second;
7864 Value *OldV = U->get();
Johannes Doerfert54ec9b52020-03-16 20:23:27 -05007865
7866 // Do not replace uses in returns if the value is a must-tail call we will
7867 // not delete.
7868 if (isa<ReturnInst>(U->getUser()))
7869 if (auto *CI = dyn_cast<CallInst>(OldV->stripPointerCasts()))
7870 if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI))
7871 continue;
7872
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007873 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
7874 << " instead of " << *OldV << "\n");
7875 U->set(NewV);
Johannes Doerfert137c99a2020-02-14 20:06:34 -06007876 // Do not modify call instructions outside the SCC.
7877 if (auto *CB = dyn_cast<CallBase>(OldV))
7878 if (!Functions.count(CB->getCaller()))
7879 continue;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007880 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
7881 CGModifiedFunctions.insert(I->getFunction());
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007882 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007883 isInstructionTriviallyDead(I))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007884 DeadInsts.push_back(I);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007885 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007886 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
7887 Instruction *UserI = cast<Instruction>(U->getUser());
7888 if (isa<UndefValue>(NewV)) {
Hideto Uenocb5eb132019-12-27 02:39:37 +09007889 ToBeChangedToUnreachableInsts.insert(UserI);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007890 } else {
7891 TerminatorsToFold.push_back(UserI);
7892 }
7893 }
Johannes Doerfert2f622062019-09-04 16:35:20 +00007894 }
Johannes Doerferta4088c72020-01-07 16:01:57 -06007895 for (auto &V : InvokeWithDeadSuccessor)
7896 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
7897 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
7898 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
7899 bool Invoke2CallAllowed =
7900 !AAIsDeadFunction::mayCatchAsynchronousExceptions(
7901 *II->getFunction());
7902 assert((UnwindBBIsDead || NormalBBIsDead) &&
7903 "Invoke does not have dead successors!");
7904 BasicBlock *BB = II->getParent();
7905 BasicBlock *NormalDestBB = II->getNormalDest();
7906 if (UnwindBBIsDead) {
7907 Instruction *NormalNextIP = &NormalDestBB->front();
7908 if (Invoke2CallAllowed) {
7909 changeToCall(II);
7910 NormalNextIP = BB->getTerminator();
7911 }
7912 if (NormalBBIsDead)
7913 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
7914 } else {
7915 assert(NormalBBIsDead && "Broken invariant!");
7916 if (!NormalDestBB->getUniquePredecessor())
7917 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
7918 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
7919 }
7920 }
Stefanos Baziotis01c48d7d2020-03-07 12:38:44 +02007921 for (Instruction *I : TerminatorsToFold) {
7922 CGModifiedFunctions.insert(I->getFunction());
7923 ConstantFoldTerminator(I->getParent());
7924 }
Johannes Doerfert1e46eb72020-01-07 15:10:30 -06007925 for (auto &V : ToBeChangedToUnreachableInsts)
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007926 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
7927 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfert1e46eb72020-01-07 15:10:30 -06007928 changeToUnreachable(I, /* UseLLVMTrap */ false);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007929 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007930
Johannes Doerfert5429c822020-01-11 23:30:36 -06007931 for (auto &V : ToBeDeletedInsts) {
7932 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007933 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfertb4352e42020-02-13 20:10:59 -06007934 if (!I->getType()->isVoidTy())
7935 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Johannes Doerfert5429c822020-01-11 23:30:36 -06007936 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
7937 DeadInsts.push_back(I);
7938 else
7939 I->eraseFromParent();
7940 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007941 }
7942
7943 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
Johannes Doerfertb19cd272019-09-03 20:42:16 +00007944
Johannes Doerfert2f622062019-09-04 16:35:20 +00007945 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
7946 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
7947 ToBeDeletedBBs.reserve(NumDeadBlocks);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007948 for (BasicBlock *BB : ToBeDeletedBlocks) {
7949 CGModifiedFunctions.insert(BB->getParent());
7950 ToBeDeletedBBs.push_back(BB);
7951 }
Johannes Doerfert5e442a52019-10-30 17:34:59 -05007952 // Actually we do not delete the blocks but squash them into a single
7953 // unreachable but untangling branches that jump here is something we need
7954 // to do in a more generic way.
7955 DetatchDeadBlocks(ToBeDeletedBBs, nullptr);
7956 STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
7957 BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size();
Johannes Doerfert2f622062019-09-04 16:35:20 +00007958 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00007959
Johannes Doerfert2f622062019-09-04 16:35:20 +00007960 // Identify dead internal functions and delete them. This happens outside
7961 // the other fixpoint analysis as we might treat potentially dead functions
7962 // as live to lower the number of iterations. If they happen to be dead, the
7963 // below fixpoint loop will identify and eliminate them.
7964 SmallVector<Function *, 8> InternalFns;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007965 for (Function *F : Functions)
7966 if (F->hasLocalLinkage())
7967 InternalFns.push_back(F);
Johannes Doerfert2f622062019-09-04 16:35:20 +00007968
7969 bool FoundDeadFn = true;
7970 while (FoundDeadFn) {
7971 FoundDeadFn = false;
7972 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
7973 Function *F = InternalFns[u];
7974 if (!F)
7975 continue;
7976
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007977 bool AllCallSitesKnown;
Johannes Doerfert6b9ee2d2020-01-02 16:53:37 -06007978 if (!checkForAllCallSites(
7979 [this](AbstractCallSite ACS) {
7980 return ToBeDeletedFunctions.count(
7981 ACS.getInstruction()->getFunction());
7982 },
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007983 *F, true, nullptr, AllCallSitesKnown))
Johannes Doerfert2f622062019-09-04 16:35:20 +00007984 continue;
7985
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007986 ToBeDeletedFunctions.insert(F);
Johannes Doerfert2f622062019-09-04 16:35:20 +00007987 InternalFns[u] = nullptr;
7988 FoundDeadFn = true;
7989 }
7990 }
Johannes Doerfert39681e72019-08-27 04:57:54 +00007991 }
7992
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007993 // Rewrite the functions as requested during manifest.
7994 ManifestChange =
7995 ManifestChange | rewriteFunctionSignatures(CGModifiedFunctions);
7996
7997 for (Function *Fn : CGModifiedFunctions)
7998 CGUpdater.reanalyzeFunction(*Fn);
7999
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008000 STATS_DECL(AAIsDead, Function, "Number of dead functions deleted.");
8001 BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size();
8002
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008003 for (Function *Fn : ToBeDeletedFunctions)
8004 CGUpdater.removeFunction(*Fn);
Johannes Doerfert6b9ee2d2020-01-02 16:53:37 -06008005
Johannes Doerfertbf112132019-08-29 01:29:44 +00008006 if (VerifyMaxFixpointIterations &&
8007 IterationCounter != MaxFixpointIterations) {
8008 errs() << "\n[Attributor] Fixpoint iteration done after: "
8009 << IterationCounter << "/" << MaxFixpointIterations
8010 << " iterations\n";
8011 llvm_unreachable("The fixpoint was not reached with exactly the number of "
8012 "specified iterations!");
8013 }
8014
Johannes Doerfertaade7822019-06-05 03:02:24 +00008015 return ManifestChange;
8016}
8017
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008018bool Attributor::isValidFunctionSignatureRewrite(
8019 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
Johannes Doerfert75133632019-10-10 01:39:16 -05008020
8021 auto CallSiteCanBeChanged = [](AbstractCallSite ACS) {
8022 // Forbid must-tail calls for now.
8023 return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall();
8024 };
8025
8026 Function *Fn = Arg.getParent();
8027 // Avoid var-arg functions for now.
8028 if (Fn->isVarArg()) {
8029 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
8030 return false;
8031 }
8032
8033 // Avoid functions with complicated argument passing semantics.
8034 AttributeList FnAttributeList = Fn->getAttributes();
8035 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
8036 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
8037 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) {
8038 LLVM_DEBUG(
8039 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
8040 return false;
8041 }
8042
8043 // Avoid callbacks for now.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06008044 bool AllCallSitesKnown;
8045 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
8046 AllCallSitesKnown)) {
Johannes Doerfert75133632019-10-10 01:39:16 -05008047 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
8048 return false;
8049 }
8050
8051 auto InstPred = [](Instruction &I) {
8052 if (auto *CI = dyn_cast<CallInst>(&I))
8053 return !CI->isMustTailCall();
8054 return true;
8055 };
8056
8057 // Forbid must-tail calls for now.
8058 // TODO:
Johannes Doerfert75133632019-10-10 01:39:16 -05008059 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008060 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
8061 nullptr, {Instruction::Call})) {
Johannes Doerfert75133632019-10-10 01:39:16 -05008062 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
8063 return false;
8064 }
8065
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008066 return true;
8067}
8068
8069bool Attributor::registerFunctionSignatureRewrite(
8070 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
8071 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
8072 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
8073 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
8074 << Arg.getParent()->getName() << " with "
8075 << ReplacementTypes.size() << " replacements\n");
8076 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
8077 "Cannot register an invalid rewrite");
8078
8079 Function *Fn = Arg.getParent();
Johannes Doerfert75133632019-10-10 01:39:16 -05008080 SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn];
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008081 if (ARIs.empty())
Johannes Doerfert75133632019-10-10 01:39:16 -05008082 ARIs.resize(Fn->arg_size());
8083
8084 // If we have a replacement already with less than or equal new arguments,
8085 // ignore this request.
8086 ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()];
8087 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
8088 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
8089 return false;
8090 }
8091
8092 // If we have a replacement already but we like the new one better, delete
8093 // the old.
8094 if (ARI)
8095 delete ARI;
8096
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008097 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
8098 << Arg.getParent()->getName() << " with "
8099 << ReplacementTypes.size() << " replacements\n");
8100
Johannes Doerfert75133632019-10-10 01:39:16 -05008101 // Remember the replacement.
8102 ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
8103 std::move(CalleeRepairCB),
8104 std::move(ACSRepairCB));
8105
8106 return true;
8107}
8108
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008109ChangeStatus Attributor::rewriteFunctionSignatures(
8110 SmallPtrSetImpl<Function *> &ModifiedFns) {
Johannes Doerfert75133632019-10-10 01:39:16 -05008111 ChangeStatus Changed = ChangeStatus::UNCHANGED;
8112
8113 for (auto &It : ArgumentReplacementMap) {
8114 Function *OldFn = It.getFirst();
8115
8116 // Deleted functions do not require rewrites.
8117 if (ToBeDeletedFunctions.count(OldFn))
8118 continue;
8119
8120 const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond();
8121 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
8122
8123 SmallVector<Type *, 16> NewArgumentTypes;
8124 SmallVector<AttributeSet, 16> NewArgumentAttributes;
8125
8126 // Collect replacement argument types and copy over existing attributes.
8127 AttributeList OldFnAttributeList = OldFn->getAttributes();
8128 for (Argument &Arg : OldFn->args()) {
8129 if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) {
8130 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
8131 ARI->ReplacementTypes.end());
8132 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
8133 AttributeSet());
8134 } else {
8135 NewArgumentTypes.push_back(Arg.getType());
8136 NewArgumentAttributes.push_back(
8137 OldFnAttributeList.getParamAttributes(Arg.getArgNo()));
8138 }
8139 }
8140
8141 FunctionType *OldFnTy = OldFn->getFunctionType();
8142 Type *RetTy = OldFnTy->getReturnType();
8143
8144 // Construct the new function type using the new arguments types.
8145 FunctionType *NewFnTy =
8146 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
8147
8148 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
8149 << "' from " << *OldFn->getFunctionType() << " to "
8150 << *NewFnTy << "\n");
8151
8152 // Create the new function body and insert it into the module.
8153 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
8154 OldFn->getAddressSpace(), "");
8155 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
8156 NewFn->takeName(OldFn);
8157 NewFn->copyAttributesFrom(OldFn);
8158
8159 // Patch the pointer to LLVM function in debug info descriptor.
8160 NewFn->setSubprogram(OldFn->getSubprogram());
8161 OldFn->setSubprogram(nullptr);
8162
8163 // Recompute the parameter attributes list based on the new arguments for
8164 // the function.
8165 LLVMContext &Ctx = OldFn->getContext();
8166 NewFn->setAttributes(AttributeList::get(
8167 Ctx, OldFnAttributeList.getFnAttributes(),
8168 OldFnAttributeList.getRetAttributes(), NewArgumentAttributes));
8169
8170 // Since we have now created the new function, splice the body of the old
8171 // function right into the new function, leaving the old rotting hulk of the
8172 // function empty.
8173 NewFn->getBasicBlockList().splice(NewFn->begin(),
8174 OldFn->getBasicBlockList());
8175
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008176 // Set of all "call-like" instructions that invoke the old function mapped
8177 // to their new replacements.
8178 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
Johannes Doerfert75133632019-10-10 01:39:16 -05008179
8180 // Callback to create a new "call-like" instruction for a given one.
8181 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
8182 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
8183 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
8184
8185 // Collect the new argument operands for the replacement call site.
8186 SmallVector<Value *, 16> NewArgOperands;
8187 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
8188 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
8189 unsigned NewFirstArgNum = NewArgOperands.size();
Ilya Biryukov4f82af82019-12-31 10:21:52 +01008190 (void)NewFirstArgNum; // only used inside assert.
Johannes Doerfert75133632019-10-10 01:39:16 -05008191 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
8192 if (ARI->ACSRepairCB)
8193 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
8194 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
8195 NewArgOperands.size() &&
8196 "ACS repair callback did not provide as many operand as new "
8197 "types were registered!");
8198 // TODO: Exose the attribute set to the ACS repair callback
8199 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
8200 AttributeSet());
8201 } else {
8202 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
8203 NewArgOperandAttributes.push_back(
8204 OldCallAttributeList.getParamAttributes(OldArgNum));
8205 }
8206 }
8207
8208 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
8209 "Mismatch # argument operands vs. # argument operand attributes!");
8210 assert(NewArgOperands.size() == NewFn->arg_size() &&
8211 "Mismatch # argument operands vs. # function arguments!");
8212
8213 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
8214 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
8215
8216 // Create a new call or invoke instruction to replace the old one.
8217 CallBase *NewCB;
8218 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
8219 NewCB =
8220 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
8221 NewArgOperands, OperandBundleDefs, "", OldCB);
8222 } else {
8223 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
8224 "", OldCB);
8225 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
8226 NewCB = NewCI;
8227 }
8228
8229 // Copy over various properties and the new attributes.
Johannes Doerfert75133632019-10-10 01:39:16 -05008230 uint64_t W;
8231 if (OldCB->extractProfTotalWeight(W))
8232 NewCB->setProfWeight(W);
8233 NewCB->setCallingConv(OldCB->getCallingConv());
8234 NewCB->setDebugLoc(OldCB->getDebugLoc());
8235 NewCB->takeName(OldCB);
8236 NewCB->setAttributes(AttributeList::get(
8237 Ctx, OldCallAttributeList.getFnAttributes(),
8238 OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes));
8239
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008240 CallSitePairs.push_back({OldCB, NewCB});
Johannes Doerfert75133632019-10-10 01:39:16 -05008241 return true;
8242 };
8243
8244 // Use the CallSiteReplacementCreator to create replacement call sites.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06008245 bool AllCallSitesKnown;
8246 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
8247 true, nullptr, AllCallSitesKnown);
Ilya Biryukov4f82af82019-12-31 10:21:52 +01008248 (void)Success;
Johannes Doerfert75133632019-10-10 01:39:16 -05008249 assert(Success && "Assumed call site replacement to succeed!");
8250
8251 // Rewire the arguments.
8252 auto OldFnArgIt = OldFn->arg_begin();
8253 auto NewFnArgIt = NewFn->arg_begin();
8254 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
8255 ++OldArgNum, ++OldFnArgIt) {
8256 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
8257 if (ARI->CalleeRepairCB)
8258 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
8259 NewFnArgIt += ARI->ReplacementTypes.size();
8260 } else {
8261 NewFnArgIt->takeName(&*OldFnArgIt);
8262 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
8263 ++NewFnArgIt;
8264 }
8265 }
8266
8267 // Eliminate the instructions *after* we visited all of them.
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008268 for (auto &CallSitePair : CallSitePairs) {
8269 CallBase &OldCB = *CallSitePair.first;
8270 CallBase &NewCB = *CallSitePair.second;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008271 // We do not modify the call graph here but simply reanalyze the old
8272 // function. This should be revisited once the old PM is gone.
8273 ModifiedFns.insert(OldCB.getFunction());
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008274 OldCB.replaceAllUsesWith(&NewCB);
8275 OldCB.eraseFromParent();
8276 }
Johannes Doerfert75133632019-10-10 01:39:16 -05008277
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008278 // Replace the function in the call graph (if any).
8279 CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
8280
8281 // If the old function was modified and needed to be reanalyzed, the new one
8282 // does now.
8283 if (ModifiedFns.erase(OldFn))
8284 ModifiedFns.insert(NewFn);
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008285
Johannes Doerfert75133632019-10-10 01:39:16 -05008286 Changed = ChangeStatus::CHANGED;
8287 }
8288
8289 return Changed;
8290}
8291
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008292void Attributor::initializeInformationCache(Function &F) {
8293
8294 // Walk all instructions to find interesting instructions that might be
8295 // queried by abstract attributes during their initialization or update.
8296 // This has to happen before we create attributes.
8297 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
8298 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
8299
8300 for (Instruction &I : instructions(&F)) {
8301 bool IsInterestingOpcode = false;
8302
8303 // To allow easy access to all instructions in a function with a given
8304 // opcode we store them in the InfoCache. As not all opcodes are interesting
8305 // to concrete attributes we only cache the ones that are as identified in
8306 // the following switch.
8307 // Note: There are no concrete attributes now so this is initially empty.
8308 switch (I.getOpcode()) {
8309 default:
8310 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
8311 "New call site/base instruction type needs to be known int the "
8312 "Attributor.");
8313 break;
8314 case Instruction::Load:
8315 // The alignment of a pointer is interesting for loads.
8316 case Instruction::Store:
8317 // The alignment of a pointer is interesting for stores.
8318 case Instruction::Call:
8319 case Instruction::CallBr:
8320 case Instruction::Invoke:
8321 case Instruction::CleanupRet:
8322 case Instruction::CatchSwitch:
Johannes Doerfert5732f562019-12-24 19:25:08 -06008323 case Instruction::AtomicRMW:
8324 case Instruction::AtomicCmpXchg:
Hideto Uenoef4febd2019-12-29 17:34:08 +09008325 case Instruction::Br:
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008326 case Instruction::Resume:
8327 case Instruction::Ret:
8328 IsInterestingOpcode = true;
8329 }
8330 if (IsInterestingOpcode)
8331 InstOpcodeMap[I.getOpcode()].push_back(&I);
8332 if (I.mayReadOrWriteMemory())
8333 ReadOrWriteInsts.push_back(&I);
8334 }
Johannes Doerferta198adb2020-03-13 00:32:38 -05008335
8336 if (F.hasFnAttribute(Attribute::AlwaysInline) &&
8337 isInlineViable(F).isSuccess())
8338 InfoCache.InlineableFunctions.insert(&F);
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008339}
8340
Johannes Doerfert12173e62019-10-13 20:25:25 -05008341void Attributor::recordDependence(const AbstractAttribute &FromAA,
Johannes Doerfert680f6382019-11-02 02:48:05 -05008342 const AbstractAttribute &ToAA,
8343 DepClassTy DepClass) {
Johannes Doerfert2dad7292019-10-13 21:10:31 -05008344 if (FromAA.getState().isAtFixpoint())
8345 return;
8346
Johannes Doerfert680f6382019-11-02 02:48:05 -05008347 if (DepClass == DepClassTy::REQUIRED)
8348 QueryMap[&FromAA].RequiredAAs.insert(
8349 const_cast<AbstractAttribute *>(&ToAA));
8350 else
8351 QueryMap[&FromAA].OptionalAAs.insert(
8352 const_cast<AbstractAttribute *>(&ToAA));
Johannes Doerfert2dad7292019-10-13 21:10:31 -05008353 QueriedNonFixAA = true;
Johannes Doerfert12173e62019-10-13 20:25:25 -05008354}
8355
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00008356void Attributor::identifyDefaultAbstractAttributes(Function &F) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00008357 if (!VisitedFunctions.insert(&F).second)
8358 return;
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05008359 if (F.isDeclaration())
8360 return;
Johannes Doerfertaade7822019-06-05 03:02:24 +00008361
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008362 IRPosition FPos = IRPosition::function(F);
8363
Johannes Doerfert305b9612019-08-04 18:40:01 +00008364 // Check for dead BasicBlocks in every function.
Johannes Doerfert21fe0a32019-08-06 00:55:11 +00008365 // We need dead instruction detection because we do not want to deal with
8366 // broken IR in which SSA rules do not apply.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008367 getOrCreateAAFor<AAIsDead>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00008368
8369 // Every function might be "will-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008370 getOrCreateAAFor<AAWillReturn>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00008371
Johannes Doerfert58f324a2019-12-24 18:48:50 -06008372 // Every function might contain instructions that cause "undefined behavior".
8373 getOrCreateAAFor<AAUndefinedBehavior>(FPos);
8374
Stefan Stipanovic53605892019-06-27 11:27:54 +00008375 // Every function can be nounwind.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008376 getOrCreateAAFor<AANoUnwind>(FPos);
Stefan Stipanovic53605892019-06-27 11:27:54 +00008377
Stefan Stipanovic06263672019-07-11 21:37:40 +00008378 // Every function might be marked "nosync"
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008379 getOrCreateAAFor<AANoSync>(FPos);
Stefan Stipanovic06263672019-07-11 21:37:40 +00008380
Hideto Ueno65bbaf92019-07-12 17:38:51 +00008381 // Every function might be "no-free".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008382 getOrCreateAAFor<AANoFree>(FPos);
Hideto Ueno65bbaf92019-07-12 17:38:51 +00008383
Johannes Doerferte83f3032019-08-05 23:22:05 +00008384 // Every function might be "no-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008385 getOrCreateAAFor<AANoReturn>(FPos);
Johannes Doerferte83f3032019-08-05 23:22:05 +00008386
Hideto Ueno63f60662019-09-21 15:13:19 +00008387 // Every function might be "no-recurse".
8388 getOrCreateAAFor<AANoRecurse>(FPos);
8389
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008390 // Every function might be "readnone/readonly/writeonly/...".
8391 getOrCreateAAFor<AAMemoryBehavior>(FPos);
8392
Johannes Doerfert282f5d72020-01-26 02:51:57 -06008393 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
8394 getOrCreateAAFor<AAMemoryLocation>(FPos);
8395
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008396 // Every function might be applicable for Heap-To-Stack conversion.
8397 if (EnableHeapToStack)
8398 getOrCreateAAFor<AAHeapToStack>(FPos);
8399
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00008400 // Return attributes are only appropriate if the return type is non void.
8401 Type *ReturnType = F.getReturnType();
8402 if (!ReturnType->isVoidTy()) {
8403 // Argument attribute "returned" --- Create only one per function even
8404 // though it is an argument attribute.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008405 getOrCreateAAFor<AAReturnedValues>(FPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00008406
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008407 IRPosition RetPos = IRPosition::returned(F);
8408
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008409 // Every returned value might be dead.
8410 getOrCreateAAFor<AAIsDead>(RetPos);
8411
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008412 // Every function might be simplified.
8413 getOrCreateAAFor<AAValueSimplify>(RetPos);
8414
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008415 if (ReturnType->isPointerTy()) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008416
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008417 // Every function with pointer return type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008418 getOrCreateAAFor<AAAlign>(RetPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008419
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008420 // Every function with pointer return type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008421 getOrCreateAAFor<AANonNull>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008422
8423 // Every function with pointer return type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008424 getOrCreateAAFor<AANoAlias>(RetPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008425
8426 // Every function with pointer return type might be marked
8427 // dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008428 getOrCreateAAFor<AADereferenceable>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008429 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00008430 }
8431
Hideto Ueno54869ec2019-07-15 06:49:04 +00008432 for (Argument &Arg : F.args()) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008433 IRPosition ArgPos = IRPosition::argument(Arg);
8434
8435 // Every argument might be simplified.
8436 getOrCreateAAFor<AAValueSimplify>(ArgPos);
8437
Johannes Doerfert1e99fc92020-02-16 16:42:47 -06008438 // Every argument might be dead.
8439 getOrCreateAAFor<AAIsDead>(ArgPos);
8440
Hideto Ueno19c07af2019-07-23 08:16:17 +00008441 if (Arg.getType()->isPointerTy()) {
8442 // Every argument with pointer type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008443 getOrCreateAAFor<AANonNull>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008444
Hideto Uenocbab3342019-08-29 05:52:00 +00008445 // Every argument with pointer type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008446 getOrCreateAAFor<AANoAlias>(ArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00008447
Hideto Ueno19c07af2019-07-23 08:16:17 +00008448 // Every argument with pointer type might be marked dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008449 getOrCreateAAFor<AADereferenceable>(ArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008450
8451 // Every argument with pointer type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008452 getOrCreateAAFor<AAAlign>(ArgPos);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00008453
8454 // Every argument with pointer type might be marked nocapture.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008455 getOrCreateAAFor<AANoCapture>(ArgPos);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008456
8457 // Every argument with pointer type might be marked
8458 // "readnone/readonly/writeonly/..."
8459 getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01008460
8461 // Every argument with pointer type might be marked nofree.
8462 getOrCreateAAFor<AANoFree>(ArgPos);
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008463
8464 // Every argument with pointer type might be privatizable (or promotable)
8465 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008466 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00008467 }
8468
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008469 auto CallSitePred = [&](Instruction &I) -> bool {
Hideto Ueno54869ec2019-07-15 06:49:04 +00008470 CallSite CS(&I);
Johannes Doerfert86509e82020-01-12 00:34:38 -06008471 IRPosition CSRetPos = IRPosition::callsite_returned(CS);
8472
8473 // Call sites might be dead if they do not have side effects and no live
8474 // users. The return value might be dead if there are no live users.
8475 getOrCreateAAFor<AAIsDead>(CSRetPos);
8476
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008477 if (Function *Callee = CS.getCalledFunction()) {
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05008478 // Skip declerations except if annotations on their call sites were
8479 // explicitly requested.
Johannes Doerfert139c9ef2019-12-13 23:41:02 -06008480 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
8481 !Callee->hasMetadata(LLVMContext::MD_callback))
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05008482 return true;
8483
8484 if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) {
Hideto Ueno188f9a32020-01-15 15:25:52 +09008485
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008486 IRPosition CSRetPos = IRPosition::callsite_returned(CS);
8487
Hideto Ueno188f9a32020-01-15 15:25:52 +09008488 // Call site return integer values might be limited by a constant range.
Johannes Doerfert86509e82020-01-12 00:34:38 -06008489 if (Callee->getReturnType()->isIntegerTy())
Hideto Ueno188f9a32020-01-15 15:25:52 +09008490 getOrCreateAAFor<AAValueConstantRange>(CSRetPos);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008491 }
8492
Johannes Doerfert28880192019-12-31 00:57:00 -06008493 for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008494
8495 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
8496
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008497 // Every call site argument might be dead.
8498 getOrCreateAAFor<AAIsDead>(CSArgPos);
8499
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008500 // Call site argument might be simplified.
8501 getOrCreateAAFor<AAValueSimplify>(CSArgPos);
8502
Hideto Ueno54869ec2019-07-15 06:49:04 +00008503 if (!CS.getArgument(i)->getType()->isPointerTy())
8504 continue;
8505
8506 // Call site argument attribute "non-null".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008507 getOrCreateAAFor<AANonNull>(CSArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008508
Hideto Uenocbab3342019-08-29 05:52:00 +00008509 // Call site argument attribute "no-alias".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008510 getOrCreateAAFor<AANoAlias>(CSArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00008511
Hideto Ueno19c07af2019-07-23 08:16:17 +00008512 // Call site argument attribute "dereferenceable".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008513 getOrCreateAAFor<AADereferenceable>(CSArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008514
8515 // Call site argument attribute "align".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008516 getOrCreateAAFor<AAAlign>(CSArgPos);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01008517
Johannes Doerfert28880192019-12-31 00:57:00 -06008518 // Call site argument attribute
8519 // "readnone/readonly/writeonly/..."
8520 getOrCreateAAFor<AAMemoryBehavior>(CSArgPos);
8521
Hideto Ueno4ecf2552019-12-12 13:42:40 +00008522 // Call site argument attribute "nofree".
8523 getOrCreateAAFor<AANoFree>(CSArgPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00008524 }
8525 }
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008526 return true;
8527 };
8528
8529 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008530 bool Success;
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008531 Success = checkForAllInstructionsImpl(
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008532 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008533 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
8534 (unsigned)Instruction::Call});
8535 (void)Success;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008536 assert(Success && "Expected the check call to be successful!");
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008537
8538 auto LoadStorePred = [&](Instruction &I) -> bool {
8539 if (isa<LoadInst>(I))
8540 getOrCreateAAFor<AAAlign>(
8541 IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
8542 else
8543 getOrCreateAAFor<AAAlign>(
8544 IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
8545 return true;
8546 };
8547 Success = checkForAllInstructionsImpl(
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008548 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008549 {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
8550 (void)Success;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008551 assert(Success && "Expected the check call to be successful!");
Johannes Doerfertaade7822019-06-05 03:02:24 +00008552}
8553
8554/// Helpers to ease debugging through output streams and print calls.
8555///
8556///{
8557raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
8558 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
8559}
8560
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008561raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00008562 switch (AP) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008563 case IRPosition::IRP_INVALID:
8564 return OS << "inv";
8565 case IRPosition::IRP_FLOAT:
8566 return OS << "flt";
8567 case IRPosition::IRP_RETURNED:
8568 return OS << "fn_ret";
8569 case IRPosition::IRP_CALL_SITE_RETURNED:
8570 return OS << "cs_ret";
8571 case IRPosition::IRP_FUNCTION:
8572 return OS << "fn";
8573 case IRPosition::IRP_CALL_SITE:
8574 return OS << "cs";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008575 case IRPosition::IRP_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00008576 return OS << "arg";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008577 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00008578 return OS << "cs_arg";
Johannes Doerfertaade7822019-06-05 03:02:24 +00008579 }
8580 llvm_unreachable("Unknown attribute position!");
8581}
8582
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008583raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008584 const Value &AV = Pos.getAssociatedValue();
8585 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008586 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
8587}
8588
Johannes Doerfert1a746452019-10-20 22:28:49 -05008589template <typename base_ty, base_ty BestState, base_ty WorstState>
Hideto Ueno188f9a32020-01-15 15:25:52 +09008590raw_ostream &
8591llvm::operator<<(raw_ostream &OS,
8592 const IntegerStateBase<base_ty, BestState, WorstState> &S) {
Johannes Doerfertacc80792019-08-12 22:07:34 +00008593 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
8594 << static_cast<const AbstractState &>(S);
8595}
8596
Hideto Ueno188f9a32020-01-15 15:25:52 +09008597raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
8598 OS << "range-state(" << S.getBitWidth() << ")<";
8599 S.getKnown().print(OS);
8600 OS << " / ";
8601 S.getAssumed().print(OS);
8602 OS << ">";
8603
8604 return OS << static_cast<const AbstractState &>(S);
8605}
8606
Johannes Doerfertaade7822019-06-05 03:02:24 +00008607raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
8608 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
8609}
8610
8611raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
8612 AA.print(OS);
8613 return OS;
8614}
8615
8616void AbstractAttribute::print(raw_ostream &OS) const {
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008617 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
8618 << "]";
Johannes Doerfertaade7822019-06-05 03:02:24 +00008619}
8620///}
8621
8622/// ----------------------------------------------------------------------------
8623/// Pass (Manager) Boilerplate
8624/// ----------------------------------------------------------------------------
8625
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008626static bool runAttributorOnFunctions(InformationCache &InfoCache,
8627 SetVector<Function *> &Functions,
8628 AnalysisGetter &AG,
8629 CallGraphUpdater &CGUpdater) {
8630 if (DisableAttributor || Functions.empty())
Johannes Doerfertaade7822019-06-05 03:02:24 +00008631 return false;
8632
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008633 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size()
Johannes Doerfertaade7822019-06-05 03:02:24 +00008634 << " functions.\n");
8635
8636 // Create an Attributor and initially empty information cache that is filled
8637 // while we identify default attribute opportunities.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008638 Attributor A(Functions, InfoCache, CGUpdater, DepRecInterval);
Johannes Doerfertaade7822019-06-05 03:02:24 +00008639
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008640 for (Function *F : Functions)
8641 A.initializeInformationCache(*F);
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008642
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008643 for (Function *F : Functions) {
8644 if (F->hasExactDefinition())
Johannes Doerfertb0412e42019-09-04 16:16:13 +00008645 NumFnWithExactDefinition++;
8646 else
Johannes Doerfertaade7822019-06-05 03:02:24 +00008647 NumFnWithoutExactDefinition++;
Johannes Doerfertaade7822019-06-05 03:02:24 +00008648
Johannes Doerfert2f622062019-09-04 16:35:20 +00008649 // We look at internal functions only on-demand but if any use is not a
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008650 // direct call or outside the current set of analyzed functions, we have to
8651 // do it eagerly.
8652 if (F->hasLocalLinkage()) {
8653 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
8654 ImmutableCallSite ICS(U.getUser());
8655 return ICS && ICS.isCallee(&U) &&
8656 Functions.count(const_cast<Function *>(ICS.getCaller()));
Johannes Doerfert2f622062019-09-04 16:35:20 +00008657 }))
8658 continue;
8659 }
8660
Johannes Doerfertaade7822019-06-05 03:02:24 +00008661 // Populate the Attributor with abstract attribute opportunities in the
8662 // function and the information cache with IR information.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008663 A.identifyDefaultAbstractAttributes(*F);
Johannes Doerfertaade7822019-06-05 03:02:24 +00008664 }
8665
Johannes Doerfert09950012020-03-16 20:18:07 -05008666 Module &M = *Functions.front()->getParent();
Johannes Doerfert77a9e612020-01-11 23:36:17 -06008667 ChangeStatus Changed = A.run();
Johannes Doerfert09950012020-03-16 20:18:07 -05008668 assert(!verifyModule(M, &errs()) && "Module verification failed!");
Johannes Doerfert77a9e612020-01-11 23:36:17 -06008669 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
8670 << " functions, result: " << Changed << ".\n");
8671 return Changed == ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +00008672}
8673
8674PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008675 FunctionAnalysisManager &FAM =
8676 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
8677 AnalysisGetter AG(FAM);
8678
8679 SetVector<Function *> Functions;
8680 for (Function &F : M)
8681 Functions.insert(&F);
8682
8683 CallGraphUpdater CGUpdater;
8684 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
8685 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
8686 // FIXME: Think about passes we will preserve and add them here.
8687 return PreservedAnalyses::none();
8688 }
8689 return PreservedAnalyses::all();
8690}
8691
8692PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
8693 CGSCCAnalysisManager &AM,
8694 LazyCallGraph &CG,
8695 CGSCCUpdateResult &UR) {
8696 FunctionAnalysisManager &FAM =
8697 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
8698 AnalysisGetter AG(FAM);
8699
8700 SetVector<Function *> Functions;
8701 for (LazyCallGraph::Node &N : C)
8702 Functions.insert(&N.getFunction());
8703
8704 if (Functions.empty())
8705 return PreservedAnalyses::all();
8706
8707 Module &M = *Functions.back()->getParent();
8708 CallGraphUpdater CGUpdater;
8709 CGUpdater.initialize(CG, C, AM, UR);
8710 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
8711 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00008712 // FIXME: Think about passes we will preserve and add them here.
8713 return PreservedAnalyses::none();
8714 }
8715 return PreservedAnalyses::all();
8716}
8717
8718namespace {
8719
8720struct AttributorLegacyPass : public ModulePass {
8721 static char ID;
8722
8723 AttributorLegacyPass() : ModulePass(ID) {
8724 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
8725 }
8726
8727 bool runOnModule(Module &M) override {
8728 if (skipModule(M))
8729 return false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008730
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00008731 AnalysisGetter AG;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008732 SetVector<Function *> Functions;
8733 for (Function &F : M)
8734 Functions.insert(&F);
8735
8736 CallGraphUpdater CGUpdater;
8737 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
8738 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
Johannes Doerfertaade7822019-06-05 03:02:24 +00008739 }
8740
8741 void getAnalysisUsage(AnalysisUsage &AU) const override {
8742 // FIXME: Think about passes we will preserve and add them here.
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008743 AU.addRequired<TargetLibraryInfoWrapperPass>();
Johannes Doerfertaade7822019-06-05 03:02:24 +00008744 }
8745};
8746
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008747struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
8748 CallGraphUpdater CGUpdater;
8749 static char ID;
8750
8751 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
8752 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
8753 }
8754
8755 bool runOnSCC(CallGraphSCC &SCC) override {
8756 if (skipSCC(SCC))
8757 return false;
8758
8759 SetVector<Function *> Functions;
8760 for (CallGraphNode *CGN : SCC)
8761 if (Function *Fn = CGN->getFunction())
8762 if (!Fn->isDeclaration())
8763 Functions.insert(Fn);
8764
8765 if (Functions.empty())
8766 return false;
8767
8768 AnalysisGetter AG;
8769 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
8770 CGUpdater.initialize(CG, SCC);
8771 Module &M = *Functions.back()->getParent();
8772 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
8773 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
8774 }
8775
8776 bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); }
8777
8778 void getAnalysisUsage(AnalysisUsage &AU) const override {
8779 // FIXME: Think about passes we will preserve and add them here.
8780 AU.addRequired<TargetLibraryInfoWrapperPass>();
8781 CallGraphSCCPass::getAnalysisUsage(AU);
8782 }
8783};
8784
Johannes Doerfertaade7822019-06-05 03:02:24 +00008785} // end anonymous namespace
8786
8787Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008788Pass *llvm::createAttributorCGSCCLegacyPass() {
8789 return new AttributorCGSCCLegacyPass();
8790}
Johannes Doerfertaade7822019-06-05 03:02:24 +00008791
8792char AttributorLegacyPass::ID = 0;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008793char AttributorCGSCCLegacyPass::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008794
8795const char AAReturnedValues::ID = 0;
8796const char AANoUnwind::ID = 0;
8797const char AANoSync::ID = 0;
Johannes Doerferteccdf082019-08-05 23:35:12 +00008798const char AANoFree::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008799const char AANonNull::ID = 0;
8800const char AANoRecurse::ID = 0;
8801const char AAWillReturn::ID = 0;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06008802const char AAUndefinedBehavior::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008803const char AANoAlias::ID = 0;
Pankaj Gode04945c92019-11-22 18:40:47 +05308804const char AAReachability::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008805const char AANoReturn::ID = 0;
8806const char AAIsDead::ID = 0;
8807const char AADereferenceable::ID = 0;
8808const char AAAlign::ID = 0;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00008809const char AANoCapture::ID = 0;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008810const char AAValueSimplify::ID = 0;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008811const char AAHeapToStack::ID = 0;
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008812const char AAPrivatizablePtr::ID = 0;
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008813const char AAMemoryBehavior::ID = 0;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06008814const char AAMemoryLocation::ID = 0;
Hideto Ueno188f9a32020-01-15 15:25:52 +09008815const char AAValueConstantRange::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008816
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008817// Macro magic to create the static generator function for attributes that
8818// follow the naming scheme.
8819
8820#define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
8821 case IRPosition::PK: \
8822 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
8823
8824#define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
8825 case IRPosition::PK: \
8826 AA = new CLASS##SUFFIX(IRP); \
8827 break;
8828
8829#define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8830 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8831 CLASS *AA = nullptr; \
8832 switch (IRP.getPositionKind()) { \
8833 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8834 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
8835 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
8836 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
8837 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
8838 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
8839 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8840 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
8841 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008842 return *AA; \
8843 }
8844
8845#define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8846 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8847 CLASS *AA = nullptr; \
8848 switch (IRP.getPositionKind()) { \
8849 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8850 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
8851 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
8852 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
8853 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
8854 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
8855 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
8856 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
8857 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008858 return *AA; \
8859 }
8860
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008861#define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8862 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8863 CLASS *AA = nullptr; \
8864 switch (IRP.getPositionKind()) { \
8865 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8866 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8867 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
8868 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
8869 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
8870 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
8871 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
8872 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
8873 } \
8874 return *AA; \
8875 }
8876
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008877#define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8878 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8879 CLASS *AA = nullptr; \
8880 switch (IRP.getPositionKind()) { \
8881 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8882 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
8883 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
8884 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
8885 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
8886 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
8887 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
8888 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8889 } \
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008890 return *AA; \
8891 }
8892
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008893#define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8894 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8895 CLASS *AA = nullptr; \
8896 switch (IRP.getPositionKind()) { \
8897 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8898 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
8899 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8900 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
8901 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
8902 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
8903 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
8904 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
8905 } \
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008906 return *AA; \
8907 }
8908
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008909CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
8910CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008911CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
8912CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
8913CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008914CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
Johannes Doerfert282f5d72020-01-26 02:51:57 -06008915CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008916
8917CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
8918CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008919CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008920CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
8921CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00008922CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
Hideto Ueno188f9a32020-01-15 15:25:52 +09008923CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008924
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008925CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008926CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01008927CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008928
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008929CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
Pankaj Gode04945c92019-11-22 18:40:47 +05308930CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
Johannes Doerfert58f324a2019-12-24 18:48:50 -06008931CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008932
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008933CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
8934
Johannes Doerfertd4bea882019-10-07 23:28:54 +00008935#undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008936#undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfertd4bea882019-10-07 23:28:54 +00008937#undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008938#undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008939#undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008940#undef SWITCH_PK_CREATE
8941#undef SWITCH_PK_INV
8942
Johannes Doerfertaade7822019-06-05 03:02:24 +00008943INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
8944 "Deduce and propagate attributes", false, false)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008945INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Johannes Doerfertaade7822019-06-05 03:02:24 +00008946INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
8947 "Deduce and propagate attributes", false, false)
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008948INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
8949 "Deduce and propagate attributes (CGSCC pass)", false,
8950 false)
8951INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
8952INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
8953INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
8954 "Deduce and propagate attributes (CGSCC pass)", false,
8955 false)