1 //===- LibCallAliasAnalysis.cpp - Implement AliasAnalysis for libcalls ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LibCallAliasAnalysis class.
12 //===----------------------------------------------------------------------===//
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"
21 // Register this pass...
22 char LibCallAliasAnalysis::ID
= 0;
23 INITIALIZE_AG_PASS(LibCallAliasAnalysis
, AliasAnalysis
, "libcall-aa",
24 "LibCall Alias Analysis", false, true, false)
26 FunctionPass
*llvm::createLibCallAliasAnalysisPass(LibCallInfo
*LCI
) {
27 return new LibCallAliasAnalysis(LCI
);
30 LibCallAliasAnalysis::~LibCallAliasAnalysis() {
34 void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage
&AU
) const {
35 AliasAnalysis::getAnalysisUsage(AU
);
36 AU
.setPreservesAll(); // Does not transform code
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
,
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
;
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
56 const LibCallFunctionInfo::LocationMRInfo
*Details
= FI
->LocationDetails
;
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.
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;
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
);
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
);
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;
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
) {
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
);
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.
112 // Otherwise, return any other info gained so far.
116 // getModRefInfo - Check to see if the specified callsite can clobber the
117 // specified memory object.
119 AliasAnalysis::ModRefResult
120 LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS
,
121 const Location
&Loc
) {
122 ModRefResult MRInfo
= ModRef
;
124 // If this is a direct call to a function that LCI knows about, get the
125 // information about the runtime function.
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
;
135 // The AliasAnalysis base class has some smarts, lets use them.
136 return (ModRefResult
)(MRInfo
| AliasAnalysis::getModRefInfo(CS
, Loc
));