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