• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Passes.h"
16 #include "llvm/Analysis/LibCallSemantics.h"
17 #include "llvm/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 
createLibCallAliasAnalysisPass(LibCallInfo * LCI)26 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
27   return new LibCallAliasAnalysis(LCI);
28 }
29 
~LibCallAliasAnalysis()30 LibCallAliasAnalysis::~LibCallAliasAnalysis() {
31   delete LCI;
32 }
33 
getAnalysisUsage(AnalysisUsage & AU) const34 void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
35   AliasAnalysis::getAnalysisUsage(AU);
36   AU.setPreservesAll();                         // Does not transform code
37 }
38 
39 
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
AnalyzeLibCallDetails(const LibCallFunctionInfo * FI,ImmutableCallSite CS,const Location & Loc)45 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
46                                             ImmutableCallSite CS,
47                                             const Location &Loc) {
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) {
67       const LibCallLocationInfo &LocInfo =
68       LCI->getLocationInfo(Details[i].LocationID);
69       LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
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) {
88     const LibCallLocationInfo &LocInfo =
89     LCI->getLocationInfo(Details[i].LocationID);
90     LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
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
getModRefInfo(ImmutableCallSite CS,const Location & Loc)120 LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
121                                     const Location &Loc) {
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.
126   if (LCI) {
127     if (const Function *F = CS.getCalledFunction()) {
128       if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
129         MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
130         if (MRInfo == NoModRef) return NoModRef;
131       }
132     }
133   }
134 
135   // The AliasAnalysis base class has some smarts, lets use them.
136   return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
137 }
138