LibCallAliasAnalysis.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/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. /// AnalyzeLibCallDetails - Given a call to a function with the specified
  34. /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
  35. /// vs the specified pointer/size.
  36. AliasAnalysis::ModRefResult
  37. LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
  38. ImmutableCallSite CS,
  39. const Location &Loc) {
  40. // If we have a function, check to see what kind of mod/ref effects it
  41. // has. Start by including any info globally known about the function.
  42. AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
  43. if (MRInfo == NoModRef) return MRInfo;
  44. // If that didn't tell us that the function is 'readnone', check to see
  45. // if we have detailed info and if 'P' is any of the locations we know
  46. // about.
  47. const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
  48. if (Details == 0)
  49. return MRInfo;
  50. // If the details array is of the 'DoesNot' kind, we only know something if
  51. // the pointer is a match for one of the locations in 'Details'. If we find a
  52. // match, we can prove some interactions cannot happen.
  53. //
  54. if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
  55. // Find out if the pointer refers to a known location.
  56. for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
  57. const LibCallLocationInfo &LocInfo =
  58. LCI->getLocationInfo(Details[i].LocationID);
  59. LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
  60. if (Res != LibCallLocationInfo::Yes) continue;
  61. // If we find a match against a location that we 'do not' interact with,
  62. // learn this info into MRInfo.
  63. return ModRefResult(MRInfo & ~Details[i].MRInfo);
  64. }
  65. return MRInfo;
  66. }
  67. // If the details are of the 'DoesOnly' sort, we know something if the pointer
  68. // is a match for one of the locations in 'Details'. Also, if we can prove
  69. // that the pointers is *not* one of the locations in 'Details', we know that
  70. // the call is NoModRef.
  71. assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
  72. // Find out if the pointer refers to a known location.
  73. bool NoneMatch = true;
  74. for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
  75. const LibCallLocationInfo &LocInfo =
  76. LCI->getLocationInfo(Details[i].LocationID);
  77. LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
  78. if (Res == LibCallLocationInfo::No) continue;
  79. // If we don't know if this pointer points to the location, then we have to
  80. // assume it might alias in some case.
  81. if (Res == LibCallLocationInfo::Unknown) {
  82. NoneMatch = false;
  83. continue;
  84. }
  85. // If we know that this pointer definitely is pointing into the location,
  86. // merge in this information.
  87. return ModRefResult(MRInfo & Details[i].MRInfo);
  88. }
  89. // If we found that the pointer is guaranteed to not match any of the
  90. // locations in our 'DoesOnly' rule, then we know that the pointer must point
  91. // to some other location. Since the libcall doesn't mod/ref any other
  92. // locations, return NoModRef.
  93. if (NoneMatch)
  94. return NoModRef;
  95. // Otherwise, return any other info gained so far.
  96. return MRInfo;
  97. }
  98. // getModRefInfo - Check to see if the specified callsite can clobber the
  99. // specified memory object.
  100. //
  101. AliasAnalysis::ModRefResult
  102. LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
  103. const Location &Loc) {
  104. ModRefResult MRInfo = ModRef;
  105. // If this is a direct call to a function that LCI knows about, get the
  106. // information about the runtime function.
  107. if (LCI) {
  108. if (const Function *F = CS.getCalledFunction()) {
  109. if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
  110. MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
  111. if (MRInfo == NoModRef) return NoModRef;
  112. }
  113. }
  114. }
  115. // The AliasAnalysis base class has some smarts, lets use them.
  116. return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
  117. }