LLVM 24.0.0git
TypeBasedAliasAnalysis.cpp
Go to the documentation of this file.
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
103#include "llvm/ADT/SetVector.h"
106#include "llvm/IR/Constants.h"
107#include "llvm/IR/DataLayout.h"
108#include "llvm/IR/DerivedTypes.h"
109#include "llvm/IR/InstrTypes.h"
110#include "llvm/IR/LLVMContext.h"
111#include "llvm/IR/Metadata.h"
112#include "llvm/IR/Module.h"
114#include "llvm/Pass.h"
115#include "llvm/Support/Casting.h"
118#include <cassert>
119#include <cstdint>
120
121using namespace llvm;
122
123// A handy option for disabling TBAA functionality. The same effect can also be
124// achieved by stripping the !tbaa tags from IR, but this option is sometimes
125// more convenient.
126static cl::opt<bool> EnableTBAA("enable-tbaa", cl::init(true), cl::Hidden);
127
128namespace {
129
130/// isNewFormatTypeNode - Return true iff the given type node is in the new
131/// size-aware format.
132static bool isNewFormatTypeNode(const MDNode *N) {
133 if (N->getNumOperands() < 3)
134 return false;
135 // In the old format the first operand is a string.
136 if (!isa<MDNode>(N->getOperand(0)))
137 return false;
138 return true;
139}
140
141/// This is a simple wrapper around an MDNode which provides a higher-level
142/// interface by hiding the details of how alias analysis information is encoded
143/// in its operands.
144template<typename MDNodeTy>
145class TBAANodeImpl {
146 MDNodeTy *Node = nullptr;
147
148public:
149 TBAANodeImpl() = default;
150 explicit TBAANodeImpl(MDNodeTy *N) : Node(N) {}
151
152 /// getNode - Get the MDNode for this TBAANode.
153 MDNodeTy *getNode() const { return Node; }
154
155 /// isNewFormat - Return true iff the wrapped type node is in the new
156 /// size-aware format.
157 bool isNewFormat() const { return isNewFormatTypeNode(Node); }
158
159 /// getParent - Get this TBAANode's Alias tree parent.
160 TBAANodeImpl<MDNodeTy> getParent() const {
161 if (isNewFormat())
162 return TBAANodeImpl(cast<MDNodeTy>(Node->getOperand(0)));
163
164 if (Node->getNumOperands() < 2)
165 return TBAANodeImpl<MDNodeTy>();
166 MDNodeTy *P = dyn_cast_or_null<MDNodeTy>(Node->getOperand(1));
167 if (!P)
168 return TBAANodeImpl<MDNodeTy>();
169 // Ok, this node has a valid parent. Return it.
170 return TBAANodeImpl<MDNodeTy>(P);
171 }
172
173 /// Test if this TBAANode represents a type for objects which are
174 /// not modified (by any means) in the context where this
175 /// AliasAnalysis is relevant.
176 bool isTypeImmutable() const {
177 if (Node->getNumOperands() < 3)
178 return false;
179 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(2));
180 if (!CI)
181 return false;
182 return CI->getValue()[0];
183 }
184};
185
186/// \name Specializations of \c TBAANodeImpl for const and non const qualified
187/// \c MDNode.
188/// @{
189using TBAANode = TBAANodeImpl<const MDNode>;
190using MutableTBAANode = TBAANodeImpl<MDNode>;
191/// @}
192
193/// This is a simple wrapper around an MDNode which provides a
194/// higher-level interface by hiding the details of how alias analysis
195/// information is encoded in its operands.
196template<typename MDNodeTy>
197class TBAAStructTagNodeImpl {
198 /// This node should be created with createTBAAAccessTag().
199 MDNodeTy *Node;
200
201public:
202 explicit TBAAStructTagNodeImpl(MDNodeTy *N) : Node(N) {}
203
204 /// Get the MDNode for this TBAAStructTagNode.
205 MDNodeTy *getNode() const { return Node; }
206
207 /// isNewFormat - Return true iff the wrapped access tag is in the new
208 /// size-aware format.
209 bool isNewFormat() const {
210 if (Node->getNumOperands() < 4)
211 return false;
212 if (MDNodeTy *AccessType = getAccessType())
213 if (!TBAANodeImpl<MDNodeTy>(AccessType).isNewFormat())
214 return false;
215 return true;
216 }
217
218 MDNodeTy *getBaseType() const {
219 return dyn_cast_or_null<MDNode>(Node->getOperand(0));
220 }
221
222 MDNodeTy *getAccessType() const {
223 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
224 }
225
226 uint64_t getOffset() const {
227 return mdconst::extract<ConstantInt>(Node->getOperand(2))->getZExtValue();
228 }
229
230 uint64_t getSize() const {
231 if (!isNewFormat())
232 return UINT64_MAX;
233 return mdconst::extract<ConstantInt>(Node->getOperand(3))->getZExtValue();
234 }
235
236 /// Test if this TBAAStructTagNode represents a type for objects
237 /// which are not modified (by any means) in the context where this
238 /// AliasAnalysis is relevant.
239 bool isTypeImmutable() const {
240 unsigned OpNo = isNewFormat() ? 4 : 3;
241 if (Node->getNumOperands() < OpNo + 1)
242 return false;
243 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(OpNo));
244 if (!CI)
245 return false;
246 return CI->getValue()[0];
247 }
248};
249
250/// \name Specializations of \c TBAAStructTagNodeImpl for const and non const
251/// qualified \c MDNods.
252/// @{
253using TBAAStructTagNode = TBAAStructTagNodeImpl<const MDNode>;
254using MutableTBAAStructTagNode = TBAAStructTagNodeImpl<MDNode>;
255/// @}
256
257/// This is a simple wrapper around an MDNode which provides a
258/// higher-level interface by hiding the details of how alias analysis
259/// information is encoded in its operands.
260class TBAAStructTypeNode {
261 /// This node should be created with createTBAATypeNode().
262 const MDNode *Node = nullptr;
263
264public:
265 TBAAStructTypeNode() = default;
266 explicit TBAAStructTypeNode(const MDNode *N) : Node(N) {}
267
268 /// Get the MDNode for this TBAAStructTypeNode.
269 const MDNode *getNode() const { return Node; }
270
271 /// isNewFormat - Return true iff the wrapped type node is in the new
272 /// size-aware format.
273 bool isNewFormat() const { return isNewFormatTypeNode(Node); }
274
275 bool operator==(const TBAAStructTypeNode &Other) const {
276 return getNode() == Other.getNode();
277 }
278
279 /// getId - Return type identifier.
280 Metadata *getId() const {
281 return Node->getOperand(isNewFormat() ? 2 : 0);
282 }
283
284 unsigned getNumFields() const {
285 unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
286 unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
287 return (getNode()->getNumOperands() - FirstFieldOpNo) / NumOpsPerField;
288 }
289
290 TBAAStructTypeNode getFieldType(unsigned FieldIndex) const {
291 unsigned FirstFieldOpNo = isNewFormat() ? 3 : 1;
292 unsigned NumOpsPerField = isNewFormat() ? 3 : 2;
293 unsigned OpIndex = FirstFieldOpNo + FieldIndex * NumOpsPerField;
294 auto *TypeNode = cast<MDNode>(getNode()->getOperand(OpIndex));
295 return TBAAStructTypeNode(TypeNode);
296 }
297
298 /// Get this TBAAStructTypeNode's field in the type DAG with
299 /// given offset. Update the offset to be relative to the field type.
300 TBAAStructTypeNode getField(uint64_t &Offset) const {
301 bool NewFormat = isNewFormat();
302 const ArrayRef<MDOperand> Operands = Node->operands();
303 const unsigned NumOperands = Operands.size();
304
305 if (NewFormat) {
306 // New-format root and scalar type nodes have no fields.
307 if (NumOperands < 6)
308 return TBAAStructTypeNode();
309 } else {
310 // Parent can be omitted for the root node.
311 if (NumOperands < 2)
312 return TBAAStructTypeNode();
313
314 // Fast path for a scalar type node and a struct type node with a single
315 // field.
316 if (NumOperands <= 3) {
317 uint64_t Cur =
318 NumOperands == 2
319 ? 0
320 : mdconst::extract<ConstantInt>(Operands[2])->getZExtValue();
321 Offset -= Cur;
322 MDNode *P = dyn_cast_or_null<MDNode>(Operands[1]);
323 if (!P)
324 return TBAAStructTypeNode();
325 return TBAAStructTypeNode(P);
326 }
327 }
328
329 // Assume the offsets are in order. We return the previous field if
330 // the current offset is bigger than the given offset.
331 unsigned FirstFieldOpNo = NewFormat ? 3 : 1;
332 unsigned NumOpsPerField = NewFormat ? 3 : 2;
333 unsigned TheIdx = 0;
334
335 for (unsigned Idx = FirstFieldOpNo; Idx < NumOperands;
336 Idx += NumOpsPerField) {
337 uint64_t Cur =
338 mdconst::extract<ConstantInt>(Operands[Idx + 1])->getZExtValue();
339 if (Cur > Offset) {
340 assert(Idx >= FirstFieldOpNo + NumOpsPerField &&
341 "TBAAStructTypeNode::getField should have an offset match!");
342 TheIdx = Idx - NumOpsPerField;
343 break;
344 }
345 }
346 // Move along the last field.
347 if (TheIdx == 0)
348 TheIdx = NumOperands - NumOpsPerField;
349 uint64_t Cur =
350 mdconst::extract<ConstantInt>(Operands[TheIdx + 1])->getZExtValue();
351 Offset -= Cur;
352 MDNode *P = dyn_cast_or_null<MDNode>(Operands[TheIdx]);
353 if (!P)
354 return TBAAStructTypeNode();
355 return TBAAStructTypeNode(P);
356 }
357};
358
359} // end anonymous namespace
360
362 const MemoryLocation &LocB,
363 AAQueryInfo &AAQI, const Instruction *) {
364 if (!shouldUseTBAA())
366
367 if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA))
369
370 // Otherwise return a definitive result.
372}
373
375 const Instruction *CtxI) {
376 if (!shouldUseTBAA())
378
379 const auto *N = Loc.AATags.TBAA;
380 if (!N)
382
383 // There cannot be any alias with errno if TBAA proves the given memory
384 // location does not alias errno.
385 const auto *ErrnoTBAAMD =
386 CtxI->getModule()->getNamedMetadata("llvm.errno.tbaa");
387 if (!ErrnoTBAAMD || any_of(ErrnoTBAAMD->operands(), [&](const auto *Node) {
388 return Aliases(N, Node);
389 }))
392}
393
395 AAQueryInfo &AAQI,
396 bool IgnoreLocals) {
397 if (!shouldUseTBAA())
398 return ModRefInfo::ModRef;
399
400 const MDNode *M = Loc.AATags.TBAA;
401 if (!M)
402 return ModRefInfo::ModRef;
403
404 // If this is an "immutable" type, we can assume the pointer is pointing
405 // to constant memory.
406 if (TBAAStructTagNode(M).isTypeImmutable())
408
409 return ModRefInfo::ModRef;
410}
411
413 AAQueryInfo &AAQI) {
414 if (!shouldUseTBAA())
415 return MemoryEffects::unknown();
416
417 // If this is an "immutable" type, the access is not observable.
418 if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
419 if (TBAAStructTagNode(M).isTypeImmutable())
420 return MemoryEffects::none();
421
422 return MemoryEffects::unknown();
423}
424
426 // Functions don't have metadata.
427 return MemoryEffects::unknown();
428}
429
431 const MemoryLocation &Loc,
432 AAQueryInfo &AAQI) {
433 if (!shouldUseTBAA())
434 return ModRefInfo::ModRef;
435
436 if (const MDNode *L = Loc.AATags.TBAA)
437 if (const MDNode *M = Call->getMetadata(LLVMContext::MD_tbaa))
438 if (!Aliases(L, M))
440
441 return ModRefInfo::ModRef;
442}
443
445 const CallBase *Call2,
446 AAQueryInfo &AAQI) {
447 if (!shouldUseTBAA())
448 return ModRefInfo::ModRef;
449
450 if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
451 if (const MDNode *M2 = Call2->getMetadata(LLVMContext::MD_tbaa))
452 if (!Aliases(M1, M2))
454
455 return ModRefInfo::ModRef;
456}
457
459 // For struct-path aware TBAA, we use the access type of the tag.
460 TBAAStructTagNode Tag(this);
461 TBAAStructTypeNode AccessType(Tag.getAccessType());
462 if(auto *Id = dyn_cast<MDString>(AccessType.getId()))
463 if (Id->getString() == "vtable pointer")
464 return true;
465 return false;
466}
467
468static bool matchAccessTags(const MDNode *A, const MDNode *B,
469 const MDNode **GenericTag = nullptr);
470
472 const MDNode *GenericTag;
473 matchAccessTags(A, B, &GenericTag);
474 return const_cast<MDNode*>(GenericTag);
475}
476
477static const MDNode *getLeastCommonType(const MDNode *A, const MDNode *B) {
478 if (!A || !B)
479 return nullptr;
480
481 if (A == B)
482 return A;
483
485 TBAANode TA(A);
486 while (TA.getNode()) {
487 if (!PathA.insert(TA.getNode()))
488 report_fatal_error("Cycle found in TBAA metadata.");
489 TA = TA.getParent();
490 }
491
493 TBAANode TB(B);
494 while (TB.getNode()) {
495 if (!PathB.insert(TB.getNode()))
496 report_fatal_error("Cycle found in TBAA metadata.");
497 TB = TB.getParent();
498 }
499
500 int IA = PathA.size() - 1;
501 int IB = PathB.size() - 1;
502
503 const MDNode *Ret = nullptr;
504 while (IA >= 0 && IB >= 0) {
505 if (PathA[IA] == PathB[IB])
506 Ret = PathA[IA];
507 else
508 break;
509 --IA;
510 --IB;
511 }
512
513 return Ret;
514}
515
517 AAMDNodes Result;
518 Result.TBAA = MDNode::getMostGenericTBAA(TBAA, Other.TBAA);
519 Result.TBAAStruct = nullptr;
520 Result.Scope = MDNode::getMostGenericAliasScope(Scope, Other.Scope);
521 Result.NoAlias = MDNode::intersect(NoAlias, Other.NoAlias);
522 Result.NoAliasAddrSpace = MDNode::getMostGenericNoaliasAddrspace(
523 NoAliasAddrSpace, Other.NoAliasAddrSpace);
524 return Result;
525}
526
528 AAMDNodes Result;
529 Result.TBAA = Result.TBAAStruct = nullptr;
530 Result.Scope = MDNode::getMostGenericAliasScope(Scope, Other.Scope);
531 Result.NoAlias = MDNode::intersect(NoAlias, Other.NoAlias);
532 Result.NoAliasAddrSpace = MDNode::getMostGenericNoaliasAddrspace(
533 NoAliasAddrSpace, Other.NoAliasAddrSpace);
534 return Result;
535}
536
537static const MDNode *createAccessTag(const MDNode *AccessType) {
538 // If there is no access type or the access type is the root node, then
539 // we don't have any useful access tag to return.
540 if (!AccessType || AccessType->getNumOperands() < 2)
541 return nullptr;
542
543 Type *Int64 = IntegerType::get(AccessType->getContext(), 64);
544 auto *OffsetNode = ConstantAsMetadata::get(ConstantInt::get(Int64, 0));
545
546 if (TBAAStructTypeNode(AccessType).isNewFormat()) {
547 // TODO: Take access ranges into account when matching access tags and
548 // fix this code to generate actual access sizes for generic tags.
549 uint64_t AccessSize = UINT64_MAX;
550 auto *SizeNode =
551 ConstantAsMetadata::get(ConstantInt::get(Int64, AccessSize));
552 Metadata *Ops[] = {const_cast<MDNode*>(AccessType),
553 const_cast<MDNode*>(AccessType),
554 OffsetNode, SizeNode};
555 return MDNode::get(AccessType->getContext(), Ops);
556 }
557
558 Metadata *Ops[] = {const_cast<MDNode*>(AccessType),
559 const_cast<MDNode*>(AccessType),
560 OffsetNode};
561 return MDNode::get(AccessType->getContext(), Ops);
562}
563
564static bool hasField(TBAAStructTypeNode BaseType,
565 TBAAStructTypeNode FieldType) {
566 for (unsigned I = 0, E = BaseType.getNumFields(); I != E; ++I) {
567 TBAAStructTypeNode T = BaseType.getFieldType(I);
568 if (T == FieldType || hasField(T, FieldType))
569 return true;
570 }
571 return false;
572}
573
574/// Return true if for two given accesses, one of the accessed objects may be a
575/// subobject of the other. The \p BaseTag and \p SubobjectTag parameters
576/// describe the accesses to the base object and the subobject respectively.
577/// \p CommonType must be the metadata node describing the common type of the
578/// accessed objects. On return, \p MayAlias is set to true iff these accesses
579/// may alias and \p Generic, if not null, points to the most generic access
580/// tag for the given two.
581static bool mayBeAccessToSubobjectOf(TBAAStructTagNode BaseTag,
582 TBAAStructTagNode SubobjectTag,
583 const MDNode *CommonType,
584 const MDNode **GenericTag,
585 bool &MayAlias) {
586 // If the base object is of the least common type, then this may be an access
587 // to its subobject.
588 if (BaseTag.getAccessType() == BaseTag.getBaseType() &&
589 BaseTag.getAccessType() == CommonType) {
590 if (GenericTag)
591 *GenericTag = createAccessTag(CommonType);
592 MayAlias = true;
593 return true;
594 }
595
596 // If the access to the base object is through a field of the subobject's
597 // type, then this may be an access to that field. To check for that we start
598 // from the base type, follow the edge with the correct offset in the type DAG
599 // and adjust the offset until we reach the field type or until we reach the
600 // access type.
601 bool NewFormat = BaseTag.isNewFormat();
602 TBAAStructTypeNode BaseType(BaseTag.getBaseType());
603 uint64_t OffsetInBase = BaseTag.getOffset();
604
605 for (;;) {
606 // In the old format there is no distinction between fields and parent
607 // types, so in this case we consider all nodes up to the root.
608 if (!BaseType.getNode()) {
609 assert(!NewFormat && "Did not see access type in access path!");
610 break;
611 }
612
613 if (BaseType.getNode() == SubobjectTag.getBaseType()) {
614 MayAlias = OffsetInBase == SubobjectTag.getOffset() ||
615 BaseType.getNode() == BaseTag.getAccessType() ||
616 SubobjectTag.getBaseType() == SubobjectTag.getAccessType();
617 if (GenericTag) {
618 *GenericTag =
619 MayAlias ? SubobjectTag.getNode() : createAccessTag(CommonType);
620 }
621 return true;
622 }
623
624 // With new-format nodes we stop at the access type.
625 if (NewFormat && BaseType.getNode() == BaseTag.getAccessType())
626 break;
627
628 // Follow the edge with the correct offset. Offset will be adjusted to
629 // be relative to the field type.
630 BaseType = BaseType.getField(OffsetInBase);
631 }
632
633 // If the base object has a direct or indirect field of the subobject's type,
634 // then this may be an access to that field. We need this to check now that
635 // we support aggregates as access types.
636 if (NewFormat) {
637 // TBAAStructTypeNode BaseAccessType(BaseTag.getAccessType());
638 TBAAStructTypeNode FieldType(SubobjectTag.getBaseType());
639 if (hasField(BaseType, FieldType)) {
640 if (GenericTag)
641 *GenericTag = createAccessTag(CommonType);
642 MayAlias = true;
643 return true;
644 }
645 }
646
647 return false;
648}
649
650/// matchTags - Return true if the given couple of accesses are allowed to
651/// overlap. If \arg GenericTag is not null, then on return it points to the
652/// most generic access descriptor for the given two.
653static bool matchAccessTags(const MDNode *A, const MDNode *B,
654 const MDNode **GenericTag) {
655 if (A == B) {
656 if (GenericTag)
657 *GenericTag = A;
658 return true;
659 }
660
661 // Accesses with no TBAA information may alias with any other accesses.
662 if (!A || !B) {
663 if (GenericTag)
664 *GenericTag = nullptr;
665 return true;
666 }
667
668 TBAAStructTagNode TagA(A), TagB(B);
669 const MDNode *CommonType = getLeastCommonType(TagA.getAccessType(),
670 TagB.getAccessType());
671
672 // If the final access types have different roots, they're part of different
673 // potentially unrelated type systems, so we must be conservative.
674 if (!CommonType) {
675 if (GenericTag)
676 *GenericTag = nullptr;
677 return true;
678 }
679
680 // If one of the accessed objects may be a subobject of the other, then such
681 // accesses may alias.
682 bool MayAlias;
683 if (mayBeAccessToSubobjectOf(/* BaseTag= */ TagA, /* SubobjectTag= */ TagB,
684 CommonType, GenericTag, MayAlias) ||
685 mayBeAccessToSubobjectOf(/* BaseTag= */ TagB, /* SubobjectTag= */ TagA,
686 CommonType, GenericTag, MayAlias))
687 return MayAlias;
688
689 // Otherwise, we've proved there's no alias.
690 if (GenericTag)
691 *GenericTag = createAccessTag(CommonType);
692 return false;
693}
694
695/// Aliases - Test whether the access represented by tag A may alias the
696/// access represented by tag B.
697bool TypeBasedAAResult::Aliases(const MDNode *A, const MDNode *B) const {
698 return matchAccessTags(A, B);
699}
700
701bool TypeBasedAAResult::shouldUseTBAA() const {
702 return EnableTBAA && !UsingTypeSanitizer;
703}
704
705AnalysisKey TypeBasedAA::Key;
706
708 return TypeBasedAAResult(F.hasFnAttribute(Attribute::SanitizeType));
709}
710
712INITIALIZE_PASS(TypeBasedAAWrapperPass, "tbaa", "Type-Based Alias Analysis",
713 false, true)
714
716 return new TypeBasedAAWrapperPass();
717}
718
720
722 Result.reset(new TypeBasedAAResult(/*UsingTypeSanitizer=*/false));
723 return false;
724}
725
727 Result.reset();
728 return false;
729}
730
734
736 // Fast path if there's no offset
737 if (Offset == 0)
738 return MD;
739
740 // The correct behavior here is to add the offset into the TBAA
741 // struct node offset. The base type, however may not have defined
742 // a type at this additional offset, resulting in errors. Since
743 // this method is only used within a given load/store access
744 // the offset provided is only used to subdivide the previous load
745 // maintaining the validity of the previous TBAA.
746 //
747 // This, however, should be revisited in the future.
748 return MD;
749}
750
752 // Fast path if there's no offset
753 if (Offset == 0)
754 return MD;
756 for (size_t i = 0, size = MD->getNumOperands(); i < size; i += 3) {
758 ConstantInt *InnerSize =
760 // Don't include any triples that aren't in bounds
761 if (InnerOffset->getZExtValue() + InnerSize->getZExtValue() <= Offset)
762 continue;
763
764 uint64_t NewSize = InnerSize->getZExtValue();
765 uint64_t NewOffset = InnerOffset->getZExtValue() - Offset;
766 if (InnerOffset->getZExtValue() < Offset) {
767 NewOffset = 0;
768 NewSize -= Offset - InnerOffset->getZExtValue();
769 }
770
771 // Shift the offset of the triple
773 ConstantInt::get(InnerOffset->getType(), NewOffset)));
775 ConstantInt::get(InnerSize->getType(), NewSize)));
776 Sub.push_back(MD->getOperand(i + 2));
777 }
778 return MDNode::get(MD->getContext(), Sub);
779}
780
782 // Fast path if 0-length
783 if (Len == 0)
784 return nullptr;
785
786 TBAAStructTagNode Tag(MD);
787
788 // Only new format TBAA has a size
789 if (!Tag.isNewFormat())
790 return MD;
791
792 // If unknown size, drop the TBAA.
793 if (Len == -1)
794 return nullptr;
795
796 // Otherwise, create TBAA with the new Len
797 ArrayRef<MDOperand> MDOperands = MD->operands();
798 SmallVector<Metadata *, 4> NextNodes(MDOperands);
799 ConstantInt *PreviousSize = mdconst::extract<ConstantInt>(NextNodes[3]);
800
801 // Don't create a new MDNode if it is the same length.
802 if (PreviousSize->equalsInt(Len))
803 return MD;
804
805 NextNodes[3] =
806 ConstantAsMetadata::get(ConstantInt::get(PreviousSize->getType(), Len));
807 return MDNode::get(MD->getContext(), NextNodes);
808}
809
811 AAMDNodes New = *this;
812 MDNode *M = New.TBAAStruct;
813 if (!New.TBAA && M && M->getNumOperands() >= 3 && M->getOperand(0) &&
814 mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
815 mdconst::extract<ConstantInt>(M->getOperand(0))->isZero() &&
816 M->getOperand(1) && mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
817 mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
818 AccessSize &&
819 M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
820 New.TBAA = cast<MDNode>(M->getOperand(2));
821
822 New.TBAAStruct = nullptr;
823 return New;
824}
825
827 const DataLayout &DL) {
828 AAMDNodes New = shift(Offset);
829 if (!DL.typeSizeEqualsStoreSize(AccessTy))
830 return New;
831 TypeSize Size = DL.getTypeStoreSize(AccessTy);
832 if (Size.isScalable())
833 return New;
834
835 return New.adjustForAccess(Size.getKnownMinValue());
836}
837
838AAMDNodes AAMDNodes::adjustForAccess(size_t Offset, unsigned AccessSize) {
839 AAMDNodes New = shift(Offset);
840 return New.adjustForAccess(AccessSize);
841}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static MemAccessTy getAccessType(const TargetTransformInfo &TTI, Instruction *Inst, Value *OperandVal)
Return the type of the memory being accessed.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
SI Fold Operands
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null,...
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
This file implements a set that has insertion order iteration characteristics.
static bool matchAccessTags(const MDNode *A, const MDNode *B, const MDNode **GenericTag=nullptr)
matchTags - Return true if the given couple of accesses are allowed to overlap.
static cl::opt< bool > EnableTBAA("enable-tbaa", cl::init(true), cl::Hidden)
static bool mayBeAccessToSubobjectOf(TBAAStructTagNode BaseTag, TBAAStructTagNode SubobjectTag, const MDNode *CommonType, const MDNode **GenericTag, bool &MayAlias)
Return true if for two given accesses, one of the accessed objects may be a subobject of the other.
static bool hasField(TBAAStructTypeNode BaseType, TBAAStructTypeNode FieldType)
static const MDNode * createAccessTag(const MDNode *AccessType)
static const MDNode * getLeastCommonType(const MDNode *A, const MDNode *B)
This is the interface for a metadata-based TBAA.
static unsigned getSize(unsigned Kind)
This class stores info we want to provide to or retain within an alias query.
The possible results of an alias query.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
This is the shared class of boolean and integer constants.
Definition Constants.h:87
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
bool equalsInt(uint64_t V) const
A helper method that can be used to determine if the constant contained within is equal to a constant...
Definition Constants.h:194
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition Pass.h:285
ImmutablePass(char &pid)
Definition Pass.h:287
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:348
Metadata node.
Definition Metadata.h:1069
static LLVM_ABI MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
LLVM_ABI bool isTBAAVtableAccess() const
Check whether MDNode is a vtable access.
static LLVM_ABI MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
static LLVM_ABI MDNode * getMostGenericNoaliasAddrspace(MDNode *A, MDNode *B)
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1424
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1432
LLVM_ABI MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2={})
Definition Metadata.cpp:667
static LLVM_ABI MDNode * intersect(MDNode *A, MDNode *B)
LLVMContext & getContext() const
Definition Metadata.h:1233
static MemoryEffectsBase none()
Definition ModRef.h:128
static MemoryEffectsBase unknown()
Definition ModRef.h:123
Representation for a specific memory location.
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
Root of the metadata hierarchy.
Definition Metadata.h:64
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
Definition Module.cpp:301
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A simple AA result that uses TBAA metadata to answer queries.
LLVM_ABI AliasResult aliasErrno(const MemoryLocation &Loc, const Instruction *CtxI)
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *CtxI)
LLVM_ABI ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI, bool IgnoreLocals)
LLVM_ABI ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc, AAQueryInfo &AAQI)
LLVM_ABI MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI)
Legacy wrapper pass to provide the TypeBasedAAResult object.
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
LLVM_ABI TypeBasedAAResult run(Function &F, FunctionAnalysisManager &AM)
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
CallInst * Call
#define UINT64_MAX
Definition DataTypes.h:77
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, bool > hasa(Y &&MD)
Check whether Metadata has a Value.
Definition Metadata.h:651
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:696
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
unsigned M1(unsigned Val)
Definition VE.h:377
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
@ ModRef
The access may reference and may modify the value stored in memory.
Definition ModRef.h:36
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
@ Other
Any other memory.
Definition ModRef.h:68
@ Sub
Subtraction of integers.
LLVM_ABI ImmutablePass * createTypeBasedAAWrapperPass()
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
#define N
LLVM_ABI AAMDNodes concat(const AAMDNodes &Other) const
Determine the best AAMDNodes after concatenating two different locations together.
static LLVM_ABI MDNode * shiftTBAAStruct(MDNode *M, size_t off)
MDNode * NoAliasAddrSpace
The tag specifying the noalias address spaces.
Definition Metadata.h:792
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition Metadata.h:786
static LLVM_ABI MDNode * extendToTBAA(MDNode *TBAA, ssize_t len)
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:780
AAMDNodes shift(size_t Offset) const
Create a new AAMDNode that describes this AAMDNode after applying a constant offset to the start of t...
Definition Metadata.h:822
LLVM_ABI AAMDNodes merge(const AAMDNodes &Other) const
Given two sets of AAMDNodes applying to potentially different locations, determine the best AAMDNodes...
MDNode * NoAlias
The tag specifying the noalias scope.
Definition Metadata.h:789
LLVM_ABI AAMDNodes adjustForAccess(unsigned AccessSize)
Create a new AAMDNode for accessing AccessSize bytes of this AAMDNode.
AAMDNodes()=default
static LLVM_ABI MDNode * shiftTBAA(MDNode *M, size_t off)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29