llvm.org GIT mirror llvm / 029840c
Add a new LibCallAliasAnalysis pass, which is parameterized by an instance of LibCallInfo to provide mod/ref info of standard library functions. This is powerful enough to say that 'sqrt' is readonly except that it modifies errno, or that "printf doesn't store to memory unless the %n constraint is present" etc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50827 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
3 changed file(s) with 179 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1919 class ImmutablePass;
2020 class ModulePass;
2121 class Pass;
22 class LibCallInfo;
2223
2324 //===--------------------------------------------------------------------===//
2425 //
5960 // analysis.
6061 //
6162 ImmutablePass *createBasicAliasAnalysisPass();
63
64 //===--------------------------------------------------------------------===//
65 //
66 /// createLibCallAliasAnalysisPass - Create an alias analysis pass that knows
67 /// about the semantics of a set of libcalls specified by LCI. The newly
68 /// constructed pass takes ownership of the pointer that is provided.
69 ///
70 FunctionPass *createLibCallAliasAnalysisPass(LibCallInfo *LCI);
6271
6372 //===--------------------------------------------------------------------===//
6473 //
4747 (void) llvm::createArgumentPromotionPass();
4848 (void) llvm::createStructRetPromotionPass();
4949 (void) llvm::createBasicAliasAnalysisPass();
50 (void) llvm::createLibCallAliasAnalysisPass(0);
5051 (void) llvm::createBasicVNPass();
5152 (void) llvm::createBlockPlacementPass();
5253 (void) llvm::createBlockProfilerPass();
0 //===- LibCallAliasAnalysis.cpp - Implement AliasAnalysis for libcalls ----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LibCallAliasAnalysis class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Analysis/Passes.h"
14 #include "llvm/Analysis/LibCallSemantics.h"
15 #include "llvm/Function.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Target/TargetData.h"
18 using namespace llvm;
19
20 namespace llvm {
21 /// LibCallAliasAnalysis - Alias analysis driven from LibCallInfo.
22 struct LibCallAliasAnalysis : public FunctionPass, AliasAnalysis {
23 static char ID; // Class identification
24
25 LibCallInfo *LCI;
26
27 LibCallAliasAnalysis(LibCallInfo *LC = 0)
28 : FunctionPass((intptr_t)&ID), LCI(LC) {
29 }
30 ~LibCallAliasAnalysis() {
31 delete LCI;
32 }
33
34 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
35 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
36 // TODO: Could compare two direct calls against each other if we cared to.
37 return AliasAnalysis::getModRefInfo(CS1,CS2);
38 }
39
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AliasAnalysis::getAnalysisUsage(AU);
42 AU.addRequired();
43 AU.setPreservesAll(); // Does not transform code
44 }
45
46 virtual bool runOnFunction(Function &F) {
47 InitializeAliasAnalysis(this); // set up super class
48 return false;
49 }
50
51 /// hasNoModRefInfoForCalls - We can provide mod/ref information against
52 /// non-escaping allocations.
53 virtual bool hasNoModRefInfoForCalls() const { return false; }
54 private:
55 ModRefResult AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
56 CallSite CS, Value *P, unsigned Size);
57 };
58
59 // Register this pass...
60 char LibCallAliasAnalysis::ID = 0;
61 RegisterPass
62 X("libcall-aa", "LibCall Alias Analysis", false, true);
63
64 // Declare that we implement the AliasAnalysis interface
65 RegisterAnalysisGroup Y(X);
66 } // End of llvm namespace
67
68 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
69 return new LibCallAliasAnalysis(LCI);
70 }
71
72
73 /// AnalyzeLibCallDetails - Given a call to a function with the specified
74 /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
75 /// vs the specified pointer/size.
76 AliasAnalysis::ModRefResult
77 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
78 CallSite CS, Value *P,
79 unsigned Size) {
80 // If we have a function, check to see what kind of mod/ref effects it
81 // has. Start by including any info globally known about the function.
82 AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
83 if (MRInfo == NoModRef) return MRInfo;
84
85 // If that didn't tell us that the function is 'readnone', check to see
86 // if we have detailed info and if 'P' is any of the locations we know
87 // about.
88 const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
89 if (Details == 0)
90 return MRInfo;
91
92 // If the details array is of the 'DoesNot' kind, we only know something if
93 // the pointer is a match for one of the locations in 'Details'. If we find a
94 // match, we can prove some interactions cannot happen.
95 //
96 if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
97 // Find out if the pointer refers to a known location.
98 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
99 const LibCallLocationInfo &Loc =
100 LCI->getLocationInfo(Details[i].LocationID);
101 LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
102 if (Res != LibCallLocationInfo::Yes) continue;
103
104 // If we find a match against a location that we 'do not' interact with,
105 // learn this info into MRInfo.
106 return ModRefResult(MRInfo & ~Details[i].MRInfo);
107 }
108 return MRInfo;
109 }
110
111 // If the details are of the 'DoesOnly' sort, we know something if the pointer
112 // is a match for one of the locations in 'Details'. Also, if we can prove
113 // that the pointers is *not* one of the locations in 'Details', we know that
114 // the call is NoModRef.
115 assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
116
117 // Find out if the pointer refers to a known location.
118 bool NoneMatch = true;
119 for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
120 const LibCallLocationInfo &Loc =
121 LCI->getLocationInfo(Details[i].LocationID);
122 LibCallLocationInfo::LocResult Res = Loc.isLocation(CS, P, Size);
123 if (Res == LibCallLocationInfo::No) continue;
124
125 // If we don't know if this pointer points to the location, then we have to
126 // assume it might alias in some case.
127 if (Res == LibCallLocationInfo::Unknown) {
128 NoneMatch = false;
129 continue;
130 }
131
132 // If we know that this pointer definitely is pointing into the location,
133 // merge in this information.
134 return ModRefResult(MRInfo & Details[i].MRInfo);
135 }
136
137 // If we found that the pointer is guaranteed to not match any of the
138 // locations in our 'DoesOnly' rule, then we know that the pointer must point
139 // to some other location. Since the libcall doesn't mod/ref any other
140 // locations, return NoModRef.
141 if (NoneMatch)
142 return NoModRef;
143
144 // Otherwise, return any other info gained so far.
145 return MRInfo;
146 }
147
148 // getModRefInfo - Check to see if the specified callsite can clobber the
149 // specified memory object.
150 //
151 AliasAnalysis::ModRefResult
152 LibCallAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
153 ModRefResult MRInfo = ModRef;
154
155 // If this is a direct call to a function that LCI knows about, get the
156 // information about the runtime function.
157 if (Function *F = CS.getCalledFunction()) {
158 if (LCI && F->isDeclaration()) {
159 if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
160 MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, P, Size));
161 if (MRInfo == NoModRef) return NoModRef;
162 }
163 }
164 }
165
166 // The AliasAnalysis base class has some smarts, lets use them.
167 return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, P, Size));
168 }