blob: 991a0e3e2752c91cd4208b80bea7820f6da4e9fc [file] [log] [blame]
Chris Lattner621d33b2008-05-07 19:53:05 +00001//===- LibCallAliasAnalysis.cpp - Implement AliasAnalysis for libcalls ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LibCallAliasAnalysis class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner3816aa72008-06-05 23:45:18 +000014#include "llvm/Analysis/LibCallAliasAnalysis.h"
Chris Lattner621d33b2008-05-07 19:53:05 +000015#include "llvm/Analysis/LibCallSemantics.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Analysis/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/Function.h"
Chris Lattner621d33b2008-05-07 19:53:05 +000018#include "llvm/Pass.h"
Chris Lattner621d33b2008-05-07 19:53:05 +000019using namespace llvm;
Dan Gohmanf52c8042008-05-15 19:43:55 +000020
21// Register this pass...
22char LibCallAliasAnalysis::ID = 0;
Owen Andersonac4a1ed2010-07-21 23:07:00 +000023INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis, "libcall-aa",
Owen Andersondf7a4f22010-10-07 22:25:06 +000024 "LibCall Alias Analysis", false, true, false)
Chris Lattner621d33b2008-05-07 19:53:05 +000025
26FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
27 return new LibCallAliasAnalysis(LCI);
28}
29
Chris Lattner3816aa72008-06-05 23:45:18 +000030LibCallAliasAnalysis::~LibCallAliasAnalysis() {
31 delete LCI;
32}
33
34void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
35 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner3816aa72008-06-05 23:45:18 +000036 AU.setPreservesAll(); // Does not transform code
37}
38
Mehdi Amini46a43552015-03-04 18:43:29 +000039bool LibCallAliasAnalysis::runOnFunction(Function &F) {
40 // set up super class
41 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
42 return false;
43}
Chris Lattner621d33b2008-05-07 19:53:05 +000044
45/// AnalyzeLibCallDetails - Given a call to a function with the specified
46/// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
47/// vs the specified pointer/size.
48AliasAnalysis::ModRefResult
49LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
Dan Gohman41f14cf2010-09-14 21:25:10 +000050 ImmutableCallSite CS,
Chandler Carruthac80dc72015-06-17 07:18:54 +000051 const MemoryLocation &Loc) {
Chris Lattner621d33b2008-05-07 19:53:05 +000052 // If we have a function, check to see what kind of mod/ref effects it
53 // has. Start by including any info globally known about the function.
54 AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
55 if (MRInfo == NoModRef) return MRInfo;
56
57 // If that didn't tell us that the function is 'readnone', check to see
58 // if we have detailed info and if 'P' is any of the locations we know
59 // about.
60 const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
Craig Topper9f008862014-04-15 04:59:12 +000061 if (Details == nullptr)
Chris Lattner621d33b2008-05-07 19:53:05 +000062 return MRInfo;
63
64 // If the details array is of the 'DoesNot' kind, we only know something if
65 // the pointer is a match for one of the locations in 'Details'. If we find a
66 // match, we can prove some interactions cannot happen.
67 //
68 if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
69 // Find out if the pointer refers to a known location.
70 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
Dan Gohman41f14cf2010-09-14 21:25:10 +000071 const LibCallLocationInfo &LocInfo =
Chris Lattner621d33b2008-05-07 19:53:05 +000072 LCI->getLocationInfo(Details[i].LocationID);
Dan Gohman41f14cf2010-09-14 21:25:10 +000073 LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
Chris Lattner621d33b2008-05-07 19:53:05 +000074 if (Res != LibCallLocationInfo::Yes) continue;
75
76 // If we find a match against a location that we 'do not' interact with,
77 // learn this info into MRInfo.
78 return ModRefResult(MRInfo & ~Details[i].MRInfo);
79 }
80 return MRInfo;
81 }
82
83 // If the details are of the 'DoesOnly' sort, we know something if the pointer
84 // is a match for one of the locations in 'Details'. Also, if we can prove
85 // that the pointers is *not* one of the locations in 'Details', we know that
86 // the call is NoModRef.
87 assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
88
89 // Find out if the pointer refers to a known location.
90 bool NoneMatch = true;
91 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
Dan Gohman41f14cf2010-09-14 21:25:10 +000092 const LibCallLocationInfo &LocInfo =
Chris Lattner621d33b2008-05-07 19:53:05 +000093 LCI->getLocationInfo(Details[i].LocationID);
Dan Gohman41f14cf2010-09-14 21:25:10 +000094 LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
Chris Lattner621d33b2008-05-07 19:53:05 +000095 if (Res == LibCallLocationInfo::No) continue;
96
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) {
100 NoneMatch = false;
101 continue;
102 }
103
104 // If we know that this pointer definitely is pointing into the location,
105 // merge in this information.
106 return ModRefResult(MRInfo & Details[i].MRInfo);
107 }
108
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 NoModRef.
113 if (NoneMatch)
114 return NoModRef;
115
116 // Otherwise, return any other info gained so far.
117 return MRInfo;
118}
119
120// getModRefInfo - Check to see if the specified callsite can clobber the
121// specified memory object.
122//
123AliasAnalysis::ModRefResult
Dan Gohman5442c712010-08-03 21:48:53 +0000124LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
Chandler Carruthac80dc72015-06-17 07:18:54 +0000125 const MemoryLocation &Loc) {
Chris Lattner621d33b2008-05-07 19:53:05 +0000126 ModRefResult MRInfo = ModRef;
127
128 // If this is a direct call to a function that LCI knows about, get the
129 // information about the runtime function.
Chris Lattner0282d022008-06-05 23:38:34 +0000130 if (LCI) {
Dan Gohman5442c712010-08-03 21:48:53 +0000131 if (const Function *F = CS.getCalledFunction()) {
Chris Lattner621d33b2008-05-07 19:53:05 +0000132 if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
Dan Gohman41f14cf2010-09-14 21:25:10 +0000133 MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
Chris Lattner621d33b2008-05-07 19:53:05 +0000134 if (MRInfo == NoModRef) return NoModRef;
135 }
136 }
137 }
138
139 // The AliasAnalysis base class has some smarts, lets use them.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000140 return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
Chris Lattner621d33b2008-05-07 19:53:05 +0000141}