Fix a typo in a comment.
[oota-llvm.git] / lib / Analysis / LibCallAliasAnalysis.cpp
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
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Analysis/LibCallSemantics.h"
16 #include "llvm/Function.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Target/TargetData.h"
19 using namespace llvm;
20
21 namespace {
22   /// LibCallAliasAnalysis - Alias analysis driven from LibCallInfo.
23   struct LibCallAliasAnalysis : public FunctionPass, AliasAnalysis {
24     static char ID; // Class identification
25     
26     LibCallInfo *LCI;
27     
28     explicit LibCallAliasAnalysis(LibCallInfo *LC = 0)
29       : FunctionPass((intptr_t)&ID), LCI(LC) {
30     }
31     ~LibCallAliasAnalysis() {
32       delete LCI;
33     }
34     
35     ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
36     ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
37       // TODO: Could compare two direct calls against each other if we cared to.
38       return AliasAnalysis::getModRefInfo(CS1,CS2);
39     }
40     
41     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42       AliasAnalysis::getAnalysisUsage(AU);
43       AU.addRequired<TargetData>();
44       AU.setPreservesAll();                         // Does not transform code
45     }
46     
47     virtual bool runOnFunction(Function &F) {
48       InitializeAliasAnalysis(this);                 // set up super class
49       return false;
50     }
51     
52     /// hasNoModRefInfoForCalls - We can provide mod/ref information against
53     /// non-escaping allocations.
54     virtual bool hasNoModRefInfoForCalls() const { return false; }
55   private:
56     ModRefResult AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
57                                        CallSite CS, Value *P, unsigned Size);
58   };
59 }  // End of anonymous namespace
60   
61 // Register this pass...
62 char LibCallAliasAnalysis::ID = 0;
63 static RegisterPass<LibCallAliasAnalysis>
64 X("libcall-aa", "LibCall Alias Analysis", false, true);
65   
66 // Declare that we implement the AliasAnalysis interface
67 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
68
69 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
70   return new LibCallAliasAnalysis(LCI);
71 }
72
73
74 /// AnalyzeLibCallDetails - Given a call to a function with the specified
75 /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
76 /// vs the specified pointer/size.
77 AliasAnalysis::ModRefResult
78 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
79                                             CallSite CS, Value *P,
80                                             unsigned Size) {
81   // If we have a function, check to see what kind of mod/ref effects it
82   // has.  Start by including any info globally known about the function.
83   AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
84   if (MRInfo == NoModRef) return MRInfo;
85   
86   // If that didn't tell us that the function is 'readnone', check to see
87   // if we have detailed info and if 'P' is any of the locations we know
88   // about.
89   const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
90   if (Details == 0)
91     return MRInfo;
92   
93   // If the details array is of the 'DoesNot' kind, we only know something if
94   // the pointer is a match for one of the locations in 'Details'.  If we find a
95   // match, we can prove some interactions cannot happen.
96   // 
97   if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
98     // Find out if the pointer refers to a known location.
99     for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
100       const LibCallLocationInfo &Loc =
101       LCI->getLocationInfo(Details[i].LocationID);
102       LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
103       if (Res != LibCallLocationInfo::Yes) continue;
104       
105       // If we find a match against a location that we 'do not' interact with,
106       // learn this info into MRInfo.
107       return ModRefResult(MRInfo & ~Details[i].MRInfo);
108     }
109     return MRInfo;
110   }
111   
112   // If the details are of the 'DoesOnly' sort, we know something if the pointer
113   // is a match for one of the locations in 'Details'.  Also, if we can prove
114   // that the pointers is *not* one of the locations in 'Details', we know that
115   // the call is NoModRef.
116   assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
117   
118   // Find out if the pointer refers to a known location.
119   bool NoneMatch = true;
120   for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
121     const LibCallLocationInfo &Loc =
122     LCI->getLocationInfo(Details[i].LocationID);
123     LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
124     if (Res == LibCallLocationInfo::No) continue;
125     
126     // If we don't know if this pointer points to the location, then we have to
127     // assume it might alias in some case.
128     if (Res == LibCallLocationInfo::Unknown) {
129       NoneMatch = false;
130       continue;
131     }
132     
133     // If we know that this pointer definitely is pointing into the location,
134     // merge in this information.
135     return ModRefResult(MRInfo & Details[i].MRInfo);
136   }
137   
138   // If we found that the pointer is guaranteed to not match any of the
139   // locations in our 'DoesOnly' rule, then we know that the pointer must point
140   // to some other location.  Since the libcall doesn't mod/ref any other
141   // locations, return NoModRef.
142   if (NoneMatch)
143     return NoModRef;
144   
145   // Otherwise, return any other info gained so far.
146   return MRInfo;
147 }
148
149 // getModRefInfo - Check to see if the specified callsite can clobber the
150 // specified memory object.
151 //
152 AliasAnalysis::ModRefResult
153 LibCallAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
154   ModRefResult MRInfo = ModRef;
155   
156   // If this is a direct call to a function that LCI knows about, get the
157   // information about the runtime function.
158   if (Function *F = CS.getCalledFunction()) {
159     if (LCI && F->isDeclaration()) {
160       if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
161         MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, P, Size));
162         if (MRInfo == NoModRef) return NoModRef;
163       }
164     }
165   }
166   
167   // The AliasAnalysis base class has some smarts, lets use them.
168   return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, P, Size));
169 }