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