LLVM 23.0.0git
AliasAnalysisEvaluator.cpp
Go to the documentation of this file.
1//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
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
10#include "llvm/ADT/SetVector.h"
12#include "llvm/IR/DataLayout.h"
13#include "llvm/IR/Function.h"
16#include "llvm/IR/Module.h"
19using namespace llvm;
20
21static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
22
23static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
24static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
25static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden);
26static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
27
28static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
32
33static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden);
34
35static void PrintResults(AliasResult AR, bool P,
36 std::pair<const Value *, Type *> Loc1,
37 std::pair<const Value *, Type *> Loc2,
38 const Module *M) {
39 if (PrintAll || P) {
40 Type *Ty1 = Loc1.second, *Ty2 = Loc2.second;
41 unsigned AS1 = Loc1.first->getType()->getPointerAddressSpace();
42 unsigned AS2 = Loc2.first->getType()->getPointerAddressSpace();
43 std::string o1, o2;
44 {
45 raw_string_ostream os1(o1), os2(o2);
46 Loc1.first->printAsOperand(os1, false, M);
47 Loc2.first->printAsOperand(os2, false, M);
48 }
49
50 if (o2 < o1) {
51 std::swap(o1, o2);
52 std::swap(Ty1, Ty2);
53 std::swap(AS1, AS2);
54 // Change offset sign for the local AR, for printing only.
55 AR.swap();
56 }
57 errs() << " " << AR << ":\t";
58 Ty1->print(errs(), false, /* NoDetails */ true);
59 if (AS1 != 0)
60 errs() << " addrspace(" << AS1 << ")";
61 errs() << "* " << o1 << ", ";
62 Ty2->print(errs(), false, /* NoDetails */ true);
63 if (AS2 != 0)
64 errs() << " addrspace(" << AS2 << ")";
65 errs() << "* " << o2 << "\n";
66 }
67}
68
69static inline void PrintModRefResults(
70 const char *Msg, bool P, Instruction *I,
71 std::pair<const Value *, Type *> Loc, Module *M) {
72 if (PrintAll || P) {
73 errs() << " " << Msg << ": Ptr: ";
74 Loc.second->print(errs(), false, /* NoDetails */ true);
75 errs() << "* ";
76 Loc.first->printAsOperand(errs(), false, M);
77 errs() << "\t<->" << *I << '\n';
78 }
79}
80
81static inline void PrintModRefResults(const char *Msg, bool P,
82 Instruction *MemOpA, Instruction *MemOpB,
83 Module *M) {
84 if (PrintAll || P) {
85 errs() << " " << Msg << ": " << *MemOpA << " <-> " << *MemOpB << '\n';
86 }
87}
88
89static inline void PrintLoadStoreResults(AliasResult AR, bool P,
90 const Value *V1, const Value *V2,
91 const Module *M) {
92 if (PrintAll || P) {
93 errs() << " " << AR << ": " << *V1 << " <-> " << *V2 << '\n';
94 }
95}
96
101
102void AAEvaluator::runInternal(Function &F, AAResults &AA) {
103 const DataLayout &DL = F.getDataLayout();
104
105 ++FunctionCount;
106
109 SetVector<Value *> Loads;
110 SetVector<Value *> Stores;
111
112 for (Instruction &Inst : instructions(F)) {
113 if (auto *LI = dyn_cast<LoadInst>(&Inst)) {
114 Pointers.insert({LI->getPointerOperand(), LI->getType()});
115 Loads.insert(LI);
116 } else if (auto *SI = dyn_cast<StoreInst>(&Inst)) {
117 Pointers.insert({SI->getPointerOperand(),
118 SI->getValueOperand()->getType()});
119 Stores.insert(SI);
120 }
121
122 if (isa<CallBase>(Inst) || Inst.isAtomic())
123 OtherMemOps.insert(&Inst);
124 }
125
128 errs() << "Function: " << F.getName() << ": " << Pointers.size()
129 << " pointers, " << OtherMemOps.size() << " call sites\n";
130
131 // iterate over the worklist, and run the full (n^2)/2 disambiguations
132 for (auto I1 = Pointers.begin(), E = Pointers.end(); I1 != E; ++I1) {
133 LocationSize Size1 = LocationSize::precise(DL.getTypeStoreSize(I1->second));
134 for (auto I2 = Pointers.begin(); I2 != I1; ++I2) {
135 LocationSize Size2 =
136 LocationSize::precise(DL.getTypeStoreSize(I2->second));
137 AliasResult AR = AA.alias(I1->first, Size1, I2->first, Size2);
138 switch (AR) {
140 PrintResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
141 ++NoAliasCount;
142 break;
144 PrintResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
145 ++MayAliasCount;
146 break;
148 PrintResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
149 ++PartialAliasCount;
150 break;
152 PrintResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
153 ++MustAliasCount;
154 break;
155 }
156 }
157 }
158
159 if (EvalAAMD) {
160 // iterate over all pairs of load, store
161 for (Value *Load : Loads) {
162 for (Value *Store : Stores) {
163 AliasResult AR = AA.alias(MemoryLocation::get(cast<LoadInst>(Load)),
165 switch (AR) {
167 PrintLoadStoreResults(AR, PrintNoAlias, Load, Store, F.getParent());
168 ++NoAliasCount;
169 break;
171 PrintLoadStoreResults(AR, PrintMayAlias, Load, Store, F.getParent());
172 ++MayAliasCount;
173 break;
175 PrintLoadStoreResults(AR, PrintPartialAlias, Load, Store, F.getParent());
176 ++PartialAliasCount;
177 break;
179 PrintLoadStoreResults(AR, PrintMustAlias, Load, Store, F.getParent());
180 ++MustAliasCount;
181 break;
182 }
183 }
184 }
185
186 // iterate over all pairs of store, store
187 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
188 I1 != E; ++I1) {
189 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) {
190 AliasResult AR = AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
192 switch (AR) {
194 PrintLoadStoreResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
195 ++NoAliasCount;
196 break;
198 PrintLoadStoreResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
199 ++MayAliasCount;
200 break;
202 PrintLoadStoreResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
203 ++PartialAliasCount;
204 break;
206 PrintLoadStoreResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
207 ++MustAliasCount;
208 break;
209 }
210 }
211 }
212 }
213
214 // Mod/ref alias analysis: compare all pairs of mem ops and values
215 for (Instruction *MemOp : OtherMemOps) {
216 for (const auto &Pointer : Pointers) {
217 LocationSize Size =
218 LocationSize::precise(DL.getTypeStoreSize(Pointer.second));
219 switch (AA.getModRefInfo(MemOp, Pointer.first, Size)) {
221 PrintModRefResults("NoModRef", PrintNoModRef, MemOp, Pointer,
222 F.getParent());
223 ++NoModRefCount;
224 break;
225 case ModRefInfo::Mod:
226 PrintModRefResults("Just Mod", PrintMod, MemOp, Pointer, F.getParent());
227 ++ModCount;
228 break;
229 case ModRefInfo::Ref:
230 PrintModRefResults("Just Ref", PrintRef, MemOp, Pointer, F.getParent());
231 ++RefCount;
232 break;
234 PrintModRefResults("Both ModRef", PrintModRef, MemOp, Pointer,
235 F.getParent());
236 ++ModRefCount;
237 break;
238 }
239 }
240 }
241
242 // Mod/ref alias analysis: compare all pairs of mem ops
243 for (Instruction *MemOpA : OtherMemOps) {
244 for (Instruction *MemOpB : OtherMemOps) {
245 if (MemOpA == MemOpB)
246 continue;
247 switch (AA.getModRefInfo(MemOpA, MemOpB)) {
249 PrintModRefResults("NoModRef", PrintNoModRef, MemOpA, MemOpB,
250 F.getParent());
251 ++NoModRefCount;
252 break;
253 case ModRefInfo::Mod:
254 PrintModRefResults("Just Mod", PrintMod, MemOpA, MemOpB, F.getParent());
255 ++ModCount;
256 break;
257 case ModRefInfo::Ref:
258 PrintModRefResults("Just Ref", PrintRef, MemOpA, MemOpB, F.getParent());
259 ++RefCount;
260 break;
262 PrintModRefResults("Both ModRef", PrintModRef, MemOpA, MemOpB,
263 F.getParent());
264 ++ModRefCount;
265 break;
266 }
267 }
268 }
269}
270
271static void PrintPercent(int64_t Num, int64_t Sum) {
272 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
273 << "%)\n";
274}
275
277 if (FunctionCount == 0)
278 return;
279
280 int64_t AliasSum =
281 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
282 errs() << "===== Alias Analysis Evaluator Report =====\n";
283 if (AliasSum == 0) {
284 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
285 } else {
286 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
287 errs() << " " << NoAliasCount << " no alias responses ";
288 PrintPercent(NoAliasCount, AliasSum);
289 errs() << " " << MayAliasCount << " may alias responses ";
290 PrintPercent(MayAliasCount, AliasSum);
291 errs() << " " << PartialAliasCount << " partial alias responses ";
292 PrintPercent(PartialAliasCount, AliasSum);
293 errs() << " " << MustAliasCount << " must alias responses ";
294 PrintPercent(MustAliasCount, AliasSum);
295 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
296 << NoAliasCount * 100 / AliasSum << "%/"
297 << MayAliasCount * 100 / AliasSum << "%/"
298 << PartialAliasCount * 100 / AliasSum << "%/"
299 << MustAliasCount * 100 / AliasSum << "%\n";
300 }
301
302 // Display the summary for mod/ref analysis
303 int64_t ModRefSum = NoModRefCount + RefCount + ModCount + ModRefCount;
304 if (ModRefSum == 0) {
305 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
306 "mod/ref!\n";
307 } else {
308 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
309 errs() << " " << NoModRefCount << " no mod/ref responses ";
310 PrintPercent(NoModRefCount, ModRefSum);
311 errs() << " " << ModCount << " mod responses ";
312 PrintPercent(ModCount, ModRefSum);
313 errs() << " " << RefCount << " ref responses ";
314 PrintPercent(RefCount, ModRefSum);
315 errs() << " " << ModRefCount << " mod & ref responses ";
316 PrintPercent(ModRefCount, ModRefSum);
317 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
318 << NoModRefCount * 100 / ModRefSum << "%/"
319 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
320 << "%/" << ModRefCount * 100 / ModRefSum << "%\n";
321 }
322}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void PrintModRefResults(const char *Msg, bool P, Instruction *I, std::pair< const Value *, Type * > Loc, Module *M)
static cl::opt< bool > PrintModRef("print-modref", cl::ReallyHidden)
static void PrintLoadStoreResults(AliasResult AR, bool P, const Value *V1, const Value *V2, const Module *M)
static void PrintPercent(int64_t Num, int64_t Sum)
static cl::opt< bool > EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden)
static cl::opt< bool > PrintRef("print-ref", cl::ReallyHidden)
static cl::opt< bool > PrintNoAlias("print-no-aliases", cl::ReallyHidden)
static cl::opt< bool > PrintMayAlias("print-may-aliases", cl::ReallyHidden)
static cl::opt< bool > PrintMod("print-mod", cl::ReallyHidden)
static cl::opt< bool > PrintMustAlias("print-must-aliases", cl::ReallyHidden)
static cl::opt< bool > PrintAll("print-all-alias-modref-info", cl::ReallyHidden)
static cl::opt< bool > PrintNoModRef("print-no-modref", cl::ReallyHidden)
static cl::opt< bool > PrintPartialAlias("print-partial-aliases", cl::ReallyHidden)
This file implements a simple N^2 alias analysis accuracy evaluator.
static cl::opt< bool > PrintResults("print-debug-ata", cl::init(false), cl::Hidden)
Print the results of the analysis. Respects -filter-print-funcs.
Expand Atomic instructions
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
This file implements a set that has insertion order iteration characteristics.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
A manager for alias analyses.
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
The possible results of an alias query.
void swap(bool DoSwap=true)
Helper for processing AliasResult for swapped memory location pairs.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static LocationSize precise(uint64_t Value)
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
typename vector_type::const_iterator iterator
Definition SetVector.h:72
iterator end()
Get an iterator to the end of the SetVector.
Definition SetVector.h:112
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:106
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false, bool NoDetails=false) const
Print the current type.
LLVM Value Representation.
Definition Value.h:75
A raw_ostream that writes to an std::string.
Abstract Attribute helper functions.
Definition Attributor.h:165
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ ModRef
The access may reference and may modify the value stored in memory.
Definition ModRef.h:36
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
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.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872