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/LibCallSemantics.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/Pass.h"
20 // Register this pass...
21 char LibCallAliasAnalysis::ID = 0;
22 INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis, "libcall-aa",
23 "LibCall Alias Analysis", false, true, false)
25 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
26 return new LibCallAliasAnalysis(LCI);
29 LibCallAliasAnalysis::~LibCallAliasAnalysis() { delete LCI; }
31 void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
32 AliasAnalysis::getAnalysisUsage(AU);
33 AU.setPreservesAll(); // Does not transform code
36 bool LibCallAliasAnalysis::runOnFunction(Function &F) {
38 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
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.
46 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
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)
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
58 const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
59 if (Details == nullptr)
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.
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)
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);
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);
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)
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) {
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);
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.
116 // Otherwise, return any other info gained so far.
120 // getModRefInfo - Check to see if the specified callsite can clobber the
121 // specified memory object.
123 ModRefInfo LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
124 const MemoryLocation &Loc) {
125 ModRefInfo MRInfo = MRI_ModRef;
127 // If this is a direct call to a function that LCI knows about, get the
128 // information about the runtime function.
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)
139 // The AliasAnalysis base class has some smarts, lets use them.
140 return (ModRefInfo)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));