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