LLVM 21.0.0git
BasicAliasAnalysis.cpp
Go to the documentation of this file.
1//===- BasicAliasAnalysis.cpp - Stateless Alias Analysis Impl -------------===//
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 defines the primary stateless implementation of the
10// Alias Analysis interface that implements identities (two different
11// globals cannot alias, etc), but does no stateful analysis.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ScopeExit.h"
20#include "llvm/ADT/Statistic.h"
23#include "llvm/Analysis/CFG.h"
29#include "llvm/IR/Argument.h"
30#include "llvm/IR/Attributes.h"
31#include "llvm/IR/Constant.h"
33#include "llvm/IR/Constants.h"
34#include "llvm/IR/DataLayout.h"
36#include "llvm/IR/Dominators.h"
37#include "llvm/IR/Function.h"
39#include "llvm/IR/GlobalAlias.h"
41#include "llvm/IR/InstrTypes.h"
42#include "llvm/IR/Instruction.h"
45#include "llvm/IR/Intrinsics.h"
46#include "llvm/IR/Operator.h"
48#include "llvm/IR/Type.h"
49#include "llvm/IR/User.h"
50#include "llvm/IR/Value.h"
52#include "llvm/Pass.h"
58#include <cassert>
59#include <cstdint>
60#include <cstdlib>
61#include <optional>
62#include <utility>
63
64#define DEBUG_TYPE "basicaa"
65
66using namespace llvm;
67
68/// Enable analysis of recursive PHI nodes.
70 cl::init(true));
71
72static cl::opt<bool> EnableSeparateStorageAnalysis("basic-aa-separate-storage",
73 cl::Hidden, cl::init(true));
74
75/// SearchLimitReached / SearchTimes shows how often the limit of
76/// to decompose GEPs is reached. It will affect the precision
77/// of basic alias analysis.
78STATISTIC(SearchLimitReached, "Number of times the limit to "
79 "decompose GEPs is reached");
80STATISTIC(SearchTimes, "Number of times a GEP is decomposed");
81
82// The max limit of the search depth in DecomposeGEPExpression() and
83// getUnderlyingObject().
84static const unsigned MaxLookupSearchDepth = 6;
85
88 // We don't care if this analysis itself is preserved, it has no state. But
89 // we need to check that the analyses it depends on have been. Note that we
90 // may be created without handles to some analyses and in that case don't
91 // depend on them.
92 if (Inv.invalidate<AssumptionAnalysis>(Fn, PA) ||
93 (DT_ && Inv.invalidate<DominatorTreeAnalysis>(Fn, PA)))
94 return true;
95
96 // Otherwise this analysis result remains valid.
97 return false;
98}
99
100//===----------------------------------------------------------------------===//
101// Useful predicates
102//===----------------------------------------------------------------------===//
103
104/// Returns the size of the object specified by V or UnknownSize if unknown.
105static std::optional<TypeSize> getObjectSize(const Value *V,
106 const DataLayout &DL,
107 const TargetLibraryInfo &TLI,
108 bool NullIsValidLoc,
109 bool RoundToAlign = false) {
111 ObjectSizeOpts Opts;
112 Opts.RoundToAlign = RoundToAlign;
113 Opts.NullIsUnknownSize = NullIsValidLoc;
114 if (getObjectSize(V, Size, DL, &TLI, Opts))
115 return TypeSize::getFixed(Size);
116 return std::nullopt;
117}
118
119/// Returns true if we can prove that the object specified by V is smaller than
120/// Size. Bails out early unless the root object is passed as the first
121/// parameter.
123 const DataLayout &DL,
124 const TargetLibraryInfo &TLI,
125 bool NullIsValidLoc) {
126 // Note that the meanings of the "object" are slightly different in the
127 // following contexts:
128 // c1: llvm::getObjectSize()
129 // c2: llvm.objectsize() intrinsic
130 // c3: isObjectSmallerThan()
131 // c1 and c2 share the same meaning; however, the meaning of "object" in c3
132 // refers to the "entire object".
133 //
134 // Consider this example:
135 // char *p = (char*)malloc(100)
136 // char *q = p+80;
137 //
138 // In the context of c1 and c2, the "object" pointed by q refers to the
139 // stretch of memory of q[0:19]. So, getObjectSize(q) should return 20.
140 //
141 // In the context of c3, the "object" refers to the chunk of memory being
142 // allocated. So, the "object" has 100 bytes, and q points to the middle the
143 // "object". However, unless p, the root object, is passed as the first
144 // parameter, the call to isIdentifiedObject() makes isObjectSmallerThan()
145 // bail out early.
146 if (!isIdentifiedObject(V))
147 return false;
148
149 // This function needs to use the aligned object size because we allow
150 // reads a bit past the end given sufficient alignment.
151 std::optional<TypeSize> ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
152 /*RoundToAlign*/ true);
153
154 return ObjectSize && TypeSize::isKnownLT(*ObjectSize, Size);
155}
156
157/// Return the minimal extent from \p V to the end of the underlying object,
158/// assuming the result is used in an aliasing query. E.g., we do use the query
159/// location size and the fact that null pointers cannot alias here.
161 const LocationSize &LocSize,
162 const DataLayout &DL,
163 bool NullIsValidLoc) {
164 // If we have dereferenceability information we know a lower bound for the
165 // extent as accesses for a lower offset would be valid. We need to exclude
166 // the "or null" part if null is a valid pointer. We can ignore frees, as an
167 // access after free would be undefined behavior.
168 bool CanBeNull, CanBeFreed;
169 uint64_t DerefBytes =
170 V.getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
171 DerefBytes = (CanBeNull && NullIsValidLoc) ? 0 : DerefBytes;
172 // If queried with a precise location size, we assume that location size to be
173 // accessed, thus valid.
174 if (LocSize.isPrecise())
175 DerefBytes = std::max(DerefBytes, LocSize.getValue().getKnownMinValue());
176 return TypeSize::getFixed(DerefBytes);
177}
178
179/// Returns true if we can prove that the object specified by V has size Size.
180static bool isObjectSize(const Value *V, TypeSize Size, const DataLayout &DL,
181 const TargetLibraryInfo &TLI, bool NullIsValidLoc) {
182 std::optional<TypeSize> ObjectSize =
183 getObjectSize(V, DL, TLI, NullIsValidLoc);
184 return ObjectSize && *ObjectSize == Size;
185}
186
187/// Return true if both V1 and V2 are VScale
188static bool areBothVScale(const Value *V1, const Value *V2) {
191}
192
193//===----------------------------------------------------------------------===//
194// CaptureAnalysis implementations
195//===----------------------------------------------------------------------===//
196
198
200 const Instruction *I,
201 bool OrAt) {
202 return isNonEscapingLocalObject(Object, &IsCapturedCache);
203}
204
205static bool isNotInCycle(const Instruction *I, const DominatorTree *DT,
206 const LoopInfo *LI) {
207 BasicBlock *BB = const_cast<BasicBlock *>(I->getParent());
209 return Succs.empty() ||
210 !isPotentiallyReachableFromMany(Succs, BB, nullptr, DT, LI);
211}
212
214 const Instruction *I,
215 bool OrAt) {
216 if (!isIdentifiedFunctionLocal(Object))
217 return false;
218
219 auto Iter = EarliestEscapes.insert({Object, nullptr});
220 if (Iter.second) {
221 Instruction *EarliestCapture = FindEarliestCapture(
222 Object, *const_cast<Function *>(DT.getRoot()->getParent()),
223 /*ReturnCaptures=*/false, /*StoreCaptures=*/true, DT);
224 if (EarliestCapture)
225 Inst2Obj[EarliestCapture].push_back(Object);
226 Iter.first->second = EarliestCapture;
227 }
228
229 // No capturing instruction.
230 if (!Iter.first->second)
231 return true;
232
233 // No context instruction means any use is capturing.
234 if (!I)
235 return false;
236
237 if (I == Iter.first->second) {
238 if (OrAt)
239 return false;
240 return isNotInCycle(I, &DT, LI);
241 }
242
243 return !isPotentiallyReachable(Iter.first->second, I, nullptr, &DT, LI);
244}
245
247 auto Iter = Inst2Obj.find(I);
248 if (Iter != Inst2Obj.end()) {
249 for (const Value *Obj : Iter->second)
250 EarliestEscapes.erase(Obj);
251 Inst2Obj.erase(I);
252 }
253}
254
255//===----------------------------------------------------------------------===//
256// GetElementPtr Instruction Decomposition and Analysis
257//===----------------------------------------------------------------------===//
258
259namespace {
260/// Represents zext(sext(trunc(V))).
261struct CastedValue {
262 const Value *V;
263 unsigned ZExtBits = 0;
264 unsigned SExtBits = 0;
265 unsigned TruncBits = 0;
266 /// Whether trunc(V) is non-negative.
267 bool IsNonNegative = false;
268
269 explicit CastedValue(const Value *V) : V(V) {}
270 explicit CastedValue(const Value *V, unsigned ZExtBits, unsigned SExtBits,
271 unsigned TruncBits, bool IsNonNegative)
272 : V(V), ZExtBits(ZExtBits), SExtBits(SExtBits), TruncBits(TruncBits),
273 IsNonNegative(IsNonNegative) {}
274
275 unsigned getBitWidth() const {
276 return V->getType()->getPrimitiveSizeInBits() - TruncBits + ZExtBits +
277 SExtBits;
278 }
279
280 CastedValue withValue(const Value *NewV, bool PreserveNonNeg) const {
281 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits,
282 IsNonNegative && PreserveNonNeg);
283 }
284
285 /// Replace V with zext(NewV)
286 CastedValue withZExtOfValue(const Value *NewV, bool ZExtNonNegative) const {
287 unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
289 if (ExtendBy <= TruncBits)
290 // zext<nneg>(trunc(zext(NewV))) == zext<nneg>(trunc(NewV))
291 // The nneg can be preserved on the outer zext here.
292 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
293 IsNonNegative);
294
295 // zext(sext(zext(NewV))) == zext(zext(zext(NewV)))
296 ExtendBy -= TruncBits;
297 // zext<nneg>(zext(NewV)) == zext(NewV)
298 // zext(zext<nneg>(NewV)) == zext<nneg>(NewV)
299 // The nneg can be preserved from the inner zext here but must be dropped
300 // from the outer.
301 return CastedValue(NewV, ZExtBits + SExtBits + ExtendBy, 0, 0,
302 ZExtNonNegative);
303 }
304
305 /// Replace V with sext(NewV)
306 CastedValue withSExtOfValue(const Value *NewV) const {
307 unsigned ExtendBy = V->getType()->getPrimitiveSizeInBits() -
309 if (ExtendBy <= TruncBits)
310 // zext<nneg>(trunc(sext(NewV))) == zext<nneg>(trunc(NewV))
311 // The nneg can be preserved on the outer zext here
312 return CastedValue(NewV, ZExtBits, SExtBits, TruncBits - ExtendBy,
313 IsNonNegative);
314
315 // zext(sext(sext(NewV)))
316 ExtendBy -= TruncBits;
317 // zext<nneg>(sext(sext(NewV))) = zext<nneg>(sext(NewV))
318 // The nneg can be preserved on the outer zext here
319 return CastedValue(NewV, ZExtBits, SExtBits + ExtendBy, 0, IsNonNegative);
320 }
321
322 APInt evaluateWith(APInt N) const {
323 assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
324 "Incompatible bit width");
325 if (TruncBits) N = N.trunc(N.getBitWidth() - TruncBits);
326 if (SExtBits) N = N.sext(N.getBitWidth() + SExtBits);
327 if (ZExtBits) N = N.zext(N.getBitWidth() + ZExtBits);
328 return N;
329 }
330
331 ConstantRange evaluateWith(ConstantRange N) const {
332 assert(N.getBitWidth() == V->getType()->getPrimitiveSizeInBits() &&
333 "Incompatible bit width");
334 if (TruncBits) N = N.truncate(N.getBitWidth() - TruncBits);
335 if (IsNonNegative && !N.isAllNonNegative())
336 N = N.intersectWith(
337 ConstantRange(APInt::getZero(N.getBitWidth()),
338 APInt::getSignedMinValue(N.getBitWidth())));
339 if (SExtBits) N = N.signExtend(N.getBitWidth() + SExtBits);
340 if (ZExtBits) N = N.zeroExtend(N.getBitWidth() + ZExtBits);
341 return N;
342 }
343
344 bool canDistributeOver(bool NUW, bool NSW) const {
345 // zext(x op<nuw> y) == zext(x) op<nuw> zext(y)
346 // sext(x op<nsw> y) == sext(x) op<nsw> sext(y)
347 // trunc(x op y) == trunc(x) op trunc(y)
348 return (!ZExtBits || NUW) && (!SExtBits || NSW);
349 }
350
351 bool hasSameCastsAs(const CastedValue &Other) const {
352 if (V->getType() != Other.V->getType())
353 return false;
354
355 if (ZExtBits == Other.ZExtBits && SExtBits == Other.SExtBits &&
356 TruncBits == Other.TruncBits)
357 return true;
358 // If either CastedValue has a nneg zext then the sext/zext bits are
359 // interchangable for that value.
360 if (IsNonNegative || Other.IsNonNegative)
361 return (ZExtBits + SExtBits == Other.ZExtBits + Other.SExtBits &&
362 TruncBits == Other.TruncBits);
363 return false;
364 }
365};
366
367/// Represents zext(sext(trunc(V))) * Scale + Offset.
368struct LinearExpression {
369 CastedValue Val;
370 APInt Scale;
372
373 /// True if all operations in this expression are NUW.
374 bool IsNUW;
375 /// True if all operations in this expression are NSW.
376 bool IsNSW;
377
378 LinearExpression(const CastedValue &Val, const APInt &Scale,
379 const APInt &Offset, bool IsNUW, bool IsNSW)
380 : Val(Val), Scale(Scale), Offset(Offset), IsNUW(IsNUW), IsNSW(IsNSW) {}
381
382 LinearExpression(const CastedValue &Val)
383 : Val(Val), IsNUW(true), IsNSW(true) {
384 unsigned BitWidth = Val.getBitWidth();
385 Scale = APInt(BitWidth, 1);
386 Offset = APInt(BitWidth, 0);
387 }
388
389 LinearExpression mul(const APInt &Other, bool MulIsNUW, bool MulIsNSW) const {
390 // The check for zero offset is necessary, because generally
391 // (X +nsw Y) *nsw Z does not imply (X *nsw Z) +nsw (Y *nsw Z).
392 bool NSW = IsNSW && (Other.isOne() || (MulIsNSW && Offset.isZero()));
393 bool NUW = IsNUW && (Other.isOne() || MulIsNUW);
394 return LinearExpression(Val, Scale * Other, Offset * Other, NUW, NSW);
395 }
396};
397}
398
399/// Analyzes the specified value as a linear expression: "A*V + B", where A and
400/// B are constant integers.
401static LinearExpression GetLinearExpression(
402 const CastedValue &Val, const DataLayout &DL, unsigned Depth,
404 // Limit our recursion depth.
405 if (Depth == 6)
406 return Val;
407
408 if (const ConstantInt *Const = dyn_cast<ConstantInt>(Val.V))
409 return LinearExpression(Val, APInt(Val.getBitWidth(), 0),
410 Val.evaluateWith(Const->getValue()), true, true);
411
412 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(Val.V)) {
413 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
414 APInt RHS = Val.evaluateWith(RHSC->getValue());
415 // The only non-OBO case we deal with is or, and only limited to the
416 // case where it is both nuw and nsw.
417 bool NUW = true, NSW = true;
418 if (isa<OverflowingBinaryOperator>(BOp)) {
419 NUW &= BOp->hasNoUnsignedWrap();
420 NSW &= BOp->hasNoSignedWrap();
421 }
422 if (!Val.canDistributeOver(NUW, NSW))
423 return Val;
424
425 // While we can distribute over trunc, we cannot preserve nowrap flags
426 // in that case.
427 if (Val.TruncBits)
428 NUW = NSW = false;
429
430 LinearExpression E(Val);
431 switch (BOp->getOpcode()) {
432 default:
433 // We don't understand this instruction, so we can't decompose it any
434 // further.
435 return Val;
436 case Instruction::Or:
437 // X|C == X+C if it is disjoint. Otherwise we can't analyze it.
438 if (!cast<PossiblyDisjointInst>(BOp)->isDisjoint())
439 return Val;
440
441 [[fallthrough]];
442 case Instruction::Add: {
443 E = GetLinearExpression(Val.withValue(BOp->getOperand(0), false), DL,
444 Depth + 1, AC, DT);
445 E.Offset += RHS;
446 E.IsNUW &= NUW;
447 E.IsNSW &= NSW;
448 break;
449 }
450 case Instruction::Sub: {
451 E = GetLinearExpression(Val.withValue(BOp->getOperand(0), false), DL,
452 Depth + 1, AC, DT);
453 E.Offset -= RHS;
454 E.IsNUW = false; // sub nuw x, y is not add nuw x, -y.
455 E.IsNSW &= NSW;
456 break;
457 }
458 case Instruction::Mul:
459 E = GetLinearExpression(Val.withValue(BOp->getOperand(0), false), DL,
460 Depth + 1, AC, DT)
461 .mul(RHS, NUW, NSW);
462 break;
463 case Instruction::Shl:
464 // We're trying to linearize an expression of the kind:
465 // shl i8 -128, 36
466 // where the shift count exceeds the bitwidth of the type.
467 // We can't decompose this further (the expression would return
468 // a poison value).
469 if (RHS.getLimitedValue() > Val.getBitWidth())
470 return Val;
471
472 E = GetLinearExpression(Val.withValue(BOp->getOperand(0), NSW), DL,
473 Depth + 1, AC, DT);
474 E.Offset <<= RHS.getLimitedValue();
475 E.Scale <<= RHS.getLimitedValue();
476 E.IsNUW &= NUW;
477 E.IsNSW &= NSW;
478 break;
479 }
480 return E;
481 }
482 }
483
484 if (const auto *ZExt = dyn_cast<ZExtInst>(Val.V))
485 return GetLinearExpression(
486 Val.withZExtOfValue(ZExt->getOperand(0), ZExt->hasNonNeg()), DL,
487 Depth + 1, AC, DT);
488
489 if (isa<SExtInst>(Val.V))
490 return GetLinearExpression(
491 Val.withSExtOfValue(cast<CastInst>(Val.V)->getOperand(0)),
492 DL, Depth + 1, AC, DT);
493
494 return Val;
495}
496
497namespace {
498// A linear transformation of a Value; this class represents
499// ZExt(SExt(Trunc(V, TruncBits), SExtBits), ZExtBits) * Scale.
500struct VariableGEPIndex {
501 CastedValue Val;
502 APInt Scale;
503
504 // Context instruction to use when querying information about this index.
505 const Instruction *CxtI;
506
507 /// True if all operations in this expression are NSW.
508 bool IsNSW;
509
510 /// True if the index should be subtracted rather than added. We don't simply
511 /// negate the Scale, to avoid losing the NSW flag: X - INT_MIN*1 may be
512 /// non-wrapping, while X + INT_MIN*(-1) wraps.
513 bool IsNegated;
514
515 bool hasNegatedScaleOf(const VariableGEPIndex &Other) const {
516 if (IsNegated == Other.IsNegated)
517 return Scale == -Other.Scale;
518 return Scale == Other.Scale;
519 }
520
521 void dump() const {
522 print(dbgs());
523 dbgs() << "\n";
524 }
525 void print(raw_ostream &OS) const {
526 OS << "(V=" << Val.V->getName()
527 << ", zextbits=" << Val.ZExtBits
528 << ", sextbits=" << Val.SExtBits
529 << ", truncbits=" << Val.TruncBits
530 << ", scale=" << Scale
531 << ", nsw=" << IsNSW
532 << ", negated=" << IsNegated << ")";
533 }
534};
535}
536
537// Represents the internal structure of a GEP, decomposed into a base pointer,
538// constant offsets, and variable scaled indices.
540 // Base pointer of the GEP
541 const Value *Base;
542 // Total constant offset from base.
544 // Scaled variable (non-constant) indices.
546 // Nowrap flags common to all GEP operations involved in expression.
548
549 void dump() const {
550 print(dbgs());
551 dbgs() << "\n";
552 }
553 void print(raw_ostream &OS) const {
554 OS << ", inbounds=" << (NWFlags.isInBounds() ? "1" : "0")
555 << ", nuw=" << (NWFlags.hasNoUnsignedWrap() ? "1" : "0")
556 << "(DecomposedGEP Base=" << Base->getName() << ", Offset=" << Offset
557 << ", VarIndices=[";
558 for (size_t i = 0; i < VarIndices.size(); i++) {
559 if (i != 0)
560 OS << ", ";
561 VarIndices[i].print(OS);
562 }
563 OS << "])";
564 }
565};
566
567
568/// If V is a symbolic pointer expression, decompose it into a base pointer
569/// with a constant offset and a number of scaled symbolic offsets.
570///
571/// The scaled symbolic offsets (represented by pairs of a Value* and a scale
572/// in the VarIndices vector) are Value*'s that are known to be scaled by the
573/// specified amount, but which may have other unrepresented high bits. As
574/// such, the gep cannot necessarily be reconstructed from its decomposed form.
576BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
578 // Limit recursion depth to limit compile time in crazy cases.
579 unsigned MaxLookup = MaxLookupSearchDepth;
580 SearchTimes++;
581 const Instruction *CxtI = dyn_cast<Instruction>(V);
582
583 unsigned IndexSize = DL.getIndexTypeSizeInBits(V->getType());
584 DecomposedGEP Decomposed;
585 Decomposed.Offset = APInt(IndexSize, 0);
586 do {
587 // See if this is a bitcast or GEP.
588 const Operator *Op = dyn_cast<Operator>(V);
589 if (!Op) {
590 // The only non-operator case we can handle are GlobalAliases.
591 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
592 if (!GA->isInterposable()) {
593 V = GA->getAliasee();
594 continue;
595 }
596 }
597 Decomposed.Base = V;
598 return Decomposed;
599 }
600
601 if (Op->getOpcode() == Instruction::BitCast ||
602 Op->getOpcode() == Instruction::AddrSpaceCast) {
603 Value *NewV = Op->getOperand(0);
604 // Don't look through casts between address spaces with differing index
605 // widths.
606 if (DL.getIndexTypeSizeInBits(NewV->getType()) != IndexSize) {
607 Decomposed.Base = V;
608 return Decomposed;
609 }
610 V = NewV;
611 continue;
612 }
613
614 const GEPOperator *GEPOp = dyn_cast<GEPOperator>(Op);
615 if (!GEPOp) {
616 if (const auto *PHI = dyn_cast<PHINode>(V)) {
617 // Look through single-arg phi nodes created by LCSSA.
618 if (PHI->getNumIncomingValues() == 1) {
619 V = PHI->getIncomingValue(0);
620 continue;
621 }
622 } else if (const auto *Call = dyn_cast<CallBase>(V)) {
623 // CaptureTracking can know about special capturing properties of some
624 // intrinsics like launder.invariant.group, that can't be expressed with
625 // the attributes, but have properties like returning aliasing pointer.
626 // Because some analysis may assume that nocaptured pointer is not
627 // returned from some special intrinsic (because function would have to
628 // be marked with returns attribute), it is crucial to use this function
629 // because it should be in sync with CaptureTracking. Not using it may
630 // cause weird miscompilations where 2 aliasing pointers are assumed to
631 // noalias.
632 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
633 V = RP;
634 continue;
635 }
636 }
637
638 Decomposed.Base = V;
639 return Decomposed;
640 }
641
642 // Track the common nowrap flags for all GEPs we see.
643 Decomposed.NWFlags &= GEPOp->getNoWrapFlags();
644
645 assert(GEPOp->getSourceElementType()->isSized() && "GEP must be sized");
646
647 // Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
649 for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
650 I != E; ++I, ++GTI) {
651 const Value *Index = *I;
652 // Compute the (potentially symbolic) offset in bytes for this index.
653 if (StructType *STy = GTI.getStructTypeOrNull()) {
654 // For a struct, add the member offset.
655 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
656 if (FieldNo == 0)
657 continue;
658
659 Decomposed.Offset += DL.getStructLayout(STy)->getElementOffset(FieldNo);
660 continue;
661 }
662
663 // For an array/pointer, add the element offset, explicitly scaled.
664 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Index)) {
665 if (CIdx->isZero())
666 continue;
667
668 // Don't attempt to analyze GEPs if the scalable index is not zero.
669 TypeSize AllocTypeSize = GTI.getSequentialElementStride(DL);
670 if (AllocTypeSize.isScalable()) {
671 Decomposed.Base = V;
672 return Decomposed;
673 }
674
675 Decomposed.Offset += AllocTypeSize.getFixedValue() *
676 CIdx->getValue().sextOrTrunc(IndexSize);
677 continue;
678 }
679
680 TypeSize AllocTypeSize = GTI.getSequentialElementStride(DL);
681 if (AllocTypeSize.isScalable()) {
682 Decomposed.Base = V;
683 return Decomposed;
684 }
685
686 // If the integer type is smaller than the index size, it is implicitly
687 // sign extended or truncated to index size.
688 bool NUSW = GEPOp->hasNoUnsignedSignedWrap();
689 bool NUW = GEPOp->hasNoUnsignedWrap();
690 bool NonNeg = NUSW && NUW;
691 unsigned Width = Index->getType()->getIntegerBitWidth();
692 unsigned SExtBits = IndexSize > Width ? IndexSize - Width : 0;
693 unsigned TruncBits = IndexSize < Width ? Width - IndexSize : 0;
694 LinearExpression LE = GetLinearExpression(
695 CastedValue(Index, 0, SExtBits, TruncBits, NonNeg), DL, 0, AC, DT);
696
697 // Scale by the type size.
698 unsigned TypeSize = AllocTypeSize.getFixedValue();
699 LE = LE.mul(APInt(IndexSize, TypeSize), NUW, NUSW);
700 Decomposed.Offset += LE.Offset;
701 APInt Scale = LE.Scale;
702 if (!LE.IsNUW)
703 Decomposed.NWFlags = Decomposed.NWFlags.withoutNoUnsignedWrap();
704
705 // If we already had an occurrence of this index variable, merge this
706 // scale into it. For example, we want to handle:
707 // A[x][x] -> x*16 + x*4 -> x*20
708 // This also ensures that 'x' only appears in the index list once.
709 for (unsigned i = 0, e = Decomposed.VarIndices.size(); i != e; ++i) {
710 if ((Decomposed.VarIndices[i].Val.V == LE.Val.V ||
711 areBothVScale(Decomposed.VarIndices[i].Val.V, LE.Val.V)) &&
712 Decomposed.VarIndices[i].Val.hasSameCastsAs(LE.Val)) {
713 Scale += Decomposed.VarIndices[i].Scale;
714 // We cannot guarantee no-wrap for the merge.
715 LE.IsNSW = LE.IsNUW = false;
716 Decomposed.VarIndices.erase(Decomposed.VarIndices.begin() + i);
717 break;
718 }
719 }
720
721 if (!!Scale) {
722 VariableGEPIndex Entry = {LE.Val, Scale, CxtI, LE.IsNSW,
723 /* IsNegated */ false};
724 Decomposed.VarIndices.push_back(Entry);
725 }
726 }
727
728 // Analyze the base pointer next.
729 V = GEPOp->getOperand(0);
730 } while (--MaxLookup);
731
732 // If the chain of expressions is too deep, just return early.
733 Decomposed.Base = V;
734 SearchLimitReached++;
735 return Decomposed;
736}
737
739 AAQueryInfo &AAQI,
740 bool IgnoreLocals) {
741 assert(Visited.empty() && "Visited must be cleared after use!");
742 auto _ = make_scope_exit([&] { Visited.clear(); });
743
744 unsigned MaxLookup = 8;
746 Worklist.push_back(Loc.Ptr);
748
749 do {
750 const Value *V = getUnderlyingObject(Worklist.pop_back_val());
751 if (!Visited.insert(V).second)
752 continue;
753
754 // Ignore allocas if we were instructed to do so.
755 if (IgnoreLocals && isa<AllocaInst>(V))
756 continue;
757
758 // If the location points to memory that is known to be invariant for
759 // the life of the underlying SSA value, then we can exclude Mod from
760 // the set of valid memory effects.
761 //
762 // An argument that is marked readonly and noalias is known to be
763 // invariant while that function is executing.
764 if (const Argument *Arg = dyn_cast<Argument>(V)) {
765 if (Arg->hasNoAliasAttr() && Arg->onlyReadsMemory()) {
766 Result |= ModRefInfo::Ref;
767 continue;
768 }
769 }
770
771 // A global constant can't be mutated.
772 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
773 // Note: this doesn't require GV to be "ODR" because it isn't legal for a
774 // global to be marked constant in some modules and non-constant in
775 // others. GV may even be a declaration, not a definition.
776 if (!GV->isConstant())
777 return ModRefInfo::ModRef;
778 continue;
779 }
780
781 // If both select values point to local memory, then so does the select.
782 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
783 Worklist.push_back(SI->getTrueValue());
784 Worklist.push_back(SI->getFalseValue());
785 continue;
786 }
787
788 // If all values incoming to a phi node point to local memory, then so does
789 // the phi.
790 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
791 // Don't bother inspecting phi nodes with many operands.
792 if (PN->getNumIncomingValues() > MaxLookup)
793 return ModRefInfo::ModRef;
794 append_range(Worklist, PN->incoming_values());
795 continue;
796 }
797
798 // Otherwise be conservative.
799 return ModRefInfo::ModRef;
800 } while (!Worklist.empty() && --MaxLookup);
801
802 // If we hit the maximum number of instructions to examine, be conservative.
803 if (!Worklist.empty())
804 return ModRefInfo::ModRef;
805
806 return Result;
807}
808
809static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID) {
810 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Call);
811 return II && II->getIntrinsicID() == IID;
812}
813
814/// Returns the behavior when calling the given call site.
816 AAQueryInfo &AAQI) {
817 MemoryEffects Min = Call->getAttributes().getMemoryEffects();
818
819 if (const Function *F = dyn_cast<Function>(Call->getCalledOperand())) {
820 MemoryEffects FuncME = AAQI.AAR.getMemoryEffects(F);
821 // Operand bundles on the call may also read or write memory, in addition
822 // to the behavior of the called function.
823 if (Call->hasReadingOperandBundles())
824 FuncME |= MemoryEffects::readOnly();
825 if (Call->hasClobberingOperandBundles())
826 FuncME |= MemoryEffects::writeOnly();
827 Min &= FuncME;
828 }
829
830 return Min;
831}
832
833/// Returns the behavior when calling the given function. For use when the call
834/// site is not known.
836 switch (F->getIntrinsicID()) {
837 case Intrinsic::experimental_guard:
838 case Intrinsic::experimental_deoptimize:
839 // These intrinsics can read arbitrary memory, and additionally modref
840 // inaccessible memory to model control dependence.
841 return MemoryEffects::readOnly() |
843 }
844
845 return F->getMemoryEffects();
846}
847
849 unsigned ArgIdx) {
850 if (Call->paramHasAttr(ArgIdx, Attribute::WriteOnly))
851 return ModRefInfo::Mod;
852
853 if (Call->paramHasAttr(ArgIdx, Attribute::ReadOnly))
854 return ModRefInfo::Ref;
855
856 if (Call->paramHasAttr(ArgIdx, Attribute::ReadNone))
858
859 return ModRefInfo::ModRef;
860}
861
862#ifndef NDEBUG
863static const Function *getParent(const Value *V) {
864 if (const Instruction *inst = dyn_cast<Instruction>(V)) {
865 if (!inst->getParent())
866 return nullptr;
867 return inst->getParent()->getParent();
868 }
869
870 if (const Argument *arg = dyn_cast<Argument>(V))
871 return arg->getParent();
872
873 return nullptr;
874}
875
876static bool notDifferentParent(const Value *O1, const Value *O2) {
877
878 const Function *F1 = getParent(O1);
879 const Function *F2 = getParent(O2);
880
881 return !F1 || !F2 || F1 == F2;
882}
883#endif
884
886 const MemoryLocation &LocB, AAQueryInfo &AAQI,
887 const Instruction *CtxI) {
888 assert(notDifferentParent(LocA.Ptr, LocB.Ptr) &&
889 "BasicAliasAnalysis doesn't support interprocedural queries.");
890 return aliasCheck(LocA.Ptr, LocA.Size, LocB.Ptr, LocB.Size, AAQI, CtxI);
891}
892
893/// Checks to see if the specified callsite can clobber the specified memory
894/// object.
895///
896/// Since we only look at local properties of this function, we really can't
897/// say much about this query. We do, however, use simple "address taken"
898/// analysis on local objects.
900 const MemoryLocation &Loc,
901 AAQueryInfo &AAQI) {
902 assert(notDifferentParent(Call, Loc.Ptr) &&
903 "AliasAnalysis query involving multiple functions!");
904
905 const Value *Object = getUnderlyingObject(Loc.Ptr);
906
907 // Calls marked 'tail' cannot read or write allocas from the current frame
908 // because the current frame might be destroyed by the time they run. However,
909 // a tail call may use an alloca with byval. Calling with byval copies the
910 // contents of the alloca into argument registers or stack slots, so there is
911 // no lifetime issue.
912 if (isa<AllocaInst>(Object))
913 if (const CallInst *CI = dyn_cast<CallInst>(Call))
914 if (CI->isTailCall() &&
915 !CI->getAttributes().hasAttrSomewhere(Attribute::ByVal))
917
918 // Stack restore is able to modify unescaped dynamic allocas. Assume it may
919 // modify them even though the alloca is not escaped.
920 if (auto *AI = dyn_cast<AllocaInst>(Object))
921 if (!AI->isStaticAlloca() && isIntrinsicCall(Call, Intrinsic::stackrestore))
922 return ModRefInfo::Mod;
923
924 // A call can access a locally allocated object either because it is passed as
925 // an argument to the call, or because it has escaped prior to the call.
926 //
927 // Make sure the object has not escaped here, and then check that none of the
928 // call arguments alias the object below.
929 //
930 // We model calls that can return twice (setjmp) as clobbering non-escaping
931 // objects, to model any accesses that may occur prior to the second return.
932 // As an exception, ignore allocas, as setjmp is not required to preserve
933 // non-volatile stores for them.
934 if (!isa<Constant>(Object) && Call != Object &&
935 AAQI.CA->isNotCapturedBefore(Object, Call, /*OrAt*/ false) &&
936 (isa<AllocaInst>(Object) || !Call->hasFnAttr(Attribute::ReturnsTwice))) {
937
938 // Optimistically assume that call doesn't touch Object and check this
939 // assumption in the following loop.
941
942 unsigned OperandNo = 0;
943 for (auto CI = Call->data_operands_begin(), CE = Call->data_operands_end();
944 CI != CE; ++CI, ++OperandNo) {
945 if (!(*CI)->getType()->isPointerTy())
946 continue;
947
948 // Call doesn't access memory through this operand, so we don't care
949 // if it aliases with Object.
950 if (Call->doesNotAccessMemory(OperandNo))
951 continue;
952
953 // If this is a no-capture pointer argument, see if we can tell that it
954 // is impossible to alias the pointer we're checking.
955 AliasResult AR =
958 // Operand doesn't alias 'Object', continue looking for other aliases
959 if (AR == AliasResult::NoAlias)
960 continue;
961 // Operand aliases 'Object', but call doesn't modify it. Strengthen
962 // initial assumption and keep looking in case if there are more aliases.
963 if (Call->onlyReadsMemory(OperandNo)) {
964 Result |= ModRefInfo::Ref;
965 continue;
966 }
967 // Operand aliases 'Object' but call only writes into it.
968 if (Call->onlyWritesMemory(OperandNo)) {
969 Result |= ModRefInfo::Mod;
970 continue;
971 }
972 // This operand aliases 'Object' and call reads and writes into it.
973 // Setting ModRef will not yield an early return below, MustAlias is not
974 // used further.
975 Result = ModRefInfo::ModRef;
976 break;
977 }
978
979 // Early return if we improved mod ref information
980 if (!isModAndRefSet(Result))
981 return Result;
982 }
983
984 // If the call is malloc/calloc like, we can assume that it doesn't
985 // modify any IR visible value. This is only valid because we assume these
986 // routines do not read values visible in the IR. TODO: Consider special
987 // casing realloc and strdup routines which access only their arguments as
988 // well. Or alternatively, replace all of this with inaccessiblememonly once
989 // that's implemented fully.
990 if (isMallocOrCallocLikeFn(Call, &TLI)) {
991 // Be conservative if the accessed pointer may alias the allocation -
992 // fallback to the generic handling below.
993 if (AAQI.AAR.alias(MemoryLocation::getBeforeOrAfter(Call), Loc, AAQI) ==
996 }
997
998 // Like assumes, invariant.start intrinsics were also marked as arbitrarily
999 // writing so that proper control dependencies are maintained but they never
1000 // mod any particular memory location visible to the IR.
1001 // *Unlike* assumes (which are now modeled as NoModRef), invariant.start
1002 // intrinsic is now modeled as reading memory. This prevents hoisting the
1003 // invariant.start intrinsic over stores. Consider:
1004 // *ptr = 40;
1005 // *ptr = 50;
1006 // invariant_start(ptr)
1007 // int val = *ptr;
1008 // print(val);
1009 //
1010 // This cannot be transformed to:
1011 //
1012 // *ptr = 40;
1013 // invariant_start(ptr)
1014 // *ptr = 50;
1015 // int val = *ptr;
1016 // print(val);
1017 //
1018 // The transformation will cause the second store to be ignored (based on
1019 // rules of invariant.start) and print 40, while the first program always
1020 // prints 50.
1021 if (isIntrinsicCall(Call, Intrinsic::invariant_start))
1022 return ModRefInfo::Ref;
1023
1024 // Be conservative.
1025 return ModRefInfo::ModRef;
1026}
1027
1029 const CallBase *Call2,
1030 AAQueryInfo &AAQI) {
1031 // Guard intrinsics are marked as arbitrarily writing so that proper control
1032 // dependencies are maintained but they never mods any particular memory
1033 // location.
1034 //
1035 // *Unlike* assumes, guard intrinsics are modeled as reading memory since the
1036 // heap state at the point the guard is issued needs to be consistent in case
1037 // the guard invokes the "deopt" continuation.
1038
1039 // NB! This function is *not* commutative, so we special case two
1040 // possibilities for guard intrinsics.
1041
1042 if (isIntrinsicCall(Call1, Intrinsic::experimental_guard))
1043 return isModSet(getMemoryEffects(Call2, AAQI).getModRef())
1046
1047 if (isIntrinsicCall(Call2, Intrinsic::experimental_guard))
1048 return isModSet(getMemoryEffects(Call1, AAQI).getModRef())
1051
1052 // Be conservative.
1053 return ModRefInfo::ModRef;
1054}
1055
1056/// Provides a bunch of ad-hoc rules to disambiguate a GEP instruction against
1057/// another pointer.
1058///
1059/// We know that V1 is a GEP, but we don't know anything about V2.
1060/// UnderlyingV1 is getUnderlyingObject(GEP1), UnderlyingV2 is the same for
1061/// V2.
1062AliasResult BasicAAResult::aliasGEP(
1063 const GEPOperator *GEP1, LocationSize V1Size,
1064 const Value *V2, LocationSize V2Size,
1065 const Value *UnderlyingV1, const Value *UnderlyingV2, AAQueryInfo &AAQI) {
1066 auto BaseObjectsAlias = [&]() {
1067 AliasResult BaseAlias =
1068 AAQI.AAR.alias(MemoryLocation::getBeforeOrAfter(UnderlyingV1),
1069 MemoryLocation::getBeforeOrAfter(UnderlyingV2), AAQI);
1070 return BaseAlias == AliasResult::NoAlias ? AliasResult::NoAlias
1072 };
1073
1074 if (!V1Size.hasValue() && !V2Size.hasValue()) {
1075 // TODO: This limitation exists for compile-time reasons. Relax it if we
1076 // can avoid exponential pathological cases.
1077 if (!isa<GEPOperator>(V2))
1078 return AliasResult::MayAlias;
1079
1080 // If both accesses have unknown size, we can only check whether the base
1081 // objects don't alias.
1082 return BaseObjectsAlias();
1083 }
1084
1085 DominatorTree *DT = getDT(AAQI);
1086 DecomposedGEP DecompGEP1 = DecomposeGEPExpression(GEP1, DL, &AC, DT);
1087 DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT);
1088
1089 // Bail if we were not able to decompose anything.
1090 if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2)
1091 return AliasResult::MayAlias;
1092
1093 // Fall back to base objects if pointers have different index widths.
1094 if (DecompGEP1.Offset.getBitWidth() != DecompGEP2.Offset.getBitWidth())
1095 return BaseObjectsAlias();
1096
1097 // Swap GEP1 and GEP2 if GEP2 has more variable indices.
1098 if (DecompGEP1.VarIndices.size() < DecompGEP2.VarIndices.size()) {
1099 std::swap(DecompGEP1, DecompGEP2);
1100 std::swap(V1Size, V2Size);
1101 std::swap(UnderlyingV1, UnderlyingV2);
1102 }
1103
1104 // Subtract the GEP2 pointer from the GEP1 pointer to find out their
1105 // symbolic difference.
1106 subtractDecomposedGEPs(DecompGEP1, DecompGEP2, AAQI);
1107
1108 // If an inbounds GEP would have to start from an out of bounds address
1109 // for the two to alias, then we can assume noalias.
1110 // TODO: Remove !isScalable() once BasicAA fully support scalable location
1111 // size
1112
1113 if (DecompGEP1.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1114 V2Size.hasValue() && !V2Size.isScalable() &&
1115 DecompGEP1.Offset.sge(V2Size.getValue()) &&
1116 isBaseOfObject(DecompGEP2.Base))
1117 return AliasResult::NoAlias;
1118
1119 // Symmetric case to above.
1120 if (DecompGEP2.NWFlags.isInBounds() && DecompGEP1.VarIndices.empty() &&
1121 V1Size.hasValue() && !V1Size.isScalable() &&
1122 DecompGEP1.Offset.sle(-V1Size.getValue()) &&
1123 isBaseOfObject(DecompGEP1.Base))
1124 return AliasResult::NoAlias;
1125
1126 // For GEPs with identical offsets, we can preserve the size and AAInfo
1127 // when performing the alias check on the underlying objects.
1128 if (DecompGEP1.Offset == 0 && DecompGEP1.VarIndices.empty())
1129 return AAQI.AAR.alias(MemoryLocation(DecompGEP1.Base, V1Size),
1130 MemoryLocation(DecompGEP2.Base, V2Size), AAQI);
1131
1132 // Do the base pointers alias?
1133 AliasResult BaseAlias =
1134 AAQI.AAR.alias(MemoryLocation::getBeforeOrAfter(DecompGEP1.Base),
1135 MemoryLocation::getBeforeOrAfter(DecompGEP2.Base), AAQI);
1136
1137 // If we get a No or May, then return it immediately, no amount of analysis
1138 // will improve this situation.
1139 if (BaseAlias != AliasResult::MustAlias) {
1140 assert(BaseAlias == AliasResult::NoAlias ||
1141 BaseAlias == AliasResult::MayAlias);
1142 return BaseAlias;
1143 }
1144
1145 // If there is a constant difference between the pointers, but the difference
1146 // is less than the size of the associated memory object, then we know
1147 // that the objects are partially overlapping. If the difference is
1148 // greater, we know they do not overlap.
1149 if (DecompGEP1.VarIndices.empty()) {
1150 APInt &Off = DecompGEP1.Offset;
1151
1152 // Initialize for Off >= 0 (V2 <= GEP1) case.
1153 LocationSize VLeftSize = V2Size;
1154 LocationSize VRightSize = V1Size;
1155 const bool Swapped = Off.isNegative();
1156
1157 if (Swapped) {
1158 // Swap if we have the situation where:
1159 // + +
1160 // | BaseOffset |
1161 // ---------------->|
1162 // |-->V1Size |-------> V2Size
1163 // GEP1 V2
1164 std::swap(VLeftSize, VRightSize);
1165 Off = -Off;
1166 }
1167
1168 if (!VLeftSize.hasValue())
1169 return AliasResult::MayAlias;
1170
1171 const TypeSize LSize = VLeftSize.getValue();
1172 if (!LSize.isScalable()) {
1173 if (Off.ult(LSize)) {
1174 // Conservatively drop processing if a phi was visited and/or offset is
1175 // too big.
1177 if (VRightSize.hasValue() && !VRightSize.isScalable() &&
1178 Off.ule(INT32_MAX) && (Off + VRightSize.getValue()).ule(LSize)) {
1179 // Memory referenced by right pointer is nested. Save the offset in
1180 // cache. Note that originally offset estimated as GEP1-V2, but
1181 // AliasResult contains the shift that represents GEP1+Offset=V2.
1182 AR.setOffset(-Off.getSExtValue());
1183 AR.swap(Swapped);
1184 }
1185 return AR;
1186 }
1187 return AliasResult::NoAlias;
1188 } else {
1189 // We can use the getVScaleRange to prove that Off >= (CR.upper * LSize).
1190 ConstantRange CR = getVScaleRange(&F, Off.getBitWidth());
1191 bool Overflow;
1192 APInt UpperRange = CR.getUnsignedMax().umul_ov(
1193 APInt(Off.getBitWidth(), LSize.getKnownMinValue()), Overflow);
1194 if (!Overflow && Off.uge(UpperRange))
1195 return AliasResult::NoAlias;
1196 }
1197 }
1198
1199 // VScale Alias Analysis - Given one scalable offset between accesses and a
1200 // scalable typesize, we can divide each side by vscale, treating both values
1201 // as a constant. We prove that Offset/vscale >= TypeSize/vscale.
1202 if (DecompGEP1.VarIndices.size() == 1 &&
1203 DecompGEP1.VarIndices[0].Val.TruncBits == 0 &&
1204 DecompGEP1.Offset.isZero() &&
1205 PatternMatch::match(DecompGEP1.VarIndices[0].Val.V,
1207 const VariableGEPIndex &ScalableVar = DecompGEP1.VarIndices[0];
1208 APInt Scale =
1209 ScalableVar.IsNegated ? -ScalableVar.Scale : ScalableVar.Scale;
1210 LocationSize VLeftSize = Scale.isNegative() ? V1Size : V2Size;
1211
1212 // Check if the offset is known to not overflow, if it does then attempt to
1213 // prove it with the known values of vscale_range.
1214 bool Overflows = !DecompGEP1.VarIndices[0].IsNSW;
1215 if (Overflows) {
1216 ConstantRange CR = getVScaleRange(&F, Scale.getBitWidth());
1217 (void)CR.getSignedMax().smul_ov(Scale, Overflows);
1218 }
1219
1220 if (!Overflows) {
1221 // Note that we do not check that the typesize is scalable, as vscale >= 1
1222 // so noalias still holds so long as the dependency distance is at least
1223 // as big as the typesize.
1224 if (VLeftSize.hasValue() &&
1225 Scale.abs().uge(VLeftSize.getValue().getKnownMinValue()))
1226 return AliasResult::NoAlias;
1227 }
1228 }
1229
1230 // If the difference between pointers is Offset +<nuw> Indices then we know
1231 // that the addition does not wrap the pointer index type (add nuw) and the
1232 // constant Offset is a lower bound on the distance between the pointers. We
1233 // can then prove NoAlias via Offset u>= VLeftSize.
1234 // + + +
1235 // | BaseOffset | +<nuw> Indices |
1236 // ---------------->|-------------------->|
1237 // |-->V2Size | |-------> V1Size
1238 // LHS RHS
1239 if (!DecompGEP1.VarIndices.empty() &&
1240 DecompGEP1.NWFlags.hasNoUnsignedWrap() && V2Size.hasValue() &&
1241 !V2Size.isScalable() && DecompGEP1.Offset.uge(V2Size.getValue()))
1242 return AliasResult::NoAlias;
1243
1244 // Bail on analysing scalable LocationSize
1245 if (V1Size.isScalable() || V2Size.isScalable())
1246 return AliasResult::MayAlias;
1247
1248 // We need to know both acess sizes for all the following heuristics.
1249 if (!V1Size.hasValue() || !V2Size.hasValue())
1250 return AliasResult::MayAlias;
1251
1252 APInt GCD;
1253 ConstantRange OffsetRange = ConstantRange(DecompGEP1.Offset);
1254 for (unsigned i = 0, e = DecompGEP1.VarIndices.size(); i != e; ++i) {
1255 const VariableGEPIndex &Index = DecompGEP1.VarIndices[i];
1256 const APInt &Scale = Index.Scale;
1257 APInt ScaleForGCD = Scale;
1258 if (!Index.IsNSW)
1259 ScaleForGCD =
1261
1262 if (i == 0)
1263 GCD = ScaleForGCD.abs();
1264 else
1265 GCD = APIntOps::GreatestCommonDivisor(GCD, ScaleForGCD.abs());
1266
1267 ConstantRange CR = computeConstantRange(Index.Val.V, /* ForSigned */ false,
1268 true, &AC, Index.CxtI);
1269 KnownBits Known =
1270 computeKnownBits(Index.Val.V, DL, 0, &AC, Index.CxtI, DT);
1271 CR = CR.intersectWith(
1272 ConstantRange::fromKnownBits(Known, /* Signed */ true),
1274 CR = Index.Val.evaluateWith(CR).sextOrTrunc(OffsetRange.getBitWidth());
1275
1276 assert(OffsetRange.getBitWidth() == Scale.getBitWidth() &&
1277 "Bit widths are normalized to MaxIndexSize");
1278 if (Index.IsNSW)
1279 CR = CR.smul_sat(ConstantRange(Scale));
1280 else
1281 CR = CR.smul_fast(ConstantRange(Scale));
1282
1283 if (Index.IsNegated)
1284 OffsetRange = OffsetRange.sub(CR);
1285 else
1286 OffsetRange = OffsetRange.add(CR);
1287 }
1288
1289 // We now have accesses at two offsets from the same base:
1290 // 1. (...)*GCD + DecompGEP1.Offset with size V1Size
1291 // 2. 0 with size V2Size
1292 // Using arithmetic modulo GCD, the accesses are at
1293 // [ModOffset..ModOffset+V1Size) and [0..V2Size). If the first access fits
1294 // into the range [V2Size..GCD), then we know they cannot overlap.
1295 APInt ModOffset = DecompGEP1.Offset.srem(GCD);
1296 if (ModOffset.isNegative())
1297 ModOffset += GCD; // We want mod, not rem.
1298 if (ModOffset.uge(V2Size.getValue()) &&
1299 (GCD - ModOffset).uge(V1Size.getValue()))
1300 return AliasResult::NoAlias;
1301
1302 // Compute ranges of potentially accessed bytes for both accesses. If the
1303 // interseciton is empty, there can be no overlap.
1304 unsigned BW = OffsetRange.getBitWidth();
1305 ConstantRange Range1 = OffsetRange.add(
1306 ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
1307 ConstantRange Range2 =
1308 ConstantRange(APInt(BW, 0), APInt(BW, V2Size.getValue()));
1309 if (Range1.intersectWith(Range2).isEmptySet())
1310 return AliasResult::NoAlias;
1311
1312 // Try to determine the range of values for VarIndex such that
1313 // VarIndex <= -MinAbsVarIndex || MinAbsVarIndex <= VarIndex.
1314 std::optional<APInt> MinAbsVarIndex;
1315 if (DecompGEP1.VarIndices.size() == 1) {
1316 // VarIndex = Scale*V.
1317 const VariableGEPIndex &Var = DecompGEP1.VarIndices[0];
1318 if (Var.Val.TruncBits == 0 &&
1319 isKnownNonZero(Var.Val.V, SimplifyQuery(DL, DT, &AC, Var.CxtI))) {
1320 // Check if abs(V*Scale) >= abs(Scale) holds in the presence of
1321 // potentially wrapping math.
1322 auto MultiplyByScaleNoWrap = [](const VariableGEPIndex &Var) {
1323 if (Var.IsNSW)
1324 return true;
1325
1326 int ValOrigBW = Var.Val.V->getType()->getPrimitiveSizeInBits();
1327 // If Scale is small enough so that abs(V*Scale) >= abs(Scale) holds.
1328 // The max value of abs(V) is 2^ValOrigBW - 1. Multiplying with a
1329 // constant smaller than 2^(bitwidth(Val) - ValOrigBW) won't wrap.
1330 int MaxScaleValueBW = Var.Val.getBitWidth() - ValOrigBW;
1331 if (MaxScaleValueBW <= 0)
1332 return false;
1333 return Var.Scale.ule(
1334 APInt::getMaxValue(MaxScaleValueBW).zext(Var.Scale.getBitWidth()));
1335 };
1336 // Refine MinAbsVarIndex, if abs(Scale*V) >= abs(Scale) holds in the
1337 // presence of potentially wrapping math.
1338 if (MultiplyByScaleNoWrap(Var)) {
1339 // If V != 0 then abs(VarIndex) >= abs(Scale).
1340 MinAbsVarIndex = Var.Scale.abs();
1341 }
1342 }
1343 } else if (DecompGEP1.VarIndices.size() == 2) {
1344 // VarIndex = Scale*V0 + (-Scale)*V1.
1345 // If V0 != V1 then abs(VarIndex) >= abs(Scale).
1346 // Check that MayBeCrossIteration is false, to avoid reasoning about
1347 // inequality of values across loop iterations.
1348 const VariableGEPIndex &Var0 = DecompGEP1.VarIndices[0];
1349 const VariableGEPIndex &Var1 = DecompGEP1.VarIndices[1];
1350 if (Var0.hasNegatedScaleOf(Var1) && Var0.Val.TruncBits == 0 &&
1351 Var0.Val.hasSameCastsAs(Var1.Val) && !AAQI.MayBeCrossIteration &&
1352 isKnownNonEqual(Var0.Val.V, Var1.Val.V,
1353 SimplifyQuery(DL, DT, &AC, /*CxtI=*/Var0.CxtI
1354 ? Var0.CxtI
1355 : Var1.CxtI)))
1356 MinAbsVarIndex = Var0.Scale.abs();
1357 }
1358
1359 if (MinAbsVarIndex) {
1360 // The constant offset will have added at least +/-MinAbsVarIndex to it.
1361 APInt OffsetLo = DecompGEP1.Offset - *MinAbsVarIndex;
1362 APInt OffsetHi = DecompGEP1.Offset + *MinAbsVarIndex;
1363 // We know that Offset <= OffsetLo || Offset >= OffsetHi
1364 if (OffsetLo.isNegative() && (-OffsetLo).uge(V1Size.getValue()) &&
1365 OffsetHi.isNonNegative() && OffsetHi.uge(V2Size.getValue()))
1366 return AliasResult::NoAlias;
1367 }
1368
1369 if (constantOffsetHeuristic(DecompGEP1, V1Size, V2Size, &AC, DT, AAQI))
1370 return AliasResult::NoAlias;
1371
1372 // Statically, we can see that the base objects are the same, but the
1373 // pointers have dynamic offsets which we can't resolve. And none of our
1374 // little tricks above worked.
1375 return AliasResult::MayAlias;
1376}
1377
1379 // If the results agree, take it.
1380 if (A == B)
1381 return A;
1382 // A mix of PartialAlias and MustAlias is PartialAlias.
1386 // Otherwise, we don't know anything.
1387 return AliasResult::MayAlias;
1388}
1389
1390/// Provides a bunch of ad-hoc rules to disambiguate a Select instruction
1391/// against another.
1393BasicAAResult::aliasSelect(const SelectInst *SI, LocationSize SISize,
1394 const Value *V2, LocationSize V2Size,
1395 AAQueryInfo &AAQI) {
1396 // If the values are Selects with the same condition, we can do a more precise
1397 // check: just check for aliases between the values on corresponding arms.
1398 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2))
1399 if (isValueEqualInPotentialCycles(SI->getCondition(), SI2->getCondition(),
1400 AAQI)) {
1401 AliasResult Alias =
1402 AAQI.AAR.alias(MemoryLocation(SI->getTrueValue(), SISize),
1403 MemoryLocation(SI2->getTrueValue(), V2Size), AAQI);
1404 if (Alias == AliasResult::MayAlias)
1405 return AliasResult::MayAlias;
1406 AliasResult ThisAlias =
1407 AAQI.AAR.alias(MemoryLocation(SI->getFalseValue(), SISize),
1408 MemoryLocation(SI2->getFalseValue(), V2Size), AAQI);
1409 return MergeAliasResults(ThisAlias, Alias);
1410 }
1411
1412 // If both arms of the Select node NoAlias or MustAlias V2, then returns
1413 // NoAlias / MustAlias. Otherwise, returns MayAlias.
1414 AliasResult Alias = AAQI.AAR.alias(MemoryLocation(SI->getTrueValue(), SISize),
1415 MemoryLocation(V2, V2Size), AAQI);
1416 if (Alias == AliasResult::MayAlias)
1417 return AliasResult::MayAlias;
1418
1419 AliasResult ThisAlias =
1420 AAQI.AAR.alias(MemoryLocation(SI->getFalseValue(), SISize),
1421 MemoryLocation(V2, V2Size), AAQI);
1422 return MergeAliasResults(ThisAlias, Alias);
1423}
1424
1425/// Provide a bunch of ad-hoc rules to disambiguate a PHI instruction against
1426/// another.
1427AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize,
1428 const Value *V2, LocationSize V2Size,
1429 AAQueryInfo &AAQI) {
1430 if (!PN->getNumIncomingValues())
1431 return AliasResult::NoAlias;
1432 // If the values are PHIs in the same block, we can do a more precise
1433 // as well as efficient check: just check for aliases between the values
1434 // on corresponding edges. Don't do this if we are analyzing across
1435 // iterations, as we may pick a different phi entry in different iterations.
1436 if (const PHINode *PN2 = dyn_cast<PHINode>(V2))
1437 if (PN2->getParent() == PN->getParent() && !AAQI.MayBeCrossIteration) {
1438 std::optional<AliasResult> Alias;
1439 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1440 AliasResult ThisAlias = AAQI.AAR.alias(
1441 MemoryLocation(PN->getIncomingValue(i), PNSize),
1443 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), V2Size),
1444 AAQI);
1445 if (Alias)
1446 *Alias = MergeAliasResults(*Alias, ThisAlias);
1447 else
1448 Alias = ThisAlias;
1449 if (*Alias == AliasResult::MayAlias)
1450 break;
1451 }
1452 return *Alias;
1453 }
1454
1456 // If a phi operand recurses back to the phi, we can still determine NoAlias
1457 // if we don't alias the underlying objects of the other phi operands, as we
1458 // know that the recursive phi needs to be based on them in some way.
1459 bool isRecursive = false;
1460 auto CheckForRecPhi = [&](Value *PV) {
1462 return false;
1463 if (getUnderlyingObject(PV) == PN) {
1464 isRecursive = true;
1465 return true;
1466 }
1467 return false;
1468 };
1469
1470 SmallPtrSet<Value *, 4> UniqueSrc;
1471 Value *OnePhi = nullptr;
1472 for (Value *PV1 : PN->incoming_values()) {
1473 // Skip the phi itself being the incoming value.
1474 if (PV1 == PN)
1475 continue;
1476
1477 if (isa<PHINode>(PV1)) {
1478 if (OnePhi && OnePhi != PV1) {
1479 // To control potential compile time explosion, we choose to be
1480 // conserviate when we have more than one Phi input. It is important
1481 // that we handle the single phi case as that lets us handle LCSSA
1482 // phi nodes and (combined with the recursive phi handling) simple
1483 // pointer induction variable patterns.
1484 return AliasResult::MayAlias;
1485 }
1486 OnePhi = PV1;
1487 }
1488
1489 if (CheckForRecPhi(PV1))
1490 continue;
1491
1492 if (UniqueSrc.insert(PV1).second)
1493 V1Srcs.push_back(PV1);
1494 }
1495
1496 if (OnePhi && UniqueSrc.size() > 1)
1497 // Out of an abundance of caution, allow only the trivial lcssa and
1498 // recursive phi cases.
1499 return AliasResult::MayAlias;
1500
1501 // If V1Srcs is empty then that means that the phi has no underlying non-phi
1502 // value. This should only be possible in blocks unreachable from the entry
1503 // block, but return MayAlias just in case.
1504 if (V1Srcs.empty())
1505 return AliasResult::MayAlias;
1506
1507 // If this PHI node is recursive, indicate that the pointer may be moved
1508 // across iterations. We can only prove NoAlias if different underlying
1509 // objects are involved.
1510 if (isRecursive)
1512
1513 // In the recursive alias queries below, we may compare values from two
1514 // different loop iterations.
1515 SaveAndRestore SavedMayBeCrossIteration(AAQI.MayBeCrossIteration, true);
1516
1517 AliasResult Alias = AAQI.AAR.alias(MemoryLocation(V1Srcs[0], PNSize),
1518 MemoryLocation(V2, V2Size), AAQI);
1519
1520 // Early exit if the check of the first PHI source against V2 is MayAlias.
1521 // Other results are not possible.
1522 if (Alias == AliasResult::MayAlias)
1523 return AliasResult::MayAlias;
1524 // With recursive phis we cannot guarantee that MustAlias/PartialAlias will
1525 // remain valid to all elements and needs to conservatively return MayAlias.
1526 if (isRecursive && Alias != AliasResult::NoAlias)
1527 return AliasResult::MayAlias;
1528
1529 // If all sources of the PHI node NoAlias or MustAlias V2, then returns
1530 // NoAlias / MustAlias. Otherwise, returns MayAlias.
1531 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) {
1532 Value *V = V1Srcs[i];
1533
1534 AliasResult ThisAlias = AAQI.AAR.alias(
1535 MemoryLocation(V, PNSize), MemoryLocation(V2, V2Size), AAQI);
1536 Alias = MergeAliasResults(ThisAlias, Alias);
1537 if (Alias == AliasResult::MayAlias)
1538 break;
1539 }
1540
1541 return Alias;
1542}
1543
1544/// Provides a bunch of ad-hoc rules to disambiguate in common cases, such as
1545/// array references.
1546AliasResult BasicAAResult::aliasCheck(const Value *V1, LocationSize V1Size,
1547 const Value *V2, LocationSize V2Size,
1548 AAQueryInfo &AAQI,
1549 const Instruction *CtxI) {
1550 // If either of the memory references is empty, it doesn't matter what the
1551 // pointer values are.
1552 if (V1Size.isZero() || V2Size.isZero())
1553 return AliasResult::NoAlias;
1554
1555 // Strip off any casts if they exist.
1557 V2 = V2->stripPointerCastsForAliasAnalysis();
1558
1559 // If V1 or V2 is undef, the result is NoAlias because we can always pick a
1560 // value for undef that aliases nothing in the program.
1561 if (isa<UndefValue>(V1) || isa<UndefValue>(V2))
1562 return AliasResult::NoAlias;
1563
1564 // Are we checking for alias of the same value?
1565 // Because we look 'through' phi nodes, we could look at "Value" pointers from
1566 // different iterations. We must therefore make sure that this is not the
1567 // case. The function isValueEqualInPotentialCycles ensures that this cannot
1568 // happen by looking at the visited phi nodes and making sure they cannot
1569 // reach the value.
1570 if (isValueEqualInPotentialCycles(V1, V2, AAQI))
1572
1573 if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy())
1574 return AliasResult::NoAlias; // Scalars cannot alias each other
1575
1576 // Figure out what objects these things are pointing to if we can.
1579
1580 // Null values in the default address space don't point to any object, so they
1581 // don't alias any other pointer.
1582 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1))
1583 if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1584 return AliasResult::NoAlias;
1585 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2))
1586 if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
1587 return AliasResult::NoAlias;
1588
1589 if (O1 != O2) {
1590 // If V1/V2 point to two different objects, we know that we have no alias.
1592 return AliasResult::NoAlias;
1593
1594 // Function arguments can't alias with things that are known to be
1595 // unambigously identified at the function level.
1596 if ((isa<Argument>(O1) && isIdentifiedFunctionLocal(O2)) ||
1597 (isa<Argument>(O2) && isIdentifiedFunctionLocal(O1)))
1598 return AliasResult::NoAlias;
1599
1600 // If one pointer is the result of a call/invoke or load and the other is a
1601 // non-escaping local object within the same function, then we know the
1602 // object couldn't escape to a point where the call could return it.
1603 //
1604 // Note that if the pointers are in different functions, there are a
1605 // variety of complications. A call with a nocapture argument may still
1606 // temporary store the nocapture argument's value in a temporary memory
1607 // location if that memory location doesn't escape. Or it may pass a
1608 // nocapture value to other functions as long as they don't capture it.
1609 if (isEscapeSource(O1) && AAQI.CA->isNotCapturedBefore(
1610 O2, dyn_cast<Instruction>(O1), /*OrAt*/ true))
1611 return AliasResult::NoAlias;
1612 if (isEscapeSource(O2) && AAQI.CA->isNotCapturedBefore(
1613 O1, dyn_cast<Instruction>(O2), /*OrAt*/ true))
1614 return AliasResult::NoAlias;
1615 }
1616
1617 // If the size of one access is larger than the entire object on the other
1618 // side, then we know such behavior is undefined and can assume no alias.
1619 bool NullIsValidLocation = NullPointerIsDefined(&F);
1621 O2, getMinimalExtentFrom(*V1, V1Size, DL, NullIsValidLocation), DL,
1622 TLI, NullIsValidLocation)) ||
1624 O1, getMinimalExtentFrom(*V2, V2Size, DL, NullIsValidLocation), DL,
1625 TLI, NullIsValidLocation)))
1626 return AliasResult::NoAlias;
1627
1629 for (AssumptionCache::ResultElem &Elem : AC.assumptionsFor(O1)) {
1630 if (!Elem || Elem.Index == AssumptionCache::ExprResultIdx)
1631 continue;
1632
1633 AssumeInst *Assume = cast<AssumeInst>(Elem);
1634 OperandBundleUse OBU = Assume->getOperandBundleAt(Elem.Index);
1635 if (OBU.getTagName() == "separate_storage") {
1636 assert(OBU.Inputs.size() == 2);
1637 const Value *Hint1 = OBU.Inputs[0].get();
1638 const Value *Hint2 = OBU.Inputs[1].get();
1639 // This is often a no-op; instcombine rewrites this for us. No-op
1640 // getUnderlyingObject calls are fast, though.
1641 const Value *HintO1 = getUnderlyingObject(Hint1);
1642 const Value *HintO2 = getUnderlyingObject(Hint2);
1643
1644 DominatorTree *DT = getDT(AAQI);
1645 auto ValidAssumeForPtrContext = [&](const Value *Ptr) {
1646 if (const Instruction *PtrI = dyn_cast<Instruction>(Ptr)) {
1647 return isValidAssumeForContext(Assume, PtrI, DT,
1648 /* AllowEphemerals */ true);
1649 }
1650 if (const Argument *PtrA = dyn_cast<Argument>(Ptr)) {
1651 const Instruction *FirstI =
1652 &*PtrA->getParent()->getEntryBlock().begin();
1653 return isValidAssumeForContext(Assume, FirstI, DT,
1654 /* AllowEphemerals */ true);
1655 }
1656 return false;
1657 };
1658
1659 if ((O1 == HintO1 && O2 == HintO2) || (O1 == HintO2 && O2 == HintO1)) {
1660 // Note that we go back to V1 and V2 for the
1661 // ValidAssumeForPtrContext checks; they're dominated by O1 and O2,
1662 // so strictly more assumptions are valid for them.
1663 if ((CtxI && isValidAssumeForContext(Assume, CtxI, DT,
1664 /* AllowEphemerals */ true)) ||
1665 ValidAssumeForPtrContext(V1) || ValidAssumeForPtrContext(V2)) {
1666 return AliasResult::NoAlias;
1667 }
1668 }
1669 }
1670 }
1671 }
1672
1673 // If one the accesses may be before the accessed pointer, canonicalize this
1674 // by using unknown after-pointer sizes for both accesses. This is
1675 // equivalent, because regardless of which pointer is lower, one of them
1676 // will always came after the other, as long as the underlying objects aren't
1677 // disjoint. We do this so that the rest of BasicAA does not have to deal
1678 // with accesses before the base pointer, and to improve cache utilization by
1679 // merging equivalent states.
1680 if (V1Size.mayBeBeforePointer() || V2Size.mayBeBeforePointer()) {
1681 V1Size = LocationSize::afterPointer();
1682 V2Size = LocationSize::afterPointer();
1683 }
1684
1685 // FIXME: If this depth limit is hit, then we may cache sub-optimal results
1686 // for recursive queries. For this reason, this limit is chosen to be large
1687 // enough to be very rarely hit, while still being small enough to avoid
1688 // stack overflows.
1689 if (AAQI.Depth >= 512)
1690 return AliasResult::MayAlias;
1691
1692 // Check the cache before climbing up use-def chains. This also terminates
1693 // otherwise infinitely recursive queries. Include MayBeCrossIteration in the
1694 // cache key, because some cases where MayBeCrossIteration==false returns
1695 // MustAlias or NoAlias may become MayAlias under MayBeCrossIteration==true.
1696 AAQueryInfo::LocPair Locs({V1, V1Size, AAQI.MayBeCrossIteration},
1697 {V2, V2Size, AAQI.MayBeCrossIteration});
1698 const bool Swapped = V1 > V2;
1699 if (Swapped)
1700 std::swap(Locs.first, Locs.second);
1701 const auto &Pair = AAQI.AliasCache.try_emplace(
1703 if (!Pair.second) {
1704 auto &Entry = Pair.first->second;
1705 if (!Entry.isDefinitive()) {
1706 // Remember that we used an assumption. This may either be a direct use
1707 // of an assumption, or a use of an entry that may itself be based on an
1708 // assumption.
1709 ++AAQI.NumAssumptionUses;
1710 if (Entry.isAssumption())
1711 ++Entry.NumAssumptionUses;
1712 }
1713 // Cache contains sorted {V1,V2} pairs but we should return original order.
1714 auto Result = Entry.Result;
1715 Result.swap(Swapped);
1716 return Result;
1717 }
1718
1719 int OrigNumAssumptionUses = AAQI.NumAssumptionUses;
1720 unsigned OrigNumAssumptionBasedResults = AAQI.AssumptionBasedResults.size();
1722 aliasCheckRecursive(V1, V1Size, V2, V2Size, AAQI, O1, O2);
1723
1724 auto It = AAQI.AliasCache.find(Locs);
1725 assert(It != AAQI.AliasCache.end() && "Must be in cache");
1726 auto &Entry = It->second;
1727
1728 // Check whether a NoAlias assumption has been used, but disproven.
1729 bool AssumptionDisproven =
1730 Entry.NumAssumptionUses > 0 && Result != AliasResult::NoAlias;
1731 if (AssumptionDisproven)
1733
1734 // This is a definitive result now, when considered as a root query.
1735 AAQI.NumAssumptionUses -= Entry.NumAssumptionUses;
1736 Entry.Result = Result;
1737 // Cache contains sorted {V1,V2} pairs.
1738 Entry.Result.swap(Swapped);
1739
1740 // If the assumption has been disproven, remove any results that may have
1741 // been based on this assumption. Do this after the Entry updates above to
1742 // avoid iterator invalidation.
1743 if (AssumptionDisproven)
1744 while (AAQI.AssumptionBasedResults.size() > OrigNumAssumptionBasedResults)
1746
1747 // The result may still be based on assumptions higher up in the chain.
1748 // Remember it, so it can be purged from the cache later.
1749 if (OrigNumAssumptionUses != AAQI.NumAssumptionUses &&
1750 Result != AliasResult::MayAlias) {
1753 } else {
1754 Entry.NumAssumptionUses = AAQueryInfo::CacheEntry::Definitive;
1755 }
1756
1757 // Depth is incremented before this function is called, so Depth==1 indicates
1758 // a root query.
1759 if (AAQI.Depth == 1) {
1760 // Any remaining assumption based results must be based on proven
1761 // assumptions, so convert them to definitive results.
1762 for (const auto &Loc : AAQI.AssumptionBasedResults) {
1763 auto It = AAQI.AliasCache.find(Loc);
1764 if (It != AAQI.AliasCache.end())
1765 It->second.NumAssumptionUses = AAQueryInfo::CacheEntry::Definitive;
1766 }
1768 AAQI.NumAssumptionUses = 0;
1769 }
1770 return Result;
1771}
1772
1773AliasResult BasicAAResult::aliasCheckRecursive(
1774 const Value *V1, LocationSize V1Size,
1775 const Value *V2, LocationSize V2Size,
1776 AAQueryInfo &AAQI, const Value *O1, const Value *O2) {
1777 if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) {
1778 AliasResult Result = aliasGEP(GV1, V1Size, V2, V2Size, O1, O2, AAQI);
1779 if (Result != AliasResult::MayAlias)
1780 return Result;
1781 } else if (const GEPOperator *GV2 = dyn_cast<GEPOperator>(V2)) {
1782 AliasResult Result = aliasGEP(GV2, V2Size, V1, V1Size, O2, O1, AAQI);
1783 Result.swap();
1784 if (Result != AliasResult::MayAlias)
1785 return Result;
1786 }
1787
1788 if (const PHINode *PN = dyn_cast<PHINode>(V1)) {
1789 AliasResult Result = aliasPHI(PN, V1Size, V2, V2Size, AAQI);
1790 if (Result != AliasResult::MayAlias)
1791 return Result;
1792 } else if (const PHINode *PN = dyn_cast<PHINode>(V2)) {
1793 AliasResult Result = aliasPHI(PN, V2Size, V1, V1Size, AAQI);
1794 Result.swap();
1795 if (Result != AliasResult::MayAlias)
1796 return Result;
1797 }
1798
1799 if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) {
1800 AliasResult Result = aliasSelect(S1, V1Size, V2, V2Size, AAQI);
1801 if (Result != AliasResult::MayAlias)
1802 return Result;
1803 } else if (const SelectInst *S2 = dyn_cast<SelectInst>(V2)) {
1804 AliasResult Result = aliasSelect(S2, V2Size, V1, V1Size, AAQI);
1805 Result.swap();
1806 if (Result != AliasResult::MayAlias)
1807 return Result;
1808 }
1809
1810 // If both pointers are pointing into the same object and one of them
1811 // accesses the entire object, then the accesses must overlap in some way.
1812 if (O1 == O2) {
1813 bool NullIsValidLocation = NullPointerIsDefined(&F);
1814 if (V1Size.isPrecise() && V2Size.isPrecise() &&
1815 (isObjectSize(O1, V1Size.getValue(), DL, TLI, NullIsValidLocation) ||
1816 isObjectSize(O2, V2Size.getValue(), DL, TLI, NullIsValidLocation)))
1818 }
1819
1820 return AliasResult::MayAlias;
1821}
1822
1823/// Check whether two Values can be considered equivalent.
1824///
1825/// If the values may come from different cycle iterations, this will also
1826/// check that the values are not part of cycle. We have to do this because we
1827/// are looking through phi nodes, that is we say
1828/// noalias(V, phi(VA, VB)) if noalias(V, VA) and noalias(V, VB).
1829bool BasicAAResult::isValueEqualInPotentialCycles(const Value *V,
1830 const Value *V2,
1831 const AAQueryInfo &AAQI) {
1832 if (V != V2)
1833 return false;
1834
1835 if (!AAQI.MayBeCrossIteration)
1836 return true;
1837
1838 // Non-instructions and instructions in the entry block cannot be part of
1839 // a loop.
1840 const Instruction *Inst = dyn_cast<Instruction>(V);
1841 if (!Inst || Inst->getParent()->isEntryBlock())
1842 return true;
1843
1844 return isNotInCycle(Inst, getDT(AAQI), /*LI*/ nullptr);
1845}
1846
1847/// Computes the symbolic difference between two de-composed GEPs.
1848void BasicAAResult::subtractDecomposedGEPs(DecomposedGEP &DestGEP,
1849 const DecomposedGEP &SrcGEP,
1850 const AAQueryInfo &AAQI) {
1851 // Drop nuw flag from GEP if subtraction of constant offsets overflows in an
1852 // unsigned sense.
1853 if (DestGEP.Offset.ult(SrcGEP.Offset))
1854 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1855
1856 DestGEP.Offset -= SrcGEP.Offset;
1857 for (const VariableGEPIndex &Src : SrcGEP.VarIndices) {
1858 // Find V in Dest. This is N^2, but pointer indices almost never have more
1859 // than a few variable indexes.
1860 bool Found = false;
1861 for (auto I : enumerate(DestGEP.VarIndices)) {
1862 VariableGEPIndex &Dest = I.value();
1863 if ((!isValueEqualInPotentialCycles(Dest.Val.V, Src.Val.V, AAQI) &&
1864 !areBothVScale(Dest.Val.V, Src.Val.V)) ||
1865 !Dest.Val.hasSameCastsAs(Src.Val))
1866 continue;
1867
1868 // Normalize IsNegated if we're going to lose the NSW flag anyway.
1869 if (Dest.IsNegated) {
1870 Dest.Scale = -Dest.Scale;
1871 Dest.IsNegated = false;
1872 Dest.IsNSW = false;
1873 }
1874
1875 // If we found it, subtract off Scale V's from the entry in Dest. If it
1876 // goes to zero, remove the entry.
1877 if (Dest.Scale != Src.Scale) {
1878 // Drop nuw flag from GEP if subtraction of V's Scale overflows in an
1879 // unsigned sense.
1880 if (Dest.Scale.ult(Src.Scale))
1881 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1882
1883 Dest.Scale -= Src.Scale;
1884 Dest.IsNSW = false;
1885 } else {
1886 DestGEP.VarIndices.erase(DestGEP.VarIndices.begin() + I.index());
1887 }
1888 Found = true;
1889 break;
1890 }
1891
1892 // If we didn't consume this entry, add it to the end of the Dest list.
1893 if (!Found) {
1894 VariableGEPIndex Entry = {Src.Val, Src.Scale, Src.CxtI, Src.IsNSW,
1895 /* IsNegated */ true};
1896 DestGEP.VarIndices.push_back(Entry);
1897
1898 // Drop nuw flag when we have unconsumed variable indices from SrcGEP.
1899 DestGEP.NWFlags = DestGEP.NWFlags.withoutNoUnsignedWrap();
1900 }
1901 }
1902}
1903
1904bool BasicAAResult::constantOffsetHeuristic(const DecomposedGEP &GEP,
1905 LocationSize MaybeV1Size,
1906 LocationSize MaybeV2Size,
1907 AssumptionCache *AC,
1908 DominatorTree *DT,
1909 const AAQueryInfo &AAQI) {
1910 if (GEP.VarIndices.size() != 2 || !MaybeV1Size.hasValue() ||
1911 !MaybeV2Size.hasValue())
1912 return false;
1913
1914 const uint64_t V1Size = MaybeV1Size.getValue();
1915 const uint64_t V2Size = MaybeV2Size.getValue();
1916
1917 const VariableGEPIndex &Var0 = GEP.VarIndices[0], &Var1 = GEP.VarIndices[1];
1918
1919 if (Var0.Val.TruncBits != 0 || !Var0.Val.hasSameCastsAs(Var1.Val) ||
1920 !Var0.hasNegatedScaleOf(Var1) ||
1921 Var0.Val.V->getType() != Var1.Val.V->getType())
1922 return false;
1923
1924 // We'll strip off the Extensions of Var0 and Var1 and do another round
1925 // of GetLinearExpression decomposition. In the example above, if Var0
1926 // is zext(%x + 1) we should get V1 == %x and V1Offset == 1.
1927
1928 LinearExpression E0 =
1929 GetLinearExpression(CastedValue(Var0.Val.V), DL, 0, AC, DT);
1930 LinearExpression E1 =
1931 GetLinearExpression(CastedValue(Var1.Val.V), DL, 0, AC, DT);
1932 if (E0.Scale != E1.Scale || !E0.Val.hasSameCastsAs(E1.Val) ||
1933 !isValueEqualInPotentialCycles(E0.Val.V, E1.Val.V, AAQI))
1934 return false;
1935
1936 // We have a hit - Var0 and Var1 only differ by a constant offset!
1937
1938 // If we've been sext'ed then zext'd the maximum difference between Var0 and
1939 // Var1 is possible to calculate, but we're just interested in the absolute
1940 // minimum difference between the two. The minimum distance may occur due to
1941 // wrapping; consider "add i3 %i, 5": if %i == 7 then 7 + 5 mod 8 == 4, and so
1942 // the minimum distance between %i and %i + 5 is 3.
1943 APInt MinDiff = E0.Offset - E1.Offset, Wrapped = -MinDiff;
1944 MinDiff = APIntOps::umin(MinDiff, Wrapped);
1945 APInt MinDiffBytes =
1946 MinDiff.zextOrTrunc(Var0.Scale.getBitWidth()) * Var0.Scale.abs();
1947
1948 // We can't definitely say whether GEP1 is before or after V2 due to wrapping
1949 // arithmetic (i.e. for some values of GEP1 and V2 GEP1 < V2, and for other
1950 // values GEP1 > V2). We'll therefore only declare NoAlias if both V1Size and
1951 // V2Size can fit in the MinDiffBytes gap.
1952 return MinDiffBytes.uge(V1Size + GEP.Offset.abs()) &&
1953 MinDiffBytes.uge(V2Size + GEP.Offset.abs());
1954}
1955
1956//===----------------------------------------------------------------------===//
1957// BasicAliasAnalysis Pass
1958//===----------------------------------------------------------------------===//
1959
1960AnalysisKey BasicAA::Key;
1961
1963 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1964 auto &AC = AM.getResult<AssumptionAnalysis>(F);
1965 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
1966 return BasicAAResult(F.getDataLayout(), F, TLI, AC, DT);
1967}
1968
1971}
1972
1973char BasicAAWrapperPass::ID = 0;
1974
1975void BasicAAWrapperPass::anchor() {}
1976
1978 "Basic Alias Analysis (stateless AA impl)", true, true)
1983 "Basic Alias Analysis (stateless AA impl)", true, true)
1984
1986 return new BasicAAWrapperPass();
1987}
1988
1990 auto &ACT = getAnalysis<AssumptionCacheTracker>();
1991 auto &TLIWP = getAnalysis<TargetLibraryInfoWrapperPass>();
1992 auto &DTWP = getAnalysis<DominatorTreeWrapperPass>();
1993
1994 Result.reset(new BasicAAResult(F.getDataLayout(), F,
1995 TLIWP.getTLI(F), ACT.getAssumptionCache(F),
1996 &DTWP.getDomTree()));
1997
1998 return false;
1999}
2000
2002 AU.setPreservesAll();
2006}
static const LLT S1
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
This file contains the simple types necessary to represent the attributes associated with functions a...
static cl::opt< bool > EnableRecPhiAnalysis("basic-aa-recphi", cl::Hidden, cl::init(true))
Enable analysis of recursive PHI nodes.
static const Function * getParent(const Value *V)
static bool isObjectSmallerThan(const Value *V, TypeSize Size, const DataLayout &DL, const TargetLibraryInfo &TLI, bool NullIsValidLoc)
Returns true if we can prove that the object specified by V is smaller than Size.
static bool isObjectSize(const Value *V, TypeSize Size, const DataLayout &DL, const TargetLibraryInfo &TLI, bool NullIsValidLoc)
Returns true if we can prove that the object specified by V has size Size.
static cl::opt< bool > EnableSeparateStorageAnalysis("basic-aa-separate-storage", cl::Hidden, cl::init(true))
static bool notDifferentParent(const Value *O1, const Value *O2)
static LinearExpression GetLinearExpression(const CastedValue &Val, const DataLayout &DL, unsigned Depth, AssumptionCache *AC, DominatorTree *DT)
Analyzes the specified value as a linear expression: "A*V + B", where A and B are constant integers.
static bool isNotInCycle(const Instruction *I, const DominatorTree *DT, const LoopInfo *LI)
static bool areBothVScale(const Value *V1, const Value *V2)
Return true if both V1 and V2 are VScale.
static TypeSize getMinimalExtentFrom(const Value &V, const LocationSize &LocSize, const DataLayout &DL, bool NullIsValidLoc)
Return the minimal extent from V to the end of the underlying object, assuming the result is used in ...
static AliasResult MergeAliasResults(AliasResult A, AliasResult B)
static bool isIntrinsicCall(const CallBase *Call, Intrinsic::ID IID)
basic Basic Alias true
static const unsigned MaxLookupSearchDepth
This is the interface for LLVM's primary stateless and local alias analysis.
block Block Frequency Analysis
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
uint64_t Size
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1315
Hexagon Common GEP
#define _
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file provides utility analysis objects describing memory locations.
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
place backedge safepoints impl
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file provides utility classes that use RAII to save and restore values.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Value * RHS
This class stores info we want to provide to or retain within an alias query.
SmallVector< AAQueryInfo::LocPair, 4 > AssumptionBasedResults
Location pairs for which an assumption based result is currently stored.
unsigned Depth
Query depth used to distinguish recursive queries.
int NumAssumptionUses
How many active NoAlias assumption uses there are.
std::pair< AACacheLoc, AACacheLoc > LocPair
AliasCacheT AliasCache
bool MayBeCrossIteration
Tracks whether the accesses may be on different cycle iterations.
CaptureAnalysis * CA
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
MemoryEffects getMemoryEffects(const CallBase *Call)
Return the behavior of the given call site.
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1945
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:986
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1007
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:206
APInt abs() const
Get the absolute value.
Definition: APInt.h:1773
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1468
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1618
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1710
APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1934
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
The possible results of an alias query.
Definition: AliasAnalysis.h:77
void swap(bool DoSwap=true)
Helper for processing AliasResult for swapped memory location pairs.
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:98
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:95
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
void setOffset(int32_t NewOffset)
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition: PassManager.h:310
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
This represents the llvm.assume intrinsic.
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
This is the AA result object for the basic, local, and stateless alias analysis.
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
Checks to see if the specified callsite can clobber the specified memory object.
ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx)
Get the location associated with a pointer argument of a callsite.
MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
Returns the behavior when calling the given call site.
ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals=false)
Returns a bitmask that should be unconditionally applied to the ModRef info of a memory location.
bool invalidate(Function &Fn, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Handle invalidation events in the new pass manager.
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI)
Legacy wrapper pass to provide the BasicAAResult object.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
BasicAAResult run(Function &F, FunctionAnalysisManager &AM)
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:220
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1112
This class represents a function call, abstracting a target machine's calling convention.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
A constant pointer value that points to null.
Definition: Constants.h:552
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
ConstantRange smul_fast(const ConstantRange &Other) const
Return range of possible values for a signed multiplication of this and Other.
bool isEmptySet() const
Return true if this set contains no members.
ConstantRange smul_sat(const ConstantRange &Other) const
Perform a signed saturating multiplication of two constant ranges.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition: DenseMap.h:226
bool erase(const KeyT &Val)
Definition: DenseMap.h:321
iterator end()
Definition: DenseMap.h:84
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
NodeT * getRoot() const
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
void removeInstruction(Instruction *I)
bool isNotCapturedBefore(const Value *Object, const Instruction *I, bool OrAt) override
Check whether Object is not captured before instruction I.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags all()
bool hasNoUnsignedWrap() const
bool isInBounds() const
bool hasNoUnsignedSignedWrap() const
Definition: Operator.h:437
Type * getSourceElementType() const
Definition: Operator.cpp:70
bool hasNoUnsignedWrap() const
Definition: Operator.h:441
GEPNoWrapFlags getNoWrapFlags() const
Definition: Operator.h:430
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:657
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
bool hasValue() const
bool mayBeBeforePointer() const
Whether accesses before the base pointer are possible.
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
bool isScalable() const
TypeSize getValue() const
bool isZero() const
bool isPrecise() const
static constexpr LocationSize afterPointer()
Any location after the base pointer (but still within the underlying object).
static MemoryEffectsBase readOnly()
Create MemoryEffectsBase that can read any memory.
Definition: ModRef.h:122
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible memory.
Definition: ModRef.h:138
static MemoryEffectsBase writeOnly()
Create MemoryEffectsBase that can write any memory.
Definition: ModRef.h:127
Representation for a specific memory location.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
static MemoryLocation getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location before or after Ptr, while remaining within the underl...
const Value * Ptr
The address of the start of the location.
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
This class represents the LLVM 'select' instruction.
bool isNotCapturedBefore(const Value *Object, const Instruction *I, bool OrAt) override
Check whether Object is not captured before instruction I.
size_type size() const
Definition: SmallPtrSet.h:94
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
Class to represent struct types.
Definition: DerivedTypes.h:218
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:345
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:264
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:310
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
op_iterator op_begin()
Definition: User.h:280
Value * getOperand(unsigned i) const
Definition: User.h:228
op_iterator op_end()
Definition: User.h:282
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
const Value * stripPointerCastsForAliasAnalysis() const
Strip off pointer casts, all-zero GEPs, single-argument phi nodes and invariant group info.
Definition: Value.cpp:710
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:218
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition: ilist_node.h:32
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition: APInt.h:2227
APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
Definition: APInt.cpp:771
@ Entry
Definition: COFF.h:844
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
VScaleVal_match m_VScale()
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
@ Assume
Do not drop type tests (default).
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
@ Offset
Definition: DWP.cpp:480
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2448
bool isPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const BasicBlock *StopBB, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether there is at least one path from a block in 'Worklist' to 'StopBB' without passing t...
Definition: CFG.cpp:239
auto successors(const MachineBasicBlock *BB)
bool isBaseOfObject(const Value *V)
Return true if we know V to the base address of the corresponding memory object.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isNonEscapingLocalObject(const Value *V, SmallDenseMap< const Value *, bool, 8 > *IsCapturedCache=nullptr)
Returns true if the pointer is to a function-local object that never escapes from the function.
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:48
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1187
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
FunctionPass * createBasicAAWrapperPass()
bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory similar to malloc or...
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:27
@ Ref
The access may reference the value stored in memory.
@ ModRef
The access may reference and may modify the value stored in memory.
@ Mod
The access may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
Instruction * FindEarliestCapture(const Value *V, Function &F, bool ReturnCaptures, bool StoreCaptures, const DominatorTree &DT, unsigned MaxUsesToExplore=0)
void initializeBasicAAWrapperPassPass(PassRegistry &)
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
bool isModAndRefSet(const ModRefInfo MRI)
Definition: ModRef.h:45
bool isIdentifiedFunctionLocal(const Value *V)
Return true if V is umabigously identified at the function-level.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
bool isEscapeSource(const Value *V)
Returns true if the pointer is one which would have been considered an escape by isNonEscapingLocalOb...
gep_type_iterator gep_type_begin(const User *GEP)
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition: CFG.cpp:281
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
SmallVector< VariableGEPIndex, 4 > VarIndices
void print(raw_ostream &OS) const
static constexpr int Definitive
Cache entry is neither an assumption nor does it use a (non-definitive) assumption.
static constexpr int AssumptionBased
Cache entry is not an assumption itself, but may be using an assumption from higher up the stack.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
virtual ~CaptureAnalysis()=0
virtual bool isNotCapturedBefore(const Value *Object, const Instruction *I, bool OrAt)=0
Check whether Object is not captured before instruction I.
Various options to control the behavior of getObjectSize.
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
A lightweight accessor for an operand bundle meant to be passed around by value.
Definition: InstrTypes.h:1007
StringRef getTagName() const
Return the tag of this operand bundle as a string.
Definition: InstrTypes.h:1026
ArrayRef< Use > Inputs
Definition: InstrTypes.h:1008
A utility class that uses RAII to save and restore the value of a variable.