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"
19 #include "llvm/Target/TargetData.h"
22 // Register this pass...
23 char LibCallAliasAnalysis::ID
= 0;
24 static RegisterPass
<LibCallAliasAnalysis
>
25 X("libcall-aa", "LibCall Alias Analysis", false, true);
27 // Declare that we implement the AliasAnalysis interface
28 static RegisterAnalysisGroup
<AliasAnalysis
> Y(X
);
30 FunctionPass
*llvm::createLibCallAliasAnalysisPass(LibCallInfo
*LCI
) {
31 return new LibCallAliasAnalysis(LCI
);
34 LibCallAliasAnalysis::~LibCallAliasAnalysis() {
38 void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage
&AU
) const {
39 AliasAnalysis::getAnalysisUsage(AU
);
40 AU
.addRequired
<TargetData
>();
41 AU
.setPreservesAll(); // Does not transform code
46 /// AnalyzeLibCallDetails - Given a call to a function with the specified
47 /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
48 /// vs the specified pointer/size.
49 AliasAnalysis::ModRefResult
50 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo
*FI
,
51 CallSite CS
, Value
*P
,
53 // If we have a function, check to see what kind of mod/ref effects it
54 // has. Start by including any info globally known about the function.
55 AliasAnalysis::ModRefResult MRInfo
= FI
->UniversalBehavior
;
56 if (MRInfo
== NoModRef
) return MRInfo
;
58 // If that didn't tell us that the function is 'readnone', check to see
59 // if we have detailed info and if 'P' is any of the locations we know
61 const LibCallFunctionInfo::LocationMRInfo
*Details
= FI
->LocationDetails
;
65 // If the details array is of the 'DoesNot' kind, we only know something if
66 // the pointer is a match for one of the locations in 'Details'. If we find a
67 // match, we can prove some interactions cannot happen.
69 if (FI
->DetailsType
== LibCallFunctionInfo::DoesNot
) {
70 // Find out if the pointer refers to a known location.
71 for (unsigned i
= 0; Details
[i
].LocationID
!= ~0U; ++i
) {
72 const LibCallLocationInfo
&Loc
=
73 LCI
->getLocationInfo(Details
[i
].LocationID
);
74 LibCallLocationInfo::LocResult Res
= Loc
.isLocation(CS
, P
, Size
);
75 if (Res
!= LibCallLocationInfo::Yes
) continue;
77 // If we find a match against a location that we 'do not' interact with,
78 // learn this info into MRInfo.
79 return ModRefResult(MRInfo
& ~Details
[i
].MRInfo
);
84 // If the details are of the 'DoesOnly' sort, we know something if the pointer
85 // is a match for one of the locations in 'Details'. Also, if we can prove
86 // that the pointers is *not* one of the locations in 'Details', we know that
87 // the call is NoModRef.
88 assert(FI
->DetailsType
== LibCallFunctionInfo::DoesOnly
);
90 // Find out if the pointer refers to a known location.
91 bool NoneMatch
= true;
92 for (unsigned i
= 0; Details
[i
].LocationID
!= ~0U; ++i
) {
93 const LibCallLocationInfo
&Loc
=
94 LCI
->getLocationInfo(Details
[i
].LocationID
);
95 LibCallLocationInfo::LocResult Res
= Loc
.isLocation(CS
, P
, Size
);
96 if (Res
== LibCallLocationInfo::No
) continue;
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
) {
105 // If we know that this pointer definitely is pointing into the location,
106 // merge in this information.
107 return ModRefResult(MRInfo
& Details
[i
].MRInfo
);
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 NoModRef.
117 // Otherwise, return any other info gained so far.
121 // getModRefInfo - Check to see if the specified callsite can clobber the
122 // specified memory object.
124 AliasAnalysis::ModRefResult
125 LibCallAliasAnalysis::getModRefInfo(CallSite CS
, Value
*P
, unsigned Size
) {
126 ModRefResult MRInfo
= ModRef
;
128 // If this is a direct call to a function that LCI knows about, get the
129 // information about the runtime function.
131 if (Function
*F
= CS
.getCalledFunction()) {
132 if (const LibCallFunctionInfo
*FI
= LCI
->getFunctionInfo(F
)) {
133 MRInfo
= ModRefResult(MRInfo
& AnalyzeLibCallDetails(FI
, CS
, P
, Size
));
134 if (MRInfo
== NoModRef
) return NoModRef
;
139 // The AliasAnalysis base class has some smarts, lets use them.
140 return (ModRefResult
)(MRInfo
| AliasAnalysis::getModRefInfo(CS
, P
, Size
));