blob: 58ce91c807dd77d2f044b0d990e6ced2dd5720f0 [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>
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +0000164static bool genericValueTraversal(
Johannes Doerfertdef99282019-08-14 21:29:37 +0000165 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
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +0000489namespace {
Johannes Doerfert234eda52019-08-16 19:51:23 +0000490/// Helper functions to clamp a state \p S of type \p StateType with the
491/// information in \p R and indicate/return if \p S did change (as-in update is
492/// required to be run again).
493///
494///{
495template <typename StateType>
496ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R);
497
498template <>
499ChangeStatus clampStateAndIndicateChange<IntegerState>(IntegerState &S,
500 const IntegerState &R) {
501 auto Assumed = S.getAssumed();
502 S ^= R;
503 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
504 : ChangeStatus::CHANGED;
505}
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000506
507template <>
508ChangeStatus clampStateAndIndicateChange<BooleanState>(BooleanState &S,
509 const BooleanState &R) {
510 return clampStateAndIndicateChange<IntegerState>(S, R);
511}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000512///}
513
514/// Clamp the information known for all returned values of a function
515/// (identified by \p QueryingAA) into \p S.
516template <typename AAType, typename StateType = typename AAType::StateType>
517static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA,
518 StateType &S) {
519 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
520 << static_cast<const AbstractAttribute &>(QueryingAA)
521 << " into " << S << "\n");
522
523 assert((QueryingAA.getIRPosition().getPositionKind() ==
524 IRPosition::IRP_RETURNED ||
525 QueryingAA.getIRPosition().getPositionKind() ==
526 IRPosition::IRP_CALL_SITE_RETURNED) &&
527 "Can only clamp returned value states for a function returned or call "
528 "site returned position!");
529
530 // Use an optional state as there might not be any return values and we want
531 // to join (IntegerState::operator&) the state of all there are.
532 Optional<StateType> T;
533
534 // Callback for each possibly returned value.
535 auto CheckReturnValue = [&](Value &RV) -> bool {
536 const IRPosition &RVPos = IRPosition::value(RV);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000537 const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos);
538 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
539 << " @ " << RVPos << "\n");
540 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000541 if (T.hasValue())
542 *T &= AAS;
543 else
544 T = AAS;
545 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
546 << "\n");
547 return T->isValidState();
548 };
549
550 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
551 S.indicatePessimisticFixpoint();
552 else if (T.hasValue())
553 S ^= *T;
554}
555
556/// Helper class for generic deduction: return value -> returned position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000557template <typename AAType, typename Base,
558 typename StateType = typename AAType::StateType>
559struct AAReturnedFromReturnedValues : public Base {
560 AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000561
562 /// See AbstractAttribute::updateImpl(...).
563 ChangeStatus updateImpl(Attributor &A) override {
564 StateType S;
565 clampReturnedValueStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000566 // TODO: If we know we visited all returned values, thus no are assumed
567 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000568 return clampStateAndIndicateChange<StateType>(this->getState(), S);
569 }
570};
571
572/// Clamp the information known at all call sites for a given argument
573/// (identified by \p QueryingAA) into \p S.
574template <typename AAType, typename StateType = typename AAType::StateType>
575static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
576 StateType &S) {
577 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
578 << static_cast<const AbstractAttribute &>(QueryingAA)
579 << " into " << S << "\n");
580
581 assert(QueryingAA.getIRPosition().getPositionKind() ==
582 IRPosition::IRP_ARGUMENT &&
583 "Can only clamp call site argument states for an argument position!");
584
585 // Use an optional state as there might not be any return values and we want
586 // to join (IntegerState::operator&) the state of all there are.
587 Optional<StateType> T;
588
589 // The argument number which is also the call site argument number.
590 unsigned ArgNo = QueryingAA.getIRPosition().getArgNo();
591
592 auto CallSiteCheck = [&](CallSite CS) {
593 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000594 const AAType &AA = A.getAAFor<AAType>(QueryingAA, CSArgPos);
Johannes Doerfert234eda52019-08-16 19:51:23 +0000595 LLVM_DEBUG(dbgs() << "[Attributor] CS: " << *CS.getInstruction()
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000596 << " AA: " << AA.getAsStr() << " @" << CSArgPos << "\n");
597 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000598 if (T.hasValue())
599 *T &= AAS;
600 else
601 T = AAS;
602 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
603 << "\n");
604 return T->isValidState();
605 };
606
607 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true))
608 S.indicatePessimisticFixpoint();
609 else if (T.hasValue())
610 S ^= *T;
611}
612
613/// Helper class for generic deduction: call site argument -> argument position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000614template <typename AAType, typename Base,
615 typename StateType = typename AAType::StateType>
616struct AAArgumentFromCallSiteArguments : public Base {
617 AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000618
619 /// See AbstractAttribute::updateImpl(...).
620 ChangeStatus updateImpl(Attributor &A) override {
621 StateType S;
622 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000623 // TODO: If we know we visited all incoming values, thus no are assumed
624 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000625 return clampStateAndIndicateChange<StateType>(this->getState(), S);
626 }
627};
628
629/// Helper class for generic replication: function returned -> cs returned.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000630template <typename AAType, typename Base>
631struct AACallSiteReturnedFromReturned : public Base {
632 AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000633
634 /// See AbstractAttribute::updateImpl(...).
635 ChangeStatus updateImpl(Attributor &A) override {
636 assert(this->getIRPosition().getPositionKind() ==
637 IRPosition::IRP_CALL_SITE_RETURNED &&
638 "Can only wrap function returned positions for call site returned "
639 "positions!");
640 auto &S = this->getState();
641
642 const Function *AssociatedFunction =
643 this->getIRPosition().getAssociatedFunction();
644 if (!AssociatedFunction)
645 return S.indicatePessimisticFixpoint();
646
647 IRPosition FnPos = IRPosition::returned(*AssociatedFunction);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000648 const AAType &AA = A.getAAFor<AAType>(*this, FnPos);
Johannes Doerfert234eda52019-08-16 19:51:23 +0000649 return clampStateAndIndicateChange(
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000650 S, static_cast<const typename AAType::StateType &>(AA.getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000651 }
652};
653
Stefan Stipanovic53605892019-06-27 11:27:54 +0000654/// -----------------------NoUnwind Function Attribute--------------------------
655
Johannes Doerfert344d0382019-08-07 22:34:26 +0000656struct AANoUnwindImpl : AANoUnwind {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000657 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
Stefan Stipanovic53605892019-06-27 11:27:54 +0000658
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000659 const std::string getAsStr() const override {
Stefan Stipanovic53605892019-06-27 11:27:54 +0000660 return getAssumed() ? "nounwind" : "may-unwind";
661 }
662
663 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +0000664 ChangeStatus updateImpl(Attributor &A) override {
665 auto Opcodes = {
666 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
667 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
668 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
669
670 auto CheckForNoUnwind = [&](Instruction &I) {
671 if (!I.mayThrow())
672 return true;
673
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000674 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
675 const auto &NoUnwindAA =
676 A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS));
677 return NoUnwindAA.isAssumedNoUnwind();
678 }
679 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +0000680 };
681
682 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
683 return indicatePessimisticFixpoint();
684
685 return ChangeStatus::UNCHANGED;
686 }
Stefan Stipanovic53605892019-06-27 11:27:54 +0000687};
688
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000689struct AANoUnwindFunction final : public AANoUnwindImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000690 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000691
692 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000693 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000694};
695
Johannes Doerfert66cf87e2019-08-16 19:49:00 +0000696/// NoUnwind attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +0000697struct AANoUnwindCallSite final : AANoUnwindImpl {
698 AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
699
700 /// See AbstractAttribute::initialize(...).
701 void initialize(Attributor &A) override {
702 AANoUnwindImpl::initialize(A);
703 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +0000704 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +0000705 indicatePessimisticFixpoint();
706 }
707
708 /// See AbstractAttribute::updateImpl(...).
709 ChangeStatus updateImpl(Attributor &A) override {
710 // TODO: Once we have call site specific value information we can provide
711 // call site specific liveness information and then it makes
712 // sense to specialize attributes for call sites arguments instead of
713 // redirecting requests to the callee argument.
714 Function *F = getAssociatedFunction();
715 const IRPosition &FnPos = IRPosition::function(*F);
716 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos);
717 return clampStateAndIndicateChange(
718 getState(),
719 static_cast<const AANoUnwind::StateType &>(FnAA.getState()));
720 }
721
722 /// See AbstractAttribute::trackStatistics()
723 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
724};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +0000725
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000726/// --------------------- Function Return Values -------------------------------
727
728/// "Attribute" that collects all potential returned values and the return
729/// instructions that they arise from.
730///
731/// If there is a unique returned value R, the manifest method will:
732/// - mark R with the "returned" attribute, if R is an argument.
Johannes Doerferteccdf082019-08-05 23:35:12 +0000733class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000734
735 /// Mapping of values potentially returned by the associated function to the
736 /// return instructions that might return them.
Johannes Doerferta4a308c2019-08-26 17:51:23 +0000737 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000738
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +0000739 /// Mapping to remember the number of returned values for a call site such
740 /// that we can avoid updates if nothing changed.
741 DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
742
743 /// Set of unresolved calls returned by the associated function.
Johannes Doerfert695089e2019-08-23 15:23:49 +0000744 SmallSetVector<CallBase *, 4> UnresolvedCalls;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000745
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000746 /// State flags
747 ///
748 ///{
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +0000749 bool IsFixed = false;
750 bool IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000751 ///}
752
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000753public:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000754 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000755
756 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +0000757 void initialize(Attributor &A) override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000758 // Reset the state.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000759 IsFixed = false;
760 IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000761 ReturnedValues.clear();
762
Johannes Doerfertdef99282019-08-14 21:29:37 +0000763 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +0000764 if (!F) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000765 indicatePessimisticFixpoint();
766 return;
767 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000768
769 // The map from instruction opcodes to those instructions in the function.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000770 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000771
772 // Look through all arguments, if one is marked as returned we are done.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000773 for (Argument &Arg : F->args()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000774 if (Arg.hasReturnedAttr()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000775 auto &ReturnInstSet = ReturnedValues[&Arg];
776 for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
777 ReturnInstSet.insert(cast<ReturnInst>(RI));
778
779 indicateOptimisticFixpoint();
780 return;
781 }
782 }
Johannes Doerfertb0412e42019-09-04 16:16:13 +0000783
784 if (!F->hasExactDefinition())
785 indicatePessimisticFixpoint();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000786 }
787
788 /// See AbstractAttribute::manifest(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000789 ChangeStatus manifest(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000790
791 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000792 AbstractState &getState() override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000793
794 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000795 const AbstractState &getState() const override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000796
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000797 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +0000798 ChangeStatus updateImpl(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000799
Johannes Doerfertdef99282019-08-14 21:29:37 +0000800 llvm::iterator_range<iterator> returned_values() override {
801 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
802 }
803
804 llvm::iterator_range<const_iterator> returned_values() const override {
805 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
806 }
807
Johannes Doerfert695089e2019-08-23 15:23:49 +0000808 const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000809 return UnresolvedCalls;
810 }
811
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000812 /// Return the number of potential return values, -1 if unknown.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000813 size_t getNumReturnValues() const override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000814 return isValidState() ? ReturnedValues.size() : -1;
815 }
816
817 /// Return an assumed unique return value if a single candidate is found. If
818 /// there cannot be one, return a nullptr. If it is not clear yet, return the
819 /// Optional::NoneType.
Johannes Doerfert14a04932019-08-07 22:27:24 +0000820 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000821
Johannes Doerfert14a04932019-08-07 22:27:24 +0000822 /// See AbstractState::checkForAllReturnedValues(...).
823 bool checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +0000824 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +0000825 &Pred) const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000826
827 /// Pretty print the attribute similar to the IR representation.
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000828 const std::string getAsStr() const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000829
830 /// See AbstractState::isAtFixpoint().
831 bool isAtFixpoint() const override { return IsFixed; }
832
833 /// See AbstractState::isValidState().
834 bool isValidState() const override { return IsValidState; }
835
836 /// See AbstractState::indicateOptimisticFixpoint(...).
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000837 ChangeStatus indicateOptimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000838 IsFixed = true;
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000839 return ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000840 }
Stefan Stipanovicd0216172019-08-02 21:31:22 +0000841
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000842 ChangeStatus indicatePessimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000843 IsFixed = true;
844 IsValidState = false;
Johannes Doerfertd1c37932019-08-04 18:37:38 +0000845 return ChangeStatus::CHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000846 }
847};
848
849ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
850 ChangeStatus Changed = ChangeStatus::UNCHANGED;
851
852 // Bookkeeping.
853 assert(isValidState());
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000854 STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
855 "Number of function with known return values");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000856
857 // Check if we have an assumed unique return value that we could manifest.
Johannes Doerfert14a04932019-08-07 22:27:24 +0000858 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000859
860 if (!UniqueRV.hasValue() || !UniqueRV.getValue())
861 return Changed;
862
863 // Bookkeeping.
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000864 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
865 "Number of function with unique return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000866
Johannes Doerfert23400e612019-08-23 17:41:37 +0000867 // Callback to replace the uses of CB with the constant C.
868 auto ReplaceCallSiteUsersWith = [](CallBase &CB, Constant &C) {
869 if (CB.getNumUses() == 0)
870 return ChangeStatus::UNCHANGED;
871 CB.replaceAllUsesWith(&C);
872 return ChangeStatus::CHANGED;
873 };
874
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000875 // If the assumed unique return value is an argument, annotate it.
876 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000877 getIRPosition() = IRPosition::argument(*UniqueRVArg);
Johannes Doerfert23400e612019-08-23 17:41:37 +0000878 Changed = IRAttribute::manifest(A);
879 } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
880 // We can replace the returned value with the unique returned constant.
881 Value &AnchorValue = getAnchorValue();
882 if (Function *F = dyn_cast<Function>(&AnchorValue)) {
883 for (const Use &U : F->uses())
884 if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
Johannes Doerferte7c6f972019-09-14 02:57:50 +0000885 if (CB->isCallee(&U)) {
886 Constant *RVCCast =
887 ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
888 Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
889 }
Johannes Doerfert23400e612019-08-23 17:41:37 +0000890 } else {
891 assert(isa<CallBase>(AnchorValue) &&
892 "Expcected a function or call base anchor!");
Johannes Doerferte7c6f972019-09-14 02:57:50 +0000893 Constant *RVCCast =
894 ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
895 Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
Johannes Doerfert23400e612019-08-23 17:41:37 +0000896 }
897 if (Changed == ChangeStatus::CHANGED)
898 STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
899 "Number of function returns replaced by constant return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000900 }
901
902 return Changed;
903}
904
905const std::string AAReturnedValuesImpl::getAsStr() const {
906 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
Johannes Doerfert6471bb62019-08-04 18:39:28 +0000907 (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
Johannes Doerfertdef99282019-08-14 21:29:37 +0000908 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000909}
910
Johannes Doerfert14a04932019-08-07 22:27:24 +0000911Optional<Value *>
912AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
913 // If checkForAllReturnedValues provides a unique value, ignoring potential
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000914 // undef values that can also be present, it is assumed to be the actual
915 // return value and forwarded to the caller of this method. If there are
916 // multiple, a nullptr is returned indicating there cannot be a unique
917 // returned value.
918 Optional<Value *> UniqueRV;
919
Johannes Doerfert14a04932019-08-07 22:27:24 +0000920 auto Pred = [&](Value &RV) -> bool {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000921 // If we found a second returned value and neither the current nor the saved
922 // one is an undef, there is no unique returned value. Undefs are special
923 // since we can pretend they have any value.
924 if (UniqueRV.hasValue() && UniqueRV != &RV &&
925 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
926 UniqueRV = nullptr;
927 return false;
928 }
929
930 // Do not overwrite a value with an undef.
931 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
932 UniqueRV = &RV;
933
934 return true;
935 };
936
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000937 if (!A.checkForAllReturnedValues(Pred, *this))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000938 UniqueRV = nullptr;
939
940 return UniqueRV;
941}
942
Johannes Doerfert14a04932019-08-07 22:27:24 +0000943bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +0000944 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +0000945 &Pred) const {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000946 if (!isValidState())
947 return false;
948
949 // Check all returned values but ignore call sites as long as we have not
950 // encountered an overdefined one during an update.
951 for (auto &It : ReturnedValues) {
952 Value *RV = It.first;
953
Johannes Doerfertdef99282019-08-14 21:29:37 +0000954 CallBase *CB = dyn_cast<CallBase>(RV);
955 if (CB && !UnresolvedCalls.count(CB))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000956 continue;
957
Johannes Doerfert695089e2019-08-23 15:23:49 +0000958 if (!Pred(*RV, It.second))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000959 return false;
960 }
961
962 return true;
963}
964
Johannes Doerfertece81902019-08-12 22:05:53 +0000965ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000966 size_t NumUnresolvedCalls = UnresolvedCalls.size();
967 bool Changed = false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000968
Johannes Doerfertdef99282019-08-14 21:29:37 +0000969 // State used in the value traversals starting in returned values.
970 struct RVState {
971 // The map in which we collect return values -> return instrs.
972 decltype(ReturnedValues) &RetValsMap;
973 // The flag to indicate a change.
Johannes Doerfert056f1b52019-08-19 19:14:10 +0000974 bool &Changed;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000975 // The return instrs we come from.
Johannes Doerfert695089e2019-08-23 15:23:49 +0000976 SmallSetVector<ReturnInst *, 4> RetInsts;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000977 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000978
Johannes Doerfertdef99282019-08-14 21:29:37 +0000979 // Callback for a leaf value returned by the associated function.
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000980 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000981 auto Size = RVS.RetValsMap[&Val].size();
982 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
983 bool Inserted = RVS.RetValsMap[&Val].size() != Size;
984 RVS.Changed |= Inserted;
985 LLVM_DEBUG({
986 if (Inserted)
987 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
988 << " => " << RVS.RetInsts.size() << "\n";
989 });
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000990 return true;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000991 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000992
Johannes Doerfertdef99282019-08-14 21:29:37 +0000993 // Helper method to invoke the generic value traversal.
994 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) {
995 IRPosition RetValPos = IRPosition::value(RV);
996 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this,
997 RVS, VisitValueCB);
998 };
Johannes Doerfertda4d8112019-08-01 16:21:54 +0000999
Johannes Doerfertdef99282019-08-14 21:29:37 +00001000 // Callback for all "return intructions" live in the associated function.
1001 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1002 ReturnInst &Ret = cast<ReturnInst>(I);
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001003 RVState RVS({ReturnedValues, Changed, {}});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001004 RVS.RetInsts.insert(&Ret);
Johannes Doerfertdef99282019-08-14 21:29:37 +00001005 return VisitReturnedValue(*Ret.getReturnValue(), RVS);
1006 };
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001007
Johannes Doerfertdef99282019-08-14 21:29:37 +00001008 // Start by discovering returned values from all live returned instructions in
1009 // the associated function.
1010 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1011 return indicatePessimisticFixpoint();
1012
1013 // Once returned values "directly" present in the code are handled we try to
1014 // resolve returned calls.
1015 decltype(ReturnedValues) NewRVsMap;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001016 for (auto &It : ReturnedValues) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001017 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first
1018 << " by #" << It.second.size() << " RIs\n");
1019 CallBase *CB = dyn_cast<CallBase>(It.first);
1020 if (!CB || UnresolvedCalls.count(CB))
1021 continue;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001022
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001023 if (!CB->getCalledFunction()) {
1024 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1025 << "\n");
1026 UnresolvedCalls.insert(CB);
1027 continue;
1028 }
1029
1030 // TODO: use the function scope once we have call site AAReturnedValues.
1031 const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1032 *this, IRPosition::function(*CB->getCalledFunction()));
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001033 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
1034 << static_cast<const AbstractAttribute &>(RetValAA)
1035 << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001036
1037 // Skip dead ends, thus if we do not know anything about the returned
1038 // call we mark it as unresolved and it will stay that way.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001039 if (!RetValAA.getState().isValidState()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001040 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1041 << "\n");
1042 UnresolvedCalls.insert(CB);
1043 continue;
1044 }
1045
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001046 // Do not try to learn partial information. If the callee has unresolved
1047 // return values we will treat the call as unresolved/opaque.
1048 auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1049 if (!RetValAAUnresolvedCalls.empty()) {
1050 UnresolvedCalls.insert(CB);
1051 continue;
1052 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001053
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001054 // Now check if we can track transitively returned values. If possible, thus
1055 // if all return value can be represented in the current scope, do so.
1056 bool Unresolved = false;
1057 for (auto &RetValAAIt : RetValAA.returned_values()) {
1058 Value *RetVal = RetValAAIt.first;
1059 if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1060 isa<Constant>(RetVal))
1061 continue;
1062 // Anything that did not fit in the above categories cannot be resolved,
1063 // mark the call as unresolved.
1064 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1065 "cannot be translated: "
1066 << *RetVal << "\n");
1067 UnresolvedCalls.insert(CB);
1068 Unresolved = true;
1069 break;
1070 }
1071
1072 if (Unresolved)
1073 continue;
1074
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001075 // Now track transitively returned values.
1076 unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1077 if (NumRetAA == RetValAA.getNumReturnValues()) {
1078 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1079 "changed since it was seen last\n");
1080 continue;
1081 }
1082 NumRetAA = RetValAA.getNumReturnValues();
1083
Johannes Doerfertdef99282019-08-14 21:29:37 +00001084 for (auto &RetValAAIt : RetValAA.returned_values()) {
1085 Value *RetVal = RetValAAIt.first;
1086 if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1087 // Arguments are mapped to call site operands and we begin the traversal
1088 // again.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001089 bool Unused = false;
1090 RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001091 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS);
1092 continue;
1093 } else if (isa<CallBase>(RetVal)) {
1094 // Call sites are resolved by the callee attribute over time, no need to
1095 // do anything for us.
1096 continue;
1097 } else if (isa<Constant>(RetVal)) {
1098 // Constants are valid everywhere, we can simply take them.
1099 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end());
1100 continue;
1101 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00001102 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001103 }
1104
Johannes Doerfertdef99282019-08-14 21:29:37 +00001105 // To avoid modifications to the ReturnedValues map while we iterate over it
1106 // we kept record of potential new entries in a copy map, NewRVsMap.
1107 for (auto &It : NewRVsMap) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001108 assert(!It.second.empty() && "Entry does not add anything.");
1109 auto &ReturnInsts = ReturnedValues[It.first];
1110 for (ReturnInst *RI : It.second)
Johannes Doerfert695089e2019-08-23 15:23:49 +00001111 if (ReturnInsts.insert(RI)) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001112 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1113 << *It.first << " => " << *RI << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001114 Changed = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001115 }
1116 }
1117
Johannes Doerfertdef99282019-08-14 21:29:37 +00001118 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1119 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001120}
1121
Johannes Doerfertdef99282019-08-14 21:29:37 +00001122struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1123 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1124
1125 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001126 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
Johannes Doerfertdef99282019-08-14 21:29:37 +00001127};
1128
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001129/// Returned values information for a call sites.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001130struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1131 AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1132
1133 /// See AbstractAttribute::initialize(...).
1134 void initialize(Attributor &A) override {
1135 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001136 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001137 // sense to specialize attributes for call sites instead of
1138 // redirecting requests to the callee.
1139 llvm_unreachable("Abstract attributes for returned values are not "
1140 "supported for call sites yet!");
1141 }
1142
1143 /// See AbstractAttribute::updateImpl(...).
1144 ChangeStatus updateImpl(Attributor &A) override {
1145 return indicatePessimisticFixpoint();
1146 }
1147
1148 /// See AbstractAttribute::trackStatistics()
1149 void trackStatistics() const override {}
1150};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001151
Stefan Stipanovic06263672019-07-11 21:37:40 +00001152/// ------------------------ NoSync Function Attribute -------------------------
1153
Johannes Doerfert344d0382019-08-07 22:34:26 +00001154struct AANoSyncImpl : AANoSync {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001155 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
Stefan Stipanovic06263672019-07-11 21:37:40 +00001156
Stefan Stipanoviccb5ecae2019-07-12 18:34:06 +00001157 const std::string getAsStr() const override {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001158 return getAssumed() ? "nosync" : "may-sync";
1159 }
1160
1161 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001162 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001163
Stefan Stipanovic06263672019-07-11 21:37:40 +00001164 /// Helper function used to determine whether an instruction is non-relaxed
1165 /// atomic. In other words, if an atomic instruction does not have unordered
1166 /// or monotonic ordering
1167 static bool isNonRelaxedAtomic(Instruction *I);
1168
1169 /// Helper function used to determine whether an instruction is volatile.
1170 static bool isVolatile(Instruction *I);
1171
Johannes Doerfertc7a1db32019-07-13 01:09:27 +00001172 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1173 /// memset).
Stefan Stipanovic06263672019-07-11 21:37:40 +00001174 static bool isNoSyncIntrinsic(Instruction *I);
1175};
1176
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001177bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001178 if (!I->isAtomic())
1179 return false;
1180
1181 AtomicOrdering Ordering;
1182 switch (I->getOpcode()) {
1183 case Instruction::AtomicRMW:
1184 Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1185 break;
1186 case Instruction::Store:
1187 Ordering = cast<StoreInst>(I)->getOrdering();
1188 break;
1189 case Instruction::Load:
1190 Ordering = cast<LoadInst>(I)->getOrdering();
1191 break;
1192 case Instruction::Fence: {
1193 auto *FI = cast<FenceInst>(I);
1194 if (FI->getSyncScopeID() == SyncScope::SingleThread)
1195 return false;
1196 Ordering = FI->getOrdering();
1197 break;
1198 }
1199 case Instruction::AtomicCmpXchg: {
1200 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1201 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1202 // Only if both are relaxed, than it can be treated as relaxed.
1203 // Otherwise it is non-relaxed.
1204 if (Success != AtomicOrdering::Unordered &&
1205 Success != AtomicOrdering::Monotonic)
1206 return true;
1207 if (Failure != AtomicOrdering::Unordered &&
1208 Failure != AtomicOrdering::Monotonic)
1209 return true;
1210 return false;
1211 }
1212 default:
1213 llvm_unreachable(
1214 "New atomic operations need to be known in the attributor.");
1215 }
1216
1217 // Relaxed.
1218 if (Ordering == AtomicOrdering::Unordered ||
1219 Ordering == AtomicOrdering::Monotonic)
1220 return false;
1221 return true;
1222}
1223
1224/// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1225/// FIXME: We should ipmrove the handling of intrinsics.
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001226bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001227 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1228 switch (II->getIntrinsicID()) {
1229 /// Element wise atomic memory intrinsics are can only be unordered,
1230 /// therefore nosync.
1231 case Intrinsic::memset_element_unordered_atomic:
1232 case Intrinsic::memmove_element_unordered_atomic:
1233 case Intrinsic::memcpy_element_unordered_atomic:
1234 return true;
1235 case Intrinsic::memset:
1236 case Intrinsic::memmove:
1237 case Intrinsic::memcpy:
1238 if (!cast<MemIntrinsic>(II)->isVolatile())
1239 return true;
1240 return false;
1241 default:
1242 return false;
1243 }
1244 }
1245 return false;
1246}
1247
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001248bool AANoSyncImpl::isVolatile(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001249 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
1250 "Calls should not be checked here");
1251
1252 switch (I->getOpcode()) {
1253 case Instruction::AtomicRMW:
1254 return cast<AtomicRMWInst>(I)->isVolatile();
1255 case Instruction::Store:
1256 return cast<StoreInst>(I)->isVolatile();
1257 case Instruction::Load:
1258 return cast<LoadInst>(I)->isVolatile();
1259 case Instruction::AtomicCmpXchg:
1260 return cast<AtomicCmpXchgInst>(I)->isVolatile();
1261 default:
1262 return false;
1263 }
1264}
1265
Johannes Doerfertece81902019-08-12 22:05:53 +00001266ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001267
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001268 auto CheckRWInstForNoSync = [&](Instruction &I) {
1269 /// We are looking for volatile instructions or Non-Relaxed atomics.
1270 /// FIXME: We should ipmrove the handling of intrinsics.
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001271
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001272 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1273 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001274
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001275 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1276 if (ICS.hasFnAttr(Attribute::NoSync))
1277 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001278
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001279 const auto &NoSyncAA =
1280 A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS));
1281 if (NoSyncAA.isAssumedNoSync())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001282 return true;
1283 return false;
1284 }
Stefan Stipanovic06263672019-07-11 21:37:40 +00001285
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001286 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1287 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001288
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001289 return false;
1290 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001291
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001292 auto CheckForNoSync = [&](Instruction &I) {
1293 // At this point we handled all read/write effects and they are all
1294 // nosync, so they can be skipped.
1295 if (I.mayReadOrWriteMemory())
1296 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001297
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001298 // non-convergent and readnone imply nosync.
1299 return !ImmutableCallSite(&I).isConvergent();
1300 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001301
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001302 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1303 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001304 return indicatePessimisticFixpoint();
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001305
Stefan Stipanovic06263672019-07-11 21:37:40 +00001306 return ChangeStatus::UNCHANGED;
1307}
1308
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001309struct AANoSyncFunction final : public AANoSyncImpl {
1310 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1311
1312 /// See AbstractAttribute::trackStatistics()
1313 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1314};
1315
1316/// NoSync attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001317struct AANoSyncCallSite final : AANoSyncImpl {
1318 AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1319
1320 /// See AbstractAttribute::initialize(...).
1321 void initialize(Attributor &A) override {
1322 AANoSyncImpl::initialize(A);
1323 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001324 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001325 indicatePessimisticFixpoint();
1326 }
1327
1328 /// See AbstractAttribute::updateImpl(...).
1329 ChangeStatus updateImpl(Attributor &A) override {
1330 // TODO: Once we have call site specific value information we can provide
1331 // call site specific liveness information and then it makes
1332 // sense to specialize attributes for call sites arguments instead of
1333 // redirecting requests to the callee argument.
1334 Function *F = getAssociatedFunction();
1335 const IRPosition &FnPos = IRPosition::function(*F);
1336 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos);
1337 return clampStateAndIndicateChange(
1338 getState(), static_cast<const AANoSync::StateType &>(FnAA.getState()));
1339 }
1340
1341 /// See AbstractAttribute::trackStatistics()
1342 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1343};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001344
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001345/// ------------------------ No-Free Attributes ----------------------------
1346
Johannes Doerfert344d0382019-08-07 22:34:26 +00001347struct AANoFreeImpl : public AANoFree {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001348 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001349
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001350 /// See AbstractAttribute::updateImpl(...).
1351 ChangeStatus updateImpl(Attributor &A) override {
1352 auto CheckForNoFree = [&](Instruction &I) {
1353 ImmutableCallSite ICS(&I);
1354 if (ICS.hasFnAttr(Attribute::NoFree))
1355 return true;
1356
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001357 const auto &NoFreeAA =
1358 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS));
1359 return NoFreeAA.isAssumedNoFree();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001360 };
1361
1362 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1363 return indicatePessimisticFixpoint();
1364 return ChangeStatus::UNCHANGED;
1365 }
1366
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001367 /// See AbstractAttribute::getAsStr().
1368 const std::string getAsStr() const override {
1369 return getAssumed() ? "nofree" : "may-free";
1370 }
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001371};
1372
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001373struct AANoFreeFunction final : public AANoFreeImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001374 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001375
1376 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001377 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001378};
1379
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001380/// NoFree attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001381struct AANoFreeCallSite final : AANoFreeImpl {
1382 AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1383
1384 /// See AbstractAttribute::initialize(...).
1385 void initialize(Attributor &A) override {
1386 AANoFreeImpl::initialize(A);
1387 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001388 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001389 indicatePessimisticFixpoint();
1390 }
1391
1392 /// See AbstractAttribute::updateImpl(...).
1393 ChangeStatus updateImpl(Attributor &A) override {
1394 // TODO: Once we have call site specific value information we can provide
1395 // call site specific liveness information and then it makes
1396 // sense to specialize attributes for call sites arguments instead of
1397 // redirecting requests to the callee argument.
1398 Function *F = getAssociatedFunction();
1399 const IRPosition &FnPos = IRPosition::function(*F);
1400 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos);
1401 return clampStateAndIndicateChange(
1402 getState(), static_cast<const AANoFree::StateType &>(FnAA.getState()));
1403 }
1404
1405 /// See AbstractAttribute::trackStatistics()
1406 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1407};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001408
Hideto Ueno54869ec2019-07-15 06:49:04 +00001409/// ------------------------ NonNull Argument Attribute ------------------------
Johannes Doerfert344d0382019-08-07 22:34:26 +00001410struct AANonNullImpl : AANonNull {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001411 AANonNullImpl(const IRPosition &IRP) : AANonNull(IRP) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001412
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001413 /// See AbstractAttribute::initialize(...).
1414 void initialize(Attributor &A) override {
1415 if (hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
1416 indicateOptimisticFixpoint();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001417 else
1418 AANonNull::initialize(A);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001419 }
1420
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001421 /// See AbstractAttribute::getAsStr().
1422 const std::string getAsStr() const override {
1423 return getAssumed() ? "nonnull" : "may-null";
1424 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001425};
1426
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001427/// NonNull attribute for a floating value.
1428struct AANonNullFloating : AANonNullImpl {
1429 AANonNullFloating(const IRPosition &IRP) : AANonNullImpl(IRP) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001430
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001431 /// See AbstractAttribute::initialize(...).
1432 void initialize(Attributor &A) override {
1433 AANonNullImpl::initialize(A);
Hideto Ueno54869ec2019-07-15 06:49:04 +00001434
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001435 if (isAtFixpoint())
1436 return;
Hideto Ueno54869ec2019-07-15 06:49:04 +00001437
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001438 const IRPosition &IRP = getIRPosition();
1439 const Value &V = IRP.getAssociatedValue();
1440 const DataLayout &DL = A.getDataLayout();
Hideto Ueno54869ec2019-07-15 06:49:04 +00001441
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001442 // TODO: This context sensitive query should be removed once we can do
1443 // context sensitive queries in the genericValueTraversal below.
1444 if (isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, IRP.getCtxI(),
1445 /* TODO: DT */ nullptr))
1446 indicateOptimisticFixpoint();
1447 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001448
1449 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001450 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001451 const DataLayout &DL = A.getDataLayout();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001452
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001453 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
1454 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001455 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V));
1456 if (!Stripped && this == &AA) {
1457 if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr,
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001458 /* TODO: CtxI */ nullptr,
1459 /* TODO: DT */ nullptr))
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001460 T.indicatePessimisticFixpoint();
1461 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001462 // Use abstract attribute information.
1463 const AANonNull::StateType &NS =
1464 static_cast<const AANonNull::StateType &>(AA.getState());
1465 T ^= NS;
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001466 }
1467 return T.isValidState();
1468 };
1469
1470 StateType T;
1471 if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this,
1472 T, VisitValueCB))
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001473 return indicatePessimisticFixpoint();
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001474
1475 return clampStateAndIndicateChange(getState(), T);
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001476 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001477
1478 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001479 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001480};
1481
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001482/// NonNull attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001483struct AANonNullReturned final
1484 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001485 AANonNullReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001486 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001487
1488 /// See AbstractAttribute::trackStatistics()
1489 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
1490};
1491
Hideto Ueno54869ec2019-07-15 06:49:04 +00001492/// NonNull attribute for function argument.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001493struct AANonNullArgument final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001494 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001495 AANonNullArgument(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001496 : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001497
1498 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001499 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001500};
1501
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001502struct AANonNullCallSiteArgument final : AANonNullFloating {
1503 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001504
1505 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert1aac1822019-08-29 01:26:09 +00001506 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00001507};
Johannes Doerfert007153e2019-08-05 23:26:06 +00001508
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001509/// NonNull attribute for a call site return position.
1510struct AANonNullCallSiteReturned final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001511 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001512 AANonNullCallSiteReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001513 : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001514
1515 /// See AbstractAttribute::trackStatistics()
1516 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
1517};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001518
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001519/// ------------------------ No-Recurse Attributes ----------------------------
1520
1521struct AANoRecurseImpl : public AANoRecurse {
1522 AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {}
1523
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001524 /// See AbstractAttribute::getAsStr()
1525 const std::string getAsStr() const override {
1526 return getAssumed() ? "norecurse" : "may-recurse";
1527 }
1528};
1529
1530struct AANoRecurseFunction final : AANoRecurseImpl {
1531 AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
1532
Hideto Ueno63f60662019-09-21 15:13:19 +00001533 /// See AbstractAttribute::initialize(...).
1534 void initialize(Attributor &A) override {
1535 AANoRecurseImpl::initialize(A);
1536 if (const Function *F = getAnchorScope())
1537 if (A.getInfoCache().getSccSize(*F) == 1)
1538 return;
1539 indicatePessimisticFixpoint();
1540 }
1541
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001542 /// See AbstractAttribute::updateImpl(...).
1543 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno63f60662019-09-21 15:13:19 +00001544
1545 auto CheckForNoRecurse = [&](Instruction &I) {
1546 ImmutableCallSite ICS(&I);
1547 if (ICS.hasFnAttr(Attribute::NoRecurse))
1548 return true;
1549
1550 const auto &NoRecurseAA =
1551 A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS));
1552 if (!NoRecurseAA.isAssumedNoRecurse())
1553 return false;
1554
1555 // Recursion to the same function
1556 if (ICS.getCalledFunction() == getAnchorScope())
1557 return false;
1558
1559 return true;
1560 };
1561
1562 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this))
1563 return indicatePessimisticFixpoint();
1564 return ChangeStatus::UNCHANGED;
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001565 }
1566
1567 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
1568};
1569
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001570/// NoRecurse attribute deduction for a call sites.
1571struct AANoRecurseCallSite final : AANoRecurseImpl {
1572 AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
1573
1574 /// See AbstractAttribute::initialize(...).
1575 void initialize(Attributor &A) override {
1576 AANoRecurseImpl::initialize(A);
1577 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001578 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001579 indicatePessimisticFixpoint();
1580 }
1581
1582 /// See AbstractAttribute::updateImpl(...).
1583 ChangeStatus updateImpl(Attributor &A) override {
1584 // TODO: Once we have call site specific value information we can provide
1585 // call site specific liveness information and then it makes
1586 // sense to specialize attributes for call sites arguments instead of
1587 // redirecting requests to the callee argument.
1588 Function *F = getAssociatedFunction();
1589 const IRPosition &FnPos = IRPosition::function(*F);
1590 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos);
1591 return clampStateAndIndicateChange(
1592 getState(),
1593 static_cast<const AANoRecurse::StateType &>(FnAA.getState()));
1594 }
1595
1596 /// See AbstractAttribute::trackStatistics()
1597 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
1598};
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001599
Hideto Ueno11d37102019-07-17 15:15:43 +00001600/// ------------------------ Will-Return Attributes ----------------------------
1601
Hideto Ueno11d37102019-07-17 15:15:43 +00001602// Helper function that checks whether a function has any cycle.
1603// TODO: Replace with more efficent code
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001604static bool containsCycle(Function &F) {
Hideto Ueno11d37102019-07-17 15:15:43 +00001605 SmallPtrSet<BasicBlock *, 32> Visited;
1606
1607 // Traverse BB by dfs and check whether successor is already visited.
1608 for (BasicBlock *BB : depth_first(&F)) {
1609 Visited.insert(BB);
1610 for (auto *SuccBB : successors(BB)) {
1611 if (Visited.count(SuccBB))
1612 return true;
1613 }
1614 }
1615 return false;
1616}
1617
1618// Helper function that checks the function have a loop which might become an
1619// endless loop
1620// FIXME: Any cycle is regarded as endless loop for now.
1621// We have to allow some patterns.
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001622static bool containsPossiblyEndlessLoop(Function *F) {
1623 return !F || !F->hasExactDefinition() || containsCycle(*F);
Hideto Ueno11d37102019-07-17 15:15:43 +00001624}
1625
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001626struct AAWillReturnImpl : public AAWillReturn {
1627 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
Hideto Ueno11d37102019-07-17 15:15:43 +00001628
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001629 /// See AbstractAttribute::initialize(...).
1630 void initialize(Attributor &A) override {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001631 AAWillReturn::initialize(A);
Hideto Ueno11d37102019-07-17 15:15:43 +00001632
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001633 Function *F = getAssociatedFunction();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001634 if (containsPossiblyEndlessLoop(F))
1635 indicatePessimisticFixpoint();
1636 }
Hideto Ueno11d37102019-07-17 15:15:43 +00001637
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001638 /// See AbstractAttribute::updateImpl(...).
1639 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001640 auto CheckForWillReturn = [&](Instruction &I) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001641 IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I));
1642 const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos);
1643 if (WillReturnAA.isKnownWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001644 return true;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001645 if (!WillReturnAA.isAssumedWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001646 return false;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001647 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos);
1648 return NoRecurseAA.isAssumedNoRecurse();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001649 };
1650
1651 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
1652 return indicatePessimisticFixpoint();
1653
1654 return ChangeStatus::UNCHANGED;
1655 }
1656
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001657 /// See AbstractAttribute::getAsStr()
1658 const std::string getAsStr() const override {
1659 return getAssumed() ? "willreturn" : "may-noreturn";
1660 }
1661};
1662
1663struct AAWillReturnFunction final : AAWillReturnImpl {
1664 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
1665
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001666 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001667 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001668};
Hideto Ueno11d37102019-07-17 15:15:43 +00001669
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001670/// WillReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001671struct AAWillReturnCallSite final : AAWillReturnImpl {
1672 AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
1673
1674 /// See AbstractAttribute::initialize(...).
1675 void initialize(Attributor &A) override {
1676 AAWillReturnImpl::initialize(A);
1677 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001678 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001679 indicatePessimisticFixpoint();
1680 }
1681
1682 /// See AbstractAttribute::updateImpl(...).
1683 ChangeStatus updateImpl(Attributor &A) override {
1684 // TODO: Once we have call site specific value information we can provide
1685 // call site specific liveness information and then it makes
1686 // sense to specialize attributes for call sites arguments instead of
1687 // redirecting requests to the callee argument.
1688 Function *F = getAssociatedFunction();
1689 const IRPosition &FnPos = IRPosition::function(*F);
1690 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos);
1691 return clampStateAndIndicateChange(
1692 getState(),
1693 static_cast<const AAWillReturn::StateType &>(FnAA.getState()));
1694 }
1695
1696 /// See AbstractAttribute::trackStatistics()
1697 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
1698};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001699
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001700/// ------------------------ NoAlias Argument Attribute ------------------------
1701
Johannes Doerfert344d0382019-08-07 22:34:26 +00001702struct AANoAliasImpl : AANoAlias {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001703 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001704
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001705 const std::string getAsStr() const override {
1706 return getAssumed() ? "noalias" : "may-alias";
1707 }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001708};
1709
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001710/// NoAlias attribute for a floating value.
1711struct AANoAliasFloating final : AANoAliasImpl {
1712 AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1713
Hideto Uenocbab3342019-08-29 05:52:00 +00001714 /// See AbstractAttribute::initialize(...).
1715 void initialize(Attributor &A) override {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00001716 AANoAliasImpl::initialize(A);
1717 if (isa<AllocaInst>(getAnchorValue()))
1718 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00001719 }
1720
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001721 /// See AbstractAttribute::updateImpl(...).
1722 ChangeStatus updateImpl(Attributor &A) override {
1723 // TODO: Implement this.
1724 return indicatePessimisticFixpoint();
1725 }
1726
1727 /// See AbstractAttribute::trackStatistics()
1728 void trackStatistics() const override {
1729 STATS_DECLTRACK_FLOATING_ATTR(noalias)
1730 }
1731};
1732
1733/// NoAlias attribute for an argument.
Hideto Uenocbab3342019-08-29 05:52:00 +00001734struct AANoAliasArgument final
1735 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
1736 AANoAliasArgument(const IRPosition &IRP)
1737 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>(IRP) {}
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001738
1739 /// See AbstractAttribute::trackStatistics()
1740 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
1741};
1742
1743struct AANoAliasCallSiteArgument final : AANoAliasImpl {
1744 AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1745
Hideto Uenocbab3342019-08-29 05:52:00 +00001746 /// See AbstractAttribute::initialize(...).
1747 void initialize(Attributor &A) override {
Hideto Ueno6381b142019-08-30 10:00:32 +00001748 // See callsite argument attribute and callee argument attribute.
1749 ImmutableCallSite ICS(&getAnchorValue());
1750 if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias))
1751 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00001752 }
1753
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001754 /// See AbstractAttribute::updateImpl(...).
1755 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00001756 // We can deduce "noalias" if the following conditions hold.
1757 // (i) Associated value is assumed to be noalias in the definition.
1758 // (ii) Associated value is assumed to be no-capture in all the uses
1759 // possibly executed before this callsite.
1760 // (iii) There is no other pointer argument which could alias with the
1761 // value.
1762
1763 const Value &V = getAssociatedValue();
1764 const IRPosition IRP = IRPosition::value(V);
1765
1766 // (i) Check whether noalias holds in the definition.
1767
1768 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP);
1769
1770 if (!NoAliasAA.isAssumedNoAlias())
1771 return indicatePessimisticFixpoint();
1772
1773 LLVM_DEBUG(dbgs() << "[Attributor][AANoAliasCSArg] " << V
1774 << " is assumed NoAlias in the definition\n");
1775
1776 // (ii) Check whether the value is captured in the scope using AANoCapture.
1777 // FIXME: This is conservative though, it is better to look at CFG and
1778 // check only uses possibly executed before this callsite.
1779
1780 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
1781 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned())
1782 return indicatePessimisticFixpoint();
1783
1784 // (iii) Check there is no other pointer argument which could alias with the
1785 // value.
1786 ImmutableCallSite ICS(&getAnchorValue());
1787 for (unsigned i = 0; i < ICS.getNumArgOperands(); i++) {
1788 if (getArgNo() == (int)i)
1789 continue;
1790 const Value *ArgOp = ICS.getArgOperand(i);
1791 if (!ArgOp->getType()->isPointerTy())
1792 continue;
1793
Hideto Ueno30d86f12019-09-17 06:53:27 +00001794 if (const Function *F = getAnchorScope()) {
1795 if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) {
1796 LLVM_DEBUG(dbgs()
1797 << "[Attributor][NoAliasCSArg] Check alias between "
1798 "callsite arguments "
1799 << AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " "
1800 << getAssociatedValue() << " " << *ArgOp << "\n");
Hideto Ueno1d68ed82019-09-11 07:00:33 +00001801
Hideto Ueno30d86f12019-09-17 06:53:27 +00001802 if (AAR->isNoAlias(&getAssociatedValue(), ArgOp))
1803 continue;
1804 }
1805 }
Hideto Ueno1d68ed82019-09-11 07:00:33 +00001806 return indicatePessimisticFixpoint();
1807 }
1808
1809 return ChangeStatus::UNCHANGED;
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001810 }
1811
1812 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00001813 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001814};
1815
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001816/// NoAlias attribute for function return value.
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001817struct AANoAliasReturned final : AANoAliasImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001818 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001819
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001820 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001821 virtual ChangeStatus updateImpl(Attributor &A) override {
1822
1823 auto CheckReturnValue = [&](Value &RV) -> bool {
1824 if (Constant *C = dyn_cast<Constant>(&RV))
1825 if (C->isNullValue() || isa<UndefValue>(C))
1826 return true;
1827
1828 /// For now, we can only deduce noalias if we have call sites.
1829 /// FIXME: add more support.
1830 ImmutableCallSite ICS(&RV);
1831 if (!ICS)
1832 return false;
1833
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00001834 const IRPosition &RVPos = IRPosition::value(RV);
1835 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001836 if (!NoAliasAA.isAssumedNoAlias())
1837 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001838
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00001839 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos);
1840 return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001841 };
1842
1843 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
1844 return indicatePessimisticFixpoint();
1845
1846 return ChangeStatus::UNCHANGED;
1847 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001848
1849 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001850 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00001851};
1852
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001853/// NoAlias attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001854struct AANoAliasCallSiteReturned final : AANoAliasImpl {
1855 AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
1856
1857 /// See AbstractAttribute::initialize(...).
1858 void initialize(Attributor &A) override {
1859 AANoAliasImpl::initialize(A);
1860 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001861 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001862 indicatePessimisticFixpoint();
1863 }
1864
1865 /// See AbstractAttribute::updateImpl(...).
1866 ChangeStatus updateImpl(Attributor &A) override {
1867 // TODO: Once we have call site specific value information we can provide
1868 // call site specific liveness information and then it makes
1869 // sense to specialize attributes for call sites arguments instead of
1870 // redirecting requests to the callee argument.
1871 Function *F = getAssociatedFunction();
1872 const IRPosition &FnPos = IRPosition::returned(*F);
1873 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos);
1874 return clampStateAndIndicateChange(
1875 getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState()));
1876 }
1877
1878 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00001879 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001880};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001881
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001882/// -------------------AAIsDead Function Attribute-----------------------
1883
Johannes Doerfert344d0382019-08-07 22:34:26 +00001884struct AAIsDeadImpl : public AAIsDead {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001885 AAIsDeadImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001886
Johannes Doerfertece81902019-08-12 22:05:53 +00001887 void initialize(Attributor &A) override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001888 const Function *F = getAssociatedFunction();
Johannes Doerfert97fd5822019-09-04 16:26:20 +00001889 if (F && !F->isDeclaration())
1890 exploreFromEntry(A, F);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001891 }
1892
1893 void exploreFromEntry(Attributor &A, const Function *F) {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001894 ToBeExploredPaths.insert(&(F->getEntryBlock().front()));
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001895
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001896 for (size_t i = 0; i < ToBeExploredPaths.size(); ++i)
Johannes Doerfert4361da22019-08-04 18:38:53 +00001897 if (const Instruction *NextNoReturnI =
1898 findNextNoReturn(A, ToBeExploredPaths[i]))
1899 NoReturnCalls.insert(NextNoReturnI);
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00001900
1901 // Mark the block live after we looked for no-return instructions.
1902 assumeLive(A, F->getEntryBlock());
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001903 }
1904
Johannes Doerfert4361da22019-08-04 18:38:53 +00001905 /// Find the next assumed noreturn instruction in the block of \p I starting
1906 /// from, thus including, \p I.
1907 ///
1908 /// The caller is responsible to monitor the ToBeExploredPaths set as new
1909 /// instructions discovered in other basic block will be placed in there.
1910 ///
1911 /// \returns The next assumed noreturn instructions in the block of \p I
1912 /// starting from, thus including, \p I.
1913 const Instruction *findNextNoReturn(Attributor &A, const Instruction *I);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001914
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001915 /// See AbstractAttribute::getAsStr().
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001916 const std::string getAsStr() const override {
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001917 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
Johannes Doerfert6dedc782019-08-16 21:31:11 +00001918 std::to_string(getAssociatedFunction()->size()) + "][#NRI " +
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001919 std::to_string(NoReturnCalls.size()) + "]";
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001920 }
1921
1922 /// See AbstractAttribute::manifest(...).
1923 ChangeStatus manifest(Attributor &A) override {
1924 assert(getState().isValidState() &&
1925 "Attempted to manifest an invalid state!");
1926
1927 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001928 Function &F = *getAssociatedFunction();
1929
1930 if (AssumedLiveBlocks.empty()) {
Johannes Doerfertb19cd272019-09-03 20:42:16 +00001931 A.deleteAfterManifest(F);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00001932 return ChangeStatus::CHANGED;
1933 }
Johannes Doerfert924d2132019-08-05 21:34:45 +00001934
Johannes Doerfertbeb51502019-08-07 22:36:15 +00001935 // Flag to determine if we can change an invoke to a call assuming the
1936 // callee is nounwind. This is not possible if the personality of the
1937 // function allows to catch asynchronous exceptions.
Johannes Doerfert924d2132019-08-05 21:34:45 +00001938 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001939
Johannes Doerfert4361da22019-08-04 18:38:53 +00001940 for (const Instruction *NRC : NoReturnCalls) {
1941 Instruction *I = const_cast<Instruction *>(NRC);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001942 BasicBlock *BB = I->getParent();
Johannes Doerfert4361da22019-08-04 18:38:53 +00001943 Instruction *SplitPos = I->getNextNode();
Johannes Doerfertd4108052019-08-21 20:56:41 +00001944 // TODO: mark stuff before unreachable instructions as dead.
1945 if (isa_and_nonnull<UnreachableInst>(SplitPos))
1946 continue;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001947
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001948 if (auto *II = dyn_cast<InvokeInst>(I)) {
Johannes Doerfert3d7bbc62019-08-05 21:35:02 +00001949 // If we keep the invoke the split position is at the beginning of the
1950 // normal desitination block (it invokes a noreturn function after all).
1951 BasicBlock *NormalDestBB = II->getNormalDest();
1952 SplitPos = &NormalDestBB->front();
1953
Johannes Doerfert4361da22019-08-04 18:38:53 +00001954 /// Invoke is replaced with a call and unreachable is placed after it if
1955 /// the callee is nounwind and noreturn. Otherwise, we keep the invoke
1956 /// and only place an unreachable in the normal successor.
Johannes Doerfert924d2132019-08-05 21:34:45 +00001957 if (Invoke2CallAllowed) {
Michael Liaoa99086d2019-08-20 21:02:31 +00001958 if (II->getCalledFunction()) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001959 const IRPosition &IPos = IRPosition::callsite_function(*II);
1960 const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos);
1961 if (AANoUnw.isAssumedNoUnwind()) {
Johannes Doerfert924d2132019-08-05 21:34:45 +00001962 LLVM_DEBUG(dbgs()
1963 << "[AAIsDead] Replace invoke with call inst\n");
Johannes Doerfert3d7bbc62019-08-05 21:35:02 +00001964 // We do not need an invoke (II) but instead want a call followed
1965 // by an unreachable. However, we do not remove II as other
1966 // abstract attributes might have it cached as part of their
1967 // results. Given that we modify the CFG anyway, we simply keep II
1968 // around but in a new dead block. To avoid II being live through
1969 // a different edge we have to ensure the block we place it in is
1970 // only reached from the current block of II and then not reached
1971 // at all when we insert the unreachable.
1972 SplitBlockPredecessors(NormalDestBB, {BB}, ".i2c");
1973 CallInst *CI = createCallMatchingInvoke(II);
1974 CI->insertBefore(II);
1975 CI->takeName(II);
1976 II->replaceAllUsesWith(CI);
1977 SplitPos = CI->getNextNode();
Johannes Doerfert924d2132019-08-05 21:34:45 +00001978 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00001979 }
1980 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00001981
Johannes Doerfert7ab52532019-09-04 20:34:52 +00001982 if (SplitPos == &NormalDestBB->front()) {
1983 // If this is an invoke of a noreturn function the edge to the normal
1984 // destination block is dead but not necessarily the block itself.
1985 // TODO: We need to move to an edge based system during deduction and
1986 // also manifest.
1987 assert(!NormalDestBB->isLandingPad() &&
1988 "Expected the normal destination not to be a landingpad!");
1989 BasicBlock *SplitBB =
1990 SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
1991 // The split block is live even if it contains only an unreachable
1992 // instruction at the end.
1993 assumeLive(A, *SplitBB);
1994 SplitPos = SplitBB->getTerminator();
1995 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00001996 }
1997
Johannes Doerfert3d7bbc62019-08-05 21:35:02 +00001998 BB = SplitPos->getParent();
Johannes Doerfert4361da22019-08-04 18:38:53 +00001999 SplitBlock(BB, SplitPos);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002000 changeToUnreachable(BB->getTerminator(), /* UseLLVMTrap */ false);
2001 HasChanged = ChangeStatus::CHANGED;
2002 }
2003
Johannes Doerfertb19cd272019-09-03 20:42:16 +00002004 for (BasicBlock &BB : F)
2005 if (!AssumedLiveBlocks.count(&BB))
2006 A.deleteAfterManifest(BB);
2007
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002008 return HasChanged;
2009 }
2010
2011 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00002012 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002013
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002014 /// See AAIsDead::isAssumedDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00002015 bool isAssumedDead(const BasicBlock *BB) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002016 assert(BB->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002017 "BB must be in the same anchor scope function.");
2018
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002019 if (!getAssumed())
2020 return false;
2021 return !AssumedLiveBlocks.count(BB);
2022 }
2023
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002024 /// See AAIsDead::isKnownDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00002025 bool isKnownDead(const BasicBlock *BB) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002026 return getKnown() && isAssumedDead(BB);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002027 }
2028
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002029 /// See AAIsDead::isAssumed(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00002030 bool isAssumedDead(const Instruction *I) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002031 assert(I->getParent()->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002032 "Instruction must be in the same anchor scope function.");
2033
Stefan Stipanovic7849e412019-08-03 15:27:41 +00002034 if (!getAssumed())
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002035 return false;
2036
2037 // If it is not in AssumedLiveBlocks then it for sure dead.
2038 // Otherwise, it can still be after noreturn call in a live block.
2039 if (!AssumedLiveBlocks.count(I->getParent()))
2040 return true;
2041
2042 // If it is not after a noreturn call, than it is live.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002043 return isAfterNoReturn(I);
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002044 }
2045
2046 /// See AAIsDead::isKnownDead(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00002047 bool isKnownDead(const Instruction *I) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002048 return getKnown() && isAssumedDead(I);
2049 }
2050
2051 /// Check if instruction is after noreturn call, in other words, assumed dead.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002052 bool isAfterNoReturn(const Instruction *I) const;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002053
Johannes Doerfert924d2132019-08-05 21:34:45 +00002054 /// Determine if \p F might catch asynchronous exceptions.
2055 static bool mayCatchAsynchronousExceptions(const Function &F) {
2056 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
2057 }
2058
Johannes Doerfert2f622062019-09-04 16:35:20 +00002059 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
2060 /// that internal function called from \p BB should now be looked at.
2061 void assumeLive(Attributor &A, const BasicBlock &BB) {
2062 if (!AssumedLiveBlocks.insert(&BB).second)
2063 return;
2064
2065 // We assume that all of BB is (probably) live now and if there are calls to
2066 // internal functions we will assume that those are now live as well. This
2067 // is a performance optimization for blocks with calls to a lot of internal
2068 // functions. It can however cause dead functions to be treated as live.
2069 for (const Instruction &I : BB)
2070 if (ImmutableCallSite ICS = ImmutableCallSite(&I))
2071 if (const Function *F = ICS.getCalledFunction())
2072 if (F->hasInternalLinkage())
2073 A.markLiveInternalFunction(*F);
2074 }
2075
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002076 /// Collection of to be explored paths.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002077 SmallSetVector<const Instruction *, 8> ToBeExploredPaths;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002078
2079 /// Collection of all assumed live BasicBlocks.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002080 DenseSet<const BasicBlock *> AssumedLiveBlocks;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002081
2082 /// Collection of calls with noreturn attribute, assumed or knwon.
Johannes Doerfert4361da22019-08-04 18:38:53 +00002083 SmallSetVector<const Instruction *, 4> NoReturnCalls;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002084};
2085
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002086struct AAIsDeadFunction final : public AAIsDeadImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002087 AAIsDeadFunction(const IRPosition &IRP) : AAIsDeadImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002088
2089 /// See AbstractAttribute::trackStatistics()
2090 void trackStatistics() const override {
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002091 STATS_DECL(PartiallyDeadBlocks, Function,
2092 "Number of basic blocks classified as partially dead");
2093 BUILD_STAT_NAME(PartiallyDeadBlocks, Function) += NoReturnCalls.size();
2094 }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002095};
2096
2097bool AAIsDeadImpl::isAfterNoReturn(const Instruction *I) const {
Johannes Doerfert4361da22019-08-04 18:38:53 +00002098 const Instruction *PrevI = I->getPrevNode();
Stefan Stipanovicd0216172019-08-02 21:31:22 +00002099 while (PrevI) {
2100 if (NoReturnCalls.count(PrevI))
2101 return true;
2102 PrevI = PrevI->getPrevNode();
2103 }
2104 return false;
2105}
2106
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002107const Instruction *AAIsDeadImpl::findNextNoReturn(Attributor &A,
2108 const Instruction *I) {
Johannes Doerfert4361da22019-08-04 18:38:53 +00002109 const BasicBlock *BB = I->getParent();
Johannes Doerfert924d2132019-08-05 21:34:45 +00002110 const Function &F = *BB->getParent();
2111
2112 // Flag to determine if we can change an invoke to a call assuming the callee
2113 // is nounwind. This is not possible if the personality of the function allows
2114 // to catch asynchronous exceptions.
2115 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
Johannes Doerfert4361da22019-08-04 18:38:53 +00002116
2117 // TODO: We should have a function that determines if an "edge" is dead.
2118 // Edges could be from an instruction to the next or from a terminator
2119 // to the successor. For now, we need to special case the unwind block
2120 // of InvokeInst below.
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002121
2122 while (I) {
2123 ImmutableCallSite ICS(I);
2124
2125 if (ICS) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002126 const IRPosition &IPos = IRPosition::callsite_function(ICS);
Johannes Doerfert4361da22019-08-04 18:38:53 +00002127 // Regarless of the no-return property of an invoke instruction we only
2128 // learn that the regular successor is not reachable through this
2129 // instruction but the unwind block might still be.
2130 if (auto *Invoke = dyn_cast<InvokeInst>(I)) {
2131 // Use nounwind to justify the unwind block is dead as well.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002132 const auto &AANoUnw = A.getAAFor<AANoUnwind>(*this, IPos);
2133 if (!Invoke2CallAllowed || !AANoUnw.isAssumedNoUnwind()) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00002134 assumeLive(A, *Invoke->getUnwindDest());
Johannes Doerfert4361da22019-08-04 18:38:53 +00002135 ToBeExploredPaths.insert(&Invoke->getUnwindDest()->front());
2136 }
2137 }
2138
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002139 const auto &NoReturnAA = A.getAAFor<AANoReturn>(*this, IPos);
2140 if (NoReturnAA.isAssumedNoReturn())
Johannes Doerfert4361da22019-08-04 18:38:53 +00002141 return I;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002142 }
2143
2144 I = I->getNextNode();
2145 }
2146
2147 // get new paths (reachable blocks).
Johannes Doerfert4361da22019-08-04 18:38:53 +00002148 for (const BasicBlock *SuccBB : successors(BB)) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00002149 assumeLive(A, *SuccBB);
Johannes Doerfert4361da22019-08-04 18:38:53 +00002150 ToBeExploredPaths.insert(&SuccBB->front());
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002151 }
2152
Johannes Doerfert4361da22019-08-04 18:38:53 +00002153 // No noreturn instruction found.
2154 return nullptr;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002155}
2156
Johannes Doerfertece81902019-08-12 22:05:53 +00002157ChangeStatus AAIsDeadImpl::updateImpl(Attributor &A) {
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00002158 ChangeStatus Status = ChangeStatus::UNCHANGED;
2159
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002160 // Temporary collection to iterate over existing noreturn instructions. This
2161 // will alow easier modification of NoReturnCalls collection
Johannes Doerfert4361da22019-08-04 18:38:53 +00002162 SmallVector<const Instruction *, 8> NoReturnChanged;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002163
Johannes Doerfert4361da22019-08-04 18:38:53 +00002164 for (const Instruction *I : NoReturnCalls)
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002165 NoReturnChanged.push_back(I);
2166
Johannes Doerfert4361da22019-08-04 18:38:53 +00002167 for (const Instruction *I : NoReturnChanged) {
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002168 size_t Size = ToBeExploredPaths.size();
2169
Johannes Doerfert4361da22019-08-04 18:38:53 +00002170 const Instruction *NextNoReturnI = findNextNoReturn(A, I);
2171 if (NextNoReturnI != I) {
2172 Status = ChangeStatus::CHANGED;
2173 NoReturnCalls.remove(I);
2174 if (NextNoReturnI)
2175 NoReturnCalls.insert(NextNoReturnI);
2176 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002177
Johannes Doerfert4361da22019-08-04 18:38:53 +00002178 // Explore new paths.
2179 while (Size != ToBeExploredPaths.size()) {
2180 Status = ChangeStatus::CHANGED;
2181 if (const Instruction *NextNoReturnI =
2182 findNextNoReturn(A, ToBeExploredPaths[Size++]))
2183 NoReturnCalls.insert(NextNoReturnI);
2184 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002185 }
2186
Johannes Doerfertdef99282019-08-14 21:29:37 +00002187 LLVM_DEBUG(dbgs() << "[AAIsDead] AssumedLiveBlocks: "
2188 << AssumedLiveBlocks.size() << " Total number of blocks: "
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002189 << getAssociatedFunction()->size() << "\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002190
Johannes Doerfertd6207812019-08-07 22:32:38 +00002191 // If we know everything is live there is no need to query for liveness.
2192 if (NoReturnCalls.empty() &&
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002193 getAssociatedFunction()->size() == AssumedLiveBlocks.size()) {
Johannes Doerfertd6207812019-08-07 22:32:38 +00002194 // Indicating a pessimistic fixpoint will cause the state to be "invalid"
2195 // which will cause the Attributor to not return the AAIsDead on request,
2196 // which will prevent us from querying isAssumedDead().
2197 indicatePessimisticFixpoint();
2198 assert(!isValidState() && "Expected an invalid state!");
Johannes Doerfert62a9c1d2019-08-29 01:26:58 +00002199 Status = ChangeStatus::CHANGED;
Johannes Doerfertd6207812019-08-07 22:32:38 +00002200 }
2201
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002202 return Status;
2203}
2204
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002205/// Liveness information for a call sites.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00002206struct AAIsDeadCallSite final : AAIsDeadImpl {
2207 AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadImpl(IRP) {}
2208
2209 /// See AbstractAttribute::initialize(...).
2210 void initialize(Attributor &A) override {
2211 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002212 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00002213 // sense to specialize attributes for call sites instead of
2214 // redirecting requests to the callee.
2215 llvm_unreachable("Abstract attributes for liveness are not "
2216 "supported for call sites yet!");
2217 }
2218
2219 /// See AbstractAttribute::updateImpl(...).
2220 ChangeStatus updateImpl(Attributor &A) override {
2221 return indicatePessimisticFixpoint();
2222 }
2223
2224 /// See AbstractAttribute::trackStatistics()
2225 void trackStatistics() const override {}
2226};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002227
Hideto Ueno19c07af2019-07-23 08:16:17 +00002228/// -------------------- Dereferenceable Argument Attribute --------------------
2229
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002230template <>
2231ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
2232 const DerefState &R) {
2233 ChangeStatus CS0 = clampStateAndIndicateChange<IntegerState>(
2234 S.DerefBytesState, R.DerefBytesState);
2235 ChangeStatus CS1 =
2236 clampStateAndIndicateChange<IntegerState>(S.GlobalState, R.GlobalState);
2237 return CS0 | CS1;
2238}
2239
Hideto Ueno70576ca2019-08-22 14:18:29 +00002240struct AADereferenceableImpl : AADereferenceable {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002241 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
Johannes Doerfert344d0382019-08-07 22:34:26 +00002242 using StateType = DerefState;
Hideto Ueno19c07af2019-07-23 08:16:17 +00002243
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00002244 void initialize(Attributor &A) override {
2245 SmallVector<Attribute, 4> Attrs;
2246 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
2247 Attrs);
2248 for (const Attribute &Attr : Attrs)
2249 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
2250
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002251 NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition());
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002252
2253 const IRPosition &IRP = this->getIRPosition();
2254 bool IsFnInterface = IRP.isFnInterfaceKind();
2255 const Function *FnScope = IRP.getAnchorScope();
2256 if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition()))
2257 indicatePessimisticFixpoint();
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00002258 }
2259
Hideto Ueno19c07af2019-07-23 08:16:17 +00002260 /// See AbstractAttribute::getState()
2261 /// {
Johannes Doerfert344d0382019-08-07 22:34:26 +00002262 StateType &getState() override { return *this; }
2263 const StateType &getState() const override { return *this; }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002264 /// }
2265
Johannes Doerferteccdf082019-08-05 23:35:12 +00002266 void getDeducedAttributes(LLVMContext &Ctx,
2267 SmallVectorImpl<Attribute> &Attrs) const override {
Hideto Ueno19c07af2019-07-23 08:16:17 +00002268 // TODO: Add *_globally support
2269 if (isAssumedNonNull())
2270 Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
2271 Ctx, getAssumedDereferenceableBytes()));
2272 else
2273 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
2274 Ctx, getAssumedDereferenceableBytes()));
2275 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002276
2277 /// See AbstractAttribute::getAsStr().
2278 const std::string getAsStr() const override {
2279 if (!getAssumedDereferenceableBytes())
2280 return "unknown-dereferenceable";
2281 return std::string("dereferenceable") +
2282 (isAssumedNonNull() ? "" : "_or_null") +
2283 (isAssumedGlobal() ? "_globally" : "") + "<" +
2284 std::to_string(getKnownDereferenceableBytes()) + "-" +
2285 std::to_string(getAssumedDereferenceableBytes()) + ">";
2286 }
2287};
2288
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002289/// Dereferenceable attribute for a floating value.
2290struct AADereferenceableFloating : AADereferenceableImpl {
2291 AADereferenceableFloating(const IRPosition &IRP)
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002292 : AADereferenceableImpl(IRP) {}
Hideto Ueno19c07af2019-07-23 08:16:17 +00002293
2294 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002295 ChangeStatus updateImpl(Attributor &A) override {
2296 const DataLayout &DL = A.getDataLayout();
2297
2298 auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool {
2299 unsigned IdxWidth =
2300 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
2301 APInt Offset(IdxWidth, 0);
2302 const Value *Base =
2303 V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
2304
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002305 const auto &AA =
2306 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002307 int64_t DerefBytes = 0;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002308 if (!Stripped && this == &AA) {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002309 // Use IR information if we did not strip anything.
2310 // TODO: track globally.
2311 bool CanBeNull;
2312 DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull);
2313 T.GlobalState.indicatePessimisticFixpoint();
2314 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002315 const DerefState &DS = static_cast<const DerefState &>(AA.getState());
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002316 DerefBytes = DS.DerefBytesState.getAssumed();
2317 T.GlobalState &= DS.GlobalState;
2318 }
2319
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00002320 // For now we do not try to "increase" dereferenceability due to negative
2321 // indices as we first have to come up with code to deal with loops and
2322 // for overflows of the dereferenceable bytes.
Johannes Doerfert785fad32019-08-23 17:29:23 +00002323 int64_t OffsetSExt = Offset.getSExtValue();
2324 if (OffsetSExt < 0)
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00002325 Offset = 0;
2326
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002327 T.takeAssumedDerefBytesMinimum(
Johannes Doerfert785fad32019-08-23 17:29:23 +00002328 std::max(int64_t(0), DerefBytes - OffsetSExt));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002329
Johannes Doerfert785fad32019-08-23 17:29:23 +00002330 if (this == &AA) {
2331 if (!Stripped) {
2332 // If nothing was stripped IR information is all we got.
2333 T.takeKnownDerefBytesMaximum(
2334 std::max(int64_t(0), DerefBytes - OffsetSExt));
2335 T.indicatePessimisticFixpoint();
2336 } else if (OffsetSExt > 0) {
2337 // If something was stripped but there is circular reasoning we look
2338 // for the offset. If it is positive we basically decrease the
2339 // dereferenceable bytes in a circluar loop now, which will simply
2340 // drive them down to the known value in a very slow way which we
2341 // can accelerate.
2342 T.indicatePessimisticFixpoint();
2343 }
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002344 }
2345
2346 return T.isValidState();
2347 };
2348
2349 DerefState T;
2350 if (!genericValueTraversal<AADereferenceable, DerefState>(
2351 A, getIRPosition(), *this, T, VisitValueCB))
2352 return indicatePessimisticFixpoint();
2353
2354 return clampStateAndIndicateChange(getState(), T);
2355 }
2356
2357 /// See AbstractAttribute::trackStatistics()
2358 void trackStatistics() const override {
2359 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
2360 }
2361};
2362
2363/// Dereferenceable attribute for a return value.
2364struct AADereferenceableReturned final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002365 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl,
2366 DerefState> {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002367 AADereferenceableReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002368 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl,
2369 DerefState>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002370
2371 /// See AbstractAttribute::trackStatistics()
2372 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002373 STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002374 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002375};
2376
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002377/// Dereferenceable attribute for an argument
2378struct AADereferenceableArgument final
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002379 : AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl,
2380 DerefState> {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002381 AADereferenceableArgument(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002382 : AAArgumentFromCallSiteArguments<AADereferenceable,
2383 AADereferenceableImpl, DerefState>(
2384 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002385
2386 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002387 void trackStatistics() const override {
Johannes Doerfert169af992019-08-20 06:09:56 +00002388 STATS_DECLTRACK_ARG_ATTR(dereferenceable)
2389 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002390};
2391
Hideto Ueno19c07af2019-07-23 08:16:17 +00002392/// Dereferenceable attribute for a call site argument.
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002393struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002394 AADereferenceableCallSiteArgument(const IRPosition &IRP)
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002395 : AADereferenceableFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002396
2397 /// See AbstractAttribute::trackStatistics()
2398 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002399 STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002400 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00002401};
2402
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002403/// Dereferenceable attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002404struct AADereferenceableCallSiteReturned final : AADereferenceableImpl {
2405 AADereferenceableCallSiteReturned(const IRPosition &IRP)
2406 : AADereferenceableImpl(IRP) {}
2407
2408 /// See AbstractAttribute::initialize(...).
2409 void initialize(Attributor &A) override {
2410 AADereferenceableImpl::initialize(A);
2411 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002412 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002413 indicatePessimisticFixpoint();
2414 }
2415
2416 /// See AbstractAttribute::updateImpl(...).
2417 ChangeStatus updateImpl(Attributor &A) override {
2418 // TODO: Once we have call site specific value information we can provide
2419 // call site specific liveness information and then it makes
2420 // sense to specialize attributes for call sites arguments instead of
2421 // redirecting requests to the callee argument.
2422 Function *F = getAssociatedFunction();
2423 const IRPosition &FnPos = IRPosition::returned(*F);
2424 auto &FnAA = A.getAAFor<AADereferenceable>(*this, FnPos);
2425 return clampStateAndIndicateChange(
2426 getState(), static_cast<const DerefState &>(FnAA.getState()));
2427 }
2428
2429 /// See AbstractAttribute::trackStatistics()
2430 void trackStatistics() const override {
2431 STATS_DECLTRACK_CS_ATTR(dereferenceable);
2432 }
2433};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002434
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002435// ------------------------ Align Argument Attribute ------------------------
2436
Johannes Doerfert344d0382019-08-07 22:34:26 +00002437struct AAAlignImpl : AAAlign {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002438 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002439
2440 // Max alignemnt value allowed in IR
2441 static const unsigned MAX_ALIGN = 1U << 29;
2442
Johannes Doerfert234eda52019-08-16 19:51:23 +00002443 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00002444 void initialize(Attributor &A) override {
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002445 takeAssumedMinimum(MAX_ALIGN);
2446
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002447 SmallVector<Attribute, 4> Attrs;
2448 getAttrs({Attribute::Alignment}, Attrs);
2449 for (const Attribute &Attr : Attrs)
2450 takeKnownMaximum(Attr.getValueAsInt());
Johannes Doerfert97fd5822019-09-04 16:26:20 +00002451
2452 if (getIRPosition().isFnInterfaceKind() &&
2453 (!getAssociatedFunction() ||
2454 !getAssociatedFunction()->hasExactDefinition()))
2455 indicatePessimisticFixpoint();
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002456 }
2457
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002458 /// See AbstractAttribute::manifest(...).
2459 ChangeStatus manifest(Attributor &A) override {
2460 ChangeStatus Changed = ChangeStatus::UNCHANGED;
2461
2462 // Check for users that allow alignment annotations.
2463 Value &AnchorVal = getIRPosition().getAnchorValue();
2464 for (const Use &U : AnchorVal.uses()) {
2465 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
2466 if (SI->getPointerOperand() == &AnchorVal)
2467 if (SI->getAlignment() < getAssumedAlign()) {
2468 STATS_DECLTRACK(AAAlign, Store,
2469 "Number of times alignemnt added to a store");
Guillaume Chateletd400d452019-10-03 13:17:21 +00002470 SI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002471 Changed = ChangeStatus::CHANGED;
2472 }
2473 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
2474 if (LI->getPointerOperand() == &AnchorVal)
2475 if (LI->getAlignment() < getAssumedAlign()) {
Guillaume Chatelet17380222019-09-30 09:37:05 +00002476 LI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002477 STATS_DECLTRACK(AAAlign, Load,
2478 "Number of times alignemnt added to a load");
2479 Changed = ChangeStatus::CHANGED;
2480 }
2481 }
2482 }
2483
Johannes Doerfert81df4522019-08-30 15:22:28 +00002484 return AAAlign::manifest(A) | Changed;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002485 }
2486
Johannes Doerfert81df4522019-08-30 15:22:28 +00002487 // TODO: Provide a helper to determine the implied ABI alignment and check in
2488 // the existing manifest method and a new one for AAAlignImpl that value
2489 // to avoid making the alignment explicit if it did not improve.
2490
2491 /// See AbstractAttribute::getDeducedAttributes
2492 virtual void
2493 getDeducedAttributes(LLVMContext &Ctx,
2494 SmallVectorImpl<Attribute> &Attrs) const override {
2495 if (getAssumedAlign() > 1)
2496 Attrs.emplace_back(Attribute::getWithAlignment(Ctx, getAssumedAlign()));
2497 }
2498
2499 /// See AbstractAttribute::getAsStr().
2500 const std::string getAsStr() const override {
2501 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
2502 "-" + std::to_string(getAssumedAlign()) + ">")
2503 : "unknown-align";
2504 }
2505};
2506
2507/// Align attribute for a floating value.
2508struct AAAlignFloating : AAAlignImpl {
2509 AAAlignFloating(const IRPosition &IRP) : AAAlignImpl(IRP) {}
2510
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002511 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert234eda52019-08-16 19:51:23 +00002512 ChangeStatus updateImpl(Attributor &A) override {
2513 const DataLayout &DL = A.getDataLayout();
2514
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002515 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
2516 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002517 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V));
2518 if (!Stripped && this == &AA) {
Johannes Doerfert234eda52019-08-16 19:51:23 +00002519 // Use only IR information if we did not strip anything.
2520 T.takeKnownMaximum(V.getPointerAlignment(DL));
2521 T.indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00002522 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002523 // Use abstract attribute information.
2524 const AAAlign::StateType &DS =
2525 static_cast<const AAAlign::StateType &>(AA.getState());
2526 T ^= DS;
Johannes Doerfert234eda52019-08-16 19:51:23 +00002527 }
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002528 return T.isValidState();
Johannes Doerfert234eda52019-08-16 19:51:23 +00002529 };
2530
2531 StateType T;
2532 if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
2533 VisitValueCB))
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00002534 return indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00002535
Johannes Doerfert028b2aa2019-08-20 05:57:01 +00002536 // TODO: If we know we visited all incoming values, thus no are assumed
2537 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +00002538 return clampStateAndIndicateChange(getState(), T);
2539 }
2540
2541 /// See AbstractAttribute::trackStatistics()
2542 void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
2543};
2544
2545/// Align attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002546struct AAAlignReturned final
2547 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00002548 AAAlignReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002549 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002550
2551 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002552 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002553};
2554
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002555/// Align attribute for function argument.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002556struct AAAlignArgument final
2557 : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00002558 AAAlignArgument(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002559 : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002560
2561 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert169af992019-08-20 06:09:56 +00002562 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002563};
2564
Johannes Doerfert234eda52019-08-16 19:51:23 +00002565struct AAAlignCallSiteArgument final : AAAlignFloating {
2566 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002567
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00002568 /// See AbstractAttribute::manifest(...).
2569 ChangeStatus manifest(Attributor &A) override {
2570 return AAAlignImpl::manifest(A);
2571 }
2572
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002573 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002574 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00002575};
2576
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002577/// Align attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002578struct AAAlignCallSiteReturned final : AAAlignImpl {
2579 AAAlignCallSiteReturned(const IRPosition &IRP) : AAAlignImpl(IRP) {}
2580
2581 /// See AbstractAttribute::initialize(...).
2582 void initialize(Attributor &A) override {
2583 AAAlignImpl::initialize(A);
2584 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002585 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002586 indicatePessimisticFixpoint();
2587 }
2588
2589 /// See AbstractAttribute::updateImpl(...).
2590 ChangeStatus updateImpl(Attributor &A) override {
2591 // TODO: Once we have call site specific value information we can provide
2592 // call site specific liveness information and then it makes
2593 // sense to specialize attributes for call sites arguments instead of
2594 // redirecting requests to the callee argument.
2595 Function *F = getAssociatedFunction();
2596 const IRPosition &FnPos = IRPosition::returned(*F);
2597 auto &FnAA = A.getAAFor<AAAlign>(*this, FnPos);
2598 return clampStateAndIndicateChange(
2599 getState(), static_cast<const AAAlign::StateType &>(FnAA.getState()));
2600 }
2601
2602 /// See AbstractAttribute::trackStatistics()
2603 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
2604};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002605
Johannes Doerferte83f3032019-08-05 23:22:05 +00002606/// ------------------ Function No-Return Attribute ----------------------------
Johannes Doerfert344d0382019-08-07 22:34:26 +00002607struct AANoReturnImpl : public AANoReturn {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002608 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
Johannes Doerferte83f3032019-08-05 23:22:05 +00002609
Johannes Doerferte83f3032019-08-05 23:22:05 +00002610 /// See AbstractAttribute::getAsStr().
2611 const std::string getAsStr() const override {
2612 return getAssumed() ? "noreturn" : "may-return";
2613 }
2614
Johannes Doerferte83f3032019-08-05 23:22:05 +00002615 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00002616 virtual ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00002617 auto CheckForNoReturn = [](Instruction &) { return false; };
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002618 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
Johannes Doerfertd0f64002019-08-06 00:32:43 +00002619 {(unsigned)Instruction::Ret}))
Johannes Doerferte83f3032019-08-05 23:22:05 +00002620 return indicatePessimisticFixpoint();
Johannes Doerferte83f3032019-08-05 23:22:05 +00002621 return ChangeStatus::UNCHANGED;
2622 }
2623};
2624
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002625struct AANoReturnFunction final : AANoReturnImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002626 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002627
2628 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002629 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00002630};
2631
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002632/// NoReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002633struct AANoReturnCallSite final : AANoReturnImpl {
2634 AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
2635
2636 /// See AbstractAttribute::initialize(...).
2637 void initialize(Attributor &A) override {
2638 AANoReturnImpl::initialize(A);
2639 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002640 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002641 indicatePessimisticFixpoint();
2642 }
2643
2644 /// See AbstractAttribute::updateImpl(...).
2645 ChangeStatus updateImpl(Attributor &A) override {
2646 // TODO: Once we have call site specific value information we can provide
2647 // call site specific liveness information and then it makes
2648 // sense to specialize attributes for call sites arguments instead of
2649 // redirecting requests to the callee argument.
2650 Function *F = getAssociatedFunction();
2651 const IRPosition &FnPos = IRPosition::function(*F);
2652 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos);
2653 return clampStateAndIndicateChange(
2654 getState(),
2655 static_cast<const AANoReturn::StateType &>(FnAA.getState()));
2656 }
2657
2658 /// See AbstractAttribute::trackStatistics()
2659 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
2660};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002661
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002662/// ----------------------- Variable Capturing ---------------------------------
2663
2664/// A class to hold the state of for no-capture attributes.
2665struct AANoCaptureImpl : public AANoCapture {
2666 AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {}
2667
2668 /// See AbstractAttribute::initialize(...).
2669 void initialize(Attributor &A) override {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002670 AANoCapture::initialize(A);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002671
2672 const IRPosition &IRP = getIRPosition();
2673 const Function *F =
2674 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
2675
2676 // Check what state the associated function can actually capture.
2677 if (F)
2678 determineFunctionCaptureCapabilities(*F, *this);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002679 else
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002680 indicatePessimisticFixpoint();
2681 }
2682
2683 /// See AbstractAttribute::updateImpl(...).
2684 ChangeStatus updateImpl(Attributor &A) override;
2685
2686 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
2687 virtual void
2688 getDeducedAttributes(LLVMContext &Ctx,
2689 SmallVectorImpl<Attribute> &Attrs) const override {
2690 if (!isAssumedNoCaptureMaybeReturned())
2691 return;
2692
Hideto Ueno37367642019-09-11 06:52:11 +00002693 if (getArgNo() >= 0) {
2694 if (isAssumedNoCapture())
2695 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
2696 else if (ManifestInternal)
2697 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
2698 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002699 }
2700
2701 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
2702 /// depending on the ability of the function associated with \p IRP to capture
2703 /// state in memory and through "returning/throwing", respectively.
2704 static void determineFunctionCaptureCapabilities(const Function &F,
2705 IntegerState &State) {
2706 // TODO: Once we have memory behavior attributes we should use them here.
2707
2708 // If we know we cannot communicate or write to memory, we do not care about
2709 // ptr2int anymore.
2710 if (F.onlyReadsMemory() && F.doesNotThrow() &&
2711 F.getReturnType()->isVoidTy()) {
2712 State.addKnownBits(NO_CAPTURE);
2713 return;
2714 }
2715
2716 // A function cannot capture state in memory if it only reads memory, it can
2717 // however return/throw state and the state might be influenced by the
2718 // pointer value, e.g., loading from a returned pointer might reveal a bit.
2719 if (F.onlyReadsMemory())
2720 State.addKnownBits(NOT_CAPTURED_IN_MEM);
2721
2722 // A function cannot communicate state back if it does not through
2723 // exceptions and doesn not return values.
2724 if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
2725 State.addKnownBits(NOT_CAPTURED_IN_RET);
2726 }
2727
2728 /// See AbstractState::getAsStr().
2729 const std::string getAsStr() const override {
2730 if (isKnownNoCapture())
2731 return "known not-captured";
2732 if (isAssumedNoCapture())
2733 return "assumed not-captured";
2734 if (isKnownNoCaptureMaybeReturned())
2735 return "known not-captured-maybe-returned";
2736 if (isAssumedNoCaptureMaybeReturned())
2737 return "assumed not-captured-maybe-returned";
2738 return "assumed-captured";
2739 }
2740};
2741
2742/// Attributor-aware capture tracker.
2743struct AACaptureUseTracker final : public CaptureTracker {
2744
2745 /// Create a capture tracker that can lookup in-flight abstract attributes
2746 /// through the Attributor \p A.
2747 ///
2748 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
2749 /// search is stopped. If a use leads to a return instruction,
2750 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
2751 /// If a use leads to a ptr2int which may capture the value,
2752 /// \p CapturedInInteger is set. If a use is found that is currently assumed
2753 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
2754 /// set. All values in \p PotentialCopies are later tracked as well. For every
2755 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
2756 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
2757 /// conservatively set to true.
2758 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
2759 const AAIsDead &IsDeadAA, IntegerState &State,
2760 SmallVectorImpl<const Value *> &PotentialCopies,
2761 unsigned &RemainingUsesToExplore)
2762 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
2763 PotentialCopies(PotentialCopies),
2764 RemainingUsesToExplore(RemainingUsesToExplore) {}
2765
2766 /// Determine if \p V maybe captured. *Also updates the state!*
2767 bool valueMayBeCaptured(const Value *V) {
2768 if (V->getType()->isPointerTy()) {
2769 PointerMayBeCaptured(V, this);
2770 } else {
2771 State.indicatePessimisticFixpoint();
2772 }
2773 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
2774 }
2775
2776 /// See CaptureTracker::tooManyUses().
2777 void tooManyUses() override {
2778 State.removeAssumedBits(AANoCapture::NO_CAPTURE);
2779 }
2780
2781 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
2782 if (CaptureTracker::isDereferenceableOrNull(O, DL))
2783 return true;
2784 const auto &DerefAA =
2785 A.getAAFor<AADereferenceable>(NoCaptureAA, IRPosition::value(*O));
2786 return DerefAA.getAssumedDereferenceableBytes();
2787 }
2788
2789 /// See CaptureTracker::captured(...).
2790 bool captured(const Use *U) override {
2791 Instruction *UInst = cast<Instruction>(U->getUser());
2792 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
2793 << "\n");
2794
2795 // Because we may reuse the tracker multiple times we keep track of the
2796 // number of explored uses ourselves as well.
2797 if (RemainingUsesToExplore-- == 0) {
2798 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
2799 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2800 /* Return */ true);
2801 }
2802
2803 // Deal with ptr2int by following uses.
2804 if (isa<PtrToIntInst>(UInst)) {
2805 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
2806 return valueMayBeCaptured(UInst);
2807 }
2808
2809 // Explicitly catch return instructions.
2810 if (isa<ReturnInst>(UInst))
2811 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2812 /* Return */ true);
2813
2814 // For now we only use special logic for call sites. However, the tracker
2815 // itself knows about a lot of other non-capturing cases already.
2816 CallSite CS(UInst);
2817 if (!CS || !CS.isArgOperand(U))
2818 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2819 /* Return */ true);
2820
2821 unsigned ArgNo = CS.getArgumentNo(U);
2822 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
2823 // If we have a abstract no-capture attribute for the argument we can use
2824 // it to justify a non-capture attribute here. This allows recursion!
2825 auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos);
2826 if (ArgNoCaptureAA.isAssumedNoCapture())
2827 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2828 /* Return */ false);
2829 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
2830 addPotentialCopy(CS);
2831 return isCapturedIn(/* Memory */ false, /* Integer */ false,
2832 /* Return */ false);
2833 }
2834
2835 // Lastly, we could not find a reason no-capture can be assumed so we don't.
2836 return isCapturedIn(/* Memory */ true, /* Integer */ true,
2837 /* Return */ true);
2838 }
2839
2840 /// Register \p CS as potential copy of the value we are checking.
2841 void addPotentialCopy(CallSite CS) {
2842 PotentialCopies.push_back(CS.getInstruction());
2843 }
2844
2845 /// See CaptureTracker::shouldExplore(...).
2846 bool shouldExplore(const Use *U) override {
2847 // Check liveness.
2848 return !IsDeadAA.isAssumedDead(cast<Instruction>(U->getUser()));
2849 }
2850
2851 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
2852 /// \p CapturedInRet, then return the appropriate value for use in the
2853 /// CaptureTracker::captured() interface.
2854 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
2855 bool CapturedInRet) {
2856 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
2857 << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
2858 if (CapturedInMem)
2859 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
2860 if (CapturedInInt)
2861 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
2862 if (CapturedInRet)
2863 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
2864 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
2865 }
2866
2867private:
2868 /// The attributor providing in-flight abstract attributes.
2869 Attributor &A;
2870
2871 /// The abstract attribute currently updated.
2872 AANoCapture &NoCaptureAA;
2873
2874 /// The abstract liveness state.
2875 const AAIsDead &IsDeadAA;
2876
2877 /// The state currently updated.
2878 IntegerState &State;
2879
2880 /// Set of potential copies of the tracked value.
2881 SmallVectorImpl<const Value *> &PotentialCopies;
2882
2883 /// Global counter to limit the number of explored uses.
2884 unsigned &RemainingUsesToExplore;
2885};
2886
2887ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
2888 const IRPosition &IRP = getIRPosition();
2889 const Value *V =
2890 getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue();
2891 if (!V)
2892 return indicatePessimisticFixpoint();
2893
2894 const Function *F =
2895 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
2896 assert(F && "Expected a function!");
2897 const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, IRPosition::function(*F));
2898
2899 AANoCapture::StateType T;
2900 // TODO: Once we have memory behavior attributes we should use them here
2901 // similar to the reasoning in
2902 // AANoCaptureImpl::determineFunctionCaptureCapabilities(...).
2903
2904 // TODO: Use the AAReturnedValues to learn if the argument can return or
2905 // not.
2906
2907 // Use the CaptureTracker interface and logic with the specialized tracker,
2908 // defined in AACaptureUseTracker, that can look at in-flight abstract
2909 // attributes and directly updates the assumed state.
2910 SmallVector<const Value *, 4> PotentialCopies;
2911 unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore;
2912 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
2913 RemainingUsesToExplore);
2914
2915 // Check all potential copies of the associated value until we can assume
2916 // none will be captured or we have to assume at least one might be.
2917 unsigned Idx = 0;
2918 PotentialCopies.push_back(V);
2919 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
2920 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
2921
2922 AAAlign::StateType &S = getState();
2923 auto Assumed = S.getAssumed();
2924 S.intersectAssumedBits(T.getAssumed());
2925 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
2926 : ChangeStatus::CHANGED;
2927}
2928
2929/// NoCapture attribute for function arguments.
2930struct AANoCaptureArgument final : AANoCaptureImpl {
2931 AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2932
2933 /// See AbstractAttribute::trackStatistics()
2934 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
2935};
2936
2937/// NoCapture attribute for call site arguments.
2938struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
2939 AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2940
2941 /// See AbstractAttribute::updateImpl(...).
2942 ChangeStatus updateImpl(Attributor &A) override {
2943 // TODO: Once we have call site specific value information we can provide
2944 // call site specific liveness information and then it makes
2945 // sense to specialize attributes for call sites arguments instead of
2946 // redirecting requests to the callee argument.
2947 Argument *Arg = getAssociatedArgument();
2948 if (!Arg)
2949 return indicatePessimisticFixpoint();
2950 const IRPosition &ArgPos = IRPosition::argument(*Arg);
2951 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos);
2952 return clampStateAndIndicateChange(
2953 getState(),
2954 static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
2955 }
2956
2957 /// See AbstractAttribute::trackStatistics()
2958 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
2959};
2960
2961/// NoCapture attribute for floating values.
2962struct AANoCaptureFloating final : AANoCaptureImpl {
2963 AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2964
2965 /// See AbstractAttribute::trackStatistics()
2966 void trackStatistics() const override {
2967 STATS_DECLTRACK_FLOATING_ATTR(nocapture)
2968 }
2969};
2970
2971/// NoCapture attribute for function return value.
2972struct AANoCaptureReturned final : AANoCaptureImpl {
2973 AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {
2974 llvm_unreachable("NoCapture is not applicable to function returns!");
2975 }
2976
2977 /// See AbstractAttribute::initialize(...).
2978 void initialize(Attributor &A) override {
2979 llvm_unreachable("NoCapture is not applicable to function returns!");
2980 }
2981
2982 /// See AbstractAttribute::updateImpl(...).
2983 ChangeStatus updateImpl(Attributor &A) override {
2984 llvm_unreachable("NoCapture is not applicable to function returns!");
2985 }
2986
2987 /// See AbstractAttribute::trackStatistics()
2988 void trackStatistics() const override {}
2989};
2990
2991/// NoCapture attribute deduction for a call site return value.
2992struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
2993 AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
2994
2995 /// See AbstractAttribute::trackStatistics()
2996 void trackStatistics() const override {
2997 STATS_DECLTRACK_CSRET_ATTR(nocapture)
2998 }
2999};
3000
Hideto Uenof2b9dc42019-09-07 07:03:05 +00003001/// ------------------ Value Simplify Attribute ----------------------------
3002struct AAValueSimplifyImpl : AAValueSimplify {
3003 AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {}
3004
3005 /// See AbstractAttribute::getAsStr().
3006 const std::string getAsStr() const override {
3007 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
3008 : "not-simple";
3009 }
3010
3011 /// See AbstractAttribute::trackStatistics()
3012 void trackStatistics() const override {}
3013
3014 /// See AAValueSimplify::getAssumedSimplifiedValue()
3015 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
3016 if (!getAssumed())
3017 return const_cast<Value *>(&getAssociatedValue());
3018 return SimplifiedAssociatedValue;
3019 }
3020 void initialize(Attributor &A) override {}
3021
3022 /// Helper function for querying AAValueSimplify and updating candicate.
3023 /// \param QueryingValue Value trying to unify with SimplifiedValue
3024 /// \param AccumulatedSimplifiedValue Current simplification result.
3025 static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
3026 Value &QueryingValue,
3027 Optional<Value *> &AccumulatedSimplifiedValue) {
3028 // FIXME: Add a typecast support.
3029
3030 auto &ValueSimpifyAA = A.getAAFor<AAValueSimplify>(
3031 QueryingAA, IRPosition::value(QueryingValue));
3032
3033 Optional<Value *> QueryingValueSimplified =
3034 ValueSimpifyAA.getAssumedSimplifiedValue(A);
3035
3036 if (!QueryingValueSimplified.hasValue())
3037 return true;
3038
3039 if (!QueryingValueSimplified.getValue())
3040 return false;
3041
3042 Value &QueryingValueSimplifiedUnwrapped =
3043 *QueryingValueSimplified.getValue();
3044
3045 if (isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
3046 return true;
3047
3048 if (AccumulatedSimplifiedValue.hasValue())
3049 return AccumulatedSimplifiedValue == QueryingValueSimplified;
3050
3051 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << QueryingValue
3052 << " is assumed to be "
3053 << QueryingValueSimplifiedUnwrapped << "\n");
3054
3055 AccumulatedSimplifiedValue = QueryingValueSimplified;
3056 return true;
3057 }
3058
3059 /// See AbstractAttribute::manifest(...).
3060 ChangeStatus manifest(Attributor &A) override {
3061 ChangeStatus Changed = ChangeStatus::UNCHANGED;
3062
3063 if (!SimplifiedAssociatedValue.hasValue() ||
3064 !SimplifiedAssociatedValue.getValue())
3065 return Changed;
3066
3067 if (auto *C = dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())) {
3068 // We can replace the AssociatedValue with the constant.
3069 Value &V = getAssociatedValue();
3070 if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
3071 LLVM_DEBUG(dbgs() << "[Attributor][ValueSimplify] " << V << " -> " << *C
3072 << "\n");
3073 V.replaceAllUsesWith(C);
3074 Changed = ChangeStatus::CHANGED;
3075 }
3076 }
3077
3078 return Changed | AAValueSimplify::manifest(A);
3079 }
3080
3081protected:
3082 // An assumed simplified value. Initially, it is set to Optional::None, which
3083 // means that the value is not clear under current assumption. If in the
3084 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
3085 // returns orignal associated value.
3086 Optional<Value *> SimplifiedAssociatedValue;
3087};
3088
3089struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
3090 AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3091
3092 /// See AbstractAttribute::updateImpl(...).
3093 ChangeStatus updateImpl(Attributor &A) override {
3094 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3095
3096 auto PredForCallSite = [&](CallSite CS) {
3097 return checkAndUpdate(A, *this, *CS.getArgOperand(getArgNo()),
3098 SimplifiedAssociatedValue);
3099 };
3100
3101 if (!A.checkForAllCallSites(PredForCallSite, *this, true))
3102 return indicatePessimisticFixpoint();
3103
3104 // If a candicate was found in this update, return CHANGED.
3105 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3106 ? ChangeStatus::UNCHANGED
3107 : ChangeStatus ::CHANGED;
3108 }
3109
3110 /// See AbstractAttribute::trackStatistics()
3111 void trackStatistics() const override {
3112 STATS_DECLTRACK_ARG_ATTR(value_simplify)
3113 }
3114};
3115
3116struct AAValueSimplifyReturned : AAValueSimplifyImpl {
3117 AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3118
3119 /// See AbstractAttribute::updateImpl(...).
3120 ChangeStatus updateImpl(Attributor &A) override {
3121 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3122
3123 auto PredForReturned = [&](Value &V) {
3124 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
3125 };
3126
3127 if (!A.checkForAllReturnedValues(PredForReturned, *this))
3128 return indicatePessimisticFixpoint();
3129
3130 // If a candicate was found in this update, return CHANGED.
3131 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3132 ? ChangeStatus::UNCHANGED
3133 : ChangeStatus ::CHANGED;
3134 }
3135 /// See AbstractAttribute::trackStatistics()
3136 void trackStatistics() const override {
3137 STATS_DECLTRACK_FNRET_ATTR(value_simplify)
3138 }
3139};
3140
3141struct AAValueSimplifyFloating : AAValueSimplifyImpl {
3142 AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3143
3144 /// See AbstractAttribute::initialize(...).
3145 void initialize(Attributor &A) override {
3146 Value &V = getAnchorValue();
3147
3148 // TODO: add other stuffs
3149 if (isa<Constant>(V) || isa<UndefValue>(V))
3150 indicatePessimisticFixpoint();
3151 }
3152
3153 /// See AbstractAttribute::updateImpl(...).
3154 ChangeStatus updateImpl(Attributor &A) override {
3155 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
3156
3157 auto VisitValueCB = [&](Value &V, BooleanState, bool Stripped) -> bool {
3158 auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V));
3159 if (!Stripped && this == &AA) {
3160 // TODO: Look the instruction and check recursively.
3161 LLVM_DEBUG(
3162 dbgs() << "[Attributor][ValueSimplify] Can't be stripped more : "
3163 << V << "\n");
3164 indicatePessimisticFixpoint();
3165 return false;
3166 }
3167 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
3168 };
3169
3170 if (!genericValueTraversal<AAValueSimplify, BooleanState>(
3171 A, getIRPosition(), *this, static_cast<BooleanState &>(*this),
3172 VisitValueCB))
3173 return indicatePessimisticFixpoint();
3174
3175 // If a candicate was found in this update, return CHANGED.
3176
3177 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
3178 ? ChangeStatus::UNCHANGED
3179 : ChangeStatus ::CHANGED;
3180 }
3181
3182 /// See AbstractAttribute::trackStatistics()
3183 void trackStatistics() const override {
3184 STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
3185 }
3186};
3187
3188struct AAValueSimplifyFunction : AAValueSimplifyImpl {
3189 AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
3190
3191 /// See AbstractAttribute::initialize(...).
3192 void initialize(Attributor &A) override {
3193 SimplifiedAssociatedValue = &getAnchorValue();
3194 indicateOptimisticFixpoint();
3195 }
3196 /// See AbstractAttribute::initialize(...).
3197 ChangeStatus updateImpl(Attributor &A) override {
3198 llvm_unreachable(
3199 "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
3200 }
3201 /// See AbstractAttribute::trackStatistics()
3202 void trackStatistics() const override {
3203 STATS_DECLTRACK_FN_ATTR(value_simplify)
3204 }
3205};
3206
3207struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
3208 AAValueSimplifyCallSite(const IRPosition &IRP)
3209 : AAValueSimplifyFunction(IRP) {}
3210 /// See AbstractAttribute::trackStatistics()
3211 void trackStatistics() const override {
3212 STATS_DECLTRACK_CS_ATTR(value_simplify)
3213 }
3214};
3215
3216struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
3217 AAValueSimplifyCallSiteReturned(const IRPosition &IRP)
3218 : AAValueSimplifyReturned(IRP) {}
3219
3220 void trackStatistics() const override {
3221 STATS_DECLTRACK_CSRET_ATTR(value_simplify)
3222 }
3223};
3224struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
3225 AAValueSimplifyCallSiteArgument(const IRPosition &IRP)
3226 : AAValueSimplifyFloating(IRP) {}
3227
3228 void trackStatistics() const override {
3229 STATS_DECLTRACK_CSARG_ATTR(value_simplify)
3230 }
3231};
3232
Stefan Stipanovic431141c2019-09-15 21:47:41 +00003233/// ----------------------- Heap-To-Stack Conversion ---------------------------
3234struct AAHeapToStackImpl : public AAHeapToStack {
3235 AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {}
3236
3237 const std::string getAsStr() const override {
3238 return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
3239 }
3240
3241 ChangeStatus manifest(Attributor &A) override {
3242 assert(getState().isValidState() &&
3243 "Attempted to manifest an invalid state!");
3244
3245 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
3246 Function *F = getAssociatedFunction();
3247 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
3248
3249 for (Instruction *MallocCall : MallocCalls) {
3250 // This malloc cannot be replaced.
3251 if (BadMallocCalls.count(MallocCall))
3252 continue;
3253
3254 for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
3255 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
3256 A.deleteAfterManifest(*FreeCall);
3257 HasChanged = ChangeStatus::CHANGED;
3258 }
3259
3260 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
3261 << "\n");
3262
3263 Constant *Size;
3264 if (isCallocLikeFn(MallocCall, TLI)) {
3265 auto *Num = cast<ConstantInt>(MallocCall->getOperand(0));
3266 auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1));
3267 APInt TotalSize = SizeT->getValue() * Num->getValue();
3268 Size =
3269 ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize);
3270 } else {
3271 Size = cast<ConstantInt>(MallocCall->getOperand(0));
3272 }
3273
3274 unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
3275 Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
3276 Size, "", MallocCall->getNextNode());
3277
3278 if (AI->getType() != MallocCall->getType())
3279 AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
3280 AI->getNextNode());
3281
3282 MallocCall->replaceAllUsesWith(AI);
3283
3284 if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
3285 auto *NBB = II->getNormalDest();
3286 BranchInst::Create(NBB, MallocCall->getParent());
3287 A.deleteAfterManifest(*MallocCall);
3288 } else {
3289 A.deleteAfterManifest(*MallocCall);
3290 }
3291
3292 if (isCallocLikeFn(MallocCall, TLI)) {
3293 auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
3294 AI->getNextNode());
3295 Value *Ops[] = {
3296 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
3297 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
3298
3299 Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
3300 Module *M = F->getParent();
3301 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
3302 CallInst::Create(Fn, Ops, "", BI->getNextNode());
3303 }
3304 HasChanged = ChangeStatus::CHANGED;
3305 }
3306
3307 return HasChanged;
3308 }
3309
3310 /// Collection of all malloc calls in a function.
3311 SmallSetVector<Instruction *, 4> MallocCalls;
3312
3313 /// Collection of malloc calls that cannot be converted.
3314 DenseSet<const Instruction *> BadMallocCalls;
3315
3316 /// A map for each malloc call to the set of associated free calls.
3317 DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
3318
3319 ChangeStatus updateImpl(Attributor &A) override;
3320};
3321
3322ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
3323 const Function *F = getAssociatedFunction();
3324 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
3325
3326 auto UsesCheck = [&](Instruction &I) {
3327 SmallPtrSet<const Use *, 8> Visited;
3328 SmallVector<const Use *, 8> Worklist;
3329
3330 for (Use &U : I.uses())
3331 Worklist.push_back(&U);
3332
3333 while (!Worklist.empty()) {
3334 const Use *U = Worklist.pop_back_val();
3335 if (!Visited.insert(U).second)
3336 continue;
3337
3338 auto *UserI = U->getUser();
3339
3340 if (isa<LoadInst>(UserI) || isa<StoreInst>(UserI))
3341 continue;
3342
3343 // NOTE: Right now, if a function that has malloc pointer as an argument
3344 // frees memory, we assume that the malloc pointer is freed.
3345
3346 // TODO: Add nofree callsite argument attribute to indicate that pointer
3347 // argument is not freed.
3348 if (auto *CB = dyn_cast<CallBase>(UserI)) {
3349 if (!CB->isArgOperand(U))
3350 continue;
3351
3352 if (CB->isLifetimeStartOrEnd())
3353 continue;
3354
3355 // Record malloc.
3356 if (isFreeCall(UserI, TLI)) {
3357 FreesForMalloc[&I].insert(
3358 cast<Instruction>(const_cast<User *>(UserI)));
3359 continue;
3360 }
3361
3362 // If a function does not free memory we are fine
3363 const auto &NoFreeAA =
3364 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(*CB));
3365
3366 unsigned ArgNo = U - CB->arg_begin();
3367 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
3368 *this, IRPosition::callsite_argument(*CB, ArgNo));
3369
3370 if (!NoCaptureAA.isAssumedNoCapture() || !NoFreeAA.isAssumedNoFree()) {
3371 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
3372 return false;
3373 }
3374 continue;
3375 }
3376
3377 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI)) {
3378 for (Use &U : UserI->uses())
3379 Worklist.push_back(&U);
3380 continue;
3381 }
3382
3383 // Unknown user.
3384 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
3385 return false;
3386 }
3387 return true;
3388 };
3389
3390 auto MallocCallocCheck = [&](Instruction &I) {
3391 if (isMallocLikeFn(&I, TLI)) {
3392 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
3393 if (!Size->getValue().sle(MaxHeapToStackSize))
3394 return true;
3395 } else if (isCallocLikeFn(&I, TLI)) {
3396 bool Overflow = false;
3397 if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
3398 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
3399 if (!(Size->getValue().umul_ov(Num->getValue(), Overflow))
3400 .sle(MaxHeapToStackSize))
3401 if (!Overflow)
3402 return true;
3403 } else {
3404 BadMallocCalls.insert(&I);
3405 return true;
3406 }
3407
3408 if (BadMallocCalls.count(&I))
3409 return true;
3410
3411 if (UsesCheck(I))
3412 MallocCalls.insert(&I);
3413 else
3414 BadMallocCalls.insert(&I);
3415 return true;
3416 };
3417
3418 size_t NumBadMallocs = BadMallocCalls.size();
3419
3420 A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
3421
3422 if (NumBadMallocs != BadMallocCalls.size())
3423 return ChangeStatus::CHANGED;
3424
3425 return ChangeStatus::UNCHANGED;
3426}
3427
3428struct AAHeapToStackFunction final : public AAHeapToStackImpl {
3429 AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {}
3430
3431 /// See AbstractAttribute::trackStatistics()
3432 void trackStatistics() const override {
3433 STATS_DECL(MallocCalls, Function,
3434 "Number of MallocCalls converted to allocas");
3435 BUILD_STAT_NAME(MallocCalls, Function) += MallocCalls.size();
3436 }
3437};
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +00003438} // namespace
Stefan Stipanovic431141c2019-09-15 21:47:41 +00003439
Johannes Doerfertaade7822019-06-05 03:02:24 +00003440/// ----------------------------------------------------------------------------
3441/// Attributor
3442/// ----------------------------------------------------------------------------
3443
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003444bool Attributor::isAssumedDead(const AbstractAttribute &AA,
3445 const AAIsDead *LivenessAA) {
3446 const Instruction *CtxI = AA.getIRPosition().getCtxI();
3447 if (!CtxI)
3448 return false;
3449
3450 if (!LivenessAA)
3451 LivenessAA =
Johannes Doerfert19b00432019-08-26 17:48:05 +00003452 &getAAFor<AAIsDead>(AA, IRPosition::function(*CtxI->getFunction()),
3453 /* TrackDependence */ false);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003454
3455 // Don't check liveness for AAIsDead.
3456 if (&AA == LivenessAA)
3457 return false;
3458
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003459 if (!LivenessAA->isAssumedDead(CtxI))
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003460 return false;
3461
Johannes Doerfert19b00432019-08-26 17:48:05 +00003462 // We actually used liveness information so we have to record a dependence.
3463 recordDependence(*LivenessAA, AA);
3464
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003465 return true;
3466}
3467
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003468bool Attributor::checkForAllCallSites(const function_ref<bool(CallSite)> &Pred,
Johannes Doerfert14a04932019-08-07 22:27:24 +00003469 const AbstractAttribute &QueryingAA,
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003470 bool RequireAllCallSites) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00003471 // We can try to determine information from
3472 // the call sites. However, this is only possible all call sites are known,
3473 // hence the function has internal linkage.
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003474 const IRPosition &IRP = QueryingAA.getIRPosition();
3475 const Function *AssociatedFunction = IRP.getAssociatedFunction();
3476 if (!AssociatedFunction)
3477 return false;
3478
3479 if (RequireAllCallSites && !AssociatedFunction->hasInternalLinkage()) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00003480 LLVM_DEBUG(
3481 dbgs()
Johannes Doerfert5304b722019-08-14 22:04:28 +00003482 << "[Attributor] Function " << AssociatedFunction->getName()
Hideto Ueno54869ec2019-07-15 06:49:04 +00003483 << " has no internal linkage, hence not all call sites are known\n");
3484 return false;
3485 }
3486
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003487 for (const Use &U : AssociatedFunction->uses()) {
Johannes Doerfertd98f9752019-08-21 21:48:56 +00003488 Instruction *I = dyn_cast<Instruction>(U.getUser());
3489 // TODO: Deal with abstract call sites here.
3490 if (!I)
3491 return false;
3492
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003493 Function *Caller = I->getFunction();
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003494
Johannes Doerfert19b00432019-08-26 17:48:05 +00003495 const auto &LivenessAA = getAAFor<AAIsDead>(
3496 QueryingAA, IRPosition::function(*Caller), /* TrackDependence */ false);
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003497
3498 // Skip dead calls.
Johannes Doerfert19b00432019-08-26 17:48:05 +00003499 if (LivenessAA.isAssumedDead(I)) {
3500 // We actually used liveness information so we have to record a
3501 // dependence.
3502 recordDependence(LivenessAA, QueryingAA);
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003503 continue;
Johannes Doerfert19b00432019-08-26 17:48:05 +00003504 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00003505
3506 CallSite CS(U.getUser());
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003507 if (!CS || !CS.isCallee(&U)) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00003508 if (!RequireAllCallSites)
3509 continue;
3510
Johannes Doerfert5304b722019-08-14 22:04:28 +00003511 LLVM_DEBUG(dbgs() << "[Attributor] User " << *U.getUser()
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003512 << " is an invalid use of "
3513 << AssociatedFunction->getName() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00003514 return false;
3515 }
3516
3517 if (Pred(CS))
3518 continue;
3519
Johannes Doerfert5304b722019-08-14 22:04:28 +00003520 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
Hideto Ueno54869ec2019-07-15 06:49:04 +00003521 << *CS.getInstruction() << "\n");
3522 return false;
3523 }
3524
3525 return true;
3526}
3527
Johannes Doerfert14a04932019-08-07 22:27:24 +00003528bool Attributor::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00003529 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00003530 &Pred,
3531 const AbstractAttribute &QueryingAA) {
3532
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003533 const IRPosition &IRP = QueryingAA.getIRPosition();
3534 // Since we need to provide return instructions we have to have an exact
3535 // definition.
3536 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003537 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00003538 return false;
3539
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003540 // If this is a call site query we use the call site specific return values
3541 // and liveness information.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003542 // TODO: use the function scope once we have call site AAReturnedValues.
3543 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003544 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003545 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003546 return false;
3547
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003548 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
Johannes Doerfert14a04932019-08-07 22:27:24 +00003549}
3550
3551bool Attributor::checkForAllReturnedValues(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003552 const function_ref<bool(Value &)> &Pred,
Johannes Doerfert14a04932019-08-07 22:27:24 +00003553 const AbstractAttribute &QueryingAA) {
3554
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003555 const IRPosition &IRP = QueryingAA.getIRPosition();
3556 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003557 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00003558 return false;
3559
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003560 // TODO: use the function scope once we have call site AAReturnedValues.
3561 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003562 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003563 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003564 return false;
3565
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003566 return AARetVal.checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00003567 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00003568 return Pred(RV);
3569 });
Johannes Doerfert14a04932019-08-07 22:27:24 +00003570}
3571
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00003572static bool
3573checkForAllInstructionsImpl(InformationCache::OpcodeInstMapTy &OpcodeInstMap,
3574 const function_ref<bool(Instruction &)> &Pred,
3575 const AAIsDead *LivenessAA, bool &AnyDead,
3576 const ArrayRef<unsigned> &Opcodes) {
3577 for (unsigned Opcode : Opcodes) {
3578 for (Instruction *I : OpcodeInstMap[Opcode]) {
3579 // Skip dead instructions.
3580 if (LivenessAA && LivenessAA->isAssumedDead(I)) {
3581 AnyDead = true;
3582 continue;
3583 }
3584
3585 if (!Pred(*I))
3586 return false;
3587 }
3588 }
3589 return true;
3590}
3591
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003592bool Attributor::checkForAllInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003593 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00003594 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes) {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003595
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003596 const IRPosition &IRP = QueryingAA.getIRPosition();
3597 // Since we need to provide instructions we have to have an exact definition.
3598 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003599 if (!AssociatedFunction)
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003600 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003601
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003602 // TODO: use the function scope once we have call site AAReturnedValues.
3603 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert19b00432019-08-26 17:48:05 +00003604 const auto &LivenessAA =
3605 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
3606 bool AnyDead = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003607
3608 auto &OpcodeInstMap =
3609 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00003610 if (!checkForAllInstructionsImpl(OpcodeInstMap, Pred, &LivenessAA, AnyDead, Opcodes))
3611 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003612
Johannes Doerfert19b00432019-08-26 17:48:05 +00003613 // If we actually used liveness information so we have to record a dependence.
3614 if (AnyDead)
3615 recordDependence(LivenessAA, QueryingAA);
3616
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003617 return true;
3618}
3619
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003620bool Attributor::checkForAllReadWriteInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003621 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00003622 AbstractAttribute &QueryingAA) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003623
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003624 const Function *AssociatedFunction =
3625 QueryingAA.getIRPosition().getAssociatedFunction();
3626 if (!AssociatedFunction)
3627 return false;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003628
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003629 // TODO: use the function scope once we have call site AAReturnedValues.
3630 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
3631 const auto &LivenessAA =
3632 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
Johannes Doerfert19b00432019-08-26 17:48:05 +00003633 bool AnyDead = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003634
3635 for (Instruction *I :
3636 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003637 // Skip dead instructions.
Johannes Doerfert19b00432019-08-26 17:48:05 +00003638 if (LivenessAA.isAssumedDead(I)) {
3639 AnyDead = true;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003640 continue;
Johannes Doerfert19b00432019-08-26 17:48:05 +00003641 }
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003642
3643 if (!Pred(*I))
3644 return false;
3645 }
3646
Johannes Doerfert19b00432019-08-26 17:48:05 +00003647 // If we actually used liveness information so we have to record a dependence.
3648 if (AnyDead)
3649 recordDependence(LivenessAA, QueryingAA);
3650
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00003651 return true;
3652}
3653
Johannes Doerfert2f622062019-09-04 16:35:20 +00003654ChangeStatus Attributor::run(Module &M) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00003655 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
3656 << AllAbstractAttributes.size()
3657 << " abstract attributes.\n");
3658
Stefan Stipanovic53605892019-06-27 11:27:54 +00003659 // Now that all abstract attributes are collected and initialized we start
3660 // the abstract analysis.
Johannes Doerfertaade7822019-06-05 03:02:24 +00003661
3662 unsigned IterationCounter = 1;
3663
3664 SmallVector<AbstractAttribute *, 64> ChangedAAs;
3665 SetVector<AbstractAttribute *> Worklist;
3666 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
3667
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003668 bool RecomputeDependences = false;
3669
Johannes Doerfertaade7822019-06-05 03:02:24 +00003670 do {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003671 // Remember the size to determine new attributes.
3672 size_t NumAAs = AllAbstractAttributes.size();
Johannes Doerfertaade7822019-06-05 03:02:24 +00003673 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
3674 << ", Worklist size: " << Worklist.size() << "\n");
3675
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003676 // If dependences (=QueryMap) are recomputed we have to look at all abstract
3677 // attributes again, regardless of what changed in the last iteration.
3678 if (RecomputeDependences) {
3679 LLVM_DEBUG(
3680 dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
3681 QueryMap.clear();
3682 ChangedAAs.clear();
3683 Worklist.insert(AllAbstractAttributes.begin(),
3684 AllAbstractAttributes.end());
3685 }
3686
Johannes Doerfertaade7822019-06-05 03:02:24 +00003687 // Add all abstract attributes that are potentially dependent on one that
3688 // changed to the work list.
3689 for (AbstractAttribute *ChangedAA : ChangedAAs) {
3690 auto &QuerriedAAs = QueryMap[ChangedAA];
3691 Worklist.insert(QuerriedAAs.begin(), QuerriedAAs.end());
3692 }
3693
Johannes Doerfertb504eb82019-08-26 18:55:47 +00003694 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
3695 << ", Worklist+Dependent size: " << Worklist.size()
3696 << "\n");
3697
Johannes Doerfertaade7822019-06-05 03:02:24 +00003698 // Reset the changed set.
3699 ChangedAAs.clear();
3700
3701 // Update all abstract attribute in the work list and record the ones that
3702 // changed.
3703 for (AbstractAttribute *AA : Worklist)
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003704 if (!isAssumedDead(*AA, nullptr))
3705 if (AA->update(*this) == ChangeStatus::CHANGED)
3706 ChangedAAs.push_back(AA);
Johannes Doerfertaade7822019-06-05 03:02:24 +00003707
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003708 // Check if we recompute the dependences in the next iteration.
3709 RecomputeDependences = (DepRecomputeInterval > 0 &&
3710 IterationCounter % DepRecomputeInterval == 0);
3711
Johannes Doerfert9543f142019-08-23 15:24:57 +00003712 // Add attributes to the changed set if they have been created in the last
3713 // iteration.
3714 ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs,
3715 AllAbstractAttributes.end());
3716
Johannes Doerfertaade7822019-06-05 03:02:24 +00003717 // Reset the work list and repopulate with the changed abstract attributes.
3718 // Note that dependent ones are added above.
3719 Worklist.clear();
3720 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
3721
Johannes Doerfertbf112132019-08-29 01:29:44 +00003722 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
3723 VerifyMaxFixpointIterations));
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00003724
Johannes Doerfertaade7822019-06-05 03:02:24 +00003725 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
3726 << IterationCounter << "/" << MaxFixpointIterations
3727 << " iterations\n");
3728
Johannes Doerfertbf112132019-08-29 01:29:44 +00003729 size_t NumFinalAAs = AllAbstractAttributes.size();
Johannes Doerfertb504eb82019-08-26 18:55:47 +00003730
Johannes Doerfertaade7822019-06-05 03:02:24 +00003731 bool FinishedAtFixpoint = Worklist.empty();
3732
3733 // Reset abstract arguments not settled in a sound fixpoint by now. This
3734 // happens when we stopped the fixpoint iteration early. Note that only the
3735 // ones marked as "changed" *and* the ones transitively depending on them
3736 // need to be reverted to a pessimistic state. Others might not be in a
3737 // fixpoint state but we can use the optimistic results for them anyway.
3738 SmallPtrSet<AbstractAttribute *, 32> Visited;
3739 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
3740 AbstractAttribute *ChangedAA = ChangedAAs[u];
3741 if (!Visited.insert(ChangedAA).second)
3742 continue;
3743
3744 AbstractState &State = ChangedAA->getState();
3745 if (!State.isAtFixpoint()) {
3746 State.indicatePessimisticFixpoint();
3747
3748 NumAttributesTimedOut++;
3749 }
3750
3751 auto &QuerriedAAs = QueryMap[ChangedAA];
3752 ChangedAAs.append(QuerriedAAs.begin(), QuerriedAAs.end());
3753 }
3754
3755 LLVM_DEBUG({
3756 if (!Visited.empty())
3757 dbgs() << "\n[Attributor] Finalized " << Visited.size()
3758 << " abstract attributes.\n";
3759 });
3760
3761 unsigned NumManifested = 0;
3762 unsigned NumAtFixpoint = 0;
3763 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
3764 for (AbstractAttribute *AA : AllAbstractAttributes) {
3765 AbstractState &State = AA->getState();
3766
3767 // If there is not already a fixpoint reached, we can now take the
3768 // optimistic state. This is correct because we enforced a pessimistic one
3769 // on abstract attributes that were transitively dependent on a changed one
3770 // already above.
3771 if (!State.isAtFixpoint())
3772 State.indicateOptimisticFixpoint();
3773
3774 // If the state is invalid, we do not try to manifest it.
3775 if (!State.isValidState())
3776 continue;
3777
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00003778 // Skip dead code.
3779 if (isAssumedDead(*AA, nullptr))
3780 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +00003781 // Manifest the state and record if we changed the IR.
3782 ChangeStatus LocalChange = AA->manifest(*this);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003783 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
3784 AA->trackStatistics();
3785
Johannes Doerfertaade7822019-06-05 03:02:24 +00003786 ManifestChange = ManifestChange | LocalChange;
3787
3788 NumAtFixpoint++;
3789 NumManifested += (LocalChange == ChangeStatus::CHANGED);
3790 }
3791
3792 (void)NumManifested;
3793 (void)NumAtFixpoint;
3794 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
3795 << " arguments while " << NumAtFixpoint
3796 << " were in a valid fixpoint state\n");
3797
3798 // If verification is requested, we finished this run at a fixpoint, and the
3799 // IR was changed, we re-run the whole fixpoint analysis, starting at
3800 // re-initialization of the arguments. This re-run should not result in an IR
3801 // change. Though, the (virtual) state of attributes at the end of the re-run
3802 // might be more optimistic than the known state or the IR state if the better
3803 // state cannot be manifested.
3804 if (VerifyAttributor && FinishedAtFixpoint &&
3805 ManifestChange == ChangeStatus::CHANGED) {
3806 VerifyAttributor = false;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003807 ChangeStatus VerifyStatus = run(M);
Johannes Doerfertaade7822019-06-05 03:02:24 +00003808 if (VerifyStatus != ChangeStatus::UNCHANGED)
3809 llvm_unreachable(
3810 "Attributor verification failed, re-run did result in an IR change "
3811 "even after a fixpoint was reached in the original run. (False "
3812 "positives possible!)");
3813 VerifyAttributor = true;
3814 }
3815
3816 NumAttributesManifested += NumManifested;
3817 NumAttributesValidFixpoint += NumAtFixpoint;
3818
Fangrui Songf1826172019-08-20 07:21:43 +00003819 (void)NumFinalAAs;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003820 assert(
3821 NumFinalAAs == AllAbstractAttributes.size() &&
3822 "Expected the final number of abstract attributes to remain unchanged!");
Johannes Doerfert39681e72019-08-27 04:57:54 +00003823
3824 // Delete stuff at the end to avoid invalid references and a nice order.
Johannes Doerfert2f622062019-09-04 16:35:20 +00003825 {
3826 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
3827 << ToBeDeletedFunctions.size() << " functions and "
3828 << ToBeDeletedBlocks.size() << " blocks and "
3829 << ToBeDeletedInsts.size() << " instructions\n");
3830 for (Instruction *I : ToBeDeletedInsts) {
3831 if (!I->use_empty())
3832 I->replaceAllUsesWith(UndefValue::get(I->getType()));
3833 I->eraseFromParent();
3834 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003835
Johannes Doerfert2f622062019-09-04 16:35:20 +00003836 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
3837 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
3838 ToBeDeletedBBs.reserve(NumDeadBlocks);
3839 ToBeDeletedBBs.append(ToBeDeletedBlocks.begin(), ToBeDeletedBlocks.end());
3840 DeleteDeadBlocks(ToBeDeletedBBs);
3841 STATS_DECLTRACK(AAIsDead, BasicBlock,
3842 "Number of dead basic blocks deleted.");
3843 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003844
Johannes Doerfert2f622062019-09-04 16:35:20 +00003845 STATS_DECL(AAIsDead, Function, "Number of dead functions deleted.");
3846 for (Function *Fn : ToBeDeletedFunctions) {
3847 Fn->replaceAllUsesWith(UndefValue::get(Fn->getType()));
3848 Fn->eraseFromParent();
3849 STATS_TRACK(AAIsDead, Function);
3850 }
3851
3852 // Identify dead internal functions and delete them. This happens outside
3853 // the other fixpoint analysis as we might treat potentially dead functions
3854 // as live to lower the number of iterations. If they happen to be dead, the
3855 // below fixpoint loop will identify and eliminate them.
3856 SmallVector<Function *, 8> InternalFns;
3857 for (Function &F : M)
3858 if (F.hasInternalLinkage())
3859 InternalFns.push_back(&F);
3860
3861 bool FoundDeadFn = true;
3862 while (FoundDeadFn) {
3863 FoundDeadFn = false;
3864 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
3865 Function *F = InternalFns[u];
3866 if (!F)
3867 continue;
3868
3869 const auto *LivenessAA =
3870 lookupAAFor<AAIsDead>(IRPosition::function(*F));
3871 if (LivenessAA &&
3872 !checkForAllCallSites([](CallSite CS) { return false; },
3873 *LivenessAA, true))
3874 continue;
3875
3876 STATS_TRACK(AAIsDead, Function);
3877 F->replaceAllUsesWith(UndefValue::get(F->getType()));
3878 F->eraseFromParent();
3879 InternalFns[u] = nullptr;
3880 FoundDeadFn = true;
3881 }
3882 }
Johannes Doerfert39681e72019-08-27 04:57:54 +00003883 }
3884
Johannes Doerfertbf112132019-08-29 01:29:44 +00003885 if (VerifyMaxFixpointIterations &&
3886 IterationCounter != MaxFixpointIterations) {
3887 errs() << "\n[Attributor] Fixpoint iteration done after: "
3888 << IterationCounter << "/" << MaxFixpointIterations
3889 << " iterations\n";
3890 llvm_unreachable("The fixpoint was not reached with exactly the number of "
3891 "specified iterations!");
3892 }
3893
Johannes Doerfertaade7822019-06-05 03:02:24 +00003894 return ManifestChange;
3895}
3896
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00003897void Attributor::initializeInformationCache(Function &F) {
3898
3899 // Walk all instructions to find interesting instructions that might be
3900 // queried by abstract attributes during their initialization or update.
3901 // This has to happen before we create attributes.
3902 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
3903 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
3904
3905 for (Instruction &I : instructions(&F)) {
3906 bool IsInterestingOpcode = false;
3907
3908 // To allow easy access to all instructions in a function with a given
3909 // opcode we store them in the InfoCache. As not all opcodes are interesting
3910 // to concrete attributes we only cache the ones that are as identified in
3911 // the following switch.
3912 // Note: There are no concrete attributes now so this is initially empty.
3913 switch (I.getOpcode()) {
3914 default:
3915 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
3916 "New call site/base instruction type needs to be known int the "
3917 "Attributor.");
3918 break;
3919 case Instruction::Load:
3920 // The alignment of a pointer is interesting for loads.
3921 case Instruction::Store:
3922 // The alignment of a pointer is interesting for stores.
3923 case Instruction::Call:
3924 case Instruction::CallBr:
3925 case Instruction::Invoke:
3926 case Instruction::CleanupRet:
3927 case Instruction::CatchSwitch:
3928 case Instruction::Resume:
3929 case Instruction::Ret:
3930 IsInterestingOpcode = true;
3931 }
3932 if (IsInterestingOpcode)
3933 InstOpcodeMap[I.getOpcode()].push_back(&I);
3934 if (I.mayReadOrWriteMemory())
3935 ReadOrWriteInsts.push_back(&I);
3936 }
3937}
3938
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00003939void Attributor::identifyDefaultAbstractAttributes(Function &F) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00003940 if (!VisitedFunctions.insert(&F).second)
3941 return;
Johannes Doerfertaade7822019-06-05 03:02:24 +00003942
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003943 IRPosition FPos = IRPosition::function(F);
3944
Johannes Doerfert305b9612019-08-04 18:40:01 +00003945 // Check for dead BasicBlocks in every function.
Johannes Doerfert21fe0a32019-08-06 00:55:11 +00003946 // We need dead instruction detection because we do not want to deal with
3947 // broken IR in which SSA rules do not apply.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003948 getOrCreateAAFor<AAIsDead>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00003949
3950 // Every function might be "will-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003951 getOrCreateAAFor<AAWillReturn>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00003952
Stefan Stipanovic53605892019-06-27 11:27:54 +00003953 // Every function can be nounwind.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003954 getOrCreateAAFor<AANoUnwind>(FPos);
Stefan Stipanovic53605892019-06-27 11:27:54 +00003955
Stefan Stipanovic06263672019-07-11 21:37:40 +00003956 // Every function might be marked "nosync"
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003957 getOrCreateAAFor<AANoSync>(FPos);
Stefan Stipanovic06263672019-07-11 21:37:40 +00003958
Hideto Ueno65bbaf92019-07-12 17:38:51 +00003959 // Every function might be "no-free".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003960 getOrCreateAAFor<AANoFree>(FPos);
Hideto Ueno65bbaf92019-07-12 17:38:51 +00003961
Johannes Doerferte83f3032019-08-05 23:22:05 +00003962 // Every function might be "no-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003963 getOrCreateAAFor<AANoReturn>(FPos);
Johannes Doerferte83f3032019-08-05 23:22:05 +00003964
Hideto Ueno63f60662019-09-21 15:13:19 +00003965 // Every function might be "no-recurse".
3966 getOrCreateAAFor<AANoRecurse>(FPos);
3967
Stefan Stipanovic431141c2019-09-15 21:47:41 +00003968 // Every function might be applicable for Heap-To-Stack conversion.
3969 if (EnableHeapToStack)
3970 getOrCreateAAFor<AAHeapToStack>(FPos);
3971
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00003972 // Return attributes are only appropriate if the return type is non void.
3973 Type *ReturnType = F.getReturnType();
3974 if (!ReturnType->isVoidTy()) {
3975 // Argument attribute "returned" --- Create only one per function even
3976 // though it is an argument attribute.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003977 getOrCreateAAFor<AAReturnedValues>(FPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00003978
Hideto Uenof2b9dc42019-09-07 07:03:05 +00003979 IRPosition RetPos = IRPosition::returned(F);
3980
3981 // Every function might be simplified.
3982 getOrCreateAAFor<AAValueSimplify>(RetPos);
3983
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003984 if (ReturnType->isPointerTy()) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003985
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003986 // Every function with pointer return type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003987 getOrCreateAAFor<AAAlign>(RetPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003988
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003989 // Every function with pointer return type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003990 getOrCreateAAFor<AANonNull>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003991
3992 // Every function with pointer return type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003993 getOrCreateAAFor<AANoAlias>(RetPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00003994
3995 // Every function with pointer return type might be marked
3996 // dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003997 getOrCreateAAFor<AADereferenceable>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00003998 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00003999 }
4000
Hideto Ueno54869ec2019-07-15 06:49:04 +00004001 for (Argument &Arg : F.args()) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004002 IRPosition ArgPos = IRPosition::argument(Arg);
4003
4004 // Every argument might be simplified.
4005 getOrCreateAAFor<AAValueSimplify>(ArgPos);
4006
Hideto Ueno19c07af2019-07-23 08:16:17 +00004007 if (Arg.getType()->isPointerTy()) {
4008 // Every argument with pointer type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004009 getOrCreateAAFor<AANonNull>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00004010
Hideto Uenocbab3342019-08-29 05:52:00 +00004011 // Every argument with pointer type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004012 getOrCreateAAFor<AANoAlias>(ArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00004013
Hideto Ueno19c07af2019-07-23 08:16:17 +00004014 // Every argument with pointer type might be marked dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004015 getOrCreateAAFor<AADereferenceable>(ArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00004016
4017 // Every argument with pointer type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004018 getOrCreateAAFor<AAAlign>(ArgPos);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004019
4020 // Every argument with pointer type might be marked nocapture.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004021 getOrCreateAAFor<AANoCapture>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00004022 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00004023 }
4024
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00004025 auto CallSitePred = [&](Instruction &I) -> bool {
Hideto Ueno54869ec2019-07-15 06:49:04 +00004026 CallSite CS(&I);
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00004027 if (CS.getCalledFunction()) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00004028 for (int i = 0, e = CS.getCalledFunction()->arg_size(); i < e; i++) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004029
4030 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
4031
4032 // Call site argument might be simplified.
4033 getOrCreateAAFor<AAValueSimplify>(CSArgPos);
4034
Hideto Ueno54869ec2019-07-15 06:49:04 +00004035 if (!CS.getArgument(i)->getType()->isPointerTy())
4036 continue;
4037
4038 // Call site argument attribute "non-null".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004039 getOrCreateAAFor<AANonNull>(CSArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00004040
Hideto Uenocbab3342019-08-29 05:52:00 +00004041 // Call site argument attribute "no-alias".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004042 getOrCreateAAFor<AANoAlias>(CSArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00004043
Hideto Ueno19c07af2019-07-23 08:16:17 +00004044 // Call site argument attribute "dereferenceable".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004045 getOrCreateAAFor<AADereferenceable>(CSArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00004046
4047 // Call site argument attribute "align".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00004048 getOrCreateAAFor<AAAlign>(CSArgPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00004049 }
4050 }
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00004051 return true;
4052 };
4053
4054 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
4055 bool Success, AnyDead = false;
4056 Success = checkForAllInstructionsImpl(
4057 OpcodeInstMap, CallSitePred, nullptr, AnyDead,
4058 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
4059 (unsigned)Instruction::Call});
4060 (void)Success;
4061 assert(Success && !AnyDead && "Expected the check call to be successful!");
4062
4063 auto LoadStorePred = [&](Instruction &I) -> bool {
4064 if (isa<LoadInst>(I))
4065 getOrCreateAAFor<AAAlign>(
4066 IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
4067 else
4068 getOrCreateAAFor<AAAlign>(
4069 IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
4070 return true;
4071 };
4072 Success = checkForAllInstructionsImpl(
4073 OpcodeInstMap, LoadStorePred, nullptr, AnyDead,
4074 {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
4075 (void)Success;
4076 assert(Success && !AnyDead && "Expected the check call to be successful!");
Johannes Doerfertaade7822019-06-05 03:02:24 +00004077}
4078
4079/// Helpers to ease debugging through output streams and print calls.
4080///
4081///{
4082raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
4083 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
4084}
4085
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004086raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00004087 switch (AP) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00004088 case IRPosition::IRP_INVALID:
4089 return OS << "inv";
4090 case IRPosition::IRP_FLOAT:
4091 return OS << "flt";
4092 case IRPosition::IRP_RETURNED:
4093 return OS << "fn_ret";
4094 case IRPosition::IRP_CALL_SITE_RETURNED:
4095 return OS << "cs_ret";
4096 case IRPosition::IRP_FUNCTION:
4097 return OS << "fn";
4098 case IRPosition::IRP_CALL_SITE:
4099 return OS << "cs";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004100 case IRPosition::IRP_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00004101 return OS << "arg";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004102 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00004103 return OS << "cs_arg";
Johannes Doerfertaade7822019-06-05 03:02:24 +00004104 }
4105 llvm_unreachable("Unknown attribute position!");
4106}
4107
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004108raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00004109 const Value &AV = Pos.getAssociatedValue();
4110 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004111 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
4112}
4113
Johannes Doerfertacc80792019-08-12 22:07:34 +00004114raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerState &S) {
4115 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
4116 << static_cast<const AbstractState &>(S);
4117}
4118
Johannes Doerfertaade7822019-06-05 03:02:24 +00004119raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
4120 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
4121}
4122
4123raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
4124 AA.print(OS);
4125 return OS;
4126}
4127
4128void AbstractAttribute::print(raw_ostream &OS) const {
Johannes Doerfertfb69f762019-08-05 23:32:31 +00004129 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
4130 << "]";
Johannes Doerfertaade7822019-06-05 03:02:24 +00004131}
4132///}
4133
4134/// ----------------------------------------------------------------------------
4135/// Pass (Manager) Boilerplate
4136/// ----------------------------------------------------------------------------
4137
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00004138static bool runAttributorOnModule(Module &M, AnalysisGetter &AG) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00004139 if (DisableAttributor)
4140 return false;
4141
4142 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << M.size()
4143 << " functions.\n");
4144
4145 // Create an Attributor and initially empty information cache that is filled
4146 // while we identify default attribute opportunities.
Hideto Ueno63f60662019-09-21 15:13:19 +00004147 InformationCache InfoCache(M, AG);
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00004148 Attributor A(InfoCache, DepRecInterval);
Johannes Doerfertaade7822019-06-05 03:02:24 +00004149
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00004150 for (Function &F : M)
4151 A.initializeInformationCache(F);
4152
Johannes Doerfertaade7822019-06-05 03:02:24 +00004153 for (Function &F : M) {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00004154 if (F.hasExactDefinition())
4155 NumFnWithExactDefinition++;
4156 else
Johannes Doerfertaade7822019-06-05 03:02:24 +00004157 NumFnWithoutExactDefinition++;
Johannes Doerfertaade7822019-06-05 03:02:24 +00004158
4159 // For now we ignore naked and optnone functions.
4160 if (F.hasFnAttribute(Attribute::Naked) ||
4161 F.hasFnAttribute(Attribute::OptimizeNone))
4162 continue;
4163
Johannes Doerfert2f622062019-09-04 16:35:20 +00004164 // We look at internal functions only on-demand but if any use is not a
4165 // direct call, we have to do it eagerly.
4166 if (F.hasInternalLinkage()) {
4167 if (llvm::all_of(F.uses(), [](const Use &U) {
4168 return ImmutableCallSite(U.getUser()) &&
4169 ImmutableCallSite(U.getUser()).isCallee(&U);
4170 }))
4171 continue;
4172 }
4173
Johannes Doerfertaade7822019-06-05 03:02:24 +00004174 // Populate the Attributor with abstract attribute opportunities in the
4175 // function and the information cache with IR information.
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00004176 A.identifyDefaultAbstractAttributes(F);
Johannes Doerfertaade7822019-06-05 03:02:24 +00004177 }
4178
Johannes Doerfert2f622062019-09-04 16:35:20 +00004179 return A.run(M) == ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +00004180}
4181
4182PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
Hideto Ueno63f60662019-09-21 15:13:19 +00004183 AnalysisGetter AG(AM);
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00004184 if (runAttributorOnModule(M, AG)) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00004185 // FIXME: Think about passes we will preserve and add them here.
4186 return PreservedAnalyses::none();
4187 }
4188 return PreservedAnalyses::all();
4189}
4190
4191namespace {
4192
4193struct AttributorLegacyPass : public ModulePass {
4194 static char ID;
4195
4196 AttributorLegacyPass() : ModulePass(ID) {
4197 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
4198 }
4199
4200 bool runOnModule(Module &M) override {
4201 if (skipModule(M))
4202 return false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004203
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00004204 AnalysisGetter AG;
4205 return runAttributorOnModule(M, AG);
Johannes Doerfertaade7822019-06-05 03:02:24 +00004206 }
4207
4208 void getAnalysisUsage(AnalysisUsage &AU) const override {
4209 // FIXME: Think about passes we will preserve and add them here.
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004210 AU.addRequired<TargetLibraryInfoWrapperPass>();
Johannes Doerfertaade7822019-06-05 03:02:24 +00004211 }
4212};
4213
4214} // end anonymous namespace
4215
4216Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
4217
4218char AttributorLegacyPass::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00004219
4220const char AAReturnedValues::ID = 0;
4221const char AANoUnwind::ID = 0;
4222const char AANoSync::ID = 0;
Johannes Doerferteccdf082019-08-05 23:35:12 +00004223const char AANoFree::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00004224const char AANonNull::ID = 0;
4225const char AANoRecurse::ID = 0;
4226const char AAWillReturn::ID = 0;
4227const char AANoAlias::ID = 0;
4228const char AANoReturn::ID = 0;
4229const char AAIsDead::ID = 0;
4230const char AADereferenceable::ID = 0;
4231const char AAAlign::ID = 0;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004232const char AANoCapture::ID = 0;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004233const char AAValueSimplify::ID = 0;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004234const char AAHeapToStack::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00004235
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004236// Macro magic to create the static generator function for attributes that
4237// follow the naming scheme.
4238
4239#define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
4240 case IRPosition::PK: \
4241 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
4242
4243#define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
4244 case IRPosition::PK: \
4245 AA = new CLASS##SUFFIX(IRP); \
4246 break;
4247
4248#define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4249 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4250 CLASS *AA = nullptr; \
4251 switch (IRP.getPositionKind()) { \
4252 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4253 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
4254 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
4255 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
4256 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
4257 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
4258 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4259 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
4260 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004261 return *AA; \
4262 }
4263
4264#define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4265 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4266 CLASS *AA = nullptr; \
4267 switch (IRP.getPositionKind()) { \
4268 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4269 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
4270 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
4271 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
4272 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
4273 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
4274 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
4275 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
4276 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004277 return *AA; \
4278 }
4279
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004280#define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4281 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4282 CLASS *AA = nullptr; \
4283 switch (IRP.getPositionKind()) { \
4284 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4285 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4286 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
4287 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
4288 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
4289 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
4290 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
4291 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
4292 } \
4293 return *AA; \
4294 }
4295
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004296#define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
4297 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
4298 CLASS *AA = nullptr; \
4299 switch (IRP.getPositionKind()) { \
4300 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
4301 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
4302 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
4303 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
4304 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
4305 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
4306 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
4307 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
4308 } \
4309 AA->initialize(A); \
4310 return *AA; \
4311 }
4312
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004313CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
4314CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
4315CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
4316CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
4317CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
4318CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
4319CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
4320CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
4321
4322CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
4323CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
4324CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
4325CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004326CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004327
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004328CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
4329
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004330CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
4331
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004332#undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
4333#undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004334#undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00004335#undef SWITCH_PK_CREATE
4336#undef SWITCH_PK_INV
4337
Johannes Doerfertaade7822019-06-05 03:02:24 +00004338INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
4339 "Deduce and propagate attributes", false, false)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004340INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Johannes Doerfertaade7822019-06-05 03:02:24 +00004341INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
4342 "Deduce and propagate attributes", false, false)