1//===- TypeBasedAliasAnalysis.cpp - Type-Based Alias Analysis -------------===//
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 TypeBasedAliasAnalysis pass, which implements
10// metadata-based TBAA.
11//
12// In LLVM IR, memory does not have types, so LLVM's own type system is not
13// suitable for doing TBAA. Instead, metadata is added to the IR to describe
14// a type system of a higher level language. This can be used to implement
15// typical C/C++ TBAA, but it can also be used to implement custom alias
16// analysis behavior for other languages.
17//
18// Scalar type nodes have up to three fields, e.g.:
19// !0 = !{ !"an example type tree" }
20// !1 = !{ !"int", !0 }
21// !2 = !{ !"float", !0 }
22// !3 = !{ !"const float", !2, i64 1 }
23//
24// The first field is an identity field. It can be any value, usually
25// an MDString, which uniquely identifies the type. The most important
26// name in the tree is the name of the root node. Two trees with
27// different root node names are entirely disjoint, even if they
28// have leaves with common names.
29//
30// The second field identifies the type's parent node in the tree, or
31// is null or omitted for a root node. A type is considered to alias
32// all of its descendants and all of its ancestors in the tree. Also,
33// a type is considered to alias all types in other trees, so that
34// bitcode produced from multiple front-ends is handled conservatively.
35//
36// If the third field is present, it's an integer which if equal to 1
37// indicates that the type is "constant" (meaning pointsToConstantMemory
38// should return true; see
39// http://llvm.org/docs/AliasAnalysis.html#OtherItfs).
40//
41// The MDNodes attached to an instruction using "!tbaa" are not plain type
42// nodes, but path tag nodes.
43//
44// The path tag node has 4 fields with the last field being optional.
45//
46// The first field is the base type node, it can be a struct type node
47// or a scalar type node. The second field is the access type node, it
48// must be a scalar type node. The third field is the offset into the base type.
49// The last field has the same meaning as the last field of our scalar TBAA:
50// it's an integer which if equal to 1 indicates that the access is "constant".
51//
52// The struct type node has a name and a list of pairs, one pair for each member
53// of the struct. The first element of each pair is a type node (a struct type
54// node or a scalar type node), specifying the type of the member, the second
55// element of each pair is the offset of the member.
56//
57// Given an example
58// typedef struct {
59// short s;
60// } A;
61// typedef struct {
62// uint16_t s;
63// A a;
64// } B;
65//
66// For an access to B.a.s, we attach !5 (a path tag node) to the load/store
67// instruction. The base type is !4 (struct B), the access type is !2 (scalar
68// type short) and the offset is 4.
69//
70// !0 = !{!"Simple C/C++ TBAA"}
71// !1 = !{!"omnipotent char", !0} // Scalar type node
72// !2 = !{!"short", !1} // Scalar type node
73// !3 = !{!"A", !2, i64 0} // Struct type node
74// !4 = !{!"B", !2, i64 0, !3, i64 4}
75// // Struct type node
76// !5 = !{!4, !2, i64 4} // Path tag node
77//
78// The struct type nodes and the scalar type nodes form a type DAG.
79// Root (!0)
80// char (!1) -- edge to Root
81// short (!2) -- edge to char
82// A (!3) -- edge with offset 0 to short
83// B (!4) -- edge with offset 0 to short and edge with offset 4 to A
84//
85// To check if two tags (tagX and tagY) can alias, we start from the base type
86// of tagX, follow the edge with the correct offset in the type DAG and adjust
87// the offset until we reach the base type of tagY or until we reach the Root
88// node.
89// If we reach the base type of tagY, compare the adjusted offset with
90// offset of tagY, return Alias if the offsets are the same, return NoAlias
91// otherwise.
92// If we reach the Root node, perform the above starting from base type of tagY
93// to see if we reach base type of tagX.
94//
95// If they have different roots, they're part of different potentially
96// unrelated type systems, so we return Alias to be conservative.
97// If neither node is an ancestor of the other and they have the same root,
98// then we say NoAlias.
99//
100//===----------------------------------------------------------------------===//
101
102#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
103#include "AnalysisOptions.h"
104#include "llvm/ADT/SetVector.h"
105#include "llvm/Analysis/AliasAnalysis.h"
106#include "llvm/Analysis/MemoryLocation.h"
107#include "llvm/IR/Constants.h"
108#include "llvm/IR/DataLayout.h"
109#include "llvm/IR/DerivedTypes.h"
110#include "llvm/IR/InstrTypes.h"
111#include "llvm/IR/LLVMContext.h"
112#include "llvm/IR/Metadata.h"
113#include "llvm/IR/Module.h"
114#include "llvm/InitializePasses.h"
115#include "llvm/Pass.h"
116#include "llvm/Support/Casting.h"
117#include <cassert>
118#include <cstdint>
119
120using namespace llvm;
121
122namespace {
123
124/// isNewFormatTypeNode - Return true iff the given type node is in the new
125/// size-aware format.
126static bool isNewFormatTypeNode(const MDNode *N) {
127 if (N->getNumOperands() < 3)
128 return false;
129 // In the old format the first operand is a string.
130 if (!isa<MDNode>(Val: N->getOperand(I: 0)))
131 return false;
132 return true;
133}
134
135/// This is a simple wrapper around an MDNode which provides a higher-level
136/// interface by hiding the details of how alias analysis information is encoded
137/// in its operands.
138class TBAANode {
139 const MDNode *Node = nullptr;
140
141public:
142 TBAANode() = default;
143 explicit TBAANode(const MDNode *N) : Node(N) {}
144
145 /// getNode - Get the MDNode for this TBAANode.
146 const MDNode *getNode() const { return Node; }
147
148 /// isNewFormat - Return true iff the wrapped type node is in the new
149 /// size-aware format.
150 bool isNewFormat() const { return isNewFormatTypeNode(N: Node); }
151
152 /// getParent - Get this TBAANode's Alias tree parent.
153 TBAANode getParent() const {
154 if (isNewFormat())
155 return TBAANode(cast<MDNode>(Val: Node->getOperand(I: 0)));
156
157 if (Node->getNumOperands() < 2)
158 return TBAANode();
159 const MDNode *P = dyn_cast_or_null<MDNode>(Val: Node->getOperand(I: 1));
160 if (!P)
161 return TBAANode();
162 // Ok, this node has a valid parent. Return it.
163 return TBAANode(P);
164 }
165};
166
167/// This is a simple wrapper around an MDNode which provides a
168/// higher-level interface by hiding the details of how alias analysis
169/// information is encoded in its operands.
170class TBAAStructTagNode {
171 /// This node should be created with createTBAAAccessTag().
172 const MDNode *Node;
173
174public:
175 explicit TBAAStructTagNode(const MDNode *N) : Node(N) {}
176
177 /// Get the MDNode for this TBAAStructTagNode.
178 const MDNode *getNode() const { return Node; }
179
180 /// isNewFormat - Return true iff the wrapped access tag is in the new
181 /// size-aware format.
182 bool isNewFormat() const {
183 if (Node->getNumOperands() < 4)
184 return false;
185 if (const MDNode *AccessType = getAccessType())
186 if (!TBAANode(AccessType).isNewFormat())
187 return false;
188 return true;
189 }
190
191 const MDNode *getBaseType() const {
192 return dyn_cast_or_null<MDNode>(Val: Node->getOperand(I: 0));
193 }
194
195 const MDNode *getAccessType() const {
196 return dyn_cast_or_null<MDNode>(Val: Node->getOperand(I: 1));
197 }
198
199 uint64_t getOffset() const {
200 return mdconst::extract<ConstantInt>(MD: Node->getOperand(I: 2))->getZExtValue();
201 }
202
203 uint64_t getSize() const {
204 if (!isNewFormat())
205 return UINT64_MAX;
206 return mdconst::extract<ConstantInt>(MD: Node->getOperand(I: 3))->getZExtValue();
207 }
208
209 /// Test if this TBAAStructTagNode represents a type for objects
210 /// which are not modified (by any means) in the context where this
211 /// AliasAnalysis is relevant.
212 bool isTypeImmutable() const {
213 unsigned OpNo = isNewFormat() ? 4 : 3;
214 if (Node->getNumOperands() < OpNo + 1)
215 return false;
216 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD: Node->getOperand(I: OpNo));
217 if (!CI)
218 return false;
219 return CI->getValue()[0];
220 }
221};
222
223/// This is a simple wrapper around an MDNode which provides a
224/// higher-level interface by hiding the details of how alias analysis
225/// information is encoded in its operands.
226class TBAAStructTypeNode {
227 /// This node should be created with createTBAATypeNode().
228 const MDNode *Node = nullptr;
229
230public:
231 TBAAStructTypeNode() = default;
232 explicit TBAAStructTypeNode(const MDNode *N) : Node(N) {}
233
234 /// Get the MDNode for this TBAAStructTypeNode.
235 const MDNode *getNode() const { return Node; }
236
237 /// isNewFormat - Return true iff the wrapped type node is in the new
238 /// size-aware format.
239 bool isNewFormat() const { return isNewFormatTypeNode(N: Node); }
240
241 bool operator==(const TBAAStructTypeNode &Other) const {
242 return getNode() == Other.getNode();
243 }
244
245 /// getId - Return type identifier.
246 Metadata *getId() const {
247 return Node->getOperand(I: isNewFormat() ? 2 : 0);
248 }
249
250 unsigned getNumFields() const {
251 unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
252 unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
253 return (getNode()->getNumOperands() - FirstFieldOpNo) / NumOpsPerField;
254 }
255
256 TBAAStructTypeNode getFieldType(unsigned FieldIndex) const {
257 unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
258 unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
259 unsigned OpIndex = FirstFieldOpNo + FieldIndex * NumOpsPerField;
260 auto *TypeNode = cast<MDNode>(Val: getNode()->getOperand(I: OpIndex));
261 return TBAAStructTypeNode(TypeNode);
262 }
263
264 /// Get this TBAAStructTypeNode's field in the type DAG with
265 /// given offset. Update the offset to be relative to the field type.
266 TBAAStructTypeNode getField(uint64_t &Offset) const {
267 bool NewFormat = isNewFormat();
268 const ArrayRef<MDOperand> Operands = Node->operands();
269 const unsigned NumOperands = Operands.size();
270
271 if (NewFormat) {
272 // New-format root and scalar type nodes have no fields.
273 if (NumOperands < 6)
274 return TBAAStructTypeNode();
275 } else {
276 // Parent can be omitted for the root node.
277 if (NumOperands < 2)
278 return TBAAStructTypeNode();
279
280 // Fast path for a scalar type node and a struct type node with a single
281 // field.
282 if (NumOperands <= 3) {
283 uint64_t Cur =
284 NumOperands == 2
285 ? 0
286 : mdconst::extract<ConstantInt>(MD: Operands[2])->getZExtValue();
287 Offset -= Cur;
288 MDNode *P = dyn_cast_or_null<MDNode>(Val: Operands[1]);
289 if (!P)
290 return TBAAStructTypeNode();
291 return TBAAStructTypeNode(P);
292 }
293 }
294
295 // Assume the offsets are in order. We return the previous field if
296 // the current offset is bigger than the given offset.
297 unsigned FirstFieldOpNo = NewFormat ? 3 : 1;
298 unsigned NumOpsPerField = NewFormat ? 3 : 2;
299 unsigned TheIdx = 0;
300
301 for (unsigned Idx = FirstFieldOpNo; Idx < NumOperands;
302 Idx += NumOpsPerField) {
303 uint64_t Cur =
304 mdconst::extract<ConstantInt>(MD: Operands[Idx + 1])->getZExtValue();
305 if (Cur > Offset) {
306 assert(Idx >= FirstFieldOpNo + NumOpsPerField &&
307 "TBAAStructTypeNode::getField should have an offset match!");
308 TheIdx = Idx - NumOpsPerField;
309 break;
310 }
311 }
312 // Move along the last field.
313 if (TheIdx == 0)
314 TheIdx = NumOperands - NumOpsPerField;
315 uint64_t Cur =
316 mdconst::extract<ConstantInt>(MD: Operands[TheIdx + 1])->getZExtValue();
317 Offset -= Cur;
318 MDNode *P = dyn_cast_or_null<MDNode>(Val: Operands[TheIdx]);
319 if (!P)
320 return TBAAStructTypeNode();
321 return TBAAStructTypeNode(P);
322 }
323};
324
325} // end anonymous namespace
326
327AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA,
328 const MemoryLocation &LocB,
329 AAQueryInfo &AAQI, const Instruction *) {
330 if (!shouldUseTBAA())
331 return AliasResult::MayAlias;
332
333 if (Aliases(A: LocA.AATags.TBAA, B: LocB.AATags.TBAA))
334 return AliasResult::MayAlias;
335
336 // Otherwise return a definitive result.
337 return AliasResult::NoAlias;
338}
339
340AliasResult TypeBasedAAResult::aliasErrno(const MemoryLocation &Loc,
341 const Instruction *CtxI) {
342 if (!shouldUseTBAA())
343 return AliasResult::MayAlias;
344
345 const auto *N = Loc.AATags.TBAA;
346 if (!N)
347 return AliasResult::MayAlias;
348
349 // There cannot be any alias with errno if TBAA proves the given memory
350 // location does not alias errno.
351 const auto *ErrnoTBAAMD =
352 CtxI->getModule()->getNamedMetadata(Name: "llvm.errno.tbaa");
353 if (!ErrnoTBAAMD || any_of(Range: ErrnoTBAAMD->operands(), P: [&](const auto *Node) {
354 return Aliases(A: N, B: Node);
355 }))
356 return AliasResult::MayAlias;
357 return AliasResult::NoAlias;
358}
359
360ModRefInfo TypeBasedAAResult::getModRefInfoMask(const MemoryLocation &Loc,
361 AAQueryInfo &AAQI,
362 bool IgnoreLocals) {
363 if (!shouldUseTBAA())
364 return ModRefInfo::ModRef;
365
366 const MDNode *M = Loc.AATags.TBAA;
367 if (!M)
368 return ModRefInfo::ModRef;
369
370 // If this is an "immutable" type, we can assume the pointer is pointing
371 // to constant memory.
372 if (TBAAStructTagNode(M).isTypeImmutable())
373 return ModRefInfo::NoModRef;
374
375 return ModRefInfo::ModRef;
376}
377
378MemoryEffects TypeBasedAAResult::getMemoryEffects(const CallBase *Call,
379 AAQueryInfo &AAQI) {
380 if (!shouldUseTBAA())
381 return MemoryEffects::unknown();
382
383 // If this is an "immutable" type, the access is not observable.
384 if (const MDNode *M = Call->getMetadata(KindID: LLVMContext::MD_tbaa))
385 if (TBAAStructTagNode(M).isTypeImmutable())
386 return MemoryEffects::none();
387
388 return MemoryEffects::unknown();
389}
390
391MemoryEffects TypeBasedAAResult::getMemoryEffects(const Function *F) {
392 // Functions don't have metadata.
393 return MemoryEffects::unknown();
394}
395
396ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
397 const MemoryLocation &Loc,
398 AAQueryInfo &AAQI) {
399 if (!shouldUseTBAA())
400 return ModRefInfo::ModRef;
401
402 if (const MDNode *L = Loc.AATags.TBAA)
403 if (const MDNode *M = Call->getMetadata(KindID: LLVMContext::MD_tbaa))
404 if (!Aliases(A: L, B: M))
405 return ModRefInfo::NoModRef;
406
407 return ModRefInfo::ModRef;
408}
409
410ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
411 const CallBase *Call2,
412 AAQueryInfo &AAQI) {
413 if (!shouldUseTBAA())
414 return ModRefInfo::ModRef;
415
416 if (const MDNode *M1 = Call1->getMetadata(KindID: LLVMContext::MD_tbaa))
417 if (const MDNode *M2 = Call2->getMetadata(KindID: LLVMContext::MD_tbaa))
418 if (!Aliases(A: M1, B: M2))
419 return ModRefInfo::NoModRef;
420
421 return ModRefInfo::ModRef;
422}
423
424bool MDNode::isTBAAVtableAccess() const {
425 // For struct-path aware TBAA, we use the access type of the tag.
426 TBAAStructTagNode Tag(this);
427 TBAAStructTypeNode AccessType(Tag.getAccessType());
428 if(auto *Id = dyn_cast<MDString>(Val: AccessType.getId()))
429 if (Id->getString() == "vtable pointer")
430 return true;
431 return false;
432}
433
434static bool matchAccessTags(const MDNode *A, const MDNode *B,
435 const MDNode **GenericTag = nullptr);
436
437MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
438 const MDNode *GenericTag;
439 matchAccessTags(A, B, GenericTag: &GenericTag);
440 return const_cast<MDNode*>(GenericTag);
441}
442
443static const MDNode *getLeastCommonType(const MDNode *A, const MDNode *B) {
444 if (!A || !B)
445 return nullptr;
446
447 if (A == B)
448 return A;
449
450 SmallSetVector<const MDNode *, 4> PathA;
451 TBAANode TA(A);
452 while (TA.getNode()) {
453 [[maybe_unused]] bool Inserted = PathA.insert(X: TA.getNode());
454 assert(Inserted && "Cycle found in TBAA metadata.");
455 TA = TA.getParent();
456 }
457
458 SmallSetVector<const MDNode *, 4> PathB;
459 TBAANode TB(B);
460 while (TB.getNode()) {
461 [[maybe_unused]] bool Inserted = PathB.insert(X: TB.getNode());
462 assert(Inserted && "Cycle found in TBAA metadata.");
463 TB = TB.getParent();
464 }
465
466 int IA = PathA.size() - 1;
467 int IB = PathB.size() - 1;
468
469 const MDNode *Ret = nullptr;
470 while (IA >= 0 && IB >= 0) {
471 if (PathA[IA] == PathB[IB])
472 Ret = PathA[IA];
473 else
474 break;
475 --IA;
476 --IB;
477 }
478
479 return Ret;
480}
481
482AAMDNodes AAMDNodes::merge(const AAMDNodes &Other) const {
483 AAMDNodes Result;
484 Result.TBAA = MDNode::getMostGenericTBAA(A: TBAA, B: Other.TBAA);
485 Result.TBAAStruct = nullptr;
486 Result.Scope = MDNode::getMostGenericAliasScope(A: Scope, B: Other.Scope);
487 Result.NoAlias = MDNode::intersect(A: NoAlias, B: Other.NoAlias);
488 Result.NoAliasAddrSpace = MDNode::getMostGenericNoaliasAddrspace(
489 A: NoAliasAddrSpace, B: Other.NoAliasAddrSpace);
490 return Result;
491}
492
493AAMDNodes AAMDNodes::concat(const AAMDNodes &Other) const {
494 AAMDNodes Result;
495 Result.TBAA = Result.TBAAStruct = nullptr;
496 Result.Scope = MDNode::getMostGenericAliasScope(A: Scope, B: Other.Scope);
497 Result.NoAlias = MDNode::intersect(A: NoAlias, B: Other.NoAlias);
498 Result.NoAliasAddrSpace = MDNode::getMostGenericNoaliasAddrspace(
499 A: NoAliasAddrSpace, B: Other.NoAliasAddrSpace);
500 return Result;
501}
502
503static const MDNode *createAccessTag(const MDNode *AccessType) {
504 // If there is no access type or the access type is the root node, then
505 // we don't have any useful access tag to return.
506 if (!AccessType || AccessType->getNumOperands() < 2)
507 return nullptr;
508
509 Type *Int64 = IntegerType::get(C&: AccessType->getContext(), NumBits: 64);
510 auto *OffsetNode = ConstantAsMetadata::get(C: ConstantInt::get(Ty: Int64, V: 0));
511
512 if (TBAAStructTypeNode(AccessType).isNewFormat()) {
513 // TODO: Take access ranges into account when matching access tags and
514 // fix this code to generate actual access sizes for generic tags.
515 uint64_t AccessSize = UINT64_MAX;
516 auto *SizeNode =
517 ConstantAsMetadata::get(C: ConstantInt::get(Ty: Int64, V: AccessSize));
518 Metadata *Ops[] = {const_cast<MDNode*>(AccessType),
519 const_cast<MDNode*>(AccessType),
520 OffsetNode, SizeNode};
521 return MDNode::get(Context&: AccessType->getContext(), MDs: Ops);
522 }
523
524 Metadata *Ops[] = {const_cast<MDNode*>(AccessType),
525 const_cast<MDNode*>(AccessType),
526 OffsetNode};
527 return MDNode::get(Context&: AccessType->getContext(), MDs: Ops);
528}
529
530static bool hasField(TBAAStructTypeNode BaseType,
531 TBAAStructTypeNode FieldType) {
532 for (unsigned I = 0, E = BaseType.getNumFields(); I != E; ++I) {
533 TBAAStructTypeNode T = BaseType.getFieldType(FieldIndex: I);
534 if (T == FieldType || hasField(BaseType: T, FieldType))
535 return true;
536 }
537 return false;
538}
539
540/// Return true if for two given accesses, one of the accessed objects may be a
541/// subobject of the other. The \p BaseTag and \p SubobjectTag parameters
542/// describe the accesses to the base object and the subobject respectively.
543/// \p CommonType must be the metadata node describing the common type of the
544/// accessed objects. On return, \p MayAlias is set to true iff these accesses
545/// may alias and \p Generic, if not null, points to the most generic access
546/// tag for the given two.
547static bool mayBeAccessToSubobjectOf(TBAAStructTagNode BaseTag,
548 TBAAStructTagNode SubobjectTag,
549 const MDNode *CommonType,
550 const MDNode **GenericTag,
551 bool &MayAlias) {
552 // If the base object is of the least common type, then this may be an access
553 // to its subobject.
554 if (BaseTag.getAccessType() == BaseTag.getBaseType() &&
555 BaseTag.getAccessType() == CommonType) {
556 if (GenericTag)
557 *GenericTag = createAccessTag(AccessType: CommonType);
558 MayAlias = true;
559 return true;
560 }
561
562 // If the access to the base object is through a field of the subobject's
563 // type, then this may be an access to that field. To check for that we start
564 // from the base type, follow the edge with the correct offset in the type DAG
565 // and adjust the offset until we reach the field type or until we reach the
566 // access type.
567 bool NewFormat = BaseTag.isNewFormat();
568 TBAAStructTypeNode BaseType(BaseTag.getBaseType());
569 uint64_t OffsetInBase = BaseTag.getOffset();
570
571 for (;;) {
572 // In the old format there is no distinction between fields and parent
573 // types, so in this case we consider all nodes up to the root.
574 if (!BaseType.getNode()) {
575 assert(!NewFormat && "Did not see access type in access path!");
576 break;
577 }
578
579 if (BaseType.getNode() == SubobjectTag.getBaseType()) {
580 MayAlias = OffsetInBase == SubobjectTag.getOffset() ||
581 BaseType.getNode() == BaseTag.getAccessType() ||
582 SubobjectTag.getBaseType() == SubobjectTag.getAccessType();
583 if (GenericTag) {
584 if (!MayAlias) {
585 *GenericTag = createAccessTag(AccessType: CommonType);
586 } else if (SubobjectTag.isTypeImmutable() &&
587 !BaseTag.isTypeImmutable()) {
588 // The generic tag can only be immutable if both accesses are, so
589 // drop the flag and keep the rest of the tag.
590 const MDNode *Tag = SubobjectTag.getNode();
591 unsigned FlagOpNo = SubobjectTag.isNewFormat() ? 4 : 3;
592 SmallVector<Metadata *, 4> Ops(Tag->operands().take_front(N: FlagOpNo));
593 *GenericTag = MDNode::get(Context&: Tag->getContext(), MDs: Ops);
594 } else {
595 *GenericTag = SubobjectTag.getNode();
596 }
597 }
598 return true;
599 }
600
601 // With new-format nodes we stop at the access type.
602 if (NewFormat && BaseType.getNode() == BaseTag.getAccessType())
603 break;
604
605 // Follow the edge with the correct offset. Offset will be adjusted to
606 // be relative to the field type.
607 BaseType = BaseType.getField(Offset&: OffsetInBase);
608 }
609
610 // If the base object has a direct or indirect field of the subobject's type,
611 // then this may be an access to that field. We need this to check now that
612 // we support aggregates as access types.
613 if (NewFormat) {
614 // TBAAStructTypeNode BaseAccessType(BaseTag.getAccessType());
615 TBAAStructTypeNode FieldType(SubobjectTag.getBaseType());
616 if (hasField(BaseType, FieldType)) {
617 if (GenericTag)
618 *GenericTag = createAccessTag(AccessType: CommonType);
619 MayAlias = true;
620 return true;
621 }
622 }
623
624 return false;
625}
626
627/// matchTags - Return true if the given couple of accesses are allowed to
628/// overlap. If \arg GenericTag is not null, then on return it points to the
629/// most generic access descriptor for the given two.
630static bool matchAccessTags(const MDNode *A, const MDNode *B,
631 const MDNode **GenericTag) {
632 if (A == B) {
633 if (GenericTag)
634 *GenericTag = A;
635 return true;
636 }
637
638 // Accesses with no TBAA information may alias with any other accesses.
639 if (!A || !B) {
640 if (GenericTag)
641 *GenericTag = nullptr;
642 return true;
643 }
644
645 TBAAStructTagNode TagA(A), TagB(B);
646 const MDNode *CommonType = getLeastCommonType(A: TagA.getAccessType(),
647 B: TagB.getAccessType());
648
649 // If the final access types have different roots, they're part of different
650 // potentially unrelated type systems, so we must be conservative.
651 if (!CommonType) {
652 if (GenericTag)
653 *GenericTag = nullptr;
654 return true;
655 }
656
657 // If one of the accessed objects may be a subobject of the other, then such
658 // accesses may alias.
659 bool MayAlias;
660 if (mayBeAccessToSubobjectOf(/* BaseTag= */ TagA, /* SubobjectTag= */ TagB,
661 CommonType, GenericTag, MayAlias) ||
662 mayBeAccessToSubobjectOf(/* BaseTag= */ TagB, /* SubobjectTag= */ TagA,
663 CommonType, GenericTag, MayAlias))
664 return MayAlias;
665
666 // Otherwise, we've proved there's no alias.
667 if (GenericTag)
668 *GenericTag = createAccessTag(AccessType: CommonType);
669 return false;
670}
671
672/// Aliases - Test whether the access represented by tag A may alias the
673/// access represented by tag B.
674bool TypeBasedAAResult::Aliases(const MDNode *A, const MDNode *B) const {
675 return matchAccessTags(A, B);
676}
677
678TypeBasedAAResult::TypeBasedAAResult(const LLVMContext &C,
679 bool UsingTypeSanitizer)
680 : Opts(getAnalysisOptions(C)), UsingTypeSanitizer(UsingTypeSanitizer) {}
681
682bool TypeBasedAAResult::shouldUseTBAA() const {
683 return Opts.enable_tbaa && !UsingTypeSanitizer;
684}
685
686AnalysisKey TypeBasedAA::Key;
687
688TypeBasedAAResult TypeBasedAA::run(Function &F, FunctionAnalysisManager &AM) {
689 return TypeBasedAAResult(F.getContext(),
690 F.hasFnAttribute(Kind: Attribute::SanitizeType));
691}
692
693char TypeBasedAAWrapperPass::ID = 0;
694INITIALIZE_PASS(TypeBasedAAWrapperPass, "tbaa", "Type-Based Alias Analysis",
695 false, true)
696
697ImmutablePass *llvm::createTypeBasedAAWrapperPass() {
698 return new TypeBasedAAWrapperPass();
699}
700
701TypeBasedAAWrapperPass::TypeBasedAAWrapperPass() : ImmutablePass(ID) {}
702
703bool TypeBasedAAWrapperPass::doInitialization(Module &M) {
704 Result.reset(
705 p: new TypeBasedAAResult(M.getContext(), /*UsingTypeSanitizer=*/false));
706 return false;
707}
708
709bool TypeBasedAAWrapperPass::doFinalization(Module &M) {
710 Result.reset();
711 return false;
712}
713
714void TypeBasedAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
715 AU.setPreservesAll();
716}
717
718MDNode *AAMDNodes::shiftTBAA(MDNode *MD, size_t Offset) {
719 // Fast path if there's no offset
720 if (Offset == 0)
721 return MD;
722
723 // The correct behavior here is to add the offset into the TBAA
724 // struct node offset. The base type, however may not have defined
725 // a type at this additional offset, resulting in errors. Since
726 // this method is only used within a given load/store access
727 // the offset provided is only used to subdivide the previous load
728 // maintaining the validity of the previous TBAA.
729 //
730 // This, however, should be revisited in the future.
731 return MD;
732}
733
734// Read a !tbaa.struct field entry (an offset or a size) as a 64-bit value.
735// Returns std::nullopt if it does not fit in 64 bits.
736static std::optional<uint64_t> getTBAAStructFieldAsInt64(const MDOperand &Op) {
737 return mdconst::extract<ConstantInt>(MD: Op)->getValue().tryZExtValue();
738}
739
740static bool isScalarAccessTag(const MDNode *Tag) {
741 TBAAStructTagNode T(Tag);
742 return T.getAccessType() == T.getBaseType();
743}
744
745MDNode *AAMDNodes::shiftTBAAStruct(MDNode *MD, size_t Offset) {
746 // Fast path if there's no offset
747 if (Offset == 0)
748 return MD;
749 SmallVector<Metadata *, 3> Sub;
750 for (size_t i = 0, size = MD->getNumOperands(); i < size; i += 3) {
751 ConstantInt *InnerOffset = mdconst::extract<ConstantInt>(MD: MD->getOperand(I: i));
752 ConstantInt *InnerSize =
753 mdconst::extract<ConstantInt>(MD: MD->getOperand(I: i + 1));
754 // Don't include any triples that aren't in bounds
755 if (InnerOffset->getZExtValue() + InnerSize->getZExtValue() <= Offset)
756 continue;
757
758 uint64_t NewSize = InnerSize->getZExtValue();
759 uint64_t NewOffset = InnerOffset->getZExtValue() - Offset;
760 if (InnerOffset->getZExtValue() < Offset) {
761 NewOffset = 0;
762 NewSize -= Offset - InnerOffset->getZExtValue();
763 }
764
765 // Shift the offset of the triple
766 Sub.push_back(Elt: ConstantAsMetadata::get(
767 C: ConstantInt::get(Ty: InnerOffset->getType(), V: NewOffset)));
768 Sub.push_back(Elt: ConstantAsMetadata::get(
769 C: ConstantInt::get(Ty: InnerSize->getType(), V: NewSize)));
770 Sub.push_back(Elt: MD->getOperand(I: i + 2));
771 }
772 return MDNode::get(Context&: MD->getContext(), MDs: Sub);
773}
774
775MDNode *AAMDNodes::extendToTBAA(MDNode *MD, std::optional<uint64_t> Len) {
776 // Fast path if 0-length
777 if (Len == 0)
778 return nullptr;
779
780 TBAAStructTagNode Tag(MD);
781
782 // Only new format TBAA has a size
783 if (!Tag.isNewFormat())
784 return MD;
785
786 // If unknown size, drop the TBAA.
787 if (!Len)
788 return nullptr;
789
790 // Otherwise, create TBAA with the new Len
791 ArrayRef<MDOperand> MDOperands = MD->operands();
792 SmallVector<Metadata *, 4> NextNodes(MDOperands);
793 ConstantInt *PreviousSize = mdconst::extract<ConstantInt>(MD&: NextNodes[3]);
794
795 // Don't create a new MDNode if it is the same length.
796 if (PreviousSize->equalsInt(V: *Len))
797 return MD;
798
799 NextNodes[3] =
800 ConstantAsMetadata::get(C: ConstantInt::get(Ty: PreviousSize->getType(), V: *Len));
801 return MDNode::get(Context&: MD->getContext(), MDs: NextNodes);
802}
803
804AAMDNodes AAMDNodes::extendTo(Value *Size) const {
805 if (auto *CI = dyn_cast<ConstantInt>(Val: Size))
806 return extendTo(Len: CI->getValue().tryZExtValue());
807 return extendTo(Len: std::nullopt);
808}
809
810AAMDNodes AAMDNodes::adjustForAccess(unsigned AccessSize) {
811 AAMDNodes New = *this;
812 MDNode *M = New.TBAAStruct;
813
814 // The access may cover several !tbaa.struct fields (e.g. a {int, int} copy
815 // widened to an i64 load/store). If those fields share a single tag and tile
816 // [0, AccessSize) with no gaps, that tag still describes the whole access.
817 New.TBAAStruct = nullptr;
818 if (New.TBAA || !M)
819 return New;
820 MDNode *CommonTag = nullptr;
821 uint64_t Offset = 0;
822 for (size_t I = 0, E = M->getNumOperands(); I + 2 < E && Offset < AccessSize;
823 I += 3) {
824 std::optional<uint64_t> FieldOffset =
825 getTBAAStructFieldAsInt64(Op: M->getOperand(I));
826 std::optional<uint64_t> FieldSize =
827 getTBAAStructFieldAsInt64(Op: M->getOperand(I: I + 1));
828 MDNode *FieldTag = dyn_cast_or_null<MDNode>(Val: M->getOperand(I: I + 2));
829 if (!FieldOffset || !FieldSize || !FieldTag ||
830 !isScalarAccessTag(Tag: FieldTag) || *FieldOffset != Offset ||
831 (CommonTag && FieldTag != CommonTag))
832 break;
833 CommonTag = FieldTag;
834 Offset += *FieldSize;
835 }
836 if (Offset == AccessSize)
837 New.TBAA = CommonTag;
838 return New;
839}
840
841AAMDNodes AAMDNodes::adjustForAccess(size_t Offset, Type *AccessTy,
842 const DataLayout &DL) {
843 AAMDNodes New = shift(Offset);
844 if (!DL.typeSizeEqualsStoreSize(Ty: AccessTy))
845 return New;
846 TypeSize Size = DL.getTypeStoreSize(Ty: AccessTy);
847 if (Size.isScalable())
848 return New;
849
850 return New.adjustForAccess(AccessSize: Size.getKnownMinValue());
851}
852
853AAMDNodes AAMDNodes::adjustForAccess(size_t Offset, unsigned AccessSize) {
854 AAMDNodes New = shift(Offset);
855 return New.adjustForAccess(AccessSize);
856}
857