blob: 9d0e978432e06730fda3b521c10755d5ce2d0f27 [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
16#include "llvm/Transforms/IPO/Attributor.h"
17
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"
Stefan Stipanovic69ebb022019-07-22 19:36:27 +000023#include "llvm/Analysis/CaptureTracking.h"
Johannes Doerfert924d2132019-08-05 21:34:45 +000024#include "llvm/Analysis/EHPersonalities.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000025#include "llvm/Analysis/GlobalsModRef.h"
Hideto Ueno19c07af2019-07-23 08:16:17 +000026#include "llvm/Analysis/Loads.h"
Stefan Stipanovic431141c2019-09-15 21:47:41 +000027#include "llvm/Analysis/MemoryBuiltins.h"
Hideto Ueno54869ec2019-07-15 06:49:04 +000028#include "llvm/Analysis/ValueTracking.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000029#include "llvm/IR/Argument.h"
30#include "llvm/IR/Attributes.h"
Hideto Ueno11d37102019-07-17 15:15:43 +000031#include "llvm/IR/CFG.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000032#include "llvm/IR/InstIterator.h"
Stefan Stipanovic06263672019-07-11 21:37:40 +000033#include "llvm/IR/IntrinsicInst.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
36#include "llvm/Support/raw_ostream.h"
Stefan Stipanovic6058b862019-07-22 23:58:23 +000037#include "llvm/Transforms/Utils/BasicBlockUtils.h"
38#include "llvm/Transforms/Utils/Local.h"
39
Johannes Doerfertaade7822019-06-05 03:02:24 +000040#include <cassert>
41
42using namespace llvm;
43
44#define DEBUG_TYPE "attributor"
45
46STATISTIC(NumFnWithExactDefinition,
47 "Number of function with exact definitions");
48STATISTIC(NumFnWithoutExactDefinition,
49 "Number of function without exact definitions");
50STATISTIC(NumAttributesTimedOut,
51 "Number of abstract attributes timed out before fixpoint");
52STATISTIC(NumAttributesValidFixpoint,
53 "Number of abstract attributes in a valid fixpoint state");
54STATISTIC(NumAttributesManifested,
55 "Number of abstract attributes manifested in IR");
56
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000057// Some helper macros to deal with statistics tracking.
58//
59// Usage:
60// For simple IR attribute tracking overload trackStatistics in the abstract
Johannes Doerfert17b578b2019-08-14 21:46:25 +000061// attribute and choose the right STATS_DECLTRACK_********* macro,
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000062// e.g.,:
63// void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +000064// STATS_DECLTRACK_ARG_ATTR(returned)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000065// }
66// If there is a single "increment" side one can use the macro
Johannes Doerfert17b578b2019-08-14 21:46:25 +000067// STATS_DECLTRACK with a custom message. If there are multiple increment
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000068// sides, STATS_DECL and STATS_TRACK can also be used separatly.
69//
70#define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \
71 ("Number of " #TYPE " marked '" #NAME "'")
72#define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
Johannes Doerferta7a3b3a2019-09-04 19:01:08 +000073#define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
74#define STATS_DECL(NAME, TYPE, MSG) \
75 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000076#define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
Johannes Doerfert17b578b2019-08-14 21:46:25 +000077#define STATS_DECLTRACK(NAME, TYPE, MSG) \
Johannes Doerfert169af992019-08-20 06:09:56 +000078 { \
79 STATS_DECL(NAME, TYPE, MSG) \
80 STATS_TRACK(NAME, TYPE) \
81 }
Johannes Doerfert17b578b2019-08-14 21:46:25 +000082#define STATS_DECLTRACK_ARG_ATTR(NAME) \
83 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
84#define STATS_DECLTRACK_CSARG_ATTR(NAME) \
85 STATS_DECLTRACK(NAME, CSArguments, \
86 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
87#define STATS_DECLTRACK_FN_ATTR(NAME) \
88 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
89#define STATS_DECLTRACK_CS_ATTR(NAME) \
90 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
91#define STATS_DECLTRACK_FNRET_ATTR(NAME) \
92 STATS_DECLTRACK(NAME, FunctionReturn, \
Johannes Doerfert2db85282019-08-21 20:56:56 +000093 BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
Johannes Doerfert17b578b2019-08-14 21:46:25 +000094#define STATS_DECLTRACK_CSRET_ATTR(NAME) \
95 STATS_DECLTRACK(NAME, CSReturn, \
96 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
97#define STATS_DECLTRACK_FLOATING_ATTR(NAME) \
98 STATS_DECLTRACK(NAME, Floating, \
99 ("Number of floating values known to be '" #NAME "'"))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000100
Johannes Doerfertaade7822019-06-05 03:02:24 +0000101// TODO: Determine a good default value.
102//
103// In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
104// (when run with the first 5 abstract attributes). The results also indicate
105// that we never reach 32 iterations but always find a fixpoint sooner.
106//
107// This will become more evolved once we perform two interleaved fixpoint
108// iterations: bottom-up and top-down.
109static cl::opt<unsigned>
110 MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
111 cl::desc("Maximal number of fixpoint iterations."),
112 cl::init(32));
Johannes Doerfertb504eb82019-08-26 18:55:47 +0000113static cl::opt<bool> VerifyMaxFixpointIterations(
114 "attributor-max-iterations-verify", cl::Hidden,
115 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
116 cl::init(false));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000117
118static cl::opt<bool> DisableAttributor(
119 "attributor-disable", cl::Hidden,
120 cl::desc("Disable the attributor inter-procedural deduction pass."),
Johannes Doerfert282d34e2019-06-14 14:53:41 +0000121 cl::init(true));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000122
Johannes Doerfert7516a5e2019-09-03 20:37:24 +0000123static cl::opt<bool> ManifestInternal(
124 "attributor-manifest-internal", cl::Hidden,
125 cl::desc("Manifest Attributor internal string attributes."),
126 cl::init(false));
127
Johannes Doerfertaade7822019-06-05 03:02:24 +0000128static cl::opt<bool> VerifyAttributor(
129 "attributor-verify", cl::Hidden,
130 cl::desc("Verify the Attributor deduction and "
131 "manifestation of attributes -- may issue false-positive errors"),
132 cl::init(false));
133
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +0000134static cl::opt<unsigned> DepRecInterval(
135 "attributor-dependence-recompute-interval", cl::Hidden,
136 cl::desc("Number of iterations until dependences are recomputed."),
137 cl::init(4));
138
Stefan Stipanovic431141c2019-09-15 21:47:41 +0000139static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
140 cl::init(true), cl::Hidden);
141
142static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size",
143 cl::init(128), cl::Hidden);
144
Johannes Doerfertaade7822019-06-05 03:02:24 +0000145/// Logic operators for the change status enum class.
146///
147///{
148ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
149 return l == ChangeStatus::CHANGED ? l : r;
150}
151ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
152 return l == ChangeStatus::UNCHANGED ? l : r;
153}
154///}
155
Johannes Doerfertdef99282019-08-14 21:29:37 +0000156/// Recursively visit all values that might become \p IRP at some point. This
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000157/// will be done by looking through cast instructions, selects, phis, and calls
Johannes Doerfertdef99282019-08-14 21:29:37 +0000158/// with the "returned" attribute. Once we cannot look through the value any
159/// further, the callback \p VisitValueCB is invoked and passed the current
160/// value, the \p State, and a flag to indicate if we stripped anything. To
161/// limit how much effort is invested, we will never visit more values than
162/// specified by \p MaxValues.
163template <typename AAType, typename StateTy>
164bool genericValueTraversal(
165 Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State,
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000166 const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB,
Johannes Doerfertdef99282019-08-14 21:29:37 +0000167 int MaxValues = 8) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000168
Johannes Doerfertdef99282019-08-14 21:29:37 +0000169 const AAIsDead *LivenessAA = nullptr;
170 if (IRP.getAnchorScope())
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000171 LivenessAA = &A.getAAFor<AAIsDead>(
Johannes Doerfert19b00432019-08-26 17:48:05 +0000172 QueryingAA, IRPosition::function(*IRP.getAnchorScope()),
173 /* TrackDependence */ false);
174 bool AnyDead = false;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000175
176 // TODO: Use Positions here to allow context sensitivity in VisitValueCB
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000177 SmallPtrSet<Value *, 16> Visited;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000178 SmallVector<Value *, 16> Worklist;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000179 Worklist.push_back(&IRP.getAssociatedValue());
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000180
181 int Iteration = 0;
182 do {
183 Value *V = Worklist.pop_back_val();
184
185 // Check if we should process the current value. To prevent endless
186 // recursion keep a record of the values we followed!
Johannes Doerfertdef99282019-08-14 21:29:37 +0000187 if (!Visited.insert(V).second)
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000188 continue;
189
190 // Make sure we limit the compile time for complex expressions.
191 if (Iteration++ >= MaxValues)
192 return false;
193
194 // Explicitly look through calls with a "returned" attribute if we do
195 // not have a pointer as stripPointerCasts only works on them.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000196 Value *NewV = nullptr;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000197 if (V->getType()->isPointerTy()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000198 NewV = V->stripPointerCasts();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000199 } else {
200 CallSite CS(V);
201 if (CS && CS.getCalledFunction()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000202 for (Argument &Arg : CS.getCalledFunction()->args())
203 if (Arg.hasReturnedAttr()) {
204 NewV = CS.getArgOperand(Arg.getArgNo());
205 break;
206 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000207 }
208 }
Johannes Doerfertdef99282019-08-14 21:29:37 +0000209 if (NewV && NewV != V) {
210 Worklist.push_back(NewV);
211 continue;
212 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000213
214 // Look through select instructions, visit both potential values.
215 if (auto *SI = dyn_cast<SelectInst>(V)) {
216 Worklist.push_back(SI->getTrueValue());
217 Worklist.push_back(SI->getFalseValue());
218 continue;
219 }
220
Johannes Doerfertdef99282019-08-14 21:29:37 +0000221 // Look through phi nodes, visit all live operands.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000222 if (auto *PHI = dyn_cast<PHINode>(V)) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000223 assert(LivenessAA &&
224 "Expected liveness in the presence of instructions!");
Johannes Doerfertdef99282019-08-14 21:29:37 +0000225 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
226 const BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
Johannes Doerfert19b00432019-08-26 17:48:05 +0000227 if (LivenessAA->isAssumedDead(IncomingBB->getTerminator())) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +0000228 AnyDead = true;
Johannes Doerfert19b00432019-08-26 17:48:05 +0000229 continue;
230 }
231 Worklist.push_back(PHI->getIncomingValue(u));
Johannes Doerfertdef99282019-08-14 21:29:37 +0000232 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000233 continue;
234 }
235
236 // Once a leaf is reached we inform the user through the callback.
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000237 if (!VisitValueCB(*V, State, Iteration > 1))
238 return false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000239 } while (!Worklist.empty());
240
Johannes Doerfert19b00432019-08-26 17:48:05 +0000241 // If we actually used liveness information so we have to record a dependence.
242 if (AnyDead)
243 A.recordDependence(*LivenessAA, QueryingAA);
244
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000245 // All values have been visited.
246 return true;
247}
248
Johannes Doerfertaade7822019-06-05 03:02:24 +0000249/// Return true if \p New is equal or worse than \p Old.
250static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
251 if (!Old.isIntAttribute())
252 return true;
253
254 return Old.getValueAsInt() >= New.getValueAsInt();
255}
256
257/// Return true if the information provided by \p Attr was added to the
258/// attribute list \p Attrs. This is only the case if it was not already present
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000259/// in \p Attrs at the position describe by \p PK and \p AttrIdx.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000260static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000261 AttributeList &Attrs, int AttrIdx) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000262
263 if (Attr.isEnumAttribute()) {
264 Attribute::AttrKind Kind = Attr.getKindAsEnum();
265 if (Attrs.hasAttribute(AttrIdx, Kind))
266 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
267 return false;
268 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
269 return true;
270 }
271 if (Attr.isStringAttribute()) {
272 StringRef Kind = Attr.getKindAsString();
273 if (Attrs.hasAttribute(AttrIdx, Kind))
274 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
275 return false;
276 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
277 return true;
278 }
Hideto Ueno19c07af2019-07-23 08:16:17 +0000279 if (Attr.isIntAttribute()) {
280 Attribute::AttrKind Kind = Attr.getKindAsEnum();
281 if (Attrs.hasAttribute(AttrIdx, Kind))
282 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
283 return false;
284 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
285 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
286 return true;
287 }
Johannes Doerfertaade7822019-06-05 03:02:24 +0000288
289 llvm_unreachable("Expected enum or string attribute!");
290}
291
Johannes Doerfertece81902019-08-12 22:05:53 +0000292ChangeStatus AbstractAttribute::update(Attributor &A) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000293 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
294 if (getState().isAtFixpoint())
295 return HasChanged;
296
297 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
298
Johannes Doerfertece81902019-08-12 22:05:53 +0000299 HasChanged = updateImpl(A);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000300
301 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
302 << "\n");
303
304 return HasChanged;
305}
306
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000307ChangeStatus
308IRAttributeManifest::manifestAttrs(Attributor &A, IRPosition &IRP,
309 const ArrayRef<Attribute> &DeducedAttrs) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000310 Function *ScopeFn = IRP.getAssociatedFunction();
Kristina Brooks26e60f02019-08-06 19:53:19 +0000311 IRPosition::Kind PK = IRP.getPositionKind();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000312
Johannes Doerfertaade7822019-06-05 03:02:24 +0000313 // In the following some generic code that will manifest attributes in
314 // DeducedAttrs if they improve the current IR. Due to the different
315 // annotation positions we use the underlying AttributeList interface.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000316
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000317 AttributeList Attrs;
318 switch (PK) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000319 case IRPosition::IRP_INVALID:
320 case IRPosition::IRP_FLOAT:
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000321 return ChangeStatus::UNCHANGED;
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000322 case IRPosition::IRP_ARGUMENT:
323 case IRPosition::IRP_FUNCTION:
324 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000325 Attrs = ScopeFn->getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000326 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000327 case IRPosition::IRP_CALL_SITE:
328 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000329 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000330 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000331 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000332 }
333
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000334 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000335 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000336 for (const Attribute &Attr : DeducedAttrs) {
Kristina Brooks26e60f02019-08-06 19:53:19 +0000337 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000338 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000339
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000340 HasChanged = ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000341 }
342
343 if (HasChanged == ChangeStatus::UNCHANGED)
344 return HasChanged;
345
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000346 switch (PK) {
347 case IRPosition::IRP_ARGUMENT:
348 case IRPosition::IRP_FUNCTION:
349 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000350 ScopeFn->setAttributes(Attrs);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000351 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000352 case IRPosition::IRP_CALL_SITE:
353 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000354 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000355 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
Johannes Doerfert4395b312019-08-14 21:46:28 +0000356 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000357 case IRPosition::IRP_INVALID:
Johannes Doerfert4395b312019-08-14 21:46:28 +0000358 case IRPosition::IRP_FLOAT:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000359 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000360 }
361
362 return HasChanged;
363}
364
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000365const IRPosition IRPosition::EmptyKey(255);
366const IRPosition IRPosition::TombstoneKey(256);
367
368SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
369 IRPositions.emplace_back(IRP);
370
371 ImmutableCallSite ICS(&IRP.getAnchorValue());
372 switch (IRP.getPositionKind()) {
373 case IRPosition::IRP_INVALID:
374 case IRPosition::IRP_FLOAT:
375 case IRPosition::IRP_FUNCTION:
376 return;
377 case IRPosition::IRP_ARGUMENT:
378 case IRPosition::IRP_RETURNED:
379 IRPositions.emplace_back(
380 IRPosition::function(*IRP.getAssociatedFunction()));
381 return;
382 case IRPosition::IRP_CALL_SITE:
383 assert(ICS && "Expected call site!");
384 // TODO: We need to look at the operand bundles similar to the redirection
385 // in CallBase.
386 if (!ICS.hasOperandBundles())
387 if (const Function *Callee = ICS.getCalledFunction())
388 IRPositions.emplace_back(IRPosition::function(*Callee));
389 return;
390 case IRPosition::IRP_CALL_SITE_RETURNED:
391 assert(ICS && "Expected call site!");
392 // TODO: We need to look at the operand bundles similar to the redirection
393 // in CallBase.
394 if (!ICS.hasOperandBundles()) {
395 if (const Function *Callee = ICS.getCalledFunction()) {
396 IRPositions.emplace_back(IRPosition::returned(*Callee));
397 IRPositions.emplace_back(IRPosition::function(*Callee));
398 }
399 }
400 IRPositions.emplace_back(
401 IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction())));
402 return;
403 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
404 int ArgNo = IRP.getArgNo();
405 assert(ICS && ArgNo >= 0 && "Expected call site!");
406 // TODO: We need to look at the operand bundles similar to the redirection
407 // in CallBase.
408 if (!ICS.hasOperandBundles()) {
409 const Function *Callee = ICS.getCalledFunction();
410 if (Callee && Callee->arg_size() > unsigned(ArgNo))
411 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
412 if (Callee)
413 IRPositions.emplace_back(IRPosition::function(*Callee));
414 }
415 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
416 return;
417 }
418 }
419}
420
421bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs) const {
422 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this))
423 for (Attribute::AttrKind AK : AKs)
424 if (EquivIRP.getAttr(AK).getKindAsEnum() == AK)
425 return true;
426 return false;
427}
428
429void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
430 SmallVectorImpl<Attribute> &Attrs) const {
431 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this))
432 for (Attribute::AttrKind AK : AKs) {
433 const Attribute &Attr = EquivIRP.getAttr(AK);
434 if (Attr.getKindAsEnum() == AK)
435 Attrs.push_back(Attr);
436 }
437}
438
439void IRPosition::verify() {
440 switch (KindOrArgNo) {
441 default:
442 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!");
443 assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) &&
444 "Expected call base or argument for positive attribute index!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000445 if (isa<Argument>(AnchorVal)) {
446 assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000447 "Argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000448 assert(cast<Argument>(AnchorVal) == &getAssociatedValue() &&
449 "Associated value mismatch!");
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000450 } else {
Simon Pilgrim920b0402019-08-29 10:08:45 +0000451 assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000452 "Call site argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000453 assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) ==
454 &getAssociatedValue() &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000455 "Associated value mismatch!");
456 }
457 break;
458 case IRP_INVALID:
459 assert(!AnchorVal && "Expected no value for an invalid position!");
460 break;
461 case IRP_FLOAT:
462 assert((!isa<CallBase>(&getAssociatedValue()) &&
463 !isa<Argument>(&getAssociatedValue())) &&
464 "Expected specialized kind for call base and argument values!");
465 break;
466 case IRP_RETURNED:
467 assert(isa<Function>(AnchorVal) &&
468 "Expected function for a 'returned' position!");
469 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
470 break;
471 case IRP_CALL_SITE_RETURNED:
472 assert((isa<CallBase>(AnchorVal)) &&
473 "Expected call base for 'call site returned' position!");
474 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
475 break;
476 case IRP_CALL_SITE:
477 assert((isa<CallBase>(AnchorVal)) &&
478 "Expected call base for 'call site function' position!");
479 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
480 break;
481 case IRP_FUNCTION:
482 assert(isa<Function>(AnchorVal) &&
483 "Expected function for a 'function' position!");
484 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
485 break;
486 }
487}
488
Johannes Doerfert234eda52019-08-16 19:51:23 +0000489/// Helper functions to clamp a state \p S of type \p StateType with the
490/// information in \p R and indicate/return if \p S did change (as-in update is
491/// required to be run again).
492///
493///{
494template <typename StateType>
495ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R);
496
497template <>
498ChangeStatus clampStateAndIndicateChange<IntegerState>(IntegerState &S,
499 const IntegerState &R) {
500 auto Assumed = S.getAssumed();
501 S ^= R;
502 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
503 : ChangeStatus::CHANGED;
504}
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000505
506template <>
507ChangeStatus clampStateAndIndicateChange<BooleanState>(BooleanState &S,
508 const BooleanState &R) {
509 return clampStateAndIndicateChange<IntegerState>(S, R);
510}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000511///}
512
513/// Clamp the information known for all returned values of a function
514/// (identified by \p QueryingAA) into \p S.
515template <typename AAType, typename StateType = typename AAType::StateType>
516static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA,
517 StateType &S) {
518 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
519 << static_cast<const AbstractAttribute &>(QueryingAA)
520 << " into " << S << "\n");
521
522 assert((QueryingAA.getIRPosition().getPositionKind() ==
523 IRPosition::IRP_RETURNED ||
524 QueryingAA.getIRPosition().getPositionKind() ==
525 IRPosition::IRP_CALL_SITE_RETURNED) &&
526 "Can only clamp returned value states for a function returned or call "
527 "site returned position!");
528
529 // Use an optional state as there might not be any return values and we want
530 // to join (IntegerState::operator&) the state of all there are.
531 Optional<StateType> T;
532
533 // Callback for each possibly returned value.
534 auto CheckReturnValue = [&](Value &RV) -> bool {
535 const IRPosition &RVPos = IRPosition::value(RV);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000536 const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos);
537 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
538 << " @ " << RVPos << "\n");
539 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000540 if (T.hasValue())
541 *T &= AAS;
542 else
543 T = AAS;
544 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
545 << "\n");
546 return T->isValidState();
547 };
548
549 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
550 S.indicatePessimisticFixpoint();
551 else if (T.hasValue())
552 S ^= *T;
553}
554
555/// Helper class for generic deduction: return value -> returned position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000556template <typename AAType, typename Base,
557 typename StateType = typename AAType::StateType>
558struct AAReturnedFromReturnedValues : public Base {
559 AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000560
561 /// See AbstractAttribute::updateImpl(...).
562 ChangeStatus updateImpl(Attributor &A) override {
563 StateType S;
564 clampReturnedValueStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000565 // TODO: If we know we visited all returned values, thus no are assumed
566 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000567 return clampStateAndIndicateChange<StateType>(this->getState(), S);
568 }
569};
570
571/// Clamp the information known at all call sites for a given argument
572/// (identified by \p QueryingAA) into \p S.
573template <typename AAType, typename StateType = typename AAType::StateType>
574static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
575 StateType &S) {
576 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
577 << static_cast<const AbstractAttribute &>(QueryingAA)
578 << " into " << S << "\n");
579
580 assert(QueryingAA.getIRPosition().getPositionKind() ==
581 IRPosition::IRP_ARGUMENT &&
582 "Can only clamp call site argument states for an argument position!");
583
584 // Use an optional state as there might not be any return values and we want
585 // to join (IntegerState::operator&) the state of all there are.
586 Optional<StateType> T;
587
588 // The argument number which is also the call site argument number.
589 unsigned ArgNo = QueryingAA.getIRPosition().getArgNo();
590
591 auto CallSiteCheck = [&](CallSite CS) {
592 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000593 const AAType &AA = A.getAAFor<AAType>(QueryingAA, CSArgPos);
Johannes Doerfert234eda52019-08-16 19:51:23 +0000594 LLVM_DEBUG(dbgs() << "[Attributor] CS: " << *CS.getInstruction()
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000595 << " AA: " << AA.getAsStr() << " @" << CSArgPos << "\n");
596 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000597 if (T.hasValue())
598 *T &= AAS;
599 else
600 T = AAS;
601 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
602 << "\n");
603 return T->isValidState();
604 };
605
606 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true))
607 S.indicatePessimisticFixpoint();
608 else if (T.hasValue())
609 S ^= *T;
610}
611
612/// Helper class for generic deduction: call site argument -> argument position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000613template <typename AAType, typename Base,
614 typename StateType = typename AAType::StateType>
615struct AAArgumentFromCallSiteArguments : public Base {
616 AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000617
618 /// See AbstractAttribute::updateImpl(...).
619 ChangeStatus updateImpl(Attributor &A) override {
620 StateType S;
621 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000622 // TODO: If we know we visited all incoming values, thus no are assumed
623 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000624 return clampStateAndIndicateChange<StateType>(this->getState(), S);
625 }
626};
627
628/// Helper class for generic replication: function returned -> cs returned.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000629template <typename AAType, typename Base>
630struct AACallSiteReturnedFromReturned : public Base {
631 AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000632
633 /// See AbstractAttribute::updateImpl(...).
634 ChangeStatus updateImpl(Attributor &A) override {
635 assert(this->getIRPosition().getPositionKind() ==
636 IRPosition::IRP_CALL_SITE_RETURNED &&
637 "Can only wrap function returned positions for call site returned "
638 "positions!");
639 auto &S = this->getState();
640
641 const Function *AssociatedFunction =
642 this->getIRPosition().getAssociatedFunction();
643 if (!AssociatedFunction)
644 return S.indicatePessimisticFixpoint();
645
646 IRPosition FnPos = IRPosition::returned(*AssociatedFunction);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000647 const AAType &AA = A.getAAFor<AAType>(*this, FnPos);
Johannes Doerfert234eda52019-08-16 19:51:23 +0000648 return clampStateAndIndicateChange(
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000649 S, static_cast<const typename AAType::StateType &>(AA.getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000650 }
651};
652
Stefan Stipanovic53605892019-06-27 11:27:54 +0000653/// -----------------------NoUnwind Function Attribute--------------------------
654
Johannes Doerfert344d0382019-08-07 22:34:26 +0000655struct AANoUnwindImpl : AANoUnwind {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000656 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
Stefan Stipanovic53605892019-06-27 11:27:54 +0000657
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000658 const std::string getAsStr() const override {
Stefan Stipanovic53605892019-06-27 11:27:54 +0000659 return getAssumed() ? "nounwind" : "may-unwind";
660 }
661
662 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +0000663 ChangeStatus updateImpl(Attributor &A) override {
664 auto Opcodes = {
665 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
666 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
667 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
668
669 auto CheckForNoUnwind = [&](Instruction &I) {
670 if (!I.mayThrow())
671 return true;
672
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000673 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
674 const auto &NoUnwindAA =
675 A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS));
676 return NoUnwindAA.isAssumedNoUnwind();
677 }
678 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +0000679 };
680
681 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
682 return indicatePessimisticFixpoint();
683
684 return ChangeStatus::UNCHANGED;
685 }
Stefan Stipanovic53605892019-06-27 11:27:54 +0000686};
687
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000688struct AANoUnwindFunction final : public AANoUnwindImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000689 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000690
691 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000692 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000693};
694
Johannes Doerfert66cf87e2019-08-16 19:49:00 +0000695/// NoUnwind attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +0000696struct AANoUnwindCallSite final : AANoUnwindImpl {
697 AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
698
699 /// See AbstractAttribute::initialize(...).
700 void initialize(Attributor &A) override {
701 AANoUnwindImpl::initialize(A);
702 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +0000703 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +0000704 indicatePessimisticFixpoint();
705 }
706
707 /// See AbstractAttribute::updateImpl(...).
708 ChangeStatus updateImpl(Attributor &A) override {
709 // TODO: Once we have call site specific value information we can provide
710 // call site specific liveness information and then it makes
711 // sense to specialize attributes for call sites arguments instead of
712 // redirecting requests to the callee argument.
713 Function *F = getAssociatedFunction();
714 const IRPosition &FnPos = IRPosition::function(*F);
715 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos);
716 return clampStateAndIndicateChange(
717 getState(),
718 static_cast<const AANoUnwind::StateType &>(FnAA.getState()));
719 }
720
721 /// See AbstractAttribute::trackStatistics()
722 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
723};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +0000724
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000725/// --------------------- Function Return Values -------------------------------
726
727/// "Attribute" that collects all potential returned values and the return
728/// instructions that they arise from.
729///
730/// If there is a unique returned value R, the manifest method will:
731/// - mark R with the "returned" attribute, if R is an argument.
Johannes Doerferteccdf082019-08-05 23:35:12 +0000732class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000733
734 /// Mapping of values potentially returned by the associated function to the
735 /// return instructions that might return them.
Johannes Doerferta4a308c2019-08-26 17:51:23 +0000736 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000737
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +0000738 /// Mapping to remember the number of returned values for a call site such
739 /// that we can avoid updates if nothing changed.
740 DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
741
742 /// Set of unresolved calls returned by the associated function.
Johannes Doerfert695089e2019-08-23 15:23:49 +0000743 SmallSetVector<CallBase *, 4> UnresolvedCalls;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000744
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000745 /// State flags
746 ///
747 ///{
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +0000748 bool IsFixed = false;
749 bool IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000750 ///}
751
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000752public:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000753 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000754
755 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +0000756 void initialize(Attributor &A) override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000757 // Reset the state.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000758 IsFixed = false;
759 IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000760 ReturnedValues.clear();
761
Johannes Doerfertdef99282019-08-14 21:29:37 +0000762 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +0000763 if (!F) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000764 indicatePessimisticFixpoint();
765 return;
766 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000767
768 // The map from instruction opcodes to those instructions in the function.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000769 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000770
771 // Look through all arguments, if one is marked as returned we are done.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000772 for (Argument &Arg : F->args()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000773 if (Arg.hasReturnedAttr()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000774 auto &ReturnInstSet = ReturnedValues[&Arg];
775 for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
776 ReturnInstSet.insert(cast<ReturnInst>(RI));
777
778 indicateOptimisticFixpoint();
779 return;
780 }
781 }
Johannes Doerfertb0412e42019-09-04 16:16:13 +0000782
783 if (!F->hasExactDefinition())
784 indicatePessimisticFixpoint();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000785 }
786
787 /// See AbstractAttribute::manifest(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000788 ChangeStatus manifest(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000789
790 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000791 AbstractState &getState() override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000792
793 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000794 const AbstractState &getState() const override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000795
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000796 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +0000797 ChangeStatus updateImpl(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000798
Johannes Doerfertdef99282019-08-14 21:29:37 +0000799 llvm::iterator_range<iterator> returned_values() override {
800 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
801 }
802
803 llvm::iterator_range<const_iterator> returned_values() const override {
804 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
805 }
806
Johannes Doerfert695089e2019-08-23 15:23:49 +0000807 const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000808 return UnresolvedCalls;
809 }
810
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000811 /// Return the number of potential return values, -1 if unknown.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000812 size_t getNumReturnValues() const override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000813 return isValidState() ? ReturnedValues.size() : -1;
814 }
815
816 /// Return an assumed unique return value if a single candidate is found. If
817 /// there cannot be one, return a nullptr. If it is not clear yet, return the
818 /// Optional::NoneType.
Johannes Doerfert14a04932019-08-07 22:27:24 +0000819 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000820
Johannes Doerfert14a04932019-08-07 22:27:24 +0000821 /// See AbstractState::checkForAllReturnedValues(...).
822 bool checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +0000823 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +0000824 &Pred) const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000825
826 /// Pretty print the attribute similar to the IR representation.
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000827 const std::string getAsStr() const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000828
829 /// See AbstractState::isAtFixpoint().
830 bool isAtFixpoint() const override { return IsFixed; }
831
832 /// See AbstractState::isValidState().
833 bool isValidState() const override { return IsValidState; }
834
835 /// See AbstractState::indicateOptimisticFixpoint(...).
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000836 ChangeStatus indicateOptimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000837 IsFixed = true;
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000838 return ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000839 }
Stefan Stipanovicd0216172019-08-02 21:31:22 +0000840
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000841 ChangeStatus indicatePessimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000842 IsFixed = true;
843 IsValidState = false;
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000844 return ChangeStatus::CHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000845 }
846};
847
848ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
849 ChangeStatus Changed = ChangeStatus::UNCHANGED;
850
851 // Bookkeeping.
852 assert(isValidState());
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000853 STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
854 "Number of function with known return values");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000855
856 // Check if we have an assumed unique return value that we could manifest.
Johannes Doerfert14a04932019-08-07 22:27:24 +0000857 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000858
859 if (!UniqueRV.hasValue() || !UniqueRV.getValue())
860 return Changed;
861
862 // Bookkeeping.
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000863 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
864 "Number of function with unique return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000865
Johannes Doerfert23400e612019-08-23 17:41:37 +0000866 // Callback to replace the uses of CB with the constant C.
867 auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) {
868 if (CB.getNumUses() == 0)
869 return ChangeStatus::UNCHANGED;
870 CB.replaceAllUsesWith(&C);
871 return ChangeStatus::CHANGED;
872 };
873
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000874 // If the assumed unique return value is an argument, annotate it.
875 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000876 getIRPosition() = IRPosition::argument(*UniqueRVArg);
Johannes Doerfert23400e612019-08-23 17:41:37 +0000877 Changed = IRAttribute::manifest(A);
878 } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
879 // We can replace the returned value with the unique returned constant.
880 Value &AnchorValue = getAnchorValue();
881 if (Function *F = dyn_cast<Function>(&AnchorValue)) {
882 for (const Use &U : F->uses())
883 if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
Johannes Doerferte7c6f972019-09-14 02:57:50 +0000884 if (CB->isCallee(&U)) {
885 Constant *RVCCast =
886 ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
887 Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
888 }
Johannes Doerfert23400e612019-08-23 17:41:37 +0000889 } else {
890 assert(isa<CallBase>(AnchorValue) &&
891 "Expcected a function or call base anchor!");
Johannes Doerferte7c6f972019-09-14 02:57:50 +0000892 Constant *RVCCast =
893 ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
894 Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
Johannes Doerfert23400e612019-08-23 17:41:37 +0000895 }
896 if (Changed == ChangeStatus::CHANGED)
897 STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
898 "Number of function returns replaced by constant return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000899 }
900
901 return Changed;
902}
903
904const std::string AAReturnedValuesImpl::getAsStr() const {
905 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
Johannes Doerfert6471bb62019-08-04 18:39:28 +0000906 (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
Johannes Doerfertdef99282019-08-14 21:29:37 +0000907 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000908}
909
Johannes Doerfert14a04932019-08-07 22:27:24 +0000910Optional<Value *>
911AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
912 // If checkForAllReturnedValues provides a unique value, ignoring potential
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000913 // undef values that can also be present, it is assumed to be the actual
914 // return value and forwarded to the caller of this method. If there are
915 // multiple, a nullptr is returned indicating there cannot be a unique
916 // returned value.
917 Optional<Value *> UniqueRV;
918
Johannes Doerfert14a04932019-08-07 22:27:24 +0000919 auto Pred = [&](Value &RV) -> bool {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000920 // If we found a second returned value and neither the current nor the saved
921 // one is an undef, there is no unique returned value. Undefs are special
922 // since we can pretend they have any value.
923 if (UniqueRV.hasValue() && UniqueRV != &RV &&
924 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
925 UniqueRV = nullptr;
926 return false;
927 }
928
929 // Do not overwrite a value with an undef.
930 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
931 UniqueRV = &RV;
932
933 return true;
934 };
935
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000936 if (!A.checkForAllReturnedValues(Pred, *this))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000937 UniqueRV = nullptr;
938
939 return UniqueRV;
940}
941
Johannes Doerfert14a04932019-08-07 22:27:24 +0000942bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +0000943 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +0000944 &Pred) const {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000945 if (!isValidState())
946 return false;
947
948 // Check all returned values but ignore call sites as long as we have not
949 // encountered an overdefined one during an update.
950 for (auto &It : ReturnedValues) {
951 Value *RV = It.first;
952
Johannes Doerfertdef99282019-08-14 21:29:37 +0000953 CallBase *CB = dyn_cast<CallBase>(RV);
954 if (CB && !UnresolvedCalls.count(CB))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000955 continue;
956
Johannes Doerfert695089e2019-08-23 15:23:49 +0000957 if (!Pred(*RV, It.second))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000958 return false;
959 }
960
961 return true;
962}
963
Johannes Doerfertece81902019-08-12 22:05:53 +0000964ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000965 size_t NumUnresolvedCalls = UnresolvedCalls.size();
966 bool Changed = false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000967
Johannes Doerfertdef99282019-08-14 21:29:37 +0000968 // State used in the value traversals starting in returned values.
969 struct RVState {
970 // The map in which we collect return values -> return instrs.
971 decltype(ReturnedValues) &RetValsMap;
972 // The flag to indicate a change.
Johannes Doerfert056f1b52019-08-19 19:14:10 +0000973 bool &Changed;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000974 // The return instrs we come from.
Johannes Doerfert695089e2019-08-23 15:23:49 +0000975 SmallSetVector<ReturnInst *, 4> RetInsts;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000976 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000977
Johannes Doerfertdef99282019-08-14 21:29:37 +0000978 // Callback for a leaf value returned by the associated function.
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000979 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000980 auto Size = RVS.RetValsMap[&Val].size();
981 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
982 bool Inserted = RVS.RetValsMap[&Val].size() != Size;
983 RVS.Changed |= Inserted;
984 LLVM_DEBUG({
985 if (Inserted)
986 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
987 << " => " << RVS.RetInsts.size() << "\n";
988 });
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000989 return true;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000990 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000991
Johannes Doerfertdef99282019-08-14 21:29:37 +0000992 // Helper method to invoke the generic value traversal.
993 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) {
994 IRPosition RetValPos = IRPosition::value(RV);
995 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this,
996 RVS, VisitValueCB);
997 };
Johannes Doerfertda4d8112019-08-01 16:21:54 +0000998
Johannes Doerfertdef99282019-08-14 21:29:37 +0000999 // Callback for all "return intructions" live in the associated function.
1000 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1001 ReturnInst &Ret = cast<ReturnInst>(I);
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001002 RVState RVS({ReturnedValues, Changed, {}});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001003 RVS.RetInsts.insert(&Ret);
Johannes Doerfertdef99282019-08-14 21:29:37 +00001004 return VisitReturnedValue(*Ret.getReturnValue(), RVS);
1005 };
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001006
Johannes Doerfertdef99282019-08-14 21:29:37 +00001007 // Start by discovering returned values from all live returned instructions in
1008 // the associated function.
1009 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1010 return indicatePessimisticFixpoint();
1011
1012 // Once returned values "directly" present in the code are handled we try to
1013 // resolve returned calls.
1014 decltype(ReturnedValues) NewRVsMap;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001015 for (auto &It : ReturnedValues) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001016 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first
1017 << " by #" << It.second.size() << " RIs\n");
1018 CallBase *CB = dyn_cast<CallBase>(It.first);
1019 if (!CB || UnresolvedCalls.count(CB))
1020 continue;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001021
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001022 if (!CB->getCalledFunction()) {
1023 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1024 << "\n");
1025 UnresolvedCalls.insert(CB);
1026 continue;
1027 }
1028
1029 // TODO: use the function scope once we have call site AAReturnedValues.
1030 const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1031 *this, IRPosition::function(*CB->getCalledFunction()));
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001032 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
1033 << static_cast<const AbstractAttribute &>(RetValAA)
1034 << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001035
1036 // Skip dead ends, thus if we do not know anything about the returned
1037 // call we mark it as unresolved and it will stay that way.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001038 if (!RetValAA.getState().isValidState()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001039 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1040 << "\n");
1041 UnresolvedCalls.insert(CB);
1042 continue;
1043 }
1044
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001045 // Do not try to learn partial information. If the callee has unresolved
1046 // return values we will treat the call as unresolved/opaque.
1047 auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1048 if (!RetValAAUnresolvedCalls.empty()) {
1049 UnresolvedCalls.insert(CB);
1050 continue;
1051 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001052
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001053 // Now check if we can track transitively returned values. If possible, thus
1054 // if all return value can be represented in the current scope, do so.
1055 bool Unresolved = false;
1056 for (auto &RetValAAIt : RetValAA.returned_values()) {
1057 Value *RetVal = RetValAAIt.first;
1058 if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1059 isa<Constant>(RetVal))
1060 continue;
1061 // Anything that did not fit in the above categories cannot be resolved,
1062 // mark the call as unresolved.
1063 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1064 "cannot be translated: "
1065 << *RetVal << "\n");
1066 UnresolvedCalls.insert(CB);
1067 Unresolved = true;
1068 break;
1069 }
1070
1071 if (Unresolved)
1072 continue;
1073
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001074 // Now track transitively returned values.
1075 unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1076 if (NumRetAA == RetValAA.getNumReturnValues()) {
1077 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1078 "changed since it was seen last\n");
1079 continue;
1080 }
1081 NumRetAA = RetValAA.getNumReturnValues();
1082
Johannes Doerfertdef99282019-08-14 21:29:37 +00001083 for (auto &RetValAAIt : RetValAA.returned_values()) {
1084 Value *RetVal = RetValAAIt.first;
1085 if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1086 // Arguments are mapped to call site operands and we begin the traversal
1087 // again.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001088 bool Unused = false;
1089 RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001090 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS);
1091 continue;
1092 } else if (isa<CallBase>(RetVal)) {
1093 // Call sites are resolved by the callee attribute over time, no need to
1094 // do anything for us.
1095 continue;
1096 } else if (isa<Constant>(RetVal)) {
1097 // Constants are valid everywhere, we can simply take them.
1098 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end());
1099 continue;
1100 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00001101 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001102 }
1103
Johannes Doerfertdef99282019-08-14 21:29:37 +00001104 // To avoid modifications to the ReturnedValues map while we iterate over it
1105 // we kept record of potential new entries in a copy map, NewRVsMap.
1106 for (auto &It : NewRVsMap) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001107 assert(!It.second.empty() && "Entry does not add anything.");
1108 auto &ReturnInsts = ReturnedValues[It.first];
1109 for (ReturnInst *RI : It.second)
Johannes Doerfert695089e2019-08-23 15:23:49 +00001110 if (ReturnInsts.insert(RI)) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001111 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1112 << *It.first << " => " << *RI << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001113 Changed = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001114 }
1115 }
1116
Johannes Doerfertdef99282019-08-14 21:29:37 +00001117 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1118 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001119}
1120
Johannes Doerfertdef99282019-08-14 21:29:37 +00001121struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1122 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1123
1124 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001125 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
Johannes Doerfertdef99282019-08-14 21:29:37 +00001126};
1127
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001128/// Returned values information for a call sites.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001129struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1130 AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1131
1132 /// See AbstractAttribute::initialize(...).
1133 void initialize(Attributor &A) override {
1134 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001135 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001136 // sense to specialize attributes for call sites instead of
1137 // redirecting requests to the callee.
1138 llvm_unreachable("Abstract attributes for returned values are not "
1139 "supported for call sites yet!");
1140 }
1141
1142 /// See AbstractAttribute::updateImpl(...).
1143 ChangeStatus updateImpl(Attributor &A) override {
1144 return indicatePessimisticFixpoint();
1145 }
1146
1147 /// See AbstractAttribute::trackStatistics()
1148 void trackStatistics() const override {}
1149};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001150
Stefan Stipanovic06263672019-07-11 21:37:40 +00001151/// ------------------------ NoSync Function Attribute -------------------------
1152
Johannes Doerfert344d0382019-08-07 22:34:26 +00001153struct AANoSyncImpl : AANoSync {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001154 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
Stefan Stipanovic06263672019-07-11 21:37:40 +00001155
Stefan Stipanoviccb5ecae2019-07-12 18:34:06 +00001156 const std::string getAsStr() const override {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001157 return getAssumed() ? "nosync" : "may-sync";
1158 }
1159
1160 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001161 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001162
Stefan Stipanovic06263672019-07-11 21:37:40 +00001163 /// Helper function used to determine whether an instruction is non-relaxed
1164 /// atomic. In other words, if an atomic instruction does not have unordered
1165 /// or monotonic ordering
1166 static bool isNonRelaxedAtomic(Instruction *I);
1167
1168 /// Helper function used to determine whether an instruction is volatile.
1169 static bool isVolatile(Instruction *I);
1170
Johannes Doerfertc7a1db32019-07-13 01:09:27 +00001171 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1172 /// memset).
Stefan Stipanovic06263672019-07-11 21:37:40 +00001173 static bool isNoSyncIntrinsic(Instruction *I);
1174};
1175
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001176bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001177 if (!I->isAtomic())
1178 return false;
1179
1180 AtomicOrdering Ordering;
1181 switch (I->getOpcode()) {
1182 case Instruction::AtomicRMW:
1183 Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1184 break;
1185 case Instruction::Store:
1186 Ordering = cast<StoreInst>(I)->getOrdering();
1187 break;
1188 case Instruction::Load:
1189 Ordering = cast<LoadInst>(I)->getOrdering();
1190 break;
1191 case Instruction::Fence: {
1192 auto *FI = cast<FenceInst>(I);
1193 if (FI->getSyncScopeID() == SyncScope::SingleThread)
1194 return false;
1195 Ordering = FI->getOrdering();
1196 break;
1197 }
1198 case Instruction::AtomicCmpXchg: {
1199 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1200 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1201 // Only if both are relaxed, than it can be treated as relaxed.
1202 // Otherwise it is non-relaxed.
1203 if (Success != AtomicOrdering::Unordered &&
1204 Success != AtomicOrdering::Monotonic)
1205 return true;
1206 if (Failure != AtomicOrdering::Unordered &&
1207 Failure != AtomicOrdering::Monotonic)
1208 return true;
1209 return false;
1210 }
1211 default:
1212 llvm_unreachable(
1213 "New atomic operations need to be known in the attributor.");
1214 }
1215
1216 // Relaxed.
1217 if (Ordering == AtomicOrdering::Unordered ||
1218 Ordering == AtomicOrdering::Monotonic)
1219 return false;
1220 return true;
1221}
1222
1223/// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1224/// FIXME: We should ipmrove the handling of intrinsics.
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001225bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001226 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1227 switch (II->getIntrinsicID()) {
1228 /// Element wise atomic memory intrinsics are can only be unordered,
1229 /// therefore nosync.
1230 case Intrinsic::memset_element_unordered_atomic:
1231 case Intrinsic::memmove_element_unordered_atomic:
1232 case Intrinsic::memcpy_element_unordered_atomic:
1233 return true;
1234 case Intrinsic::memset:
1235 case Intrinsic::memmove:
1236 case Intrinsic::memcpy:
1237 if (!cast<MemIntrinsic>(II)->isVolatile())
1238 return true;
1239 return false;
1240 default:
1241 return false;
1242 }
1243 }
1244 return false;
1245}
1246
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001247bool AANoSyncImpl::isVolatile(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001248 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
1249 "Calls should not be checked here");
1250
1251 switch (I->getOpcode()) {
1252 case Instruction::AtomicRMW:
1253 return cast<AtomicRMWInst>(I)->isVolatile();
1254 case Instruction::Store:
1255 return cast<StoreInst>(I)->isVolatile();
1256 case Instruction::Load:
1257 return cast<LoadInst>(I)->isVolatile();
1258 case Instruction::AtomicCmpXchg:
1259 return cast<AtomicCmpXchgInst>(I)->isVolatile();
1260 default:
1261 return false;
1262 }
1263}
1264
Johannes Doerfertece81902019-08-12 22:05:53 +00001265ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001266
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001267 auto CheckRWInstForNoSync = [&](Instruction &I) {
1268 /// We are looking for volatile instructions or Non-Relaxed atomics.
1269 /// FIXME: We should ipmrove the handling of intrinsics.
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001270
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001271 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1272 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001273
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001274 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1275 if (ICS.hasFnAttr(Attribute::NoSync))
1276 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001277
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001278 const auto &NoSyncAA =
1279 A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS));
1280 if (NoSyncAA.isAssumedNoSync())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001281 return true;
1282 return false;
1283 }
Stefan Stipanovic06263672019-07-11 21:37:40 +00001284
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001285 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1286 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001287
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001288 return false;
1289 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001290
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001291 auto CheckForNoSync = [&](Instruction &I) {
1292 // At this point we handled all read/write effects and they are all
1293 // nosync, so they can be skipped.
1294 if (I.mayReadOrWriteMemory())
1295 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001296
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001297 // non-convergent and readnone imply nosync.
1298 return !ImmutableCallSite(&I).isConvergent();
1299 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001300
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001301 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1302 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001303 return indicatePessimisticFixpoint();
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001304
Stefan Stipanovic06263672019-07-11 21:37:40 +00001305 return ChangeStatus::UNCHANGED;
1306}
1307
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001308struct AANoSyncFunction final : public AANoSyncImpl {
1309 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1310
1311 /// See AbstractAttribute::trackStatistics()
1312 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1313};
1314
1315/// NoSync attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001316struct AANoSyncCallSite final : AANoSyncImpl {
1317 AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1318
1319 /// See AbstractAttribute::initialize(...).
1320 void initialize(Attributor &A) override {
1321 AANoSyncImpl::initialize(A);
1322 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001323 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001324 indicatePessimisticFixpoint();
1325 }
1326
1327 /// See AbstractAttribute::updateImpl(...).
1328 ChangeStatus updateImpl(Attributor &A) override {
1329 // TODO: Once we have call site specific value information we can provide
1330 // call site specific liveness information and then it makes
1331 // sense to specialize attributes for call sites arguments instead of
1332 // redirecting requests to the callee argument.
1333 Function *F = getAssociatedFunction();
1334 const IRPosition &FnPos = IRPosition::function(*F);
1335 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos);
1336 return clampStateAndIndicateChange(
1337 getState(), static_cast<const AANoSync::StateType &>(FnAA.getState()));
1338 }
1339
1340 /// See AbstractAttribute::trackStatistics()
1341 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1342};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001343
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001344/// ------------------------ No-Free Attributes ----------------------------
1345
Johannes Doerfert344d0382019-08-07 22:34:26 +00001346struct AANoFreeImpl : public AANoFree {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001347 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001348
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001349 /// See AbstractAttribute::updateImpl(...).
1350 ChangeStatus updateImpl(Attributor &A) override {
1351 auto CheckForNoFree = [&](Instruction &I) {
1352 ImmutableCallSite ICS(&I);
1353 if (ICS.hasFnAttr(Attribute::NoFree))
1354 return true;
1355
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001356 const auto &NoFreeAA =
1357 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS));
1358 return NoFreeAA.isAssumedNoFree();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001359 };
1360
1361 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1362 return indicatePessimisticFixpoint();
1363 return ChangeStatus::UNCHANGED;
1364 }
1365
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001366 /// See AbstractAttribute::getAsStr().
1367 const std::string getAsStr() const override {
1368 return getAssumed() ? "nofree" : "may-free";
1369 }
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001370};
1371
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001372struct AANoFreeFunction final : public AANoFreeImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001373 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001374
1375 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001376 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001377};
1378
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001379/// NoFree attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001380struct AANoFreeCallSite final : AANoFreeImpl {
1381 AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1382
1383 /// See AbstractAttribute::initialize(...).
1384 void initialize(Attributor &A) override {
1385 AANoFreeImpl::initialize(A);
1386 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001387 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001388 indicatePessimisticFixpoint();
1389 }
1390
1391 /// See AbstractAttribute::updateImpl(...).
1392 ChangeStatus updateImpl(Attributor &A) override {
1393 // TODO: Once we have call site specific value information we can provide
1394 // call site specific liveness information and then it makes
1395 // sense to specialize attributes for call sites arguments instead of
1396 // redirecting requests to the callee argument.
1397 Function *F = getAssociatedFunction();
1398 const IRPosition &FnPos = IRPosition::function(*F);
1399 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos);
1400 return clampStateAndIndicateChange(
1401 getState(), static_cast<const AANoFree::StateType &>(FnAA.getState()));
1402 }
1403
1404 /// See AbstractAttribute::trackStatistics()
1405 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1406};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001407
Hideto Ueno54869ec2019-07-15 06:49:04 +00001408/// ------------------------ NonNull Argument Attribute ------------------------
Johannes Doerfert344d0382019-08-07 22:34:26 +00001409struct AANonNullImpl : AANonNull {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001410 AANonNullImpl(const IRPosition &IRP) : AANonNull(IRP) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001411
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001412 /// See AbstractAttribute::initialize(...).
1413 void initialize(Attributor &A) override {
1414 if (hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
1415 indicateOptimisticFixpoint();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001416 else
1417 AANonNull::initialize(A);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001418 }
1419
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001420 /// See AbstractAttribute::getAsStr().
1421 const std::string getAsStr() const override {
1422 return getAssumed() ? "nonnull" : "may-null";
1423 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001424};
1425
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001426/// NonNull attribute for a floating value.
1427struct AANonNullFloating : AANonNullImpl {
1428 AANonNullFloating(const IRPosition &IRP) : AANonNullImpl(IRP) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001429
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001430 /// See AbstractAttribute::initialize(...).
1431 void initialize(Attributor &A) override {
1432 AANonNullImpl::initialize(A);
Hideto Ueno54869ec2019-07-15 06:49:04 +00001433
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001434 if (isAtFixpoint())
1435 return;
Hideto Ueno54869ec2019-07-15 06:49:04 +00001436
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001437 const IRPosition &IRP = getIRPosition();
1438 const Value &V = IRP.getAssociatedValue();
1439 const DataLayout &DL = A.getDataLayout();
Hideto Ueno54869ec2019-07-15 06:49:04 +00001440
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001441 // TODO: This context sensitive query should be removed once we can do
1442 // context sensitive queries in the genericValueTraversal below.
1443 if (isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, IRP.getCtxI(),
1444 /* TODO: DT */ nullptr))
1445 indicateOptimisticFixpoint();
1446 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001447
1448 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001449 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001450 const DataLayout &DL = A.getDataLayout();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001451
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001452 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
1453 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001454 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V));
1455 if (!Stripped && this == &AA) {
1456 if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr,
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001457 /* TODO: CtxI */ nullptr,
1458 /* TODO: DT */ nullptr))
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001459 T.indicatePessimisticFixpoint();
1460 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001461 // Use abstract attribute information.
1462 const AANonNull::StateType &NS =
1463 static_cast<const AANonNull::StateType &>(AA.getState());
1464 T ^= NS;
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001465 }
1466 return T.isValidState();
1467 };
1468
1469 StateType T;
1470 if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this,
1471 T, VisitValueCB))
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001472 return indicatePessimisticFixpoint();
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001473
1474 return clampStateAndIndicateChange(getState(), T);
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001475 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001476
1477 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001478 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001479};
1480
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001481/// NonNull attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001482struct AANonNullReturned final
1483 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001484 AANonNullReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001485 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001486
1487 /// See AbstractAttribute::trackStatistics()
1488 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
1489};
1490
Hideto Ueno54869ec2019-07-15 06:49:04 +00001491/// NonNull attribute for function argument.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001492struct AANonNullArgument final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001493 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001494 AANonNullArgument(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001495 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001496
1497 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001498 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001499};
1500
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001501struct AANonNullCallSiteArgument final : AANonNullFloating {
1502 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001503
1504 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert1aac1822019-08-29 01:26:09 +00001505 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001506};
Johannes Doerfert007153e2019-08-05 23:26:06 +00001507
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001508/// NonNull attribute for a call site return position.
1509struct AANonNullCallSiteReturned final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001510 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001511 AANonNullCallSiteReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001512 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001513
1514 /// See AbstractAttribute::trackStatistics()
1515 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
1516};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001517
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001518/// ------------------------ No-Recurse Attributes ----------------------------
1519
1520struct AANoRecurseImpl : public AANoRecurse {
1521 AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {}
1522
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001523 /// See AbstractAttribute::getAsStr()
1524 const std::string getAsStr() const override {
1525 return getAssumed() ? "norecurse" : "may-recurse";
1526 }
1527};
1528
1529struct AANoRecurseFunction final : AANoRecurseImpl {
1530 AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
1531
1532 /// See AbstractAttribute::updateImpl(...).
1533 ChangeStatus updateImpl(Attributor &A) override {
1534 // TODO: Implement this.
1535 return indicatePessimisticFixpoint();
1536 }
1537
1538 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
1539};
1540
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001541/// NoRecurse attribute deduction for a call sites.
1542struct AANoRecurseCallSite final : AANoRecurseImpl {
1543 AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
1544
1545 /// See AbstractAttribute::initialize(...).
1546 void initialize(Attributor &A) override {
1547 AANoRecurseImpl::initialize(A);
1548 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001549 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001550 indicatePessimisticFixpoint();
1551 }
1552
1553 /// See AbstractAttribute::updateImpl(...).
1554 ChangeStatus updateImpl(Attributor &A) override {
1555 // TODO: Once we have call site specific value information we can provide
1556 // call site specific liveness information and then it makes
1557 // sense to specialize attributes for call sites arguments instead of
1558 // redirecting requests to the callee argument.
1559 Function *F = getAssociatedFunction();
1560 const IRPosition &FnPos = IRPosition::function(*F);
1561 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos);
1562 return clampStateAndIndicateChange(
1563 getState(),
1564 static_cast<const AANoRecurse::StateType &>(FnAA.getState()));
1565 }
1566
1567 /// See AbstractAttribute::trackStatistics()
1568 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
1569};
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001570
Hideto Ueno11d37102019-07-17 15:15:43 +00001571/// ------------------------ Will-Return Attributes ----------------------------
1572
Hideto Ueno11d37102019-07-17 15:15:43 +00001573// Helper function that checks whether a function has any cycle.
1574// TODO: Replace with more efficent code
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001575static bool containsCycle(Function &F) {
Hideto Ueno11d37102019-07-17 15:15:43 +00001576 SmallPtrSet<BasicBlock *, 32> Visited;
1577
1578 // Traverse BB by dfs and check whether successor is already visited.
1579 for (BasicBlock *BB : depth_first(&F)) {
1580 Visited.insert(BB);
1581 for (auto *SuccBB : successors(BB)) {
1582 if (Visited.count(SuccBB))
1583 return true;
1584 }
1585 }
1586 return false;
1587}
1588
1589// Helper function that checks the function have a loop which might become an
1590// endless loop
1591// FIXME: Any cycle is regarded as endless loop for now.
1592// We have to allow some patterns.
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001593static bool containsPossiblyEndlessLoop(Function *F) {
1594 return !F || !F->hasExactDefinition() || containsCycle(*F);
Hideto Ueno11d37102019-07-17 15:15:43 +00001595}
1596
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001597struct AAWillReturnImpl : public AAWillReturn {
1598 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
Hideto Ueno11d37102019-07-17 15:15:43 +00001599
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001600 /// See AbstractAttribute::initialize(...).
1601 void initialize(Attributor &A) override {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001602 AAWillReturn::initialize(A);
Hideto Ueno11d37102019-07-17 15:15:43 +00001603
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001604 Function *F = getAssociatedFunction();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001605 if (containsPossiblyEndlessLoop(F))
1606 indicatePessimisticFixpoint();
1607 }
Hideto Ueno11d37102019-07-17 15:15:43 +00001608
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001609 /// See AbstractAttribute::updateImpl(...).
1610 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001611 auto CheckForWillReturn = [&](Instruction &I) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001612 IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I));
1613 const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos);
1614 if (WillReturnAA.isKnownWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001615 return true;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001616 if (!WillReturnAA.isAssumedWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001617 return false;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001618 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos);
1619 return NoRecurseAA.isAssumedNoRecurse();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001620 };
1621
1622 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
1623 return indicatePessimisticFixpoint();
1624
1625 return ChangeStatus::UNCHANGED;
1626 }
1627
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001628 /// See AbstractAttribute::getAsStr()
1629 const std::string getAsStr() const override {
1630 return getAssumed() ? "willreturn" : "may-noreturn";
1631 }
1632};
1633
1634struct AAWillReturnFunction final : AAWillReturnImpl {
1635 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
1636
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001637 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001638 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001639};
Hideto Ueno11d37102019-07-17 15:15:43 +00001640
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001641/// WillReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001642struct AAWillReturnCallSite final : AAWillReturnImpl {
1643 AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
1644
1645 /// See AbstractAttribute::initialize(...).
1646 void initialize(Attributor &A) override {
1647 AAWillReturnImpl::initialize(A);
1648 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001649 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001650 indicatePessimisticFixpoint();
1651 }
1652
1653 /// See AbstractAttribute::updateImpl(...).
1654 ChangeStatus updateImpl(Attributor &A) override {
1655 // TODO: Once we have call site specific value information we can provide
1656 // call site specific liveness information and then it makes
1657 // sense to specialize attributes for call sites arguments instead of
1658 // redirecting requests to the callee argument.
1659 Function *F = getAssociatedFunction();
1660 const IRPosition &FnPos = IRPosition::function(*F);
1661 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos);
1662 return clampStateAndIndicateChange(
1663 getState(),
1664 static_cast<const AAWillReturn::StateType &>(FnAA.getState()));
1665 }
1666
1667 /// See AbstractAttribute::trackStatistics()
1668 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
1669};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001670
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001671/// ------------------------ NoAlias Argument Attribute ------------------------
1672
Johannes Doerfert344d0382019-08-07 22:34:26 +00001673struct AANoAliasImpl : AANoAlias {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001674 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001675
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001676 const std::string getAsStr() const override {
1677 return getAssumed() ? "noalias" : "may-alias";
1678 }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001679};
1680
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001681/// NoAlias attribute for a floating value.
1682struct AANoAliasFloating final : AANoAliasImpl {
1683 AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1684
Hideto Uenocbab3342019-08-29 05:52:00 +00001685 /// See AbstractAttribute::initialize(...).
1686 void initialize(Attributor &A) override {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00001687 AANoAliasImpl::initialize(A);
1688 if (isa<AllocaInst>(getAnchorValue()))
1689 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00001690 }
1691
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001692 /// See AbstractAttribute::updateImpl(...).
1693 ChangeStatus updateImpl(Attributor &A) override {
1694 // TODO: Implement this.
1695 return indicatePessimisticFixpoint();
1696 }
1697
1698 /// See AbstractAttribute::trackStatistics()
1699 void trackStatistics() const override {
1700 STATS_DECLTRACK_FLOATING_ATTR(noalias)
1701 }
1702};
1703
1704/// NoAlias attribute for an argument.
Hideto Uenocbab3342019-08-29 05:52:00 +00001705struct AANoAliasArgument final
1706 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
1707 AANoAliasArgument(const IRPosition &IRP)
1708 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>(IRP) {}
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001709
1710 /// See AbstractAttribute::trackStatistics()
1711 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
1712};
1713
1714struct AANoAliasCallSiteArgument final : AANoAliasImpl {
1715 AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1716
Hideto Uenocbab3342019-08-29 05:52:00 +00001717 /// See AbstractAttribute::initialize(...).
1718 void initialize(Attributor &A) override {
Hideto Ueno6381b142019-08-30 10:00:32 +00001719 // See callsite argument attribute and callee argument attribute.
1720 ImmutableCallSite ICS(&getAnchorValue());
1721 if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias))
1722 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00001723 }
1724
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001725 /// See AbstractAttribute::updateImpl(...).
1726 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00001727 // We can deduce "noalias" if the following conditions hold.
1728 // (i) Associated value is assumed to be noalias in the definition.
1729 // (ii) Associated value is assumed to be no-capture in all the uses
1730 // possibly executed before this callsite.
1731 // (iii) There is no other pointer argument which could alias with the
1732 // value.
1733
1734 const Value &V = getAssociatedValue();
1735 const IRPosition IRP = IRPosition::value(V);
1736
1737 // (i) Check whether noalias holds in the definition.
1738
1739 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP);
1740
1741 if (!NoAliasAA.isAssumedNoAlias())
1742 return indicatePessimisticFixpoint();
1743
1744 LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V
1745 << " is assumed NoAlias in the definition\n");
1746
1747 // (ii) Check whether the value is captured in the scope using AANoCapture.
1748 // FIXME: This is conservative though, it is better to look at CFG and
1749 // check only uses possibly executed before this callsite.
1750
1751 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
1752 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned())
1753 return indicatePessimisticFixpoint();
1754
1755 // (iii) Check there is no other pointer argument which could alias with the
1756 // value.
1757 ImmutableCallSite ICS(&getAnchorValue());
1758 for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) {
1759 if (getArgNo() == (int)i)
1760 continue;
1761 const Value *ArgOp = ICS.getArgOperand(i);
1762 if (!ArgOp->getType()->isPointerTy())
1763 continue;
1764
1765 // TODO: Use AliasAnalysis
1766 // AAResults& AAR = ..;
1767 // if(AAR.isNoAlias(&getAssociatedValue(), ArgOp))
1768 // return indicatePessimitisicFixpoint();
1769
1770 return indicatePessimisticFixpoint();
1771 }
1772
1773 return ChangeStatus::UNCHANGED;
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001774 }
1775
1776 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00001777 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001778};
1779
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001780/// NoAlias attribute for function return value.
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001781struct AANoAliasReturned final : AANoAliasImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001782 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001783
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001784 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001785 virtual ChangeStatus updateImpl(Attributor &A) override {
1786
1787 auto CheckReturnValue = [&](Value &RV) -> bool {
1788 if (Constant *C = dyn_cast<Constant>(&RV))
1789 if (C->isNullValue() || isa<UndefValue>(C))
1790 return true;
1791
1792 /// For now, we can only deduce noalias if we have call sites.
1793 /// FIXME: add more support.
1794 ImmutableCallSite ICS(&RV);
1795 if (!ICS)
1796 return false;
1797
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00001798 const IRPosition &RVPos = IRPosition::value(RV);
1799 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001800 if (!NoAliasAA.isAssumedNoAlias())
1801 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001802
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00001803 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos);
1804 return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001805 };
1806
1807 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
1808 return indicatePessimisticFixpoint();
1809
1810 return ChangeStatus::UNCHANGED;
1811 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001812
1813 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001814 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001815};
1816
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001817/// NoAlias attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001818struct AANoAliasCallSiteReturned final : AANoAliasImpl {
1819 AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1820
1821 /// See AbstractAttribute::initialize(...).
1822 void initialize(Attributor &A) override {
1823 AANoAliasImpl::initialize(A);
1824 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001825 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001826 indicatePessimisticFixpoint();
1827 }
1828
1829 /// See AbstractAttribute::updateImpl(...).
1830 ChangeStatus updateImpl(Attributor &A) override {
1831 // TODO: Once we have call site specific value information we can provide
1832 // call site specific liveness information and then it makes
1833 // sense to specialize attributes for call sites arguments instead of
1834 // redirecting requests to the callee argument.
1835 Function *F = getAssociatedFunction();
1836 const IRPosition &FnPos = IRPosition::returned(*F);
1837 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos);
1838 return clampStateAndIndicateChange(
1839 getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState()));
1840 }
1841
1842 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00001843 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001844};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001845
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001846/// -------------------AAIsDead Function Attribute-----------------------
1847
Johannes Doerfert344d0382019-08-07 22:34:26 +00001848struct AAIsDeadImpl : public AAIsDead {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001849 AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001850
Johannes Doerfertece81902019-08-12 22:05:53 +00001851 void initialize(Attributor &A) override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001852 const Function *F = getAssociatedFunction();
Johannes Doerfert97fd5822019-09-04 16:26:20 +00001853 if (F && !F->isDeclaration())
1854 exploreFromEntry(A, F);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001855 }
1856
1857 void exploreFromEntry(Attributor &A, const Function *F) {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001858 ToBeExploredPaths.insert(&(F->getEntryBlock().front()));
Johannes Doerfert2f622062019-09-04 16:35:20 +00001859 assumeLive(A, F->getEntryBlock());
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001860
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001861 for (size_t i = 0; i < ToBeExploredPaths.size(); ++i)
Johannes Doerfert4361da22019-08-04 18:38:53 +00001862 if (const Instruction *NextNoReturnI =
1863 findNextNoReturn(A, ToBeExploredPaths[i]))
1864 NoReturnCalls.insert(NextNoReturnI);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001865 }
1866
Johannes Doerfert4361da22019-08-04 18:38:53 +00001867 /// Find the next assumed noreturn instruction in the block of \p I starting
1868 /// from, thus including, \p I.
1869 ///
1870 /// The caller is responsible to monitor the ToBeExploredPaths set as new
1871 /// instructions discovered in other basic block will be placed in there.
1872 ///
1873 /// \returns The next assumed noreturn instructions in the block of \p I
1874 /// starting from, thus including, \p I.
1875 const Instruction *findNextNoReturn(Attributor &A, const Instruction *I);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001876
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001877 /// See AbstractAttribute::getAsStr().
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001878 const std::string getAsStr() const override {
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001879 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001880 std::to_string(getAssociatedFunction()->size()) + "][#NRI " +
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001881 std::to_string(NoReturnCalls.size()) + "]";
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001882 }
1883
1884 /// See AbstractAttribute::manifest(...).
1885 ChangeStatus manifest(Attributor &A) override {
1886 assert(getState().isValidState() &&
1887 "Attempted to manifest an invalid state!");
1888
1889 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001890 Function &F = *getAssociatedFunction();
1891
1892 if (AssumedLiveBlocks.empty()) {
Johannes Doerfertb19cd272019-09-03 20:42:16 +00001893 A.deleteAfterManifest(F);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001894 return ChangeStatus::CHANGED;
1895 }
Johannes Doerfert924d2132019-08-05 21:34:45 +00001896
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001897 // Flag to determine if we can change an invoke to a call assuming the
1898 // callee is nounwind. This is not possible if the personality of the
1899 // function allows to catch asynchronous exceptions.
Johannes Doerfert924d2132019-08-05 21:34:45 +00001900 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001901
Johannes Doerfert4361da22019-08-04 18:38:53 +00001902 for (const Instruction *NRC : NoReturnCalls) {
1903 Instruction *I = const_cast<Instruction *>(NRC);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001904 BasicBlock *BB = I->getParent();
Johannes Doerfert4361da22019-08-04 18:38:53 +00001905 Instruction *SplitPos = I->getNextNode();
Johannes Doerfertd4108052019-08-21 20:56:41 +00001906 // TODO: mark stuff before unreachable instructions as dead.
1907 if (isa_and_nonnull<UnreachableInst>(SplitPos))
1908 continue;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001909
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001910 if (auto *II = dyn_cast<InvokeInst>(I)) {
Johannes Doerfert3d7bbc62019-08-05 21:35:02 +00001911 // If we keep the invoke the split position is at the beginning of the
1912 // normal desitination block (it invokes a noreturn function after all).
1913 BasicBlock *NormalDestBB = II->getNormalDest();
1914 SplitPos = &NormalDestBB->front();
1915
Johannes Doerfert4361da22019-08-04 18:38:53 +00001916 /// Invoke is replaced with a call and unreachable is placed after it if
1917 /// the callee is nounwind and noreturn. Otherwise, we keep the invoke
1918 /// and only place an unreachable in the normal successor.
Johannes Doerfert924d2132019-08-05 21:34:45 +00001919 if (Invoke2CallAllowed) {
Michael Liaoa99086d2019-08-20 21:02:31 +00001920 if (II->getCalledFunction()) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001921 const IRPosition &IPos = IRPosition::callsite_function(*II);
1922 const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos);
1923 if (AANoUnw.isAssumedNoUnwind()) {
Johannes Doerfert924d2132019-08-05 21:34:45 +00001924 LLVM_DEBUG(dbgs()
1925 << "[AAIsDead] Replace invoke with call inst\n");
Johannes Doerfert3d7bbc62019-08-05 21:35:02 +00001926 // We do not need an invoke (II) but instead want a call followed
1927 // by an unreachable. However, we do not remove II as other
1928 // abstract attributes might have it cached as part of their
1929 // results. Given that we modify the CFG anyway, we simply keep II
1930 // around but in a new dead block. To avoid II being live through
1931 // a different edge we have to ensure the block we place it in is
1932 // only reached from the current block of II and then not reached
1933 // at all when we insert the unreachable.
1934 SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c");
1935 CallInst *CI = createCallMatchingInvoke(II);
1936 CI->insertBefore(II);
1937 CI->takeName(II);
1938 II->replaceAllUsesWith(CI);
1939 SplitPos = CI->getNextNode();
Johannes Doerfert924d2132019-08-05 21:34:45 +00001940 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00001941 }
1942 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00001943
Johannes Doerfert7ab52532019-09-04 20:34:52 +00001944 if (SplitPos == &NormalDestBB->front()) {
1945 // If this is an invoke of a noreturn function the edge to the normal
1946 // destination block is dead but not necessarily the block itself.
1947 // TODO: We need to move to an edge based system during deduction and
1948 // also manifest.
1949 assert(!NormalDestBB->isLandingPad() &&
1950 "Expected the normal destination not to be a landingpad!");
1951 BasicBlock *SplitBB =
1952 SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
1953 // The split block is live even if it contains only an unreachable
1954 // instruction at the end.
1955 assumeLive(A, *SplitBB);
1956 SplitPos = SplitBB->getTerminator();
1957 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001958 }
1959
Johannes Doerfert3d7bbc62019-08-05 21:35:02 +00001960 BB = SplitPos->getParent();
Johannes Doerfert4361da22019-08-04 18:38:53 +00001961 SplitBlock(BB, SplitPos);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001962 changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false);
1963 HasChanged = ChangeStatus::CHANGED;
1964 }
1965
Johannes Doerfertb19cd272019-09-03 20:42:16 +00001966 for (BasicBlock &BB : F)
1967 if (!AssumedLiveBlocks.count(&BB))
1968 A.deleteAfterManifest(BB);
1969
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001970 return HasChanged;
1971 }
1972
1973 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001974 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001975
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001976 /// See AAIsDead::isAssumedDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00001977 bool isAssumedDead(const BasicBlock *BB) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001978 assert(BB->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001979 "BB must be in the same anchor scope function.");
1980
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001981 if (!getAssumed())
1982 return false;
1983 return !AssumedLiveBlocks.count(BB);
1984 }
1985
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001986 /// See AAIsDead::isKnownDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00001987 bool isKnownDead(const BasicBlock *BB) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001988 return getKnown() && isAssumedDead(BB);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001989 }
1990
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001991 /// See AAIsDead::isAssumed(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00001992 bool isAssumedDead(const Instruction *I) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001993 assert(I->getParent()->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001994 "Instruction must be in the same anchor scope function.");
1995
Stefan Stipanovic7849e412019-08-03 15:27:41 +00001996 if (!getAssumed())
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001997 return false;
1998
1999 // If it is not in AssumedLiveBlocks then it for sure dead.
2000 // Otherwise, it can still be after noreturn call in a live block.
2001 if (!AssumedLiveBlocks.count(I->getParent()))
2002 return true;
2003
2004 // If it is not after a noreturn call, than it is live.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002005 return isAfterNoReturn(I);
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002006 }
2007
2008 /// See AAIsDead::isKnownDead(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00002009 bool isKnownDead(const Instruction *I) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002010 return getKnown() && isAssumedDead(I);
2011 }
2012
2013 /// Check if instruction is after noreturn call, in other words, assumed dead.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002014 bool isAfterNoReturn(const Instruction *I) const;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002015
Johannes Doerfert924d2132019-08-05 21:34:45 +00002016 /// Determine if \p F might catch asynchronous exceptions.
2017 static bool mayCatchAsynchronousExceptions(const Function &F) {
2018 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
2019 }
2020
Johannes Doerfert2f622062019-09-04 16:35:20 +00002021 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
2022 /// that internal function called from \p BB should now be looked at.
2023 void assumeLive(Attributor &A, const BasicBlock &BB) {
2024 if (!AssumedLiveBlocks.insert(&BB).second)
2025 return;
2026
2027 // We assume that all of BB is (probably) live now and if there are calls to
2028 // internal functions we will assume that those are now live as well. This
2029 // is a performance optimization for blocks with calls to a lot of internal
2030 // functions. It can however cause dead functions to be treated as live.
2031 for (const Instruction &I : BB)
2032 if (ImmutableCallSite ICS = ImmutableCallSite(&I))
2033 if (const Function *F = ICS.getCalledFunction())
2034 if (F->hasInternalLinkage())
2035 A.markLiveInternalFunction(*F);
2036 }
2037
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002038 /// Collection of to be explored paths.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002039 SmallSetVector<const Instruction *, 8> ToBeExploredPaths;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002040
2041 /// Collection of all assumed live BasicBlocks.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002042 DenseSet<const BasicBlock *> AssumedLiveBlocks;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002043
2044 /// Collection of calls with noreturn attribute, assumed or knwon.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002045 SmallSetVector<const Instruction *, 4> NoReturnCalls;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002046};
2047
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002048struct AAIsDeadFunction final : public AAIsDeadImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002049 AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002050
2051 /// See AbstractAttribute::trackStatistics()
2052 void trackStatistics() const override {
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002053 STATS_DECL(PartiallyDeadBlocks, Function,
2054 "Number of basic blocks classified as partially dead");
2055 BUILD_STAT_NAME(PartiallyDeadBlocks, Function) += NoReturnCalls.size();
2056 }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002057};
2058
2059bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const {
Johannes Doerfert4361da22019-08-04 18:38:53 +00002060 const Instruction *PrevI = I->getPrevNode();
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002061 while (PrevI) {
2062 if (NoReturnCalls.count(PrevI))
2063 return true;
2064 PrevI = PrevI->getPrevNode();
2065 }
2066 return false;
2067}
2068
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002069const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A,
2070 const Instruction *I) {
Johannes Doerfert4361da22019-08-04 18:38:53 +00002071 const BasicBlock *BB = I->getParent();
Johannes Doerfert924d2132019-08-05 21:34:45 +00002072 const Function &F = *BB->getParent();
2073
2074 // Flag to determine if we can change an invoke to a call assuming the callee
2075 // is nounwind. This is not possible if the personality of the function allows
2076 // to catch asynchronous exceptions.
2077 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
Johannes Doerfert4361da22019-08-04 18:38:53 +00002078
2079 // TODO: We should have a function that determines if an "edge" is dead.
2080 // Edges could be from an instruction to the next or from a terminator
2081 // to the successor. For now, we need to special case the unwind block
2082 // of InvokeInst below.
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002083
2084 while (I) {
2085 ImmutableCallSite ICS(I);
2086
2087 if (ICS) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002088 const IRPosition &IPos = IRPosition::callsite_function(ICS);
Johannes Doerfert4361da22019-08-04 18:38:53 +00002089 // Regarless of the no-return property of an invoke instruction we only
2090 // learn that the regular successor is not reachable through this
2091 // instruction but the unwind block might still be.
2092 if (auto *Invoke = dyn_cast<InvokeInst>(I)) {
2093 // Use nounwind to justify the unwind block is dead as well.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002094 const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos);
2095 if (!Invoke2CallAllowed || !AANoUnw.isAssumedNoUnwind()) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00002096 assumeLive(A, *Invoke->getUnwindDest());
Johannes Doerfert4361da22019-08-04 18:38:53 +00002097 ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front());
2098 }
2099 }
2100
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002101 const auto &NoReturnAA = A.getAAFor<AANoReturn>(*this, IPos);
2102 if (NoReturnAA.isAssumedNoReturn())
Johannes Doerfert4361da22019-08-04 18:38:53 +00002103 return I;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002104 }
2105
2106 I = I->getNextNode();
2107 }
2108
2109 // get new paths (reachable blocks).
Johannes Doerfert4361da22019-08-04 18:38:53 +00002110 for (const BasicBlock *SuccBB : successors(BB)) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00002111 assumeLive(A, *SuccBB);
Johannes Doerfert4361da22019-08-04 18:38:53 +00002112 ToBeExploredPaths.insert(&SuccBB->front());
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002113 }
2114
Johannes Doerfert4361da22019-08-04 18:38:53 +00002115 // No noreturn instruction found.
2116 return nullptr;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002117}
2118
Johannes Doerfertece81902019-08-12 22:05:53 +00002119ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A) {
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00002120 ChangeStatus Status = ChangeStatus::UNCHANGED;
2121
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002122 // Temporary collection to iterate over existing noreturn instructions. This
2123 // will alow easier modification of NoReturnCalls collection
Johannes Doerfert4361da22019-08-04 18:38:53 +00002124 SmallVector<const Instruction *, 8> NoReturnChanged;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002125
Johannes Doerfert4361da22019-08-04 18:38:53 +00002126 for (const Instruction *I : NoReturnCalls)
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002127 NoReturnChanged.push_back(I);
2128
Johannes Doerfert4361da22019-08-04 18:38:53 +00002129 for (const Instruction *I : NoReturnChanged) {
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002130 size_t Size = ToBeExploredPaths.size();
2131
Johannes Doerfert4361da22019-08-04 18:38:53 +00002132 const Instruction *NextNoReturnI = findNextNoReturn(A, I);
2133 if (NextNoReturnI != I) {
2134 Status = ChangeStatus::CHANGED;
2135 NoReturnCalls.remove(I);
2136 if (NextNoReturnI)
2137 NoReturnCalls.insert(NextNoReturnI);
2138 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002139
Johannes Doerfert4361da22019-08-04 18:38:53 +00002140 // Explore new paths.
2141 while (Size != ToBeExploredPaths.size()) {
2142 Status = ChangeStatus::CHANGED;
2143 if (const Instruction *NextNoReturnI =
2144 findNextNoReturn(A, ToBeExploredPaths[Size++]))
2145 NoReturnCalls.insert(NextNoReturnI);
2146 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002147 }
2148
Johannes Doerfertdef99282019-08-14 21:29:37 +00002149 LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: "
2150 << AssumedLiveBlocks.size() << " Total number of blocks: "
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002151 << getAssociatedFunction()->size() << "\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002152
Johannes Doerfertd6207812019-08-07 22:32:38 +00002153 // If we know everything is live there is no need to query for liveness.
2154 if (NoReturnCalls.empty() &&
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002155 getAssociatedFunction()->size() == AssumedLiveBlocks.size()) {
Johannes Doerfertd6207812019-08-07 22:32:38 +00002156 // Indicating a pessimistic fixpoint will cause the state to be "invalid"
2157 // which will cause the Attributor to not return the AAIsDead on request,
2158 // which will prevent us from querying isAssumedDead().
2159 indicatePessimisticFixpoint();
2160 assert(!isValidState() && "Expected an invalid state!");
Johannes Doerfert62a9c1d2019-08-29 01:26:58 +00002161 Status = ChangeStatus::CHANGED;
Johannes Doerfertd6207812019-08-07 22:32:38 +00002162 }
2163
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002164 return Status;
2165}
2166
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002167/// Liveness information for a call sites.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00002168struct AAIsDeadCallSite final : AAIsDeadImpl {
2169 AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadImpl(IRP) {}
2170
2171 /// See AbstractAttribute::initialize(...).
2172 void initialize(Attributor &A) override {
2173 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002174 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00002175 // sense to specialize attributes for call sites instead of
2176 // redirecting requests to the callee.
2177 llvm_unreachable("Abstract attributes for liveness are not "
2178 "supported for call sites yet!");
2179 }
2180
2181 /// See AbstractAttribute::updateImpl(...).
2182 ChangeStatus updateImpl(Attributor &A) override {
2183 return indicatePessimisticFixpoint();
2184 }
2185
2186 /// See AbstractAttribute::trackStatistics()
2187 void trackStatistics() const override {}
2188};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002189
Hideto Ueno19c07af2019-07-23 08:16:17 +00002190/// -------------------- Dereferenceable Argument Attribute --------------------
2191
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002192template <>
2193ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
2194 const DerefState &R) {
2195 ChangeStatus CS0 = clampStateAndIndicateChange<IntegerState>(
2196 S.DerefBytesState, R.DerefBytesState);
2197 ChangeStatus CS1 =
2198 clampStateAndIndicateChange<IntegerState>(S.GlobalState, R.GlobalState);
2199 return CS0 | CS1;
2200}
2201
Hideto Ueno70576ca2019-08-22 14:18:29 +00002202struct AADereferenceableImpl : AADereferenceable {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002203 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
Johannes Doerfert344d0382019-08-07 22:34:26 +00002204 using StateType = DerefState;
Hideto Ueno19c07af2019-07-23 08:16:17 +00002205
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00002206 void initialize(Attributor &A) override {
2207 SmallVector<Attribute, 4> Attrs;
2208 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
2209 Attrs);
2210 for (const Attribute &Attr : Attrs)
2211 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
2212
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002213 NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition());
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002214
2215 const IRPosition &IRP = this->getIRPosition();
2216 bool IsFnInterface = IRP.isFnInterfaceKind();
2217 const Function *FnScope = IRP.getAnchorScope();
2218 if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition()))
2219 indicatePessimisticFixpoint();
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00002220 }
2221
Hideto Ueno19c07af2019-07-23 08:16:17 +00002222 /// See AbstractAttribute::getState()
2223 /// {
Johannes Doerfert344d0382019-08-07 22:34:26 +00002224 StateType &getState() override { return *this; }
2225 const StateType &getState() const override { return *this; }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002226 /// }
2227
Johannes Doerferteccdf082019-08-05 23:35:12 +00002228 void getDeducedAttributes(LLVMContext &Ctx,
2229 SmallVectorImpl<Attribute> &Attrs) const override {
Hideto Ueno19c07af2019-07-23 08:16:17 +00002230 // TODO: Add *_globally support
2231 if (isAssumedNonNull())
2232 Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
2233 Ctx, getAssumedDereferenceableBytes()));
2234 else
2235 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
2236 Ctx, getAssumedDereferenceableBytes()));
2237 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002238
2239 /// See AbstractAttribute::getAsStr().
2240 const std::string getAsStr() const override {
2241 if (!getAssumedDereferenceableBytes())
2242 return "unknown-dereferenceable";
2243 return std::string("dereferenceable") +
2244 (isAssumedNonNull() ? "" : "_or_null") +
2245 (isAssumedGlobal() ? "_globally" : "") + "<" +
2246 std::to_string(getKnownDereferenceableBytes()) + "-" +
2247 std::to_string(getAssumedDereferenceableBytes()) + ">";
2248 }
2249};
2250
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002251/// Dereferenceable attribute for a floating value.
2252struct AADereferenceableFloating : AADereferenceableImpl {
2253 AADereferenceableFloating(const IRPosition &IRP)
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002254 : AADereferenceableImpl(IRP) {}
Hideto Ueno19c07af2019-07-23 08:16:17 +00002255
2256 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002257 ChangeStatus updateImpl(Attributor &A) override {
2258 const DataLayout &DL = A.getDataLayout();
2259
2260 auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool {
2261 unsigned IdxWidth =
2262 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
2263 APInt Offset(IdxWidth, 0);
2264 const Value *Base =
2265 V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
2266
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002267 const auto &AA =
2268 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002269 int64_t DerefBytes = 0;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002270 if (!Stripped && this == &AA) {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002271 // Use IR information if we did not strip anything.
2272 // TODO: track globally.
2273 bool CanBeNull;
2274 DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull);
2275 T.GlobalState.indicatePessimisticFixpoint();
2276 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002277 const DerefState &DS = static_cast<const DerefState &>(AA.getState());
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002278 DerefBytes = DS.DerefBytesState.getAssumed();
2279 T.GlobalState &= DS.GlobalState;
2280 }
2281
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00002282 // For now we do not try to "increase" dereferenceability due to negative
2283 // indices as we first have to come up with code to deal with loops and
2284 // for overflows of the dereferenceable bytes.
Johannes Doerfert785fad32019-08-23 17:29:23 +00002285 int64_t OffsetSExt = Offset.getSExtValue();
2286 if (OffsetSExt < 0)
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00002287 Offset = 0;
2288
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002289 T.takeAssumedDerefBytesMinimum(
Johannes Doerfert785fad32019-08-23 17:29:23 +00002290 std::max(int64_t(0), DerefBytes - OffsetSExt));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002291
Johannes Doerfert785fad32019-08-23 17:29:23 +00002292 if (this == &AA) {
2293 if (!Stripped) {
2294 // If nothing was stripped IR information is all we got.
2295 T.takeKnownDerefBytesMaximum(
2296 std::max(int64_t(0), DerefBytes - OffsetSExt));
2297 T.indicatePessimisticFixpoint();
2298 } else if (OffsetSExt > 0) {
2299 // If something was stripped but there is circular reasoning we look
2300 // for the offset. If it is positive we basically decrease the
2301 // dereferenceable bytes in a circluar loop now, which will simply
2302 // drive them down to the known value in a very slow way which we
2303 // can accelerate.
2304 T.indicatePessimisticFixpoint();
2305 }
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002306 }
2307
2308 return T.isValidState();
2309 };
2310
2311 DerefState T;
2312 if (!genericValueTraversal<AADereferenceable, DerefState>(
2313 A, getIRPosition(), *this, T, VisitValueCB))
2314 return indicatePessimisticFixpoint();
2315
2316 return clampStateAndIndicateChange(getState(), T);
2317 }
2318
2319 /// See AbstractAttribute::trackStatistics()
2320 void trackStatistics() const override {
2321 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
2322 }
2323};
2324
2325/// Dereferenceable attribute for a return value.
2326struct AADereferenceableReturned final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002327 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl,
2328 DerefState> {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002329 AADereferenceableReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002330 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl,
2331 DerefState>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002332
2333 /// See AbstractAttribute::trackStatistics()
2334 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002335 STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002336 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002337};
2338
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002339/// Dereferenceable attribute for an argument
2340struct AADereferenceableArgument final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002341 : AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl,
2342 DerefState> {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002343 AADereferenceableArgument(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002344 : AAArgumentFromCallSiteArguments<AADereferenceable,
2345 AADereferenceableImpl, DerefState>(
2346 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002347
2348 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002349 void trackStatistics() const override {
Johannes Doerfert169af992019-08-20 06:09:56 +00002350 STATS_DECLTRACK_ARG_ATTR(dereferenceable)
2351 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002352};
2353
Hideto Ueno19c07af2019-07-23 08:16:17 +00002354/// Dereferenceable attribute for a call site argument.
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002355struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002356 AADereferenceableCallSiteArgument(const IRPosition &IRP)
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002357 : AADereferenceableFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002358
2359 /// See AbstractAttribute::trackStatistics()
2360 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002361 STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002362 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002363};
2364
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002365/// Dereferenceable attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002366struct AADereferenceableCallSiteReturned final : AADereferenceableImpl {
2367 AADereferenceableCallSiteReturned(const IRPosition &IRP)
2368 : AADereferenceableImpl(IRP) {}
2369
2370 /// See AbstractAttribute::initialize(...).
2371 void initialize(Attributor &A) override {
2372 AADereferenceableImpl::initialize(A);
2373 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002374 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002375 indicatePessimisticFixpoint();
2376 }
2377
2378 /// See AbstractAttribute::updateImpl(...).
2379 ChangeStatus updateImpl(Attributor &A) override {
2380 // TODO: Once we have call site specific value information we can provide
2381 // call site specific liveness information and then it makes
2382 // sense to specialize attributes for call sites arguments instead of
2383 // redirecting requests to the callee argument.
2384 Function *F = getAssociatedFunction();
2385 const IRPosition &FnPos = IRPosition::returned(*F);
2386 auto &FnAA = A.getAAFor<AADereferenceable>(*this, FnPos);
2387 return clampStateAndIndicateChange(
2388 getState(), static_cast<const DerefState &>(FnAA.getState()));
2389 }
2390
2391 /// See AbstractAttribute::trackStatistics()
2392 void trackStatistics() const override {
2393 STATS_DECLTRACK_CS_ATTR(dereferenceable);
2394 }
2395};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002396
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002397// ------------------------ Align Argument Attribute ------------------------
2398
Johannes Doerfert344d0382019-08-07 22:34:26 +00002399struct AAAlignImpl : AAAlign {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002400 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002401
2402 // Max alignemnt value allowed in IR
2403 static const unsigned MAX_ALIGN = 1U << 29;
2404
Johannes Doerfert234eda52019-08-16 19:51:23 +00002405 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00002406 void initialize(Attributor &A) override {
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002407 takeAssumedMinimum(MAX_ALIGN);
2408
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002409 SmallVector<Attribute, 4> Attrs;
2410 getAttrs({Attribute::Alignment}, Attrs);
2411 for (const Attribute &Attr : Attrs)
2412 takeKnownMaximum(Attr.getValueAsInt());
Johannes Doerfert97fd5822019-09-04 16:26:20 +00002413
2414 if (getIRPosition().isFnInterfaceKind() &&
2415 (!getAssociatedFunction() ||
2416 !getAssociatedFunction()->hasExactDefinition()))
2417 indicatePessimisticFixpoint();
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002418 }
2419
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002420 /// See AbstractAttribute::manifest(...).
2421 ChangeStatus manifest(Attributor &A) override {
2422 ChangeStatus Changed = ChangeStatus::UNCHANGED;
2423
2424 // Check for users that allow alignment annotations.
2425 Value &AnchorVal = getIRPosition().getAnchorValue();
2426 for (const Use &U : AnchorVal.uses()) {
2427 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
2428 if (SI->getPointerOperand() == &AnchorVal)
2429 if (SI->getAlignment() < getAssumedAlign()) {
2430 STATS_DECLTRACK(AAAlign, Store,
2431 "Number of times alignemnt added to a store");
2432 SI->setAlignment(getAssumedAlign());
2433 Changed = ChangeStatus::CHANGED;
2434 }
2435 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
2436 if (LI->getPointerOperand() == &AnchorVal)
2437 if (LI->getAlignment() < getAssumedAlign()) {
2438 LI->setAlignment(getAssumedAlign());
2439 STATS_DECLTRACK(AAAlign, Load,
2440 "Number of times alignemnt added to a load");
2441 Changed = ChangeStatus::CHANGED;
2442 }
2443 }
2444 }
2445
Johannes Doerfert81df4522019-08-30 15:22:28 +00002446 return AAAlign::manifest(A) | Changed;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002447 }
2448
Johannes Doerfert81df4522019-08-30 15:22:28 +00002449 // TODO: Provide a helper to determine the implied ABI alignment and check in
2450 // the existing manifest method and a new one for AAAlignImpl that value
2451 // to avoid making the alignment explicit if it did not improve.
2452
2453 /// See AbstractAttribute::getDeducedAttributes
2454 virtual void
2455 getDeducedAttributes(LLVMContext &Ctx,
2456 SmallVectorImpl<Attribute> &Attrs) const override {
2457 if (getAssumedAlign() > 1)
2458 Attrs.emplace_back(Attribute::getWithAlignment(Ctx, getAssumedAlign()));
2459 }
2460
2461 /// See AbstractAttribute::getAsStr().
2462 const std::string getAsStr() const override {
2463 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
2464 "-" + std::to_string(getAssumedAlign()) + ">")
2465 : "unknown-align";
2466 }
2467};
2468
2469/// Align attribute for a floating value.
2470struct AAAlignFloating : AAAlignImpl {
2471 AAAlignFloating(const IRPosition &IRP) : AAAlignImpl(IRP) {}
2472
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002473 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert234eda52019-08-16 19:51:23 +00002474 ChangeStatus updateImpl(Attributor &A) override {
2475 const DataLayout &DL = A.getDataLayout();
2476
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002477 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
2478 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002479 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V));
2480 if (!Stripped && this == &AA) {
Johannes Doerfert234eda52019-08-16 19:51:23 +00002481 // Use only IR information if we did not strip anything.
2482 T.takeKnownMaximum(V.getPointerAlignment(DL));
2483 T.indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00002484 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002485 // Use abstract attribute information.
2486 const AAAlign::StateType &DS =
2487 static_cast<const AAAlign::StateType &>(AA.getState());
2488 T ^= DS;
Johannes Doerfert234eda52019-08-16 19:51:23 +00002489 }
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002490 return T.isValidState();
Johannes Doerfert234eda52019-08-16 19:51:23 +00002491 };
2492
2493 StateType T;
2494 if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
2495 VisitValueCB))
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002496 return indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00002497
Johannes Doerfert028b2aa2019-08-20 05:57:01 +00002498 // TODO: If we know we visited all incoming values, thus no are assumed
2499 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +00002500 return clampStateAndIndicateChange(getState(), T);
2501 }
2502
2503 /// See AbstractAttribute::trackStatistics()
2504 void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
2505};
2506
2507/// Align attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002508struct AAAlignReturned final
2509 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00002510 AAAlignReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002511 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002512
2513 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002514 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002515};
2516
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002517/// Align attribute for function argument.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002518struct AAAlignArgument final
2519 : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00002520 AAAlignArgument(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002521 : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002522
2523 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert169af992019-08-20 06:09:56 +00002524 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002525};
2526
Johannes Doerfert234eda52019-08-16 19:51:23 +00002527struct AAAlignCallSiteArgument final : AAAlignFloating {
2528 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002529
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002530 /// See AbstractAttribute::manifest(...).
2531 ChangeStatus manifest(Attributor &A) override {
2532 return AAAlignImpl::manifest(A);
2533 }
2534
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002535 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002536 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002537};
2538
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002539/// Align attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002540struct AAAlignCallSiteReturned final : AAAlignImpl {
2541 AAAlignCallSiteReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {}
2542
2543 /// See AbstractAttribute::initialize(...).
2544 void initialize(Attributor &A) override {
2545 AAAlignImpl::initialize(A);
2546 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002547 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002548 indicatePessimisticFixpoint();
2549 }
2550
2551 /// See AbstractAttribute::updateImpl(...).
2552 ChangeStatus updateImpl(Attributor &A) override {
2553 // TODO: Once we have call site specific value information we can provide
2554 // call site specific liveness information and then it makes
2555 // sense to specialize attributes for call sites arguments instead of
2556 // redirecting requests to the callee argument.
2557 Function *F = getAssociatedFunction();
2558 const IRPosition &FnPos = IRPosition::returned(*F);
2559 auto &FnAA = A.getAAFor<AAAlign>(*this, FnPos);
2560 return clampStateAndIndicateChange(
2561 getState(), static_cast<const AAAlign::StateType &>(FnAA.getState()));
2562 }
2563
2564 /// See AbstractAttribute::trackStatistics()
2565 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
2566};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002567
Johannes Doerferte83f3032019-08-05 23:22:05 +00002568/// ------------------ Function No-Return Attribute ----------------------------
Johannes Doerfert344d0382019-08-07 22:34:26 +00002569struct AANoReturnImpl : public AANoReturn {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002570 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
Johannes Doerferte83f3032019-08-05 23:22:05 +00002571
Johannes Doerferte83f3032019-08-05 23:22:05 +00002572 /// See AbstractAttribute::getAsStr().
2573 const std::string getAsStr() const override {
2574 return getAssumed() ? "noreturn" : "may-return";
2575 }
2576
Johannes Doerferte83f3032019-08-05 23:22:05 +00002577 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00002578 virtual ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00002579 auto CheckForNoReturn = [](Instruction &) { return false; };
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002580 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
Johannes Doerfertd0f64002019-08-06 00:32:43 +00002581 {(unsigned)Instruction::Ret}))
Johannes Doerferte83f3032019-08-05 23:22:05 +00002582 return indicatePessimisticFixpoint();
Johannes Doerferte83f3032019-08-05 23:22:05 +00002583 return ChangeStatus::UNCHANGED;
2584 }
2585};
2586
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002587struct AANoReturnFunction final : AANoReturnImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002588 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002589
2590 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002591 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002592};
2593
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002594/// NoReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002595struct AANoReturnCallSite final : AANoReturnImpl {
2596 AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
2597
2598 /// See AbstractAttribute::initialize(...).
2599 void initialize(Attributor &A) override {
2600 AANoReturnImpl::initialize(A);
2601 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002602 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002603 indicatePessimisticFixpoint();
2604 }
2605
2606 /// See AbstractAttribute::updateImpl(...).
2607 ChangeStatus updateImpl(Attributor &A) override {
2608 // TODO: Once we have call site specific value information we can provide
2609 // call site specific liveness information and then it makes
2610 // sense to specialize attributes for call sites arguments instead of
2611 // redirecting requests to the callee argument.
2612 Function *F = getAssociatedFunction();
2613 const IRPosition &FnPos = IRPosition::function(*F);
2614 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos);
2615 return clampStateAndIndicateChange(
2616 getState(),
2617 static_cast<const AANoReturn::StateType &>(FnAA.getState()));
2618 }
2619
2620 /// See AbstractAttribute::trackStatistics()
2621 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
2622};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002623
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002624/// ----------------------- Variable Capturing ---------------------------------
2625
2626/// A class to hold the state of for no-capture attributes.
2627struct AANoCaptureImpl : public AANoCapture {
2628 AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {}
2629
2630 /// See AbstractAttribute::initialize(...).
2631 void initialize(Attributor &A) override {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002632 AANoCapture::initialize(A);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002633
2634 const IRPosition &IRP = getIRPosition();
2635 const Function *F =
2636 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
2637
2638 // Check what state the associated function can actually capture.
2639 if (F)
2640 determineFunctionCaptureCapabilities(*F, *this);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002641 else
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002642 indicatePessimisticFixpoint();
2643 }
2644
2645 /// See AbstractAttribute::updateImpl(...).
2646 ChangeStatus updateImpl(Attributor &A) override;
2647
2648 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
2649 virtual void
2650 getDeducedAttributes(LLVMContext &Ctx,
2651 SmallVectorImpl<Attribute> &Attrs) const override {
2652 if (!isAssumedNoCaptureMaybeReturned())
2653 return;
2654
Hideto Ueno37367642019-09-11 06:52:11 +00002655 if (getArgNo() >= 0) {
2656 if (isAssumedNoCapture())
2657 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
2658 else if (ManifestInternal)
2659 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
2660 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002661 }
2662
2663 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
2664 /// depending on the ability of the function associated with \p IRP to capture
2665 /// state in memory and through "returning/throwing", respectively.
2666 static void determineFunctionCaptureCapabilities(const Function &F,
2667 IntegerState &State) {
2668 // TODO: Once we have memory behavior attributes we should use them here.
2669
2670 // If we know we cannot communicate or write to memory, we do not care about
2671 // ptr2int anymore.
2672 if (F.onlyReadsMemory() && F.doesNotThrow() &&
2673 F.getReturnType()->isVoidTy()) {
2674 State.addKnownBits(NO_CAPTURE);
2675 return;
2676 }
2677
2678 // A function cannot capture state in memory if it only reads memory, it can
2679 // however return/throw state and the state might be influenced by the
2680 // pointer value, e.g., loading from a returned pointer might reveal a bit.
2681 if (F.onlyReadsMemory())
2682 State.addKnownBits(NOT_CAPTURED_IN_MEM);
2683
2684 // A function cannot communicate state back if it does not through
2685 // exceptions and doesn not return values.
2686 if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
2687 State.addKnownBits(NOT_CAPTURED_IN_RET);
2688 }
2689
2690 /// See AbstractState::getAsStr().
2691 const std::string getAsStr() const override {
2692 if (isKnownNoCapture())
2693 return "known not-captured";
2694 if (isAssumedNoCapture())
2695 return "assumed not-captured";
2696 if (isKnownNoCaptureMaybeReturned())
2697 return "known not-captured-maybe-returned";
2698 if (isAssumedNoCaptureMaybeReturned())
2699 return "assumed not-captured-maybe-returned";
2700 return "assumed-captured";
2701 }
2702};
2703
2704/// Attributor-aware capture tracker.
2705struct AACaptureUseTracker final : public CaptureTracker {
2706
2707 /// Create a capture tracker that can lookup in-flight abstract attributes
2708 /// through the Attributor \p A.
2709 ///
2710 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
2711 /// search is stopped. If a use leads to a return instruction,
2712 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
2713 /// If a use leads to a ptr2int which may capture the value,
2714 /// \p CapturedInInteger is set. If a use is found that is currently assumed
2715 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
2716 /// set. All values in \p PotentialCopies are later tracked as well. For every
2717 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
2718 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
2719 /// conservatively set to true.
2720 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
2721 const AAIsDead &IsDeadAA, IntegerState &State,
2722 SmallVectorImpl<const Value *> &PotentialCopies,
2723 unsigned &RemainingUsesToExplore)
2724 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
2725 PotentialCopies(PotentialCopies),
2726 RemainingUsesToExplore(RemainingUsesToExplore) {}
2727
2728 /// Determine if \p V maybe captured. *Also updates the state!*
2729 bool valueMayBeCaptured(const Value *V) {
2730 if (V->getType()->isPointerTy()) {
2731 PointerMayBeCaptured(V, this);
2732 } else {
2733 State.indicatePessimisticFixpoint();
2734 }
2735 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
2736 }
2737
2738 /// See CaptureTracker::tooManyUses().
2739 void tooManyUses() override {
2740 State.removeAssumedBits(AANoCapture::NO_CAPTURE);
2741 }
2742
2743 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
2744 if (CaptureTracker::isDereferenceableOrNull(O, DL))
2745 return true;
2746 const auto &DerefAA =
2747 A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O));
2748 return DerefAA.getAssumedDereferenceableBytes();
2749 }
2750
2751 /// See CaptureTracker::captured(...).
2752 bool captured(const Use *U) override {
2753 Instruction *UInst = cast<Instruction>(U->getUser());
2754 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
2755 << "\n");
2756
2757 // Because we may reuse the tracker multiple times we keep track of the
2758 // number of explored uses ourselves as well.
2759 if (RemainingUsesToExplore-- == 0) {
2760 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
2761 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2762 /* Return */ true);
2763 }
2764
2765 // Deal with ptr2int by following uses.
2766 if (isa<PtrToIntInst>(UInst)) {
2767 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
2768 return valueMayBeCaptured(UInst);
2769 }
2770
2771 // Explicitly catch return instructions.
2772 if (isa<ReturnInst>(UInst))
2773 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2774 /* Return */ true);
2775
2776 // For now we only use special logic for call sites. However, the tracker
2777 // itself knows about a lot of other non-capturing cases already.
2778 CallSite CS(UInst);
2779 if (!CS || !CS.isArgOperand(U))
2780 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2781 /* Return */ true);
2782
2783 unsigned ArgNo = CS.getArgumentNo(U);
2784 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
2785 // If we have a abstract no-capture attribute for the argument we can use
2786 // it to justify a non-capture attribute here. This allows recursion!
2787 auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos);
2788 if (ArgNoCaptureAA.isAssumedNoCapture())
2789 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2790 /* Return */ false);
2791 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
2792 addPotentialCopy(CS);
2793 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2794 /* Return */ false);
2795 }
2796
2797 // Lastly, we could not find a reason no-capture can be assumed so we don't.
2798 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2799 /* Return */ true);
2800 }
2801
2802 /// Register \p CS as potential copy of the value we are checking.
2803 void addPotentialCopy(CallSite CS) {
2804 PotentialCopies.push_back(CS.getInstruction());
2805 }
2806
2807 /// See CaptureTracker::shouldExplore(...).
2808 bool shouldExplore(const Use *U) override {
2809 // Check liveness.
2810 return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser()));
2811 }
2812
2813 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
2814 /// \p CapturedInRet, then return the appropriate value for use in the
2815 /// CaptureTracker::captured() interface.
2816 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
2817 bool CapturedInRet) {
2818 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
2819 << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
2820 if (CapturedInMem)
2821 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
2822 if (CapturedInInt)
2823 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
2824 if (CapturedInRet)
2825 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
2826 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
2827 }
2828
2829private:
2830 /// The attributor providing in-flight abstract attributes.
2831 Attributor &A;
2832
2833 /// The abstract attribute currently updated.
2834 AANoCapture &NoCaptureAA;
2835
2836 /// The abstract liveness state.
2837 const AAIsDead &IsDeadAA;
2838
2839 /// The state currently updated.
2840 IntegerState &State;
2841
2842 /// Set of potential copies of the tracked value.
2843 SmallVectorImpl<const Value *> &PotentialCopies;
2844
2845 /// Global counter to limit the number of explored uses.
2846 unsigned &RemainingUsesToExplore;
2847};
2848
2849ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
2850 const IRPosition &IRP = getIRPosition();
2851 const Value *V =
2852 getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue();
2853 if (!V)
2854 return indicatePessimisticFixpoint();
2855
2856 const Function *F =
2857 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
2858 assert(F && "Expected a function!");
2859 const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, IRPosition::function(*F));
2860
2861 AANoCapture::StateType T;
2862 // TODO: Once we have memory behavior attributes we should use them here
2863 // similar to the reasoning in
2864 // AANoCaptureImpl::determineFunctionCaptureCapabilities(...).
2865
2866 // TODO: Use the AAReturnedValues to learn if the argument can return or
2867 // not.
2868
2869 // Use the CaptureTracker interface and logic with the specialized tracker,
2870 // defined in AACaptureUseTracker, that can look at in-flight abstract
2871 // attributes and directly updates the assumed state.
2872 SmallVector<const Value *, 4> PotentialCopies;
2873 unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore;
2874 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
2875 RemainingUsesToExplore);
2876
2877 // Check all potential copies of the associated value until we can assume
2878 // none will be captured or we have to assume at least one might be.
2879 unsigned Idx = 0;
2880 PotentialCopies.push_back(V);
2881 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
2882 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
2883
2884 AAAlign::StateType &S = getState();
2885 auto Assumed = S.getAssumed();
2886 S.intersectAssumedBits(T.getAssumed());
2887 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
2888 : ChangeStatus::CHANGED;
2889}
2890
2891/// NoCapture attribute for function arguments.
2892struct AANoCaptureArgument final : AANoCaptureImpl {
2893 AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2894
2895 /// See AbstractAttribute::trackStatistics()
2896 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
2897};
2898
2899/// NoCapture attribute for call site arguments.
2900struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
2901 AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2902
2903 /// See AbstractAttribute::updateImpl(...).
2904 ChangeStatus updateImpl(Attributor &A) override {
2905 // TODO: Once we have call site specific value information we can provide
2906 // call site specific liveness information and then it makes
2907 // sense to specialize attributes for call sites arguments instead of
2908 // redirecting requests to the callee argument.
2909 Argument *Arg = getAssociatedArgument();
2910 if (!Arg)
2911 return indicatePessimisticFixpoint();
2912 const IRPosition &ArgPos = IRPosition::argument(*Arg);
2913 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos);
2914 return clampStateAndIndicateChange(
2915 getState(),
2916 static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
2917 }
2918
2919 /// See AbstractAttribute::trackStatistics()
2920 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
2921};
2922
2923/// NoCapture attribute for floating values.
2924struct AANoCaptureFloating final : AANoCaptureImpl {
2925 AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2926
2927 /// See AbstractAttribute::trackStatistics()
2928 void trackStatistics() const override {
2929 STATS_DECLTRACK_FLOATING_ATTR(nocapture)
2930 }
2931};
2932
2933/// NoCapture attribute for function return value.
2934struct AANoCaptureReturned final : AANoCaptureImpl {
2935 AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {
2936 llvm_unreachable("NoCapture is not applicable to function returns!");
2937 }
2938
2939 /// See AbstractAttribute::initialize(...).
2940 void initialize(Attributor &A) override {
2941 llvm_unreachable("NoCapture is not applicable to function returns!");
2942 }
2943
2944 /// See AbstractAttribute::updateImpl(...).
2945 ChangeStatus updateImpl(Attributor &A) override {
2946 llvm_unreachable("NoCapture is not applicable to function returns!");
2947 }
2948
2949 /// See AbstractAttribute::trackStatistics()
2950 void trackStatistics() const override {}
2951};
2952
2953/// NoCapture attribute deduction for a call site return value.
2954struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
2955 AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2956
2957 /// See AbstractAttribute::trackStatistics()
2958 void trackStatistics() const override {
2959 STATS_DECLTRACK_CSRET_ATTR(nocapture)
2960 }
2961};
2962
Hideto Uenof2b9dc42019-09-07 07:03:05 +00002963/// ------------------ Value Simplify Attribute ----------------------------
2964struct AAValueSimplifyImpl : AAValueSimplify {
2965 AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {}
2966
2967 /// See AbstractAttribute::getAsStr().
2968 const std::string getAsStr() const override {
2969 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
2970 : "not-simple";
2971 }
2972
2973 /// See AbstractAttribute::trackStatistics()
2974 void trackStatistics() const override {}
2975
2976 /// See AAValueSimplify::getAssumedSimplifiedValue()
2977 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
2978 if (!getAssumed())
2979 return const_cast<Value *>(&getAssociatedValue());
2980 return SimplifiedAssociatedValue;
2981 }
2982 void initialize(Attributor &A) override {}
2983
2984 /// Helper function for querying AAValueSimplify and updating candicate.
2985 /// \param QueryingValue Value trying to unify with SimplifiedValue
2986 /// \param AccumulatedSimplifiedValue Current simplification result.
2987 static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
2988 Value &QueryingValue,
2989 Optional<Value *> &AccumulatedSimplifiedValue) {
2990 // FIXME: Add a typecast support.
2991
2992 auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>(
2993 QueryingAA, IRPosition::value(QueryingValue));
2994
2995 Optional<Value *> QueryingValueSimplified =
2996 ValueSimpifyAA.getAssumedSimplifiedValue(A);
2997
2998 if (!QueryingValueSimplified.hasValue())
2999 return true;
3000
3001 if (!QueryingValueSimplified.getValue())
3002 return false;
3003
3004 Value &QueryingValueSimplifiedUnwrapped =
3005 *QueryingValueSimplified.getValue();
3006
3007 if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
3008 return true;
3009
3010 if (AccumulatedSimplifiedValue.hasValue())
3011 return AccumulatedSimplifiedValue == QueryingValueSimplified;
3012
3013 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue
3014 << " is assumed to be "
3015 << QueryingValueSimplifiedUnwrapped << "\n");
3016
3017 AccumulatedSimplifiedValue = QueryingValueSimplified;
3018 return true;
3019 }
3020
3021 /// See AbstractAttribute::manifest(...).
3022 ChangeStatus manifest(Attributor &A) override {
3023 ChangeStatus Changed = ChangeStatus::UNCHANGED;
3024
3025 if (!SimplifiedAssociatedValue.hasValue() ||
3026 !SimplifiedAssociatedValue.getValue())
3027 return Changed;
3028
3029 if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) {
3030 // We can replace the AssociatedValue with the constant.
3031 Value &V = getAssociatedValue();
3032 if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
3033 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C
3034 << "\n");
3035 V.replaceAllUsesWith(C);
3036 Changed = ChangeStatus::CHANGED;
3037 }
3038 }
3039
3040 return Changed | AAValueSimplify::manifest(A);
3041 }
3042
3043protected:
3044 // An assumed simplified value. Initially, it is set to Optional::None, which
3045 // means that the value is not clear under current assumption. If in the
3046 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
3047 // returns orignal associated value.
3048 Optional<Value *> SimplifiedAssociatedValue;
3049};
3050
3051struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
3052 AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3053
3054 /// See AbstractAttribute::updateImpl(...).
3055 ChangeStatus updateImpl(Attributor &A) override {
3056 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3057
3058 auto PredForCallSite = [&](CallSite CS) {
3059 return checkAndUpdate(A, *this, *CS.getArgOperand(getArgNo()),
3060 SimplifiedAssociatedValue);
3061 };
3062
3063 if (!A.checkForAllCallSites(PredForCallSite, *this, true))
3064 return indicatePessimisticFixpoint();
3065
3066 // If a candicate was found in this update, return CHANGED.
3067 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3068 ? ChangeStatus::UNCHANGED
3069 : ChangeStatus ::CHANGED;
3070 }
3071
3072 /// See AbstractAttribute::trackStatistics()
3073 void trackStatistics() const override {
3074 STATS_DECLTRACK_ARG_ATTR(value_simplify)
3075 }
3076};
3077
3078struct AAValueSimplifyReturned : AAValueSimplifyImpl {
3079 AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3080
3081 /// See AbstractAttribute::updateImpl(...).
3082 ChangeStatus updateImpl(Attributor &A) override {
3083 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3084
3085 auto PredForReturned = [&](Value &V) {
3086 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
3087 };
3088
3089 if (!A.checkForAllReturnedValues(PredForReturned, *this))
3090 return indicatePessimisticFixpoint();
3091
3092 // If a candicate was found in this update, return CHANGED.
3093 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3094 ? ChangeStatus::UNCHANGED
3095 : ChangeStatus ::CHANGED;
3096 }
3097 /// See AbstractAttribute::trackStatistics()
3098 void trackStatistics() const override {
3099 STATS_DECLTRACK_FNRET_ATTR(value_simplify)
3100 }
3101};
3102
3103struct AAValueSimplifyFloating : AAValueSimplifyImpl {
3104 AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3105
3106 /// See AbstractAttribute::initialize(...).
3107 void initialize(Attributor &A) override {
3108 Value &V = getAnchorValue();
3109
3110 // TODO: add other stuffs
3111 if (isa<Constant>(V) || isa<UndefValue>(V))
3112 indicatePessimisticFixpoint();
3113 }
3114
3115 /// See AbstractAttribute::updateImpl(...).
3116 ChangeStatus updateImpl(Attributor &A) override {
3117 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3118
3119 auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool {
3120 auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V));
3121 if (!Stripped && this == &AA) {
3122 // TODO: Look the instruction and check recursively.
3123 LLVM_DEBUG(
3124 dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : "
3125 << V << "\n");
3126 indicatePessimisticFixpoint();
3127 return false;
3128 }
3129 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
3130 };
3131
3132 if (!genericValueTraversal<AAValueSimplify, BooleanState>(
3133 A, getIRPosition(), *this, static_cast<BooleanState &>(*this),
3134 VisitValueCB))
3135 return indicatePessimisticFixpoint();
3136
3137 // If a candicate was found in this update, return CHANGED.
3138
3139 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3140 ? ChangeStatus::UNCHANGED
3141 : ChangeStatus ::CHANGED;
3142 }
3143
3144 /// See AbstractAttribute::trackStatistics()
3145 void trackStatistics() const override {
3146 STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
3147 }
3148};
3149
3150struct AAValueSimplifyFunction : AAValueSimplifyImpl {
3151 AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3152
3153 /// See AbstractAttribute::initialize(...).
3154 void initialize(Attributor &A) override {
3155 SimplifiedAssociatedValue = &getAnchorValue();
3156 indicateOptimisticFixpoint();
3157 }
3158 /// See AbstractAttribute::initialize(...).
3159 ChangeStatus updateImpl(Attributor &A) override {
3160 llvm_unreachable(
3161 "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
3162 }
3163 /// See AbstractAttribute::trackStatistics()
3164 void trackStatistics() const override {
3165 STATS_DECLTRACK_FN_ATTR(value_simplify)
3166 }
3167};
3168
3169struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
3170 AAValueSimplifyCallSite(const IRPosition &IRP)
3171 : AAValueSimplifyFunction(IRP) {}
3172 /// See AbstractAttribute::trackStatistics()
3173 void trackStatistics() const override {
3174 STATS_DECLTRACK_CS_ATTR(value_simplify)
3175 }
3176};
3177
3178struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
3179 AAValueSimplifyCallSiteReturned(const IRPosition &IRP)
3180 : AAValueSimplifyReturned(IRP) {}
3181
3182 void trackStatistics() const override {
3183 STATS_DECLTRACK_CSRET_ATTR(value_simplify)
3184 }
3185};
3186struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
3187 AAValueSimplifyCallSiteArgument(const IRPosition &IRP)
3188 : AAValueSimplifyFloating(IRP) {}
3189
3190 void trackStatistics() const override {
3191 STATS_DECLTRACK_CSARG_ATTR(value_simplify)
3192 }
3193};
3194
Stefan Stipanovic431141c2019-09-15 21:47:41 +00003195/// ----------------------- Heap-To-Stack Conversion ---------------------------
3196struct AAHeapToStackImpl : public AAHeapToStack {
3197 AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {}
3198
3199 const std::string getAsStr() const override {
3200 return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
3201 }
3202
3203 ChangeStatus manifest(Attributor &A) override {
3204 assert(getState().isValidState() &&
3205 "Attempted to manifest an invalid state!");
3206
3207 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3208 Function *F = getAssociatedFunction();
3209 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
3210
3211 for (Instruction *MallocCall : MallocCalls) {
3212 // This malloc cannot be replaced.
3213 if (BadMallocCalls.count(MallocCall))
3214 continue;
3215
3216 for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
3217 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
3218 A.deleteAfterManifest(*FreeCall);
3219 HasChanged = ChangeStatus::CHANGED;
3220 }
3221
3222 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
3223 << "\n");
3224
3225 Constant *Size;
3226 if (isCallocLikeFn(MallocCall, TLI)) {
3227 auto *Num = cast<ConstantInt>(MallocCall->getOperand(0));
3228 auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1));
3229 APInt TotalSize = SizeT->getValue() * Num->getValue();
3230 Size =
3231 ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize);
3232 } else {
3233 Size = cast<ConstantInt>(MallocCall->getOperand(0));
3234 }
3235
3236 unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
3237 Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
3238 Size, "", MallocCall->getNextNode());
3239
3240 if (AI->getType() != MallocCall->getType())
3241 AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
3242 AI->getNextNode());
3243
3244 MallocCall->replaceAllUsesWith(AI);
3245
3246 if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
3247 auto *NBB = II->getNormalDest();
3248 BranchInst::Create(NBB, MallocCall->getParent());
3249 A.deleteAfterManifest(*MallocCall);
3250 } else {
3251 A.deleteAfterManifest(*MallocCall);
3252 }
3253
3254 if (isCallocLikeFn(MallocCall, TLI)) {
3255 auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
3256 AI->getNextNode());
3257 Value *Ops[] = {
3258 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
3259 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
3260
3261 Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
3262 Module *M = F->getParent();
3263 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
3264 CallInst::Create(Fn, Ops, "", BI->getNextNode());
3265 }
3266 HasChanged = ChangeStatus::CHANGED;
3267 }
3268
3269 return HasChanged;
3270 }
3271
3272 /// Collection of all malloc calls in a function.
3273 SmallSetVector<Instruction *, 4> MallocCalls;
3274
3275 /// Collection of malloc calls that cannot be converted.
3276 DenseSet<const Instruction *> BadMallocCalls;
3277
3278 /// A map for each malloc call to the set of associated free calls.
3279 DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
3280
3281 ChangeStatus updateImpl(Attributor &A) override;
3282};
3283
3284ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
3285 const Function *F = getAssociatedFunction();
3286 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
3287
3288 auto UsesCheck = [&](Instruction &I) {
3289 SmallPtrSet<const Use *, 8> Visited;
3290 SmallVector<const Use *, 8> Worklist;
3291
3292 for (Use &U : I.uses())
3293 Worklist.push_back(&U);
3294
3295 while (!Worklist.empty()) {
3296 const Use *U = Worklist.pop_back_val();
3297 if (!Visited.insert(U).second)
3298 continue;
3299
3300 auto *UserI = U->getUser();
3301
3302 if (isa<LoadInst>(UserI) || isa<StoreInst>(UserI))
3303 continue;
3304
3305 // NOTE: Right now, if a function that has malloc pointer as an argument
3306 // frees memory, we assume that the malloc pointer is freed.
3307
3308 // TODO: Add nofree callsite argument attribute to indicate that pointer
3309 // argument is not freed.
3310 if (auto *CB = dyn_cast<CallBase>(UserI)) {
3311 if (!CB->isArgOperand(U))
3312 continue;
3313
3314 if (CB->isLifetimeStartOrEnd())
3315 continue;
3316
3317 // Record malloc.
3318 if (isFreeCall(UserI, TLI)) {
3319 FreesForMalloc[&I].insert(
3320 cast<Instruction>(const_cast<User *>(UserI)));
3321 continue;
3322 }
3323
3324 // If a function does not free memory we are fine
3325 const auto &NoFreeAA =
3326 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(*CB));
3327
3328 unsigned ArgNo = U - CB->arg_begin();
3329 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
3330 *this, IRPosition::callsite_argument(*CB, ArgNo));
3331
3332 if (!NoCaptureAA.isAssumedNoCapture() || !NoFreeAA.isAssumedNoFree()) {
3333 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
3334 return false;
3335 }
3336 continue;
3337 }
3338
3339 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI)) {
3340 for (Use &U : UserI->uses())
3341 Worklist.push_back(&U);
3342 continue;
3343 }
3344
3345 // Unknown user.
3346 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
3347 return false;
3348 }
3349 return true;
3350 };
3351
3352 auto MallocCallocCheck = [&](Instruction &I) {
3353 if (isMallocLikeFn(&I, TLI)) {
3354 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
3355 if (!Size->getValue().sle(MaxHeapToStackSize))
3356 return true;
3357 } else if (isCallocLikeFn(&I, TLI)) {
3358 bool Overflow = false;
3359 if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
3360 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
3361 if (!(Size->getValue().umul_ov(Num->getValue(), Overflow))
3362 .sle(MaxHeapToStackSize))
3363 if (!Overflow)
3364 return true;
3365 } else {
3366 BadMallocCalls.insert(&I);
3367 return true;
3368 }
3369
3370 if (BadMallocCalls.count(&I))
3371 return true;
3372
3373 if (UsesCheck(I))
3374 MallocCalls.insert(&I);
3375 else
3376 BadMallocCalls.insert(&I);
3377 return true;
3378 };
3379
3380 size_t NumBadMallocs = BadMallocCalls.size();
3381
3382 A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
3383
3384 if (NumBadMallocs != BadMallocCalls.size())
3385 return ChangeStatus::CHANGED;
3386
3387 return ChangeStatus::UNCHANGED;
3388}
3389
3390struct AAHeapToStackFunction final : public AAHeapToStackImpl {
3391 AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {}
3392
3393 /// See AbstractAttribute::trackStatistics()
3394 void trackStatistics() const override {
3395 STATS_DECL(MallocCalls, Function,
3396 "Number of MallocCalls converted to allocas");
3397 BUILD_STAT_NAME(MallocCalls, Function) += MallocCalls.size();
3398 }
3399};
3400
Johannes Doerfertaade7822019-06-05 03:02:24 +00003401/// ----------------------------------------------------------------------------
3402/// Attributor
3403/// ----------------------------------------------------------------------------
3404
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003405bool Attributor::isAssumedDead(const AbstractAttribute &AA,
3406 const AAIsDead *LivenessAA) {
3407 const Instruction *CtxI = AA.getIRPosition().getCtxI();
3408 if (!CtxI)
3409 return false;
3410
3411 if (!LivenessAA)
3412 LivenessAA =
Johannes Doerfert19b00432019-08-26 17:48:05 +00003413 &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()),
3414 /* TrackDependence */ false);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003415
3416 // Don't check liveness for AAIsDead.
3417 if (&AA == LivenessAA)
3418 return false;
3419
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003420 if (!LivenessAA->isAssumedDead(CtxI))
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003421 return false;
3422
Johannes Doerfert19b00432019-08-26 17:48:05 +00003423 // We actually used liveness information so we have to record a dependence.
3424 recordDependence(*LivenessAA, AA);
3425
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003426 return true;
3427}
3428
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003429bool Attributor::checkForAllCallSites(const function_ref<bool(CallSite)> &Pred,
Johannes Doerfert14a04932019-08-07 22:27:24 +00003430 const AbstractAttribute &QueryingAA,
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003431 bool RequireAllCallSites) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00003432 // We can try to determine information from
3433 // the call sites. However, this is only possible all call sites are known,
3434 // hence the function has internal linkage.
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003435 const IRPosition &IRP = QueryingAA.getIRPosition();
3436 const Function *AssociatedFunction = IRP.getAssociatedFunction();
3437 if (!AssociatedFunction)
3438 return false;
3439
3440 if (RequireAllCallSites && !AssociatedFunction->hasInternalLinkage()) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00003441 LLVM_DEBUG(
3442 dbgs()
Johannes Doerfert5304b722019-08-14 22:04:28 +00003443 << "[Attributor] Function " << AssociatedFunction->getName()
Hideto Ueno54869ec2019-07-15 06:49:04 +00003444 << " has no internal linkage, hence not all call sites are known\n");
3445 return false;
3446 }
3447
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003448 for (const Use &U : AssociatedFunction->uses()) {
Johannes Doerfertd98f9752019-08-21 21:48:56 +00003449 Instruction *I = dyn_cast<Instruction>(U.getUser());
3450 // TODO: Deal with abstract call sites here.
3451 if (!I)
3452 return false;
3453
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003454 Function *Caller = I->getFunction();
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003455
Johannes Doerfert19b00432019-08-26 17:48:05 +00003456 const auto &LivenessAA = getAAFor<AAIsDead>(
3457 QueryingAA, IRPosition::function(*Caller), /* TrackDependence */ false);
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003458
3459 // Skip dead calls.
Johannes Doerfert19b00432019-08-26 17:48:05 +00003460 if (LivenessAA.isAssumedDead(I)) {
3461 // We actually used liveness information so we have to record a
3462 // dependence.
3463 recordDependence(LivenessAA, QueryingAA);
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003464 continue;
Johannes Doerfert19b00432019-08-26 17:48:05 +00003465 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00003466
3467 CallSite CS(U.getUser());
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003468 if (!CS || !CS.isCallee(&U)) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00003469 if (!RequireAllCallSites)
3470 continue;
3471
Johannes Doerfert5304b722019-08-14 22:04:28 +00003472 LLVM_DEBUG(dbgs() << "[Attributor] User " << *U.getUser()
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003473 << " is an invalid use of "
3474 << AssociatedFunction->getName() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00003475 return false;
3476 }
3477
3478 if (Pred(CS))
3479 continue;
3480
Johannes Doerfert5304b722019-08-14 22:04:28 +00003481 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
Hideto Ueno54869ec2019-07-15 06:49:04 +00003482 << *CS.getInstruction() << "\n");
3483 return false;
3484 }
3485
3486 return true;
3487}
3488
Johannes Doerfert14a04932019-08-07 22:27:24 +00003489bool Attributor::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00003490 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00003491 &Pred,
3492 const AbstractAttribute &QueryingAA) {
3493
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003494 const IRPosition &IRP = QueryingAA.getIRPosition();
3495 // Since we need to provide return instructions we have to have an exact
3496 // definition.
3497 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003498 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00003499 return false;
3500
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003501 // If this is a call site query we use the call site specific return values
3502 // and liveness information.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003503 // TODO: use the function scope once we have call site AAReturnedValues.
3504 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003505 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003506 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003507 return false;
3508
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003509 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
Johannes Doerfert14a04932019-08-07 22:27:24 +00003510}
3511
3512bool Attributor::checkForAllReturnedValues(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003513 const function_ref<bool(Value &)> &Pred,
Johannes Doerfert14a04932019-08-07 22:27:24 +00003514 const AbstractAttribute &QueryingAA) {
3515
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003516 const IRPosition &IRP = QueryingAA.getIRPosition();
3517 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003518 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00003519 return false;
3520
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003521 // TODO: use the function scope once we have call site AAReturnedValues.
3522 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003523 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003524 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003525 return false;
3526
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003527 return AARetVal.checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00003528 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00003529 return Pred(RV);
3530 });
Johannes Doerfert14a04932019-08-07 22:27:24 +00003531}
3532
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003533bool Attributor::checkForAllInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003534 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00003535 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003536
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003537 const IRPosition &IRP = QueryingAA.getIRPosition();
3538 // Since we need to provide instructions we have to have an exact definition.
3539 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003540 if (!AssociatedFunction)
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003541 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003542
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003543 // TODO: use the function scope once we have call site AAReturnedValues.
3544 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert19b00432019-08-26 17:48:05 +00003545 const auto &LivenessAA =
3546 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
3547 bool AnyDead = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003548
3549 auto &OpcodeInstMap =
3550 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003551 for (unsigned Opcode : Opcodes) {
3552 for (Instruction *I : OpcodeInstMap[Opcode]) {
3553 // Skip dead instructions.
Johannes Doerfert19b00432019-08-26 17:48:05 +00003554 if (LivenessAA.isAssumedDead(I)) {
3555 AnyDead = true;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003556 continue;
Johannes Doerfert19b00432019-08-26 17:48:05 +00003557 }
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003558
3559 if (!Pred(*I))
3560 return false;
3561 }
3562 }
3563
Johannes Doerfert19b00432019-08-26 17:48:05 +00003564 // If we actually used liveness information so we have to record a dependence.
3565 if (AnyDead)
3566 recordDependence(LivenessAA, QueryingAA);
3567
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003568 return true;
3569}
3570
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003571bool Attributor::checkForAllReadWriteInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003572 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00003573 AbstractAttribute &QueryingAA) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003574
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003575 const Function *AssociatedFunction =
3576 QueryingAA.getIRPosition().getAssociatedFunction();
3577 if (!AssociatedFunction)
3578 return false;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003579
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003580 // TODO: use the function scope once we have call site AAReturnedValues.
3581 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
3582 const auto &LivenessAA =
3583 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
Johannes Doerfert19b00432019-08-26 17:48:05 +00003584 bool AnyDead = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003585
3586 for (Instruction *I :
3587 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003588 // Skip dead instructions.
Johannes Doerfert19b00432019-08-26 17:48:05 +00003589 if (LivenessAA.isAssumedDead(I)) {
3590 AnyDead = true;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003591 continue;
Johannes Doerfert19b00432019-08-26 17:48:05 +00003592 }
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003593
3594 if (!Pred(*I))
3595 return false;
3596 }
3597
Johannes Doerfert19b00432019-08-26 17:48:05 +00003598 // If we actually used liveness information so we have to record a dependence.
3599 if (AnyDead)
3600 recordDependence(LivenessAA, QueryingAA);
3601
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003602 return true;
3603}
3604
Johannes Doerfert2f622062019-09-04 16:35:20 +00003605ChangeStatus Attributor::run(Module &M) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00003606 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
3607 << AllAbstractAttributes.size()
3608 << " abstract attributes.\n");
3609
Stefan Stipanovic53605892019-06-27 11:27:54 +00003610 // Now that all abstract attributes are collected and initialized we start
3611 // the abstract analysis.
Johannes Doerfertaade7822019-06-05 03:02:24 +00003612
3613 unsigned IterationCounter = 1;
3614
3615 SmallVector<AbstractAttribute *, 64> ChangedAAs;
3616 SetVector<AbstractAttribute *> Worklist;
3617 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
3618
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003619 bool RecomputeDependences = false;
3620
Johannes Doerfertaade7822019-06-05 03:02:24 +00003621 do {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003622 // Remember the size to determine new attributes.
3623 size_t NumAAs = AllAbstractAttributes.size();
Johannes Doerfertaade7822019-06-05 03:02:24 +00003624 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
3625 << ", Worklist size: " << Worklist.size() << "\n");
3626
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003627 // If dependences (=QueryMap) are recomputed we have to look at all abstract
3628 // attributes again, regardless of what changed in the last iteration.
3629 if (RecomputeDependences) {
3630 LLVM_DEBUG(
3631 dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
3632 QueryMap.clear();
3633 ChangedAAs.clear();
3634 Worklist.insert(AllAbstractAttributes.begin(),
3635 AllAbstractAttributes.end());
3636 }
3637
Johannes Doerfertaade7822019-06-05 03:02:24 +00003638 // Add all abstract attributes that are potentially dependent on one that
3639 // changed to the work list.
3640 for (AbstractAttribute *ChangedAA : ChangedAAs) {
3641 auto &QuerriedAAs = QueryMap[ChangedAA];
3642 Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end());
3643 }
3644
Johannes Doerfertb504eb82019-08-26 18:55:47 +00003645 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
3646 << ", Worklist+Dependent size: " << Worklist.size()
3647 << "\n");
3648
Johannes Doerfertaade7822019-06-05 03:02:24 +00003649 // Reset the changed set.
3650 ChangedAAs.clear();
3651
3652 // Update all abstract attribute in the work list and record the ones that
3653 // changed.
3654 for (AbstractAttribute *AA : Worklist)
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003655 if (!isAssumedDead(*AA, nullptr))
3656 if (AA->update(*this) == ChangeStatus::CHANGED)
3657 ChangedAAs.push_back(AA);
Johannes Doerfertaade7822019-06-05 03:02:24 +00003658
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003659 // Check if we recompute the dependences in the next iteration.
3660 RecomputeDependences = (DepRecomputeInterval > 0 &&
3661 IterationCounter % DepRecomputeInterval == 0);
3662
Johannes Doerfert9543f142019-08-23 15:24:57 +00003663 // Add attributes to the changed set if they have been created in the last
3664 // iteration.
3665 ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs,
3666 AllAbstractAttributes.end());
3667
Johannes Doerfertaade7822019-06-05 03:02:24 +00003668 // Reset the work list and repopulate with the changed abstract attributes.
3669 // Note that dependent ones are added above.
3670 Worklist.clear();
3671 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
3672
Johannes Doerfertbf112132019-08-29 01:29:44 +00003673 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
3674 VerifyMaxFixpointIterations));
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003675
Johannes Doerfertaade7822019-06-05 03:02:24 +00003676 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
3677 << IterationCounter << "/" << MaxFixpointIterations
3678 << " iterations\n");
3679
Johannes Doerfertbf112132019-08-29 01:29:44 +00003680 size_t NumFinalAAs = AllAbstractAttributes.size();
Johannes Doerfertb504eb82019-08-26 18:55:47 +00003681
Johannes Doerfertaade7822019-06-05 03:02:24 +00003682 bool FinishedAtFixpoint = Worklist.empty();
3683
3684 // Reset abstract arguments not settled in a sound fixpoint by now. This
3685 // happens when we stopped the fixpoint iteration early. Note that only the
3686 // ones marked as "changed" *and* the ones transitively depending on them
3687 // need to be reverted to a pessimistic state. Others might not be in a
3688 // fixpoint state but we can use the optimistic results for them anyway.
3689 SmallPtrSet<AbstractAttribute *, 32> Visited;
3690 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
3691 AbstractAttribute *ChangedAA = ChangedAAs[u];
3692 if (!Visited.insert(ChangedAA).second)
3693 continue;
3694
3695 AbstractState &State = ChangedAA->getState();
3696 if (!State.isAtFixpoint()) {
3697 State.indicatePessimisticFixpoint();
3698
3699 NumAttributesTimedOut++;
3700 }
3701
3702 auto &QuerriedAAs = QueryMap[ChangedAA];
3703 ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end());
3704 }
3705
3706 LLVM_DEBUG({
3707 if (!Visited.empty())
3708 dbgs() << "\n[Attributor] Finalized " << Visited.size()
3709 << " abstract attributes.\n";
3710 });
3711
3712 unsigned NumManifested = 0;
3713 unsigned NumAtFixpoint = 0;
3714 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
3715 for (AbstractAttribute *AA : AllAbstractAttributes) {
3716 AbstractState &State = AA->getState();
3717
3718 // If there is not already a fixpoint reached, we can now take the
3719 // optimistic state. This is correct because we enforced a pessimistic one
3720 // on abstract attributes that were transitively dependent on a changed one
3721 // already above.
3722 if (!State.isAtFixpoint())
3723 State.indicateOptimisticFixpoint();
3724
3725 // If the state is invalid, we do not try to manifest it.
3726 if (!State.isValidState())
3727 continue;
3728
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003729 // Skip dead code.
3730 if (isAssumedDead(*AA, nullptr))
3731 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +00003732 // Manifest the state and record if we changed the IR.
3733 ChangeStatus LocalChange = AA->manifest(*this);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003734 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
3735 AA->trackStatistics();
3736
Johannes Doerfertaade7822019-06-05 03:02:24 +00003737 ManifestChange = ManifestChange | LocalChange;
3738
3739 NumAtFixpoint++;
3740 NumManifested += (LocalChange == ChangeStatus::CHANGED);
3741 }
3742
3743 (void)NumManifested;
3744 (void)NumAtFixpoint;
3745 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
3746 << " arguments while " << NumAtFixpoint
3747 << " were in a valid fixpoint state\n");
3748
3749 // If verification is requested, we finished this run at a fixpoint, and the
3750 // IR was changed, we re-run the whole fixpoint analysis, starting at
3751 // re-initialization of the arguments. This re-run should not result in an IR
3752 // change. Though, the (virtual) state of attributes at the end of the re-run
3753 // might be more optimistic than the known state or the IR state if the better
3754 // state cannot be manifested.
3755 if (VerifyAttributor && FinishedAtFixpoint &&
3756 ManifestChange == ChangeStatus::CHANGED) {
3757 VerifyAttributor = false;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003758 ChangeStatus VerifyStatus = run(M);
Johannes Doerfertaade7822019-06-05 03:02:24 +00003759 if (VerifyStatus != ChangeStatus::UNCHANGED)
3760 llvm_unreachable(
3761 "Attributor verification failed, re-run did result in an IR change "
3762 "even after a fixpoint was reached in the original run. (False "
3763 "positives possible!)");
3764 VerifyAttributor = true;
3765 }
3766
3767 NumAttributesManifested += NumManifested;
3768 NumAttributesValidFixpoint += NumAtFixpoint;
3769
Fangrui Songf1826172019-08-20 07:21:43 +00003770 (void)NumFinalAAs;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003771 assert(
3772 NumFinalAAs == AllAbstractAttributes.size() &&
3773 "Expected the final number of abstract attributes to remain unchanged!");
Johannes Doerfert39681e72019-08-27 04:57:54 +00003774
3775 // Delete stuff at the end to avoid invalid references and a nice order.
Johannes Doerfert2f622062019-09-04 16:35:20 +00003776 {
3777 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
3778 << ToBeDeletedFunctions.size() << " functions and "
3779 << ToBeDeletedBlocks.size() << " blocks and "
3780 << ToBeDeletedInsts.size() << " instructions\n");
3781 for (Instruction *I : ToBeDeletedInsts) {
3782 if (!I->use_empty())
3783 I->replaceAllUsesWith(UndefValue::get(I->getType()));
3784 I->eraseFromParent();
3785 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003786
Johannes Doerfert2f622062019-09-04 16:35:20 +00003787 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
3788 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
3789 ToBeDeletedBBs.reserve(NumDeadBlocks);
3790 ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end());
3791 DeleteDeadBlocks(ToBeDeletedBBs);
3792 STATS_DECLTRACK(AAIsDead, BasicBlock,
3793 "Number of dead basic blocks deleted.");
3794 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003795
Johannes Doerfert2f622062019-09-04 16:35:20 +00003796 STATS_DECL(AAIsDead, Function, "Number of dead functions deleted.");
3797 for (Function *Fn : ToBeDeletedFunctions) {
3798 Fn->replaceAllUsesWith(UndefValue::get(Fn->getType()));
3799 Fn->eraseFromParent();
3800 STATS_TRACK(AAIsDead, Function);
3801 }
3802
3803 // Identify dead internal functions and delete them. This happens outside
3804 // the other fixpoint analysis as we might treat potentially dead functions
3805 // as live to lower the number of iterations. If they happen to be dead, the
3806 // below fixpoint loop will identify and eliminate them.
3807 SmallVector<Function *, 8> InternalFns;
3808 for (Function &F : M)
3809 if (F.hasInternalLinkage())
3810 InternalFns.push_back(&F);
3811
3812 bool FoundDeadFn = true;
3813 while (FoundDeadFn) {
3814 FoundDeadFn = false;
3815 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
3816 Function *F = InternalFns[u];
3817 if (!F)
3818 continue;
3819
3820 const auto *LivenessAA =
3821 lookupAAFor<AAIsDead>(IRPosition::function(*F));
3822 if (LivenessAA &&
3823 !checkForAllCallSites([](CallSite CS) { return false; },
3824 *LivenessAA, true))
3825 continue;
3826
3827 STATS_TRACK(AAIsDead, Function);
3828 F->replaceAllUsesWith(UndefValue::get(F->getType()));
3829 F->eraseFromParent();
3830 InternalFns[u] = nullptr;
3831 FoundDeadFn = true;
3832 }
3833 }
Johannes Doerfert39681e72019-08-27 04:57:54 +00003834 }
3835
Johannes Doerfertbf112132019-08-29 01:29:44 +00003836 if (VerifyMaxFixpointIterations &&
3837 IterationCounter != MaxFixpointIterations) {
3838 errs() << "\n[Attributor] Fixpoint iteration done after: "
3839 << IterationCounter << "/" << MaxFixpointIterations
3840 << " iterations\n";
3841 llvm_unreachable("The fixpoint was not reached with exactly the number of "
3842 "specified iterations!");
3843 }
3844
Johannes Doerfertaade7822019-06-05 03:02:24 +00003845 return ManifestChange;
3846}
3847
Stefan Stipanovic431141c2019-09-15 21:47:41 +00003848void Attributor::identifyDefaultAbstractAttributes(
3849 Function &F, std::function<TargetLibraryInfo *(Function &)> &TLIGetter) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00003850 if (!VisitedFunctions.insert(&F).second)
3851 return;
Johannes Doerfertaade7822019-06-05 03:02:24 +00003852
Stefan Stipanovic431141c2019-09-15 21:47:41 +00003853 if (EnableHeapToStack)
3854 InfoCache.FuncTLIMap[&F] = TLIGetter(F);
3855
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003856 IRPosition FPos = IRPosition::function(F);
3857
Johannes Doerfert305b9612019-08-04 18:40:01 +00003858 // Check for dead BasicBlocks in every function.
Johannes Doerfert21fe0a32019-08-06 00:55:11 +00003859 // We need dead instruction detection because we do not want to deal with
3860 // broken IR in which SSA rules do not apply.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003861 getOrCreateAAFor<AAIsDead>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00003862
3863 // Every function might be "will-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003864 getOrCreateAAFor<AAWillReturn>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00003865
Stefan Stipanovic53605892019-06-27 11:27:54 +00003866 // Every function can be nounwind.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003867 getOrCreateAAFor<AANoUnwind>(FPos);
Stefan Stipanovic53605892019-06-27 11:27:54 +00003868
Stefan Stipanovic06263672019-07-11 21:37:40 +00003869 // Every function might be marked "nosync"
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003870 getOrCreateAAFor<AANoSync>(FPos);
Stefan Stipanovic06263672019-07-11 21:37:40 +00003871
Hideto Ueno65bbaf92019-07-12 17:38:51 +00003872 // Every function might be "no-free".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003873 getOrCreateAAFor<AANoFree>(FPos);
Hideto Ueno65bbaf92019-07-12 17:38:51 +00003874
Johannes Doerferte83f3032019-08-05 23:22:05 +00003875 // Every function might be "no-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003876 getOrCreateAAFor<AANoReturn>(FPos);
Johannes Doerferte83f3032019-08-05 23:22:05 +00003877
Stefan Stipanovic431141c2019-09-15 21:47:41 +00003878 // Every function might be applicable for Heap-To-Stack conversion.
3879 if (EnableHeapToStack)
3880 getOrCreateAAFor<AAHeapToStack>(FPos);
3881
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00003882 // Return attributes are only appropriate if the return type is non void.
3883 Type *ReturnType = F.getReturnType();
3884 if (!ReturnType->isVoidTy()) {
3885 // Argument attribute "returned" --- Create only one per function even
3886 // though it is an argument attribute.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003887 getOrCreateAAFor<AAReturnedValues>(FPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00003888
Hideto Uenof2b9dc42019-09-07 07:03:05 +00003889 IRPosition RetPos = IRPosition::returned(F);
3890
3891 // Every function might be simplified.
3892 getOrCreateAAFor<AAValueSimplify>(RetPos);
3893
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003894 if (ReturnType->isPointerTy()) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003895
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003896 // Every function with pointer return type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003897 getOrCreateAAFor<AAAlign>(RetPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003898
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003899 // Every function with pointer return type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003900 getOrCreateAAFor<AANonNull>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003901
3902 // Every function with pointer return type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003903 getOrCreateAAFor<AANoAlias>(RetPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00003904
3905 // Every function with pointer return type might be marked
3906 // dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003907 getOrCreateAAFor<AADereferenceable>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003908 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00003909 }
3910
Hideto Ueno54869ec2019-07-15 06:49:04 +00003911 for (Argument &Arg : F.args()) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00003912 IRPosition ArgPos = IRPosition::argument(Arg);
3913
3914 // Every argument might be simplified.
3915 getOrCreateAAFor<AAValueSimplify>(ArgPos);
3916
Hideto Ueno19c07af2019-07-23 08:16:17 +00003917 if (Arg.getType()->isPointerTy()) {
3918 // Every argument with pointer type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003919 getOrCreateAAFor<AANonNull>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00003920
Hideto Uenocbab3342019-08-29 05:52:00 +00003921 // Every argument with pointer type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003922 getOrCreateAAFor<AANoAlias>(ArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00003923
Hideto Ueno19c07af2019-07-23 08:16:17 +00003924 // Every argument with pointer type might be marked dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003925 getOrCreateAAFor<AADereferenceable>(ArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003926
3927 // Every argument with pointer type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003928 getOrCreateAAFor<AAAlign>(ArgPos);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00003929
3930 // Every argument with pointer type might be marked nocapture.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003931 getOrCreateAAFor<AANoCapture>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00003932 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00003933 }
3934
Johannes Doerfertaade7822019-06-05 03:02:24 +00003935 // Walk all instructions to find more attribute opportunities and also
3936 // interesting instructions that might be queried by abstract attributes
3937 // during their initialization or update.
3938 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
3939 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
3940
3941 for (Instruction &I : instructions(&F)) {
3942 bool IsInterestingOpcode = false;
3943
3944 // To allow easy access to all instructions in a function with a given
3945 // opcode we store them in the InfoCache. As not all opcodes are interesting
3946 // to concrete attributes we only cache the ones that are as identified in
3947 // the following switch.
3948 // Note: There are no concrete attributes now so this is initially empty.
Stefan Stipanovic53605892019-06-27 11:27:54 +00003949 switch (I.getOpcode()) {
3950 default:
3951 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
3952 "New call site/base instruction type needs to be known int the "
3953 "attributor.");
3954 break;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003955 case Instruction::Load:
3956 // The alignment of a pointer is interesting for loads.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003957 getOrCreateAAFor<AAAlign>(
3958 IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003959 break;
3960 case Instruction::Store:
3961 // The alignment of a pointer is interesting for stores.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003962 getOrCreateAAFor<AAAlign>(
3963 IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003964 break;
Stefan Stipanovic53605892019-06-27 11:27:54 +00003965 case Instruction::Call:
3966 case Instruction::CallBr:
3967 case Instruction::Invoke:
3968 case Instruction::CleanupRet:
3969 case Instruction::CatchSwitch:
3970 case Instruction::Resume:
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00003971 case Instruction::Ret:
Stefan Stipanovic53605892019-06-27 11:27:54 +00003972 IsInterestingOpcode = true;
3973 }
Johannes Doerfertaade7822019-06-05 03:02:24 +00003974 if (IsInterestingOpcode)
3975 InstOpcodeMap[I.getOpcode()].push_back(&I);
3976 if (I.mayReadOrWriteMemory())
3977 ReadOrWriteInsts.push_back(&I);
Hideto Ueno54869ec2019-07-15 06:49:04 +00003978
3979 CallSite CS(&I);
3980 if (CS && CS.getCalledFunction()) {
3981 for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00003982
3983 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
3984
3985 // Call site argument might be simplified.
3986 getOrCreateAAFor<AAValueSimplify>(CSArgPos);
3987
Hideto Ueno54869ec2019-07-15 06:49:04 +00003988 if (!CS.getArgument(i)->getType()->isPointerTy())
3989 continue;
3990
3991 // Call site argument attribute "non-null".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003992 getOrCreateAAFor<AANonNull>(CSArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00003993
Hideto Uenocbab3342019-08-29 05:52:00 +00003994 // Call site argument attribute "no-alias".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003995 getOrCreateAAFor<AANoAlias>(CSArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00003996
Hideto Ueno19c07af2019-07-23 08:16:17 +00003997 // Call site argument attribute "dereferenceable".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003998 getOrCreateAAFor<AADereferenceable>(CSArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003999
4000 // Call site argument attribute "align".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004001 getOrCreateAAFor<AAAlign>(CSArgPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00004002 }
4003 }
Johannes Doerfertaade7822019-06-05 03:02:24 +00004004 }
4005}
4006
4007/// Helpers to ease debugging through output streams and print calls.
4008///
4009///{
4010raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
4011 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
4012}
4013
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004014raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00004015 switch (AP) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00004016 case IRPosition::IRP_INVALID:
4017 return OS << "inv";
4018 case IRPosition::IRP_FLOAT:
4019 return OS << "flt";
4020 case IRPosition::IRP_RETURNED:
4021 return OS << "fn_ret";
4022 case IRPosition::IRP_CALL_SITE_RETURNED:
4023 return OS << "cs_ret";
4024 case IRPosition::IRP_FUNCTION:
4025 return OS << "fn";
4026 case IRPosition::IRP_CALL_SITE:
4027 return OS << "cs";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004028 case IRPosition::IRP_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00004029 return OS << "arg";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004030 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00004031 return OS << "cs_arg";
Johannes Doerfertaade7822019-06-05 03:02:24 +00004032 }
4033 llvm_unreachable("Unknown attribute position!");
4034}
4035
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004036raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00004037 const Value &AV = Pos.getAssociatedValue();
4038 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004039 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
4040}
4041
Johannes Doerfertacc80792019-08-12 22:07:34 +00004042raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerState &S) {
4043 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
4044 << static_cast<const AbstractState &>(S);
4045}
4046
Johannes Doerfertaade7822019-06-05 03:02:24 +00004047raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
4048 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
4049}
4050
4051raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
4052 AA.print(OS);
4053 return OS;
4054}
4055
4056void AbstractAttribute::print(raw_ostream &OS) const {
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004057 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
4058 << "]";
Johannes Doerfertaade7822019-06-05 03:02:24 +00004059}
4060///}
4061
4062/// ----------------------------------------------------------------------------
4063/// Pass (Manager) Boilerplate
4064/// ----------------------------------------------------------------------------
4065
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004066static bool runAttributorOnModule(
4067 Module &M, std::function<TargetLibraryInfo *(Function &)> &TLIGetter) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00004068 if (DisableAttributor)
4069 return false;
4070
4071 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size()
4072 << " functions.\n");
4073
4074 // Create an Attributor and initially empty information cache that is filled
4075 // while we identify default attribute opportunities.
Johannes Doerfertece81902019-08-12 22:05:53 +00004076 InformationCache InfoCache(M.getDataLayout());
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00004077 Attributor A(InfoCache, DepRecInterval);
Johannes Doerfertaade7822019-06-05 03:02:24 +00004078
4079 for (Function &F : M) {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00004080 if (F.hasExactDefinition())
4081 NumFnWithExactDefinition++;
4082 else
Johannes Doerfertaade7822019-06-05 03:02:24 +00004083 NumFnWithoutExactDefinition++;
Johannes Doerfertaade7822019-06-05 03:02:24 +00004084
4085 // For now we ignore naked and optnone functions.
4086 if (F.hasFnAttribute(Attribute::Naked) ||
4087 F.hasFnAttribute(Attribute::OptimizeNone))
4088 continue;
4089
Johannes Doerfert2f622062019-09-04 16:35:20 +00004090 // We look at internal functions only on-demand but if any use is not a
4091 // direct call, we have to do it eagerly.
4092 if (F.hasInternalLinkage()) {
4093 if (llvm::all_of(F.uses(), [](const Use &U) {
4094 return ImmutableCallSite(U.getUser()) &&
4095 ImmutableCallSite(U.getUser()).isCallee(&U);
4096 }))
4097 continue;
4098 }
4099
Johannes Doerfertaade7822019-06-05 03:02:24 +00004100 // Populate the Attributor with abstract attribute opportunities in the
4101 // function and the information cache with IR information.
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004102 A.identifyDefaultAbstractAttributes(F, TLIGetter);
Johannes Doerfertaade7822019-06-05 03:02:24 +00004103 }
4104
Johannes Doerfert2f622062019-09-04 16:35:20 +00004105 return A.run(M) == ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +00004106}
4107
4108PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004109 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
4110
4111 std::function<TargetLibraryInfo *(Function &)> TLIGetter =
4112 [&](Function &F) -> TargetLibraryInfo * {
4113 return &FAM.getResult<TargetLibraryAnalysis>(F);
4114 };
4115
4116 if (runAttributorOnModule(M, TLIGetter)) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00004117 // FIXME: Think about passes we will preserve and add them here.
4118 return PreservedAnalyses::none();
4119 }
4120 return PreservedAnalyses::all();
4121}
4122
4123namespace {
4124
4125struct AttributorLegacyPass : public ModulePass {
4126 static char ID;
4127
4128 AttributorLegacyPass() : ModulePass(ID) {
4129 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
4130 }
4131
4132 bool runOnModule(Module &M) override {
4133 if (skipModule(M))
4134 return false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004135 std::function<TargetLibraryInfo *(Function &)> TLIGetter =
4136 [&](Function &F) -> TargetLibraryInfo * { return nullptr; };
4137
4138 return runAttributorOnModule(M, TLIGetter);
Johannes Doerfertaade7822019-06-05 03:02:24 +00004139 }
4140
4141 void getAnalysisUsage(AnalysisUsage &AU) const override {
4142 // FIXME: Think about passes we will preserve and add them here.
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004143 AU.addRequired<TargetLibraryInfoWrapperPass>();
Johannes Doerfertaade7822019-06-05 03:02:24 +00004144 }
4145};
4146
4147} // end anonymous namespace
4148
4149Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
4150
4151char AttributorLegacyPass::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00004152
4153const char AAReturnedValues::ID = 0;
4154const char AANoUnwind::ID = 0;
4155const char AANoSync::ID = 0;
Johannes Doerferteccdf082019-08-05 23:35:12 +00004156const char AANoFree::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00004157const char AANonNull::ID = 0;
4158const char AANoRecurse::ID = 0;
4159const char AAWillReturn::ID = 0;
4160const char AANoAlias::ID = 0;
4161const char AANoReturn::ID = 0;
4162const char AAIsDead::ID = 0;
4163const char AADereferenceable::ID = 0;
4164const char AAAlign::ID = 0;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004165const char AANoCapture::ID = 0;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004166const char AAValueSimplify::ID = 0;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004167const char AAHeapToStack::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00004168
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004169// Macro magic to create the static generator function for attributes that
4170// follow the naming scheme.
4171
4172#define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
4173 case IRPosition::PK: \
4174 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
4175
4176#define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
4177 case IRPosition::PK: \
4178 AA = new CLASS##SUFFIX(IRP); \
4179 break;
4180
4181#define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4182 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4183 CLASS *AA = nullptr; \
4184 switch (IRP.getPositionKind()) { \
4185 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4186 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
4187 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
4188 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
4189 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
4190 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
4191 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4192 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
4193 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004194 return *AA; \
4195 }
4196
4197#define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4198 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4199 CLASS *AA = nullptr; \
4200 switch (IRP.getPositionKind()) { \
4201 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4202 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
4203 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
4204 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
4205 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
4206 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
4207 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
4208 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
4209 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004210 return *AA; \
4211 }
4212
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004213#define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4214 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4215 CLASS *AA = nullptr; \
4216 switch (IRP.getPositionKind()) { \
4217 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4218 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4219 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
4220 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
4221 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
4222 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
4223 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
4224 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
4225 } \
4226 return *AA; \
4227 }
4228
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004229#define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4230 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4231 CLASS *AA = nullptr; \
4232 switch (IRP.getPositionKind()) { \
4233 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4234 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
4235 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
4236 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
4237 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
4238 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
4239 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
4240 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4241 } \
4242 AA->initialize(A); \
4243 return *AA; \
4244 }
4245
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004246CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
4247CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
4248CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
4249CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
4250CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
4251CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
4252CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
4253CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
4254
4255CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
4256CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
4257CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
4258CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004259CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004260
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004261CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
4262
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004263CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
4264
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004265#undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
4266#undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004267#undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004268#undef SWITCH_PK_CREATE
4269#undef SWITCH_PK_INV
4270
Johannes Doerfertaade7822019-06-05 03:02:24 +00004271INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
4272 "Deduce and propagate attributes", false, false)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004273INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Johannes Doerfertaade7822019-06-05 03:02:24 +00004274INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
4275 "Deduce and propagate attributes", false, false)