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