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 char LibCallAliasAnalysis::ID = 0;
21 INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis, "libcall-aa",
22 "LibCall Alias Analysis", false, true, false)
24 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
25 return new LibCallAliasAnalysis(LCI);
28 LibCallAliasAnalysis::~LibCallAliasAnalysis() { delete LCI; }
30 void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
31 AliasAnalysis::getAnalysisUsage(AU);
32 AU.setPreservesAll(); // Does not transform code
35 bool LibCallAliasAnalysis::runOnFunction(Function &F) {
37 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
41 /// Given a call to a function with the specified LibCallFunctionInfo, see if
42 /// we can improve the mod/ref footprint of the call vs the specified
45 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
47 const MemoryLocation &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 ModRefInfo MRInfo = FI->UniversalBehavior;
51 if (MRInfo == MRI_NoModRef)
54 // If that didn't tell us that the function is 'readnone', check to see
55 // if we have detailed info and if 'P' is any of the locations we know
57 const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
58 if (Details == nullptr)
61 // If the details array is of the 'DoesNot' kind, we only know something if
62 // the pointer is a match for one of the locations in 'Details'. If we find a
63 // match, we can prove some interactions cannot happen.
65 if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
66 // Find out if the pointer refers to a known location.
67 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
68 const LibCallLocationInfo &LocInfo =
69 LCI->getLocationInfo(Details[i].LocationID);
70 LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
71 if (Res != LibCallLocationInfo::Yes)
74 // If we find a match against a location that we 'do not' interact with,
75 // learn this info into MRInfo.
76 return ModRefInfo(MRInfo & ~Details[i].MRInfo);
81 // If the details are of the 'DoesOnly' sort, we know something if the pointer
82 // is a match for one of the locations in 'Details'. Also, if we can prove
83 // that the pointers is *not* one of the locations in 'Details', we know that
84 // the call is MRI_NoModRef.
85 assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
87 // Find out if the pointer refers to a known location.
88 bool NoneMatch = true;
89 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
90 const LibCallLocationInfo &LocInfo =
91 LCI->getLocationInfo(Details[i].LocationID);
92 LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
93 if (Res == LibCallLocationInfo::No)
96 // If we don't know if this pointer points to the location, then we have to
97 // assume it might alias in some case.
98 if (Res == LibCallLocationInfo::Unknown) {
103 // If we know that this pointer definitely is pointing into the location,
104 // merge in this information.
105 return ModRefInfo(MRInfo & Details[i].MRInfo);
108 // If we found that the pointer is guaranteed to not match any of the
109 // locations in our 'DoesOnly' rule, then we know that the pointer must point
110 // to some other location. Since the libcall doesn't mod/ref any other
111 // locations, return MRI_NoModRef.
115 // Otherwise, return any other info gained so far.
119 /// Check to see if the specified callsite can clobber the specified memory
121 ModRefInfo LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
122 const MemoryLocation &Loc) {
123 ModRefInfo MRInfo = MRI_ModRef;
125 // If this is a direct call to a function that LCI knows about, get the
126 // information about the runtime function.
128 if (const Function *F = CS.getCalledFunction()) {
129 if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
130 MRInfo = ModRefInfo(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
131 if (MRInfo == MRI_NoModRef)
137 // The AliasAnalysis base class has some smarts, lets use them.
138 return (ModRefInfo)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));