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