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