LLVM 20.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, CallBase *CallA,
82 CallBase *CallB, Module *M) {
83 if (PrintAll || P) {
84 errs() << " " << Msg << ": " << *CallA << " <-> " << *CallB << '\n';
85 }
86}
87
88static inline void PrintLoadStoreResults(AliasResult AR, bool P,
89 const Value *V1, const Value *V2,
90 const Module *M) {
91 if (PrintAll || P) {
92 errs() << " " << AR << ": " << *V1 << " <-> " << *V2 << '\n';
93 }
94}
95
97 runInternal(F, AM.getResult<AAManager>(F));
99}
100
101void AAEvaluator::runInternal(Function &F, AAResults &AA) {
102 const DataLayout &DL = F.getDataLayout();
103
104 ++FunctionCount;
105
108 SetVector<Value *> Loads;
109 SetVector<Value *> Stores;
110
111 for (Instruction &Inst : instructions(F)) {
112 if (auto *LI = dyn_cast<LoadInst>(&Inst)) {
113 Pointers.insert({LI->getPointerOperand(), LI->getType()});
114 Loads.insert(LI);
115 } else if (auto *SI = dyn_cast<StoreInst>(&Inst)) {
116 Pointers.insert({SI->getPointerOperand(),
117 SI->getValueOperand()->getType()});
118 Stores.insert(SI);
119 } else if (auto *CB = dyn_cast<CallBase>(&Inst))
120 Calls.insert(CB);
121 }
122
123 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias ||
125 errs() << "Function: " << F.getName() << ": " << Pointers.size()
126 << " pointers, " << Calls.size() << " call sites\n";
127
128 // iterate over the worklist, and run the full (n^2)/2 disambiguations
129 for (auto I1 = Pointers.begin(), E = Pointers.end(); I1 != E; ++I1) {
130 LocationSize Size1 = LocationSize::precise(DL.getTypeStoreSize(I1->second));
131 for (auto I2 = Pointers.begin(); I2 != I1; ++I2) {
132 LocationSize Size2 =
133 LocationSize::precise(DL.getTypeStoreSize(I2->second));
134 AliasResult AR = AA.alias(I1->first, Size1, I2->first, Size2);
135 switch (AR) {
137 PrintResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
138 ++NoAliasCount;
139 break;
141 PrintResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
142 ++MayAliasCount;
143 break;
145 PrintResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
146 ++PartialAliasCount;
147 break;
149 PrintResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
150 ++MustAliasCount;
151 break;
152 }
153 }
154 }
155
156 if (EvalAAMD) {
157 // iterate over all pairs of load, store
158 for (Value *Load : Loads) {
159 for (Value *Store : Stores) {
160 AliasResult AR = AA.alias(MemoryLocation::get(cast<LoadInst>(Load)),
161 MemoryLocation::get(cast<StoreInst>(Store)));
162 switch (AR) {
164 PrintLoadStoreResults(AR, PrintNoAlias, Load, Store, F.getParent());
165 ++NoAliasCount;
166 break;
168 PrintLoadStoreResults(AR, PrintMayAlias, Load, Store, F.getParent());
169 ++MayAliasCount;
170 break;
172 PrintLoadStoreResults(AR, PrintPartialAlias, Load, Store, F.getParent());
173 ++PartialAliasCount;
174 break;
176 PrintLoadStoreResults(AR, PrintMustAlias, Load, Store, F.getParent());
177 ++MustAliasCount;
178 break;
179 }
180 }
181 }
182
183 // iterate over all pairs of store, store
184 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end();
185 I1 != E; ++I1) {
186 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) {
187 AliasResult AR = AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)),
188 MemoryLocation::get(cast<StoreInst>(*I2)));
189 switch (AR) {
191 PrintLoadStoreResults(AR, PrintNoAlias, *I1, *I2, F.getParent());
192 ++NoAliasCount;
193 break;
195 PrintLoadStoreResults(AR, PrintMayAlias, *I1, *I2, F.getParent());
196 ++MayAliasCount;
197 break;
199 PrintLoadStoreResults(AR, PrintPartialAlias, *I1, *I2, F.getParent());
200 ++PartialAliasCount;
201 break;
203 PrintLoadStoreResults(AR, PrintMustAlias, *I1, *I2, F.getParent());
204 ++MustAliasCount;
205 break;
206 }
207 }
208 }
209 }
210
211 // Mod/ref alias analysis: compare all pairs of calls and values
212 for (CallBase *Call : Calls) {
213 for (const auto &Pointer : Pointers) {
215 LocationSize::precise(DL.getTypeStoreSize(Pointer.second));
216 switch (AA.getModRefInfo(Call, Pointer.first, Size)) {
218 PrintModRefResults("NoModRef", PrintNoModRef, Call, Pointer,
219 F.getParent());
220 ++NoModRefCount;
221 break;
222 case ModRefInfo::Mod:
223 PrintModRefResults("Just Mod", PrintMod, Call, Pointer, F.getParent());
224 ++ModCount;
225 break;
226 case ModRefInfo::Ref:
227 PrintModRefResults("Just Ref", PrintRef, Call, Pointer, F.getParent());
228 ++RefCount;
229 break;
231 PrintModRefResults("Both ModRef", PrintModRef, Call, Pointer,
232 F.getParent());
233 ++ModRefCount;
234 break;
235 }
236 }
237 }
238
239 // Mod/ref alias analysis: compare all pairs of calls
240 for (CallBase *CallA : Calls) {
241 for (CallBase *CallB : Calls) {
242 if (CallA == CallB)
243 continue;
244 switch (AA.getModRefInfo(CallA, CallB)) {
246 PrintModRefResults("NoModRef", PrintNoModRef, CallA, CallB,
247 F.getParent());
248 ++NoModRefCount;
249 break;
250 case ModRefInfo::Mod:
251 PrintModRefResults("Just Mod", PrintMod, CallA, CallB, F.getParent());
252 ++ModCount;
253 break;
254 case ModRefInfo::Ref:
255 PrintModRefResults("Just Ref", PrintRef, CallA, CallB, F.getParent());
256 ++RefCount;
257 break;
259 PrintModRefResults("Both ModRef", PrintModRef, CallA, CallB,
260 F.getParent());
261 ++ModRefCount;
262 break;
263 }
264 }
265 }
266}
267
268static void PrintPercent(int64_t Num, int64_t Sum) {
269 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10)
270 << "%)\n";
271}
272
274 if (FunctionCount == 0)
275 return;
276
277 int64_t AliasSum =
278 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount;
279 errs() << "===== Alias Analysis Evaluator Report =====\n";
280 if (AliasSum == 0) {
281 errs() << " Alias Analysis Evaluator Summary: No pointers!\n";
282 } else {
283 errs() << " " << AliasSum << " Total Alias Queries Performed\n";
284 errs() << " " << NoAliasCount << " no alias responses ";
285 PrintPercent(NoAliasCount, AliasSum);
286 errs() << " " << MayAliasCount << " may alias responses ";
287 PrintPercent(MayAliasCount, AliasSum);
288 errs() << " " << PartialAliasCount << " partial alias responses ";
289 PrintPercent(PartialAliasCount, AliasSum);
290 errs() << " " << MustAliasCount << " must alias responses ";
291 PrintPercent(MustAliasCount, AliasSum);
292 errs() << " Alias Analysis Evaluator Pointer Alias Summary: "
293 << NoAliasCount * 100 / AliasSum << "%/"
294 << MayAliasCount * 100 / AliasSum << "%/"
295 << PartialAliasCount * 100 / AliasSum << "%/"
296 << MustAliasCount * 100 / AliasSum << "%\n";
297 }
298
299 // Display the summary for mod/ref analysis
300 int64_t ModRefSum = NoModRefCount + RefCount + ModCount + ModRefCount;
301 if (ModRefSum == 0) {
302 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no "
303 "mod/ref!\n";
304 } else {
305 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n";
306 errs() << " " << NoModRefCount << " no mod/ref responses ";
307 PrintPercent(NoModRefCount, ModRefSum);
308 errs() << " " << ModCount << " mod responses ";
309 PrintPercent(ModCount, ModRefSum);
310 errs() << " " << RefCount << " ref responses ";
311 PrintPercent(RefCount, ModRefSum);
312 errs() << " " << ModRefCount << " mod & ref responses ";
313 PrintPercent(ModRefCount, ModRefSum);
314 errs() << " Alias Analysis Evaluator Mod/Ref Summary: "
315 << NoModRefCount * 100 / ModRefSum << "%/"
316 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum
317 << "%/" << ModRefCount * 100 / ModRefSum << "%\n";
318 }
319}
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
uint64_t Size
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
This file implements a set that has insertion order iteration characteristics.
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.
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.
Definition: AliasAnalysis.h:77
void swap(bool DoSwap=true)
Helper for processing AliasResult for swapped memory location pairs.
@ MayAlias
The two locations may or may not alias.
Definition: AliasAnalysis.h:98
@ NoAlias
The two locations do not alias at all.
Definition: AliasAnalysis.h:95
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
static LocationSize precise(uint64_t Value)
static 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:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
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:98
typename vector_type::const_iterator iterator
Definition: SetVector.h:69
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:113
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:103
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:370
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
void print(raw_ostream &O, bool IsForDebug=false, bool NoDetails=false) const
Print the current type.
LLVM Value Representation.
Definition: Value.h:74
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
@ ReallyHidden
Definition: CommandLine.h:138
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
@ ModRef
The access may reference and may modify the value stored in memory.
@ Mod
The access may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860