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