AliasAnalysisEvaluator.cpp 16 KB

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