xref: /llvm-project/llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h (revision a487b792e2dabcec02c63d19e32958572a257408)
1 //===- TypeBasedAliasAnalysis.h - Type-Based Alias Analysis -----*- C++ -*-===//
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 /// \file
10 /// This is the interface for a metadata-based TBAA. See the source file for
11 /// details on the algorithm.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ANALYSIS_TYPEBASEDALIASANALYSIS_H
16 #define LLVM_ANALYSIS_TYPEBASEDALIASANALYSIS_H
17 
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/Pass.h"
21 #include <memory>
22 
23 namespace llvm {
24 
25 class CallBase;
26 class Function;
27 class MDNode;
28 class MemoryLocation;
29 
30 /// A simple AA result that uses TBAA metadata to answer queries.
31 class TypeBasedAAResult : public AAResultBase {
32   /// True if type sanitizer is enabled. When TypeSanitizer is used, don't use
33   /// TBAA information for alias analysis as  this might cause us to remove
34   /// memory accesses that we need to verify at runtime.
35   bool UsingTypeSanitizer;
36 
37 public:
38   TypeBasedAAResult(bool UsingTypeSanitizer)
39       : UsingTypeSanitizer(UsingTypeSanitizer) {}
40 
41   /// Handle invalidation events from the new pass manager.
42   ///
43   /// By definition, this result is stateless and so remains valid.
44   bool invalidate(Function &, const PreservedAnalyses &,
45                   FunctionAnalysisManager::Invalidator &) {
46     return false;
47   }
48 
49   AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
50                     AAQueryInfo &AAQI, const Instruction *CtxI);
51   ModRefInfo getModRefInfoMask(const MemoryLocation &Loc, AAQueryInfo &AAQI,
52                                bool IgnoreLocals);
53 
54   MemoryEffects getMemoryEffects(const CallBase *Call, AAQueryInfo &AAQI);
55   MemoryEffects getMemoryEffects(const Function *F);
56   ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc,
57                            AAQueryInfo &AAQI);
58   ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2,
59                            AAQueryInfo &AAQI);
60 
61 private:
62   bool Aliases(const MDNode *A, const MDNode *B) const;
63 
64   /// Returns true if TBAA metadata should be used, that is if TBAA is enabled
65   /// and type sanitizer is not used.
66   bool shouldUseTBAA() const;
67 };
68 
69 /// Analysis pass providing a never-invalidated alias analysis result.
70 class TypeBasedAA : public AnalysisInfoMixin<TypeBasedAA> {
71   friend AnalysisInfoMixin<TypeBasedAA>;
72 
73   static AnalysisKey Key;
74 
75 public:
76   using Result = TypeBasedAAResult;
77 
78   TypeBasedAAResult run(Function &F, FunctionAnalysisManager &AM);
79 };
80 
81 /// Legacy wrapper pass to provide the TypeBasedAAResult object.
82 class TypeBasedAAWrapperPass : public ImmutablePass {
83   std::unique_ptr<TypeBasedAAResult> Result;
84 
85 public:
86   static char ID;
87 
88   TypeBasedAAWrapperPass();
89 
90   TypeBasedAAResult &getResult() { return *Result; }
91   const TypeBasedAAResult &getResult() const { return *Result; }
92 
93   bool doInitialization(Module &M) override;
94   bool doFinalization(Module &M) override;
95   void getAnalysisUsage(AnalysisUsage &AU) const override;
96 };
97 
98 //===--------------------------------------------------------------------===//
99 //
100 // createTypeBasedAAWrapperPass - This pass implements metadata-based
101 // type-based alias analysis.
102 //
103 ImmutablePass *createTypeBasedAAWrapperPass();
104 
105 } // end namespace llvm
106 
107 #endif // LLVM_ANALYSIS_TYPEBASEDALIASANALYSIS_H
108