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