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/Passes.h"
15 #include "llvm/Analysis/LibCallSemantics.h"
16 #include "llvm/Function.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Target/TargetData.h"
22 /// LibCallAliasAnalysis - Alias analysis driven from LibCallInfo.
23 struct LibCallAliasAnalysis : public FunctionPass, AliasAnalysis {
24 static char ID; // Class identification
28 explicit LibCallAliasAnalysis(LibCallInfo *LC = 0)
29 : FunctionPass((intptr_t)&ID), LCI(LC) {
31 ~LibCallAliasAnalysis() {
35 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
36 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
37 // TODO: Could compare two direct calls against each other if we cared to.
38 return AliasAnalysis::getModRefInfo(CS1,CS2);
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AliasAnalysis::getAnalysisUsage(AU);
43 AU.addRequired<TargetData>();
44 AU.setPreservesAll(); // Does not transform code
47 virtual bool runOnFunction(Function &F) {
48 InitializeAliasAnalysis(this); // set up super class
52 /// hasNoModRefInfoForCalls - We can provide mod/ref information against
53 /// non-escaping allocations.
54 virtual bool hasNoModRefInfoForCalls() const { return false; }
56 ModRefResult AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
57 CallSite CS, Value *P, unsigned Size);
59 } // End of anonymous namespace
61 // Register this pass...
62 char LibCallAliasAnalysis::ID = 0;
63 static RegisterPass<LibCallAliasAnalysis>
64 X("libcall-aa", "LibCall Alias Analysis", false, true);
66 // Declare that we implement the AliasAnalysis interface
67 static RegisterAnalysisGroup<AliasAnalysis> Y(X);
69 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
70 return new LibCallAliasAnalysis(LCI);
74 /// AnalyzeLibCallDetails - Given a call to a function with the specified
75 /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
76 /// vs the specified pointer/size.
77 AliasAnalysis::ModRefResult
78 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
79 CallSite CS, Value *P,
81 // If we have a function, check to see what kind of mod/ref effects it
82 // has. Start by including any info globally known about the function.
83 AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
84 if (MRInfo == NoModRef) return MRInfo;
86 // If that didn't tell us that the function is 'readnone', check to see
87 // if we have detailed info and if 'P' is any of the locations we know
89 const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
93 // If the details array is of the 'DoesNot' kind, we only know something if
94 // the pointer is a match for one of the locations in 'Details'. If we find a
95 // match, we can prove some interactions cannot happen.
97 if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
98 // Find out if the pointer refers to a known location.
99 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
100 const LibCallLocationInfo &Loc =
101 LCI->getLocationInfo(Details[i].LocationID);
102 LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
103 if (Res != LibCallLocationInfo::Yes) continue;
105 // If we find a match against a location that we 'do not' interact with,
106 // learn this info into MRInfo.
107 return ModRefResult(MRInfo & ~Details[i].MRInfo);
112 // If the details are of the 'DoesOnly' sort, we know something if the pointer
113 // is a match for one of the locations in 'Details'. Also, if we can prove
114 // that the pointers is *not* one of the locations in 'Details', we know that
115 // the call is NoModRef.
116 assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
118 // Find out if the pointer refers to a known location.
119 bool NoneMatch = true;
120 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
121 const LibCallLocationInfo &Loc =
122 LCI->getLocationInfo(Details[i].LocationID);
123 LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
124 if (Res == LibCallLocationInfo::No) continue;
126 // If we don't know if this pointer points to the location, then we have to
127 // assume it might alias in some case.
128 if (Res == LibCallLocationInfo::Unknown) {
133 // If we know that this pointer definitely is pointing into the location,
134 // merge in this information.
135 return ModRefResult(MRInfo & Details[i].MRInfo);
138 // If we found that the pointer is guaranteed to not match any of the
139 // locations in our 'DoesOnly' rule, then we know that the pointer must point
140 // to some other location. Since the libcall doesn't mod/ref any other
141 // locations, return NoModRef.
145 // Otherwise, return any other info gained so far.
149 // getModRefInfo - Check to see if the specified callsite can clobber the
150 // specified memory object.
152 AliasAnalysis::ModRefResult
153 LibCallAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
154 ModRefResult MRInfo = ModRef;
156 // If this is a direct call to a function that LCI knows about, get the
157 // information about the runtime function.
159 if (Function *F = CS.getCalledFunction()) {
160 if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
161 MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, P, Size));
162 if (MRInfo == NoModRef) return NoModRef;
167 // The AliasAnalysis base class has some smarts, lets use them.
168 return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, P, Size));