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