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 9 #include "llvm/Analysis/AliasAnalysisEvaluator.h" 10 #include "llvm/ADT/SetVector.h" 11 #include "llvm/Analysis/AliasAnalysis.h" 12 #include "llvm/IR/Constants.h" 13 #include "llvm/IR/DataLayout.h" 14 #include "llvm/IR/DerivedTypes.h" 15 #include "llvm/IR/Function.h" 16 #include "llvm/IR/InstIterator.h" 17 #include "llvm/IR/Instructions.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/InitializePasses.h" 20 #include "llvm/Pass.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/raw_ostream.h" 24 using namespace llvm; 25 26 static cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden); 27 28 static cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden); 29 static cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden); 30 static cl::opt<bool> PrintPartialAlias("print-partial-aliases", cl::ReallyHidden); 31 static cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden); 32 33 static cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden); 34 static cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden); 35 static cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden); 36 static cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden); 37 static cl::opt<bool> PrintMust("print-must", cl::ReallyHidden); 38 static cl::opt<bool> PrintMustRef("print-mustref", cl::ReallyHidden); 39 static cl::opt<bool> PrintMustMod("print-mustmod", cl::ReallyHidden); 40 static cl::opt<bool> PrintMustModRef("print-mustmodref", cl::ReallyHidden); 41 42 static cl::opt<bool> EvalAAMD("evaluate-aa-metadata", cl::ReallyHidden); 43 44 static void PrintResults(AliasResult AR, bool P, const Value *V1, 45 const Value *V2, const Module *M) { 46 if (PrintAll || P) { 47 std::string o1, o2; 48 { 49 raw_string_ostream os1(o1), os2(o2); 50 V1->printAsOperand(os1, true, M); 51 V2->printAsOperand(os2, true, M); 52 } 53 54 if (o2 < o1) 55 std::swap(o1, o2); 56 errs() << " " << AR << ":\t" << o1 << ", " << o2 << "\n"; 57 } 58 } 59 60 static inline void PrintModRefResults(const char *Msg, bool P, Instruction *I, 61 Value *Ptr, Module *M) { 62 if (PrintAll || P) { 63 errs() << " " << Msg << ": Ptr: "; 64 Ptr->printAsOperand(errs(), true, M); 65 errs() << "\t<->" << *I << '\n'; 66 } 67 } 68 69 static inline void PrintModRefResults(const char *Msg, bool P, CallBase *CallA, 70 CallBase *CallB, Module *M) { 71 if (PrintAll || P) { 72 errs() << " " << Msg << ": " << *CallA << " <-> " << *CallB << '\n'; 73 } 74 } 75 76 static inline void PrintLoadStoreResults(AliasResult AR, bool P, 77 const Value *V1, const Value *V2, 78 const Module *M) { 79 if (PrintAll || P) { 80 errs() << " " << AR << ": " << *V1 << " <-> " << *V2 << '\n'; 81 } 82 } 83 84 static inline bool isInterestingPointer(Value *V) { 85 return V->getType()->isPointerTy() 86 && !isa<ConstantPointerNull>(V); 87 } 88 89 PreservedAnalyses AAEvaluator::run(Function &F, FunctionAnalysisManager &AM) { 90 runInternal(F, AM.getResult<AAManager>(F)); 91 return PreservedAnalyses::all(); 92 } 93 94 void AAEvaluator::runInternal(Function &F, AAResults &AA) { 95 const DataLayout &DL = F.getParent()->getDataLayout(); 96 97 ++FunctionCount; 98 99 SetVector<Value *> Pointers; 100 SmallSetVector<CallBase *, 16> Calls; 101 SetVector<Value *> Loads; 102 SetVector<Value *> Stores; 103 104 for (auto &I : F.args()) 105 if (I.getType()->isPointerTy()) // Add all pointer arguments. 106 Pointers.insert(&I); 107 108 for (Instruction &Inst : instructions(F)) { 109 if (Inst.getType()->isPointerTy()) // Add all pointer instructions. 110 Pointers.insert(&Inst); 111 if (EvalAAMD && isa<LoadInst>(&Inst)) 112 Loads.insert(&Inst); 113 if (EvalAAMD && isa<StoreInst>(&Inst)) 114 Stores.insert(&Inst); 115 if (auto *Call = dyn_cast<CallBase>(&Inst)) { 116 Value *Callee = Call->getCalledOperand(); 117 // Skip actual functions for direct function calls. 118 if (!isa<Function>(Callee) && isInterestingPointer(Callee)) 119 Pointers.insert(Callee); 120 // Consider formals. 121 for (Use &DataOp : Call->data_ops()) 122 if (isInterestingPointer(DataOp)) 123 Pointers.insert(DataOp); 124 Calls.insert(Call); 125 } else { 126 // Consider all operands. 127 for (Use &Op : Inst.operands()) 128 if (isInterestingPointer(Op)) 129 Pointers.insert(Op); 130 } 131 } 132 133 if (PrintAll || PrintNoAlias || PrintMayAlias || PrintPartialAlias || 134 PrintMustAlias || PrintNoModRef || PrintMod || PrintRef || PrintModRef) 135 errs() << "Function: " << F.getName() << ": " << Pointers.size() 136 << " pointers, " << Calls.size() << " call sites\n"; 137 138 // iterate over the worklist, and run the full (n^2)/2 disambiguations 139 for (SetVector<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end(); 140 I1 != E; ++I1) { 141 auto I1Size = LocationSize::afterPointer(); 142 Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType(); 143 if (I1ElTy->isSized()) 144 I1Size = LocationSize::precise(DL.getTypeStoreSize(I1ElTy)); 145 146 for (SetVector<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) { 147 auto I2Size = LocationSize::afterPointer(); 148 Type *I2ElTy = cast<PointerType>((*I2)->getType())->getElementType(); 149 if (I2ElTy->isSized()) 150 I2Size = LocationSize::precise(DL.getTypeStoreSize(I2ElTy)); 151 152 AliasResult AR = AA.alias(*I1, I1Size, *I2, I2Size); 153 switch (AR) { 154 case NoAlias: 155 PrintResults(AR, PrintNoAlias, *I1, *I2, F.getParent()); 156 ++NoAliasCount; 157 break; 158 case MayAlias: 159 PrintResults(AR, PrintMayAlias, *I1, *I2, F.getParent()); 160 ++MayAliasCount; 161 break; 162 case PartialAlias: 163 PrintResults(AR, PrintPartialAlias, *I1, *I2, F.getParent()); 164 ++PartialAliasCount; 165 break; 166 case MustAlias: 167 PrintResults(AR, PrintMustAlias, *I1, *I2, F.getParent()); 168 ++MustAliasCount; 169 break; 170 } 171 } 172 } 173 174 if (EvalAAMD) { 175 // iterate over all pairs of load, store 176 for (Value *Load : Loads) { 177 for (Value *Store : Stores) { 178 AliasResult AR = AA.alias(MemoryLocation::get(cast<LoadInst>(Load)), 179 MemoryLocation::get(cast<StoreInst>(Store))); 180 switch (AR) { 181 case NoAlias: 182 PrintLoadStoreResults(AR, PrintNoAlias, Load, Store, F.getParent()); 183 ++NoAliasCount; 184 break; 185 case MayAlias: 186 PrintLoadStoreResults(AR, PrintMayAlias, Load, Store, F.getParent()); 187 ++MayAliasCount; 188 break; 189 case PartialAlias: 190 PrintLoadStoreResults(AR, PrintPartialAlias, Load, Store, F.getParent()); 191 ++PartialAliasCount; 192 break; 193 case MustAlias: 194 PrintLoadStoreResults(AR, PrintMustAlias, Load, Store, F.getParent()); 195 ++MustAliasCount; 196 break; 197 } 198 } 199 } 200 201 // iterate over all pairs of store, store 202 for (SetVector<Value *>::iterator I1 = Stores.begin(), E = Stores.end(); 203 I1 != E; ++I1) { 204 for (SetVector<Value *>::iterator I2 = Stores.begin(); I2 != I1; ++I2) { 205 AliasResult AR = AA.alias(MemoryLocation::get(cast<StoreInst>(*I1)), 206 MemoryLocation::get(cast<StoreInst>(*I2))); 207 switch (AR) { 208 case NoAlias: 209 PrintLoadStoreResults(AR, PrintNoAlias, *I1, *I2, F.getParent()); 210 ++NoAliasCount; 211 break; 212 case MayAlias: 213 PrintLoadStoreResults(AR, PrintMayAlias, *I1, *I2, F.getParent()); 214 ++MayAliasCount; 215 break; 216 case PartialAlias: 217 PrintLoadStoreResults(AR, PrintPartialAlias, *I1, *I2, F.getParent()); 218 ++PartialAliasCount; 219 break; 220 case MustAlias: 221 PrintLoadStoreResults(AR, PrintMustAlias, *I1, *I2, F.getParent()); 222 ++MustAliasCount; 223 break; 224 } 225 } 226 } 227 } 228 229 // Mod/ref alias analysis: compare all pairs of calls and values 230 for (CallBase *Call : Calls) { 231 for (auto Pointer : Pointers) { 232 auto Size = LocationSize::afterPointer(); 233 Type *ElTy = cast<PointerType>(Pointer->getType())->getElementType(); 234 if (ElTy->isSized()) 235 Size = LocationSize::precise(DL.getTypeStoreSize(ElTy)); 236 237 switch (AA.getModRefInfo(Call, Pointer, Size)) { 238 case ModRefInfo::NoModRef: 239 PrintModRefResults("NoModRef", PrintNoModRef, Call, Pointer, 240 F.getParent()); 241 ++NoModRefCount; 242 break; 243 case ModRefInfo::Mod: 244 PrintModRefResults("Just Mod", PrintMod, Call, Pointer, F.getParent()); 245 ++ModCount; 246 break; 247 case ModRefInfo::Ref: 248 PrintModRefResults("Just Ref", PrintRef, Call, Pointer, F.getParent()); 249 ++RefCount; 250 break; 251 case ModRefInfo::ModRef: 252 PrintModRefResults("Both ModRef", PrintModRef, Call, Pointer, 253 F.getParent()); 254 ++ModRefCount; 255 break; 256 case ModRefInfo::Must: 257 PrintModRefResults("Must", PrintMust, Call, Pointer, F.getParent()); 258 ++MustCount; 259 break; 260 case ModRefInfo::MustMod: 261 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, Call, Pointer, 262 F.getParent()); 263 ++MustModCount; 264 break; 265 case ModRefInfo::MustRef: 266 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, Call, Pointer, 267 F.getParent()); 268 ++MustRefCount; 269 break; 270 case ModRefInfo::MustModRef: 271 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, Call, 272 Pointer, F.getParent()); 273 ++MustModRefCount; 274 break; 275 } 276 } 277 } 278 279 // Mod/ref alias analysis: compare all pairs of calls 280 for (CallBase *CallA : Calls) { 281 for (CallBase *CallB : Calls) { 282 if (CallA == CallB) 283 continue; 284 switch (AA.getModRefInfo(CallA, CallB)) { 285 case ModRefInfo::NoModRef: 286 PrintModRefResults("NoModRef", PrintNoModRef, CallA, CallB, 287 F.getParent()); 288 ++NoModRefCount; 289 break; 290 case ModRefInfo::Mod: 291 PrintModRefResults("Just Mod", PrintMod, CallA, CallB, F.getParent()); 292 ++ModCount; 293 break; 294 case ModRefInfo::Ref: 295 PrintModRefResults("Just Ref", PrintRef, CallA, CallB, F.getParent()); 296 ++RefCount; 297 break; 298 case ModRefInfo::ModRef: 299 PrintModRefResults("Both ModRef", PrintModRef, CallA, CallB, 300 F.getParent()); 301 ++ModRefCount; 302 break; 303 case ModRefInfo::Must: 304 PrintModRefResults("Must", PrintMust, CallA, CallB, F.getParent()); 305 ++MustCount; 306 break; 307 case ModRefInfo::MustMod: 308 PrintModRefResults("Just Mod (MustAlias)", PrintMustMod, CallA, CallB, 309 F.getParent()); 310 ++MustModCount; 311 break; 312 case ModRefInfo::MustRef: 313 PrintModRefResults("Just Ref (MustAlias)", PrintMustRef, CallA, CallB, 314 F.getParent()); 315 ++MustRefCount; 316 break; 317 case ModRefInfo::MustModRef: 318 PrintModRefResults("Both ModRef (MustAlias)", PrintMustModRef, CallA, 319 CallB, F.getParent()); 320 ++MustModRefCount; 321 break; 322 } 323 } 324 } 325 } 326 327 static void PrintPercent(int64_t Num, int64_t Sum) { 328 errs() << "(" << Num * 100LL / Sum << "." << ((Num * 1000LL / Sum) % 10) 329 << "%)\n"; 330 } 331 332 AAEvaluator::~AAEvaluator() { 333 if (FunctionCount == 0) 334 return; 335 336 int64_t AliasSum = 337 NoAliasCount + MayAliasCount + PartialAliasCount + MustAliasCount; 338 errs() << "===== Alias Analysis Evaluator Report =====\n"; 339 if (AliasSum == 0) { 340 errs() << " Alias Analysis Evaluator Summary: No pointers!\n"; 341 } else { 342 errs() << " " << AliasSum << " Total Alias Queries Performed\n"; 343 errs() << " " << NoAliasCount << " no alias responses "; 344 PrintPercent(NoAliasCount, AliasSum); 345 errs() << " " << MayAliasCount << " may alias responses "; 346 PrintPercent(MayAliasCount, AliasSum); 347 errs() << " " << PartialAliasCount << " partial alias responses "; 348 PrintPercent(PartialAliasCount, AliasSum); 349 errs() << " " << MustAliasCount << " must alias responses "; 350 PrintPercent(MustAliasCount, AliasSum); 351 errs() << " Alias Analysis Evaluator Pointer Alias Summary: " 352 << NoAliasCount * 100 / AliasSum << "%/" 353 << MayAliasCount * 100 / AliasSum << "%/" 354 << PartialAliasCount * 100 / AliasSum << "%/" 355 << MustAliasCount * 100 / AliasSum << "%\n"; 356 } 357 358 // Display the summary for mod/ref analysis 359 int64_t ModRefSum = NoModRefCount + RefCount + ModCount + ModRefCount + 360 MustCount + MustRefCount + MustModCount + MustModRefCount; 361 if (ModRefSum == 0) { 362 errs() << " Alias Analysis Mod/Ref Evaluator Summary: no " 363 "mod/ref!\n"; 364 } else { 365 errs() << " " << ModRefSum << " Total ModRef Queries Performed\n"; 366 errs() << " " << NoModRefCount << " no mod/ref responses "; 367 PrintPercent(NoModRefCount, ModRefSum); 368 errs() << " " << ModCount << " mod responses "; 369 PrintPercent(ModCount, ModRefSum); 370 errs() << " " << RefCount << " ref responses "; 371 PrintPercent(RefCount, ModRefSum); 372 errs() << " " << ModRefCount << " mod & ref responses "; 373 PrintPercent(ModRefCount, ModRefSum); 374 errs() << " " << MustCount << " must responses "; 375 PrintPercent(MustCount, ModRefSum); 376 errs() << " " << MustModCount << " must mod responses "; 377 PrintPercent(MustModCount, ModRefSum); 378 errs() << " " << MustRefCount << " must ref responses "; 379 PrintPercent(MustRefCount, ModRefSum); 380 errs() << " " << MustModRefCount << " must mod & ref responses "; 381 PrintPercent(MustModRefCount, ModRefSum); 382 errs() << " Alias Analysis Evaluator Mod/Ref Summary: " 383 << NoModRefCount * 100 / ModRefSum << "%/" 384 << ModCount * 100 / ModRefSum << "%/" << RefCount * 100 / ModRefSum 385 << "%/" << ModRefCount * 100 / ModRefSum << "%/" 386 << MustCount * 100 / ModRefSum << "%/" 387 << MustRefCount * 100 / ModRefSum << "%/" 388 << MustModCount * 100 / ModRefSum << "%/" 389 << MustModRefCount * 100 / ModRefSum << "%\n"; 390 } 391 } 392 393 namespace llvm { 394 class AAEvalLegacyPass : public FunctionPass { 395 std::unique_ptr<AAEvaluator> P; 396 397 public: 398 static char ID; // Pass identification, replacement for typeid 399 AAEvalLegacyPass() : FunctionPass(ID) { 400 initializeAAEvalLegacyPassPass(*PassRegistry::getPassRegistry()); 401 } 402 403 void getAnalysisUsage(AnalysisUsage &AU) const override { 404 AU.addRequired<AAResultsWrapperPass>(); 405 AU.setPreservesAll(); 406 } 407 408 bool doInitialization(Module &M) override { 409 P.reset(new AAEvaluator()); 410 return false; 411 } 412 413 bool runOnFunction(Function &F) override { 414 P->runInternal(F, getAnalysis<AAResultsWrapperPass>().getAAResults()); 415 return false; 416 } 417 bool doFinalization(Module &M) override { 418 P.reset(); 419 return false; 420 } 421 }; 422 } 423 424 char AAEvalLegacyPass::ID = 0; 425 INITIALIZE_PASS_BEGIN(AAEvalLegacyPass, "aa-eval", 426 "Exhaustive Alias Analysis Precision Evaluator", false, 427 true) 428 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 429 INITIALIZE_PASS_END(AAEvalLegacyPass, "aa-eval", 430 "Exhaustive Alias Analysis Precision Evaluator", false, 431 true) 432 433 FunctionPass *llvm::createAAEvalPass() { return new AAEvalLegacyPass(); } 434