[PM/AA] Run clang-format over the ScopedNoAliasAA pass prior to making
[oota-llvm.git] / lib / Analysis / LibCallAliasAnalysis.cpp
1 //===- 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
14 #include "llvm/Analysis/LibCallAliasAnalysis.h"
15 #include "llvm/Analysis/LibCallSemantics.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/Pass.h"
18 using namespace llvm;
19   
20 char LibCallAliasAnalysis::ID = 0;
21 INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis, "libcall-aa",
22                    "LibCall Alias Analysis", false, true, false)
23
24 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
25   return new LibCallAliasAnalysis(LCI);
26 }
27
28 LibCallAliasAnalysis::~LibCallAliasAnalysis() { delete LCI; }
29
30 void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
31   AliasAnalysis::getAnalysisUsage(AU);
32   AU.setPreservesAll(); // Does not transform code
33 }
34
35 bool LibCallAliasAnalysis::runOnFunction(Function &F) {
36   // set up super class
37   InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
38   return false;
39 }
40
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
43 /// pointer/size.
44 ModRefInfo
45 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
46                                             ImmutableCallSite CS,
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)
52     return MRInfo;
53
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
56   // about.
57   const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
58   if (Details == nullptr)
59     return MRInfo;
60
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.
64   //
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)
72         continue;
73
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);
77     }
78     return MRInfo;
79   }
80
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);
86
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)
94       continue;
95
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) {
99       NoneMatch = false;
100       continue;
101     }
102
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);
106   }
107
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.
112   if (NoneMatch)
113     return MRI_NoModRef;
114
115   // Otherwise, return any other info gained so far.
116   return MRInfo;
117 }
118
119 /// Check to see if the specified callsite can clobber the specified memory
120 /// object.
121 ModRefInfo LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
122                                                const MemoryLocation &Loc) {
123   ModRefInfo MRInfo = MRI_ModRef;
124
125   // If this is a direct call to a function that LCI knows about, get the
126   // information about the runtime function.
127   if (LCI) {
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)
132           return MRI_NoModRef;
133       }
134     }
135   }
136
137   // The AliasAnalysis base class has some smarts, lets use them.
138   return (ModRefInfo)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
139 }