LibCallAliasAnalysis.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //===- LibCallAliasAnalysis.cpp - Implement AliasAnalysis for libcalls ----===//
  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. // This file implements the LibCallAliasAnalysis class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/LibCallAliasAnalysis.h"
  14. #include "llvm/Analysis/LibCallSemantics.h"
  15. #include "llvm/Analysis/Passes.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/Pass.h"
  18. using namespace llvm;
  19. // Register this pass...
  20. char LibCallAliasAnalysis::ID = 0;
  21. INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis, "libcall-aa",
  22. "LibCall Alias Analysis", false, true, false)
  23. FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
  24. return new LibCallAliasAnalysis(LCI);
  25. }
  26. LibCallAliasAnalysis::~LibCallAliasAnalysis() {
  27. delete LCI;
  28. }
  29. void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  30. AliasAnalysis::getAnalysisUsage(AU);
  31. AU.setPreservesAll(); // Does not transform code
  32. }
  33. bool LibCallAliasAnalysis::runOnFunction(Function &F) {
  34. // set up super class
  35. InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
  36. return false;
  37. }
  38. /// AnalyzeLibCallDetails - Given a call to a function with the specified
  39. /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
  40. /// vs the specified pointer/size.
  41. AliasAnalysis::ModRefResult
  42. LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
  43. ImmutableCallSite CS,
  44. const MemoryLocation &Loc) {
  45. // If we have a function, check to see what kind of mod/ref effects it
  46. // has. Start by including any info globally known about the function.
  47. AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
  48. if (MRInfo == NoModRef) return MRInfo;
  49. // If that didn't tell us that the function is 'readnone', check to see
  50. // if we have detailed info and if 'P' is any of the locations we know
  51. // about.
  52. const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
  53. if (Details == nullptr)
  54. return MRInfo;
  55. // If the details array is of the 'DoesNot' kind, we only know something if
  56. // the pointer is a match for one of the locations in 'Details'. If we find a
  57. // match, we can prove some interactions cannot happen.
  58. //
  59. if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
  60. // Find out if the pointer refers to a known location.
  61. for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
  62. const LibCallLocationInfo &LocInfo =
  63. LCI->getLocationInfo(Details[i].LocationID);
  64. LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
  65. if (Res != LibCallLocationInfo::Yes) continue;
  66. // If we find a match against a location that we 'do not' interact with,
  67. // learn this info into MRInfo.
  68. return ModRefResult(MRInfo & ~Details[i].MRInfo);
  69. }
  70. return MRInfo;
  71. }
  72. // If the details are of the 'DoesOnly' sort, we know something if the pointer
  73. // is a match for one of the locations in 'Details'. Also, if we can prove
  74. // that the pointers is *not* one of the locations in 'Details', we know that
  75. // the call is NoModRef.
  76. assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
  77. // Find out if the pointer refers to a known location.
  78. bool NoneMatch = true;
  79. for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
  80. const LibCallLocationInfo &LocInfo =
  81. LCI->getLocationInfo(Details[i].LocationID);
  82. LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
  83. if (Res == LibCallLocationInfo::No) continue;
  84. // If we don't know if this pointer points to the location, then we have to
  85. // assume it might alias in some case.
  86. if (Res == LibCallLocationInfo::Unknown) {
  87. NoneMatch = false;
  88. continue;
  89. }
  90. // If we know that this pointer definitely is pointing into the location,
  91. // merge in this information.
  92. return ModRefResult(MRInfo & Details[i].MRInfo);
  93. }
  94. // If we found that the pointer is guaranteed to not match any of the
  95. // locations in our 'DoesOnly' rule, then we know that the pointer must point
  96. // to some other location. Since the libcall doesn't mod/ref any other
  97. // locations, return NoModRef.
  98. if (NoneMatch)
  99. return NoModRef;
  100. // Otherwise, return any other info gained so far.
  101. return MRInfo;
  102. }
  103. // getModRefInfo - Check to see if the specified callsite can clobber the
  104. // specified memory object.
  105. //
  106. AliasAnalysis::ModRefResult
  107. LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
  108. const MemoryLocation &Loc) {
  109. ModRefResult MRInfo = ModRef;
  110. // If this is a direct call to a function that LCI knows about, get the
  111. // information about the runtime function.
  112. if (LCI) {
  113. if (const Function *F = CS.getCalledFunction()) {
  114. if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
  115. MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
  116. if (MRInfo == NoModRef) return NoModRef;
  117. }
  118. }
  119. }
  120. // The AliasAnalysis base class has some smarts, lets use them.
  121. return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
  122. }