Mercurial > hg > CbC > CbC_llvm
comparison lib/Transforms/IPO/FunctionAttrs.cpp @ 95:afa8332a0e37 LLVM3.8
LLVM 3.8
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Oct 2015 17:48:58 +0900 |
parents | 60c9769439b8 |
children | 7d135dc70f03 |
comparison
equal
deleted
inserted
replaced
84:f3e34b893a5f | 95:afa8332a0e37 |
---|---|
22 #include "llvm/ADT/SCCIterator.h" | 22 #include "llvm/ADT/SCCIterator.h" |
23 #include "llvm/ADT/SetVector.h" | 23 #include "llvm/ADT/SetVector.h" |
24 #include "llvm/ADT/SmallSet.h" | 24 #include "llvm/ADT/SmallSet.h" |
25 #include "llvm/ADT/Statistic.h" | 25 #include "llvm/ADT/Statistic.h" |
26 #include "llvm/Analysis/AliasAnalysis.h" | 26 #include "llvm/Analysis/AliasAnalysis.h" |
27 #include "llvm/Analysis/AssumptionCache.h" | |
28 #include "llvm/Analysis/BasicAliasAnalysis.h" | |
27 #include "llvm/Analysis/CallGraph.h" | 29 #include "llvm/Analysis/CallGraph.h" |
28 #include "llvm/Analysis/CallGraphSCCPass.h" | 30 #include "llvm/Analysis/CallGraphSCCPass.h" |
29 #include "llvm/Analysis/CaptureTracking.h" | 31 #include "llvm/Analysis/CaptureTracking.h" |
32 #include "llvm/Analysis/TargetLibraryInfo.h" | |
33 #include "llvm/Analysis/ValueTracking.h" | |
30 #include "llvm/IR/GlobalVariable.h" | 34 #include "llvm/IR/GlobalVariable.h" |
31 #include "llvm/IR/InstIterator.h" | 35 #include "llvm/IR/InstIterator.h" |
32 #include "llvm/IR/IntrinsicInst.h" | 36 #include "llvm/IR/IntrinsicInst.h" |
33 #include "llvm/IR/LLVMContext.h" | 37 #include "llvm/IR/LLVMContext.h" |
38 #include "llvm/Support/Debug.h" | |
39 #include "llvm/Support/raw_ostream.h" | |
34 #include "llvm/Analysis/TargetLibraryInfo.h" | 40 #include "llvm/Analysis/TargetLibraryInfo.h" |
35 using namespace llvm; | 41 using namespace llvm; |
36 | 42 |
37 #define DEBUG_TYPE "functionattrs" | 43 #define DEBUG_TYPE "functionattrs" |
38 | 44 |
40 STATISTIC(NumReadOnly, "Number of functions marked readonly"); | 46 STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
41 STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); | 47 STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); |
42 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); | 48 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); |
43 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); | 49 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); |
44 STATISTIC(NumNoAlias, "Number of function returns marked noalias"); | 50 STATISTIC(NumNoAlias, "Number of function returns marked noalias"); |
51 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); | |
45 STATISTIC(NumAnnotated, "Number of attributes added to library functions"); | 52 STATISTIC(NumAnnotated, "Number of attributes added to library functions"); |
46 | 53 |
47 namespace { | 54 namespace { |
48 struct FunctionAttrs : public CallGraphSCCPass { | 55 struct FunctionAttrs : public CallGraphSCCPass { |
49 static char ID; // Pass identification, replacement for typeid | 56 static char ID; // Pass identification, replacement for typeid |
50 FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) { | 57 FunctionAttrs() : CallGraphSCCPass(ID) { |
51 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); | 58 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); |
52 } | 59 } |
53 | 60 |
54 // runOnSCC - Analyze the SCC, performing the transformation if possible. | 61 bool runOnSCC(CallGraphSCC &SCC) override; |
55 bool runOnSCC(CallGraphSCC &SCC) override; | 62 |
56 | 63 void getAnalysisUsage(AnalysisUsage &AU) const override { |
57 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. | 64 AU.setPreservesCFG(); |
58 bool AddReadAttrs(const CallGraphSCC &SCC); | 65 AU.addRequired<AssumptionCacheTracker>(); |
59 | 66 AU.addRequired<TargetLibraryInfoWrapperPass>(); |
60 // AddArgumentAttrs - Deduce nocapture attributes for the SCC. | 67 CallGraphSCCPass::getAnalysisUsage(AU); |
61 bool AddArgumentAttrs(const CallGraphSCC &SCC); | 68 } |
62 | 69 |
63 // IsFunctionMallocLike - Does this function allocate new memory? | 70 private: |
64 bool IsFunctionMallocLike(Function *F, | 71 TargetLibraryInfo *TLI; |
65 SmallPtrSet<Function*, 8> &) const; | 72 |
66 | 73 bool AddReadAttrs(const CallGraphSCC &SCC); |
67 // AddNoAliasAttrs - Deduce noalias attributes for the SCC. | 74 bool AddArgumentAttrs(const CallGraphSCC &SCC); |
68 bool AddNoAliasAttrs(const CallGraphSCC &SCC); | 75 bool AddNoAliasAttrs(const CallGraphSCC &SCC); |
69 | 76 bool AddNonNullAttrs(const CallGraphSCC &SCC); |
70 // Utility methods used by inferPrototypeAttributes to add attributes | 77 bool annotateLibraryCalls(const CallGraphSCC &SCC); |
71 // and maintain annotation statistics. | 78 }; |
72 | |
73 void setDoesNotAccessMemory(Function &F) { | |
74 if (!F.doesNotAccessMemory()) { | |
75 F.setDoesNotAccessMemory(); | |
76 ++NumAnnotated; | |
77 } | |
78 } | |
79 | |
80 void setOnlyReadsMemory(Function &F) { | |
81 if (!F.onlyReadsMemory()) { | |
82 F.setOnlyReadsMemory(); | |
83 ++NumAnnotated; | |
84 } | |
85 } | |
86 | |
87 void setDoesNotThrow(Function &F) { | |
88 if (!F.doesNotThrow()) { | |
89 F.setDoesNotThrow(); | |
90 ++NumAnnotated; | |
91 } | |
92 } | |
93 | |
94 void setDoesNotCapture(Function &F, unsigned n) { | |
95 if (!F.doesNotCapture(n)) { | |
96 F.setDoesNotCapture(n); | |
97 ++NumAnnotated; | |
98 } | |
99 } | |
100 | |
101 void setOnlyReadsMemory(Function &F, unsigned n) { | |
102 if (!F.onlyReadsMemory(n)) { | |
103 F.setOnlyReadsMemory(n); | |
104 ++NumAnnotated; | |
105 } | |
106 } | |
107 | |
108 void setDoesNotAlias(Function &F, unsigned n) { | |
109 if (!F.doesNotAlias(n)) { | |
110 F.setDoesNotAlias(n); | |
111 ++NumAnnotated; | |
112 } | |
113 } | |
114 | |
115 // inferPrototypeAttributes - Analyze the name and prototype of the | |
116 // given function and set any applicable attributes. Returns true | |
117 // if any attributes were set and false otherwise. | |
118 bool inferPrototypeAttributes(Function &F); | |
119 | |
120 // annotateLibraryCalls - Adds attributes to well-known standard library | |
121 // call declarations. | |
122 bool annotateLibraryCalls(const CallGraphSCC &SCC); | |
123 | |
124 void getAnalysisUsage(AnalysisUsage &AU) const override { | |
125 AU.setPreservesCFG(); | |
126 AU.addRequired<AliasAnalysis>(); | |
127 AU.addRequired<TargetLibraryInfoWrapperPass>(); | |
128 CallGraphSCCPass::getAnalysisUsage(AU); | |
129 } | |
130 | |
131 private: | |
132 AliasAnalysis *AA; | |
133 TargetLibraryInfo *TLI; | |
134 }; | |
135 } | 79 } |
136 | 80 |
137 char FunctionAttrs::ID = 0; | 81 char FunctionAttrs::ID = 0; |
138 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", | 82 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", |
139 "Deduce function attributes", false, false) | 83 "Deduce function attributes", false, false) |
140 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) | 84 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
141 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) | 85 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
142 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) | 86 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
143 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", | 87 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", |
144 "Deduce function attributes", false, false) | 88 "Deduce function attributes", false, false) |
145 | 89 |
146 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } | 90 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } |
147 | 91 |
148 | 92 namespace { |
149 /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. | 93 /// The three kinds of memory access relevant to 'readonly' and |
94 /// 'readnone' attributes. | |
95 enum MemoryAccessKind { | |
96 MAK_ReadNone = 0, | |
97 MAK_ReadOnly = 1, | |
98 MAK_MayWrite = 2 | |
99 }; | |
100 } | |
101 | |
102 static MemoryAccessKind | |
103 checkFunctionMemoryAccess(Function &F, AAResults &AAR, | |
104 const SmallPtrSetImpl<Function *> &SCCNodes) { | |
105 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F); | |
106 if (MRB == FMRB_DoesNotAccessMemory) | |
107 // Already perfect! | |
108 return MAK_ReadNone; | |
109 | |
110 // Definitions with weak linkage may be overridden at linktime with | |
111 // something that writes memory, so treat them like declarations. | |
112 if (F.isDeclaration() || F.mayBeOverridden()) { | |
113 if (AliasAnalysis::onlyReadsMemory(MRB)) | |
114 return MAK_ReadOnly; | |
115 | |
116 // Conservatively assume it writes to memory. | |
117 return MAK_MayWrite; | |
118 } | |
119 | |
120 // Scan the function body for instructions that may read or write memory. | |
121 bool ReadsMemory = false; | |
122 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { | |
123 Instruction *I = &*II; | |
124 | |
125 // Some instructions can be ignored even if they read or write memory. | |
126 // Detect these now, skipping to the next instruction if one is found. | |
127 CallSite CS(cast<Value>(I)); | |
128 if (CS) { | |
129 // Ignore calls to functions in the same SCC. | |
130 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) | |
131 continue; | |
132 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS); | |
133 // If the call doesn't access arbitrary memory, we may be able to | |
134 // figure out something. | |
135 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { | |
136 // If the call does access argument pointees, check each argument. | |
137 if (AliasAnalysis::doesAccessArgPointees(MRB)) | |
138 // Check whether all pointer arguments point to local memory, and | |
139 // ignore calls that only access local memory. | |
140 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); | |
141 CI != CE; ++CI) { | |
142 Value *Arg = *CI; | |
143 if (Arg->getType()->isPointerTy()) { | |
144 AAMDNodes AAInfo; | |
145 I->getAAMetadata(AAInfo); | |
146 | |
147 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo); | |
148 if (!AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) { | |
149 if (MRB & MRI_Mod) | |
150 // Writes non-local memory. Give up. | |
151 return MAK_MayWrite; | |
152 if (MRB & MRI_Ref) | |
153 // Ok, it reads non-local memory. | |
154 ReadsMemory = true; | |
155 } | |
156 } | |
157 } | |
158 continue; | |
159 } | |
160 // The call could access any memory. If that includes writes, give up. | |
161 if (MRB & MRI_Mod) | |
162 return MAK_MayWrite; | |
163 // If it reads, note it. | |
164 if (MRB & MRI_Ref) | |
165 ReadsMemory = true; | |
166 continue; | |
167 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { | |
168 // Ignore non-volatile loads from local memory. (Atomic is okay here.) | |
169 if (!LI->isVolatile()) { | |
170 MemoryLocation Loc = MemoryLocation::get(LI); | |
171 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) | |
172 continue; | |
173 } | |
174 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { | |
175 // Ignore non-volatile stores to local memory. (Atomic is okay here.) | |
176 if (!SI->isVolatile()) { | |
177 MemoryLocation Loc = MemoryLocation::get(SI); | |
178 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) | |
179 continue; | |
180 } | |
181 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { | |
182 // Ignore vaargs on local memory. | |
183 MemoryLocation Loc = MemoryLocation::get(VI); | |
184 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) | |
185 continue; | |
186 } | |
187 | |
188 // Any remaining instructions need to be taken seriously! Check if they | |
189 // read or write memory. | |
190 if (I->mayWriteToMemory()) | |
191 // Writes memory. Just give up. | |
192 return MAK_MayWrite; | |
193 | |
194 // If this instruction may read memory, remember that. | |
195 ReadsMemory |= I->mayReadFromMemory(); | |
196 } | |
197 | |
198 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone; | |
199 } | |
200 | |
201 /// Deduce readonly/readnone attributes for the SCC. | |
150 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { | 202 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { |
151 SmallPtrSet<Function*, 8> SCCNodes; | 203 SmallPtrSet<Function *, 8> SCCNodes; |
152 | 204 |
153 // Fill SCCNodes with the elements of the SCC. Used for quickly | 205 // Fill SCCNodes with the elements of the SCC. Used for quickly |
154 // looking up whether a given CallGraphNode is in this SCC. | 206 // looking up whether a given CallGraphNode is in this SCC. |
155 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) | 207 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
156 SCCNodes.insert((*I)->getFunction()); | 208 SCCNodes.insert((*I)->getFunction()); |
164 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) | 216 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) |
165 // External node or node we don't want to optimize - assume it may write | 217 // External node or node we don't want to optimize - assume it may write |
166 // memory and give up. | 218 // memory and give up. |
167 return false; | 219 return false; |
168 | 220 |
169 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F); | 221 // We need to manually construct BasicAA directly in order to disable its |
170 if (MRB == AliasAnalysis::DoesNotAccessMemory) | 222 // use of other function analyses. |
171 // Already perfect! | 223 BasicAAResult BAR(createLegacyPMBasicAAResult(*this, *F)); |
172 continue; | 224 |
173 | 225 // Construct our own AA results for this function. We do this manually to |
174 // Definitions with weak linkage may be overridden at linktime with | 226 // work around the limitations of the legacy pass manager. |
175 // something that writes memory, so treat them like declarations. | 227 AAResults AAR(createLegacyPMAAResults(*this, *F, BAR)); |
176 if (F->isDeclaration() || F->mayBeOverridden()) { | 228 |
177 if (!AliasAnalysis::onlyReadsMemory(MRB)) | 229 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) { |
178 // May write memory. Just give up. | 230 case MAK_MayWrite: |
179 return false; | 231 return false; |
180 | 232 case MAK_ReadOnly: |
181 ReadsMemory = true; | 233 ReadsMemory = true; |
182 continue; | 234 break; |
183 } | 235 case MAK_ReadNone: |
184 | 236 // Nothing to do! |
185 // Scan the function body for instructions that may read or write memory. | 237 break; |
186 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { | |
187 Instruction *I = &*II; | |
188 | |
189 // Some instructions can be ignored even if they read or write memory. | |
190 // Detect these now, skipping to the next instruction if one is found. | |
191 CallSite CS(cast<Value>(I)); | |
192 if (CS) { | |
193 // Ignore calls to functions in the same SCC. | |
194 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) | |
195 continue; | |
196 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS); | |
197 // If the call doesn't access arbitrary memory, we may be able to | |
198 // figure out something. | |
199 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { | |
200 // If the call does access argument pointees, check each argument. | |
201 if (AliasAnalysis::doesAccessArgPointees(MRB)) | |
202 // Check whether all pointer arguments point to local memory, and | |
203 // ignore calls that only access local memory. | |
204 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); | |
205 CI != CE; ++CI) { | |
206 Value *Arg = *CI; | |
207 if (Arg->getType()->isPointerTy()) { | |
208 AAMDNodes AAInfo; | |
209 I->getAAMetadata(AAInfo); | |
210 | |
211 AliasAnalysis::Location Loc(Arg, | |
212 AliasAnalysis::UnknownSize, AAInfo); | |
213 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) { | |
214 if (MRB & AliasAnalysis::Mod) | |
215 // Writes non-local memory. Give up. | |
216 return false; | |
217 if (MRB & AliasAnalysis::Ref) | |
218 // Ok, it reads non-local memory. | |
219 ReadsMemory = true; | |
220 } | |
221 } | |
222 } | |
223 continue; | |
224 } | |
225 // The call could access any memory. If that includes writes, give up. | |
226 if (MRB & AliasAnalysis::Mod) | |
227 return false; | |
228 // If it reads, note it. | |
229 if (MRB & AliasAnalysis::Ref) | |
230 ReadsMemory = true; | |
231 continue; | |
232 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { | |
233 // Ignore non-volatile loads from local memory. (Atomic is okay here.) | |
234 if (!LI->isVolatile()) { | |
235 AliasAnalysis::Location Loc = AA->getLocation(LI); | |
236 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) | |
237 continue; | |
238 } | |
239 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { | |
240 // Ignore non-volatile stores to local memory. (Atomic is okay here.) | |
241 if (!SI->isVolatile()) { | |
242 AliasAnalysis::Location Loc = AA->getLocation(SI); | |
243 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) | |
244 continue; | |
245 } | |
246 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { | |
247 // Ignore vaargs on local memory. | |
248 AliasAnalysis::Location Loc = AA->getLocation(VI); | |
249 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) | |
250 continue; | |
251 } | |
252 | |
253 // Any remaining instructions need to be taken seriously! Check if they | |
254 // read or write memory. | |
255 if (I->mayWriteToMemory()) | |
256 // Writes memory. Just give up. | |
257 return false; | |
258 | |
259 // If this instruction may read memory, remember that. | |
260 ReadsMemory |= I->mayReadFromMemory(); | |
261 } | 238 } |
262 } | 239 } |
263 | 240 |
264 // Success! Functions in this SCC do not access memory, or only read memory. | 241 // Success! Functions in this SCC do not access memory, or only read memory. |
265 // Give them the appropriate attribute. | 242 // Give them the appropriate attribute. |
277 | 254 |
278 MadeChange = true; | 255 MadeChange = true; |
279 | 256 |
280 // Clear out any existing attributes. | 257 // Clear out any existing attributes. |
281 AttrBuilder B; | 258 AttrBuilder B; |
282 B.addAttribute(Attribute::ReadOnly) | 259 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); |
283 .addAttribute(Attribute::ReadNone); | 260 F->removeAttributes( |
284 F->removeAttributes(AttributeSet::FunctionIndex, | 261 AttributeSet::FunctionIndex, |
285 AttributeSet::get(F->getContext(), | 262 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B)); |
286 AttributeSet::FunctionIndex, B)); | |
287 | 263 |
288 // Add in the new attribute. | 264 // Add in the new attribute. |
289 F->addAttribute(AttributeSet::FunctionIndex, | 265 F->addAttribute(AttributeSet::FunctionIndex, |
290 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); | 266 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); |
291 | 267 |
297 | 273 |
298 return MadeChange; | 274 return MadeChange; |
299 } | 275 } |
300 | 276 |
301 namespace { | 277 namespace { |
302 // For a given pointer Argument, this retains a list of Arguments of functions | 278 /// For a given pointer Argument, this retains a list of Arguments of functions |
303 // in the same SCC that the pointer data flows into. We use this to build an | 279 /// in the same SCC that the pointer data flows into. We use this to build an |
304 // SCC of the arguments. | 280 /// SCC of the arguments. |
305 struct ArgumentGraphNode { | 281 struct ArgumentGraphNode { |
306 Argument *Definition; | 282 Argument *Definition; |
307 SmallVector<ArgumentGraphNode*, 4> Uses; | 283 SmallVector<ArgumentGraphNode *, 4> Uses; |
308 }; | 284 }; |
309 | 285 |
310 class ArgumentGraph { | 286 class ArgumentGraph { |
311 // We store pointers to ArgumentGraphNode objects, so it's important that | 287 // We store pointers to ArgumentGraphNode objects, so it's important that |
312 // that they not move around upon insert. | 288 // that they not move around upon insert. |
313 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy; | 289 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy; |
314 | 290 |
315 ArgumentMapTy ArgumentMap; | 291 ArgumentMapTy ArgumentMap; |
316 | 292 |
317 // There is no root node for the argument graph, in fact: | 293 // There is no root node for the argument graph, in fact: |
318 // void f(int *x, int *y) { if (...) f(x, y); } | 294 // void f(int *x, int *y) { if (...) f(x, y); } |
319 // is an example where the graph is disconnected. The SCCIterator requires a | 295 // is an example where the graph is disconnected. The SCCIterator requires a |
320 // single entry point, so we maintain a fake ("synthetic") root node that | 296 // single entry point, so we maintain a fake ("synthetic") root node that |
321 // uses every node. Because the graph is directed and nothing points into | 297 // uses every node. Because the graph is directed and nothing points into |
322 // the root, it will not participate in any SCCs (except for its own). | 298 // the root, it will not participate in any SCCs (except for its own). |
323 ArgumentGraphNode SyntheticRoot; | 299 ArgumentGraphNode SyntheticRoot; |
324 | 300 |
325 public: | 301 public: |
326 ArgumentGraph() { SyntheticRoot.Definition = nullptr; } | 302 ArgumentGraph() { SyntheticRoot.Definition = nullptr; } |
327 | 303 |
328 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator; | 304 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator; |
329 | 305 |
330 iterator begin() { return SyntheticRoot.Uses.begin(); } | 306 iterator begin() { return SyntheticRoot.Uses.begin(); } |
331 iterator end() { return SyntheticRoot.Uses.end(); } | 307 iterator end() { return SyntheticRoot.Uses.end(); } |
332 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } | 308 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } |
333 | 309 |
334 ArgumentGraphNode *operator[](Argument *A) { | 310 ArgumentGraphNode *operator[](Argument *A) { |
335 ArgumentGraphNode &Node = ArgumentMap[A]; | 311 ArgumentGraphNode &Node = ArgumentMap[A]; |
336 Node.Definition = A; | 312 Node.Definition = A; |
337 SyntheticRoot.Uses.push_back(&Node); | 313 SyntheticRoot.Uses.push_back(&Node); |
338 return &Node; | 314 return &Node; |
339 } | 315 } |
340 }; | 316 }; |
341 | 317 |
342 // This tracker checks whether callees are in the SCC, and if so it does not | 318 /// This tracker checks whether callees are in the SCC, and if so it does not |
343 // consider that a capture, instead adding it to the "Uses" list and | 319 /// consider that a capture, instead adding it to the "Uses" list and |
344 // continuing with the analysis. | 320 /// continuing with the analysis. |
345 struct ArgumentUsesTracker : public CaptureTracker { | 321 struct ArgumentUsesTracker : public CaptureTracker { |
346 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes) | 322 ArgumentUsesTracker(const SmallPtrSet<Function *, 8> &SCCNodes) |
347 : Captured(false), SCCNodes(SCCNodes) {} | 323 : Captured(false), SCCNodes(SCCNodes) {} |
348 | 324 |
349 void tooManyUses() override { Captured = true; } | 325 void tooManyUses() override { Captured = true; } |
350 | 326 |
351 bool captured(const Use *U) override { | 327 bool captured(const Use *U) override { |
352 CallSite CS(U->getUser()); | 328 CallSite CS(U->getUser()); |
353 if (!CS.getInstruction()) { Captured = true; return true; } | 329 if (!CS.getInstruction()) { |
354 | 330 Captured = true; |
355 Function *F = CS.getCalledFunction(); | 331 return true; |
356 if (!F || !SCCNodes.count(F)) { Captured = true; return true; } | 332 } |
357 | 333 |
358 bool Found = false; | 334 Function *F = CS.getCalledFunction(); |
359 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); | 335 if (!F || !SCCNodes.count(F)) { |
360 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end(); | 336 Captured = true; |
361 PI != PE; ++PI, ++AI) { | 337 return true; |
362 if (AI == AE) { | 338 } |
363 assert(F->isVarArg() && "More params than args in non-varargs call"); | 339 |
364 Captured = true; | 340 bool Found = false; |
365 return true; | 341 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
366 } | 342 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end(); |
367 if (PI == U) { | 343 PI != PE; ++PI, ++AI) { |
368 Uses.push_back(AI); | 344 if (AI == AE) { |
369 Found = true; | 345 assert(F->isVarArg() && "More params than args in non-varargs call"); |
370 break; | 346 Captured = true; |
371 } | 347 return true; |
372 } | 348 } |
373 assert(Found && "Capturing call-site captured nothing?"); | 349 if (PI == U) { |
374 (void)Found; | 350 Uses.push_back(AI); |
375 return false; | 351 Found = true; |
376 } | 352 break; |
377 | 353 } |
378 bool Captured; // True only if certainly captured (used outside our SCC). | 354 } |
379 SmallVector<Argument*, 4> Uses; // Uses within our SCC. | 355 assert(Found && "Capturing call-site captured nothing?"); |
380 | 356 (void)Found; |
381 const SmallPtrSet<Function*, 8> &SCCNodes; | 357 return false; |
382 }; | 358 } |
359 | |
360 bool Captured; // True only if certainly captured (used outside our SCC). | |
361 SmallVector<Argument *, 4> Uses; // Uses within our SCC. | |
362 | |
363 const SmallPtrSet<Function *, 8> &SCCNodes; | |
364 }; | |
383 } | 365 } |
384 | 366 |
385 namespace llvm { | 367 namespace llvm { |
386 template<> struct GraphTraits<ArgumentGraphNode*> { | 368 template <> struct GraphTraits<ArgumentGraphNode *> { |
387 typedef ArgumentGraphNode NodeType; | 369 typedef ArgumentGraphNode NodeType; |
388 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType; | 370 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType; |
389 | 371 |
390 static inline NodeType *getEntryNode(NodeType *A) { return A; } | 372 static inline NodeType *getEntryNode(NodeType *A) { return A; } |
391 static inline ChildIteratorType child_begin(NodeType *N) { | 373 static inline ChildIteratorType child_begin(NodeType *N) { |
392 return N->Uses.begin(); | 374 return N->Uses.begin(); |
393 } | 375 } |
394 static inline ChildIteratorType child_end(NodeType *N) { | 376 static inline ChildIteratorType child_end(NodeType *N) { |
395 return N->Uses.end(); | 377 return N->Uses.end(); |
396 } | 378 } |
397 }; | 379 }; |
398 template<> struct GraphTraits<ArgumentGraph*> | 380 template <> |
399 : public GraphTraits<ArgumentGraphNode*> { | 381 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> { |
400 static NodeType *getEntryNode(ArgumentGraph *AG) { | 382 static NodeType *getEntryNode(ArgumentGraph *AG) { |
401 return AG->getEntryNode(); | 383 return AG->getEntryNode(); |
402 } | 384 } |
403 static ChildIteratorType nodes_begin(ArgumentGraph *AG) { | 385 static ChildIteratorType nodes_begin(ArgumentGraph *AG) { |
404 return AG->begin(); | 386 return AG->begin(); |
405 } | 387 } |
406 static ChildIteratorType nodes_end(ArgumentGraph *AG) { | 388 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); } |
407 return AG->end(); | 389 }; |
408 } | 390 } |
409 }; | 391 |
410 } | 392 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. |
411 | |
412 // Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. | |
413 static Attribute::AttrKind | 393 static Attribute::AttrKind |
414 determinePointerReadAttrs(Argument *A, | 394 determinePointerReadAttrs(Argument *A, |
415 const SmallPtrSet<Argument*, 8> &SCCNodes) { | 395 const SmallPtrSet<Argument *, 8> &SCCNodes) { |
416 | 396 |
417 SmallVector<Use*, 32> Worklist; | 397 SmallVector<Use *, 32> Worklist; |
418 SmallSet<Use*, 32> Visited; | 398 SmallSet<Use *, 32> Visited; |
419 int Count = 0; | |
420 | 399 |
421 // inalloca arguments are always clobbered by the call. | 400 // inalloca arguments are always clobbered by the call. |
422 if (A->hasInAllocaAttr()) | 401 if (A->hasInAllocaAttr()) |
423 return Attribute::None; | 402 return Attribute::None; |
424 | 403 |
425 bool IsRead = false; | 404 bool IsRead = false; |
426 // We don't need to track IsWritten. If A is written to, return immediately. | 405 // We don't need to track IsWritten. If A is written to, return immediately. |
427 | 406 |
428 for (Use &U : A->uses()) { | 407 for (Use &U : A->uses()) { |
429 if (Count++ >= 20) | |
430 return Attribute::None; | |
431 | |
432 Visited.insert(&U); | 408 Visited.insert(&U); |
433 Worklist.push_back(&U); | 409 Worklist.push_back(&U); |
434 } | 410 } |
435 | 411 |
436 while (!Worklist.empty()) { | 412 while (!Worklist.empty()) { |
516 } | 492 } |
517 | 493 |
518 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; | 494 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; |
519 } | 495 } |
520 | 496 |
521 /// AddArgumentAttrs - Deduce nocapture attributes for the SCC. | 497 /// Deduce nocapture attributes for the SCC. |
522 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) { | 498 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) { |
523 bool Changed = false; | 499 bool Changed = false; |
524 | 500 |
525 SmallPtrSet<Function*, 8> SCCNodes; | 501 SmallPtrSet<Function *, 8> SCCNodes; |
526 | 502 |
527 // Fill SCCNodes with the elements of the SCC. Used for quickly | 503 // Fill SCCNodes with the elements of the SCC. Used for quickly |
528 // looking up whether a given CallGraphNode is in this SCC. | 504 // looking up whether a given CallGraphNode is in this SCC. |
529 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { | 505 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
530 Function *F = (*I)->getFunction(); | 506 Function *F = (*I)->getFunction(); |
555 | 531 |
556 // Functions that are readonly (or readnone) and nounwind and don't return | 532 // Functions that are readonly (or readnone) and nounwind and don't return |
557 // a value can't capture arguments. Don't analyze them. | 533 // a value can't capture arguments. Don't analyze them. |
558 if (F->onlyReadsMemory() && F->doesNotThrow() && | 534 if (F->onlyReadsMemory() && F->doesNotThrow() && |
559 F->getReturnType()->isVoidTy()) { | 535 F->getReturnType()->isVoidTy()) { |
560 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); | 536 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
561 A != E; ++A) { | 537 ++A) { |
562 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { | 538 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { |
563 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); | 539 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); |
564 ++NumNoCapture; | 540 ++NumNoCapture; |
565 Changed = true; | 541 Changed = true; |
566 } | 542 } |
567 } | 543 } |
568 continue; | 544 continue; |
569 } | 545 } |
570 | 546 |
571 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); | 547 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; |
572 A != E; ++A) { | 548 ++A) { |
573 if (!A->getType()->isPointerTy()) continue; | 549 if (!A->getType()->isPointerTy()) |
550 continue; | |
574 bool HasNonLocalUses = false; | 551 bool HasNonLocalUses = false; |
575 if (!A->hasNoCaptureAttr()) { | 552 if (!A->hasNoCaptureAttr()) { |
576 ArgumentUsesTracker Tracker(SCCNodes); | 553 ArgumentUsesTracker Tracker(SCCNodes); |
577 PointerMayBeCaptured(A, &Tracker); | 554 PointerMayBeCaptured(A, &Tracker); |
578 if (!Tracker.Captured) { | 555 if (!Tracker.Captured) { |
579 if (Tracker.Uses.empty()) { | 556 if (Tracker.Uses.empty()) { |
580 // If it's trivially not captured, mark it nocapture now. | 557 // If it's trivially not captured, mark it nocapture now. |
581 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B)); | 558 A->addAttr( |
559 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); | |
582 ++NumNoCapture; | 560 ++NumNoCapture; |
583 Changed = true; | 561 Changed = true; |
584 } else { | 562 } else { |
585 // If it's not trivially captured and not trivially not captured, | 563 // If it's not trivially captured and not trivially not captured, |
586 // then it must be calling into another function in our SCC. Save | 564 // then it must be calling into another function in our SCC. Save |
587 // its particulars for Argument-SCC analysis later. | 565 // its particulars for Argument-SCC analysis later. |
588 ArgumentGraphNode *Node = AG[A]; | 566 ArgumentGraphNode *Node = AG[A]; |
589 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(), | 567 for (SmallVectorImpl<Argument *>::iterator |
590 UE = Tracker.Uses.end(); UI != UE; ++UI) { | 568 UI = Tracker.Uses.begin(), |
569 UE = Tracker.Uses.end(); | |
570 UI != UE; ++UI) { | |
591 Node->Uses.push_back(AG[*UI]); | 571 Node->Uses.push_back(AG[*UI]); |
592 if (*UI != A) | 572 if (*UI != A) |
593 HasNonLocalUses = true; | 573 HasNonLocalUses = true; |
594 } | 574 } |
595 } | 575 } |
599 if (!HasNonLocalUses && !A->onlyReadsMemory()) { | 579 if (!HasNonLocalUses && !A->onlyReadsMemory()) { |
600 // Can we determine that it's readonly/readnone without doing an SCC? | 580 // Can we determine that it's readonly/readnone without doing an SCC? |
601 // Note that we don't allow any calls at all here, or else our result | 581 // Note that we don't allow any calls at all here, or else our result |
602 // will be dependent on the iteration order through the functions in the | 582 // will be dependent on the iteration order through the functions in the |
603 // SCC. | 583 // SCC. |
604 SmallPtrSet<Argument*, 8> Self; | 584 SmallPtrSet<Argument *, 8> Self; |
605 Self.insert(A); | 585 Self.insert(A); |
606 Attribute::AttrKind R = determinePointerReadAttrs(A, Self); | 586 Attribute::AttrKind R = determinePointerReadAttrs(A, Self); |
607 if (R != Attribute::None) { | 587 if (R != Attribute::None) { |
608 AttrBuilder B; | 588 AttrBuilder B; |
609 B.addAttribute(R); | 589 B.addAttribute(R); |
620 // show up as ArgumentGraphNode objects with an empty Uses list, and for | 600 // show up as ArgumentGraphNode objects with an empty Uses list, and for |
621 // these nodes the final decision about whether they capture has already been | 601 // these nodes the final decision about whether they capture has already been |
622 // made. If the definition doesn't have a 'nocapture' attribute by now, it | 602 // made. If the definition doesn't have a 'nocapture' attribute by now, it |
623 // captures. | 603 // captures. |
624 | 604 |
625 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) { | 605 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) { |
626 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; | 606 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; |
627 if (ArgumentSCC.size() == 1) { | 607 if (ArgumentSCC.size() == 1) { |
628 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node | 608 if (!ArgumentSCC[0]->Definition) |
609 continue; // synthetic root node | |
629 | 610 |
630 // eg. "void f(int* x) { if (...) f(x); }" | 611 // eg. "void f(int* x) { if (...) f(x); }" |
631 if (ArgumentSCC[0]->Uses.size() == 1 && | 612 if (ArgumentSCC[0]->Uses.size() == 1 && |
632 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { | 613 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { |
633 Argument *A = ArgumentSCC[0]->Definition; | 614 Argument *A = ArgumentSCC[0]->Definition; |
645 if (Node->Uses.empty()) { | 626 if (Node->Uses.empty()) { |
646 if (!Node->Definition->hasNoCaptureAttr()) | 627 if (!Node->Definition->hasNoCaptureAttr()) |
647 SCCCaptured = true; | 628 SCCCaptured = true; |
648 } | 629 } |
649 } | 630 } |
650 if (SCCCaptured) continue; | 631 if (SCCCaptured) |
651 | 632 continue; |
652 SmallPtrSet<Argument*, 8> ArgumentSCCNodes; | 633 |
634 SmallPtrSet<Argument *, 8> ArgumentSCCNodes; | |
653 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for | 635 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for |
654 // quickly looking up whether a given Argument is in this ArgumentSCC. | 636 // quickly looking up whether a given Argument is in this ArgumentSCC. |
655 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) { | 637 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) { |
656 ArgumentSCCNodes.insert((*I)->Definition); | 638 ArgumentSCCNodes.insert((*I)->Definition); |
657 } | 639 } |
658 | 640 |
659 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); | 641 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); |
660 I != E && !SCCCaptured; ++I) { | 642 I != E && !SCCCaptured; ++I) { |
661 ArgumentGraphNode *N = *I; | 643 ArgumentGraphNode *N = *I; |
662 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(), | 644 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(), |
663 UE = N->Uses.end(); UI != UE; ++UI) { | 645 UE = N->Uses.end(); |
646 UI != UE; ++UI) { | |
664 Argument *A = (*UI)->Definition; | 647 Argument *A = (*UI)->Definition; |
665 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) | 648 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) |
666 continue; | 649 continue; |
667 SCCCaptured = true; | 650 SCCCaptured = true; |
668 break; | 651 break; |
669 } | 652 } |
670 } | 653 } |
671 if (SCCCaptured) continue; | 654 if (SCCCaptured) |
655 continue; | |
672 | 656 |
673 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { | 657 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
674 Argument *A = ArgumentSCC[i]->Definition; | 658 Argument *A = ArgumentSCC[i]->Definition; |
675 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); | 659 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
676 ++NumNoCapture; | 660 ++NumNoCapture; |
701 ReadAttr = K; | 685 ReadAttr = K; |
702 break; | 686 break; |
703 } | 687 } |
704 | 688 |
705 if (ReadAttr != Attribute::None) { | 689 if (ReadAttr != Attribute::None) { |
706 AttrBuilder B; | 690 AttrBuilder B, R; |
707 B.addAttribute(ReadAttr); | 691 B.addAttribute(ReadAttr); |
692 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); | |
708 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { | 693 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
709 Argument *A = ArgumentSCC[i]->Definition; | 694 Argument *A = ArgumentSCC[i]->Definition; |
695 // Clear out existing readonly/readnone attributes | |
696 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R)); | |
710 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); | 697 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); |
711 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; | 698 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; |
712 Changed = true; | 699 Changed = true; |
713 } | 700 } |
714 } | 701 } |
715 } | 702 } |
716 | 703 |
717 return Changed; | 704 return Changed; |
718 } | 705 } |
719 | 706 |
720 /// IsFunctionMallocLike - A function is malloc-like if it returns either null | 707 /// Tests whether a function is "malloc-like". |
721 /// or a pointer that doesn't alias any other pointer visible to the caller. | 708 /// |
722 bool FunctionAttrs::IsFunctionMallocLike(Function *F, | 709 /// A function is "malloc-like" if it returns either null or a pointer that |
723 SmallPtrSet<Function*, 8> &SCCNodes) const { | 710 /// doesn't alias any other pointer visible to the caller. |
711 static bool isFunctionMallocLike(Function *F, | |
712 SmallPtrSet<Function *, 8> &SCCNodes) { | |
724 SmallSetVector<Value *, 8> FlowsToReturn; | 713 SmallSetVector<Value *, 8> FlowsToReturn; |
725 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) | 714 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
726 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) | 715 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) |
727 FlowsToReturn.insert(Ret->getReturnValue()); | 716 FlowsToReturn.insert(Ret->getReturnValue()); |
728 | 717 |
739 if (isa<Argument>(RetVal)) | 728 if (isa<Argument>(RetVal)) |
740 return false; | 729 return false; |
741 | 730 |
742 if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) | 731 if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
743 switch (RVI->getOpcode()) { | 732 switch (RVI->getOpcode()) { |
744 // Extend the analysis by looking upwards. | 733 // Extend the analysis by looking upwards. |
745 case Instruction::BitCast: | 734 case Instruction::BitCast: |
746 case Instruction::GetElementPtr: | 735 case Instruction::GetElementPtr: |
747 case Instruction::AddrSpaceCast: | 736 case Instruction::AddrSpaceCast: |
748 FlowsToReturn.insert(RVI->getOperand(0)); | 737 FlowsToReturn.insert(RVI->getOperand(0)); |
749 continue; | 738 continue; |
750 case Instruction::Select: { | 739 case Instruction::Select: { |
751 SelectInst *SI = cast<SelectInst>(RVI); | 740 SelectInst *SI = cast<SelectInst>(RVI); |
752 FlowsToReturn.insert(SI->getTrueValue()); | 741 FlowsToReturn.insert(SI->getTrueValue()); |
753 FlowsToReturn.insert(SI->getFalseValue()); | 742 FlowsToReturn.insert(SI->getFalseValue()); |
754 continue; | 743 continue; |
755 } | 744 } |
756 case Instruction::PHI: { | 745 case Instruction::PHI: { |
757 PHINode *PN = cast<PHINode>(RVI); | 746 PHINode *PN = cast<PHINode>(RVI); |
758 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) | 747 for (Value *IncValue : PN->incoming_values()) |
759 FlowsToReturn.insert(PN->getIncomingValue(i)); | 748 FlowsToReturn.insert(IncValue); |
760 continue; | 749 continue; |
761 } | 750 } |
762 | 751 |
763 // Check whether the pointer came from an allocation. | 752 // Check whether the pointer came from an allocation. |
764 case Instruction::Alloca: | 753 case Instruction::Alloca: |
754 break; | |
755 case Instruction::Call: | |
756 case Instruction::Invoke: { | |
757 CallSite CS(RVI); | |
758 if (CS.paramHasAttr(0, Attribute::NoAlias)) | |
765 break; | 759 break; |
766 case Instruction::Call: | 760 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) |
767 case Instruction::Invoke: { | 761 break; |
768 CallSite CS(RVI); | 762 } // fall-through |
769 if (CS.paramHasAttr(0, Attribute::NoAlias)) | 763 default: |
770 break; | 764 return false; // Did not come from an allocation. |
771 if (CS.getCalledFunction() && | |
772 SCCNodes.count(CS.getCalledFunction())) | |
773 break; | |
774 } // fall-through | |
775 default: | |
776 return false; // Did not come from an allocation. | |
777 } | 765 } |
778 | 766 |
779 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) | 767 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) |
780 return false; | 768 return false; |
781 } | 769 } |
782 | 770 |
783 return true; | 771 return true; |
784 } | 772 } |
785 | 773 |
786 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC. | 774 /// Deduce noalias attributes for the SCC. |
787 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { | 775 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { |
788 SmallPtrSet<Function*, 8> SCCNodes; | 776 SmallPtrSet<Function *, 8> SCCNodes; |
789 | 777 |
790 // Fill SCCNodes with the elements of the SCC. Used for quickly | 778 // Fill SCCNodes with the elements of the SCC. Used for quickly |
791 // looking up whether a given CallGraphNode is in this SCC. | 779 // looking up whether a given CallGraphNode is in this SCC. |
792 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) | 780 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
793 SCCNodes.insert((*I)->getFunction()); | 781 SCCNodes.insert((*I)->getFunction()); |
808 // Definitions with weak linkage may be overridden at linktime, so | 796 // Definitions with weak linkage may be overridden at linktime, so |
809 // treat them like declarations. | 797 // treat them like declarations. |
810 if (F->isDeclaration() || F->mayBeOverridden()) | 798 if (F->isDeclaration() || F->mayBeOverridden()) |
811 return false; | 799 return false; |
812 | 800 |
813 // We annotate noalias return values, which are only applicable to | 801 // We annotate noalias return values, which are only applicable to |
814 // pointer types. | 802 // pointer types. |
815 if (!F->getReturnType()->isPointerTy()) | 803 if (!F->getReturnType()->isPointerTy()) |
816 continue; | 804 continue; |
817 | 805 |
818 if (!IsFunctionMallocLike(F, SCCNodes)) | 806 if (!isFunctionMallocLike(F, SCCNodes)) |
819 return false; | 807 return false; |
820 } | 808 } |
821 | 809 |
822 bool MadeChange = false; | 810 bool MadeChange = false; |
823 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { | 811 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
831 } | 819 } |
832 | 820 |
833 return MadeChange; | 821 return MadeChange; |
834 } | 822 } |
835 | 823 |
836 /// inferPrototypeAttributes - Analyze the name and prototype of the | 824 /// Tests whether this function is known to not return null. |
837 /// given function and set any applicable attributes. Returns true | 825 /// |
838 /// if any attributes were set and false otherwise. | 826 /// Requires that the function returns a pointer. |
839 bool FunctionAttrs::inferPrototypeAttributes(Function &F) { | 827 /// |
828 /// Returns true if it believes the function will not return a null, and sets | |
829 /// \p Speculative based on whether the returned conclusion is a speculative | |
830 /// conclusion due to SCC calls. | |
831 static bool isReturnNonNull(Function *F, SmallPtrSet<Function *, 8> &SCCNodes, | |
832 const TargetLibraryInfo &TLI, bool &Speculative) { | |
833 assert(F->getReturnType()->isPointerTy() && | |
834 "nonnull only meaningful on pointer types"); | |
835 Speculative = false; | |
836 | |
837 SmallSetVector<Value *, 8> FlowsToReturn; | |
838 for (BasicBlock &BB : *F) | |
839 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) | |
840 FlowsToReturn.insert(Ret->getReturnValue()); | |
841 | |
842 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { | |
843 Value *RetVal = FlowsToReturn[i]; | |
844 | |
845 // If this value is locally known to be non-null, we're good | |
846 if (isKnownNonNull(RetVal, &TLI)) | |
847 continue; | |
848 | |
849 // Otherwise, we need to look upwards since we can't make any local | |
850 // conclusions. | |
851 Instruction *RVI = dyn_cast<Instruction>(RetVal); | |
852 if (!RVI) | |
853 return false; | |
854 switch (RVI->getOpcode()) { | |
855 // Extend the analysis by looking upwards. | |
856 case Instruction::BitCast: | |
857 case Instruction::GetElementPtr: | |
858 case Instruction::AddrSpaceCast: | |
859 FlowsToReturn.insert(RVI->getOperand(0)); | |
860 continue; | |
861 case Instruction::Select: { | |
862 SelectInst *SI = cast<SelectInst>(RVI); | |
863 FlowsToReturn.insert(SI->getTrueValue()); | |
864 FlowsToReturn.insert(SI->getFalseValue()); | |
865 continue; | |
866 } | |
867 case Instruction::PHI: { | |
868 PHINode *PN = cast<PHINode>(RVI); | |
869 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) | |
870 FlowsToReturn.insert(PN->getIncomingValue(i)); | |
871 continue; | |
872 } | |
873 case Instruction::Call: | |
874 case Instruction::Invoke: { | |
875 CallSite CS(RVI); | |
876 Function *Callee = CS.getCalledFunction(); | |
877 // A call to a node within the SCC is assumed to return null until | |
878 // proven otherwise | |
879 if (Callee && SCCNodes.count(Callee)) { | |
880 Speculative = true; | |
881 continue; | |
882 } | |
883 return false; | |
884 } | |
885 default: | |
886 return false; // Unknown source, may be null | |
887 }; | |
888 llvm_unreachable("should have either continued or returned"); | |
889 } | |
890 | |
891 return true; | |
892 } | |
893 | |
894 /// Deduce nonnull attributes for the SCC. | |
895 bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) { | |
896 SmallPtrSet<Function *, 8> SCCNodes; | |
897 | |
898 // Fill SCCNodes with the elements of the SCC. Used for quickly | |
899 // looking up whether a given CallGraphNode is in this SCC. | |
900 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) | |
901 SCCNodes.insert((*I)->getFunction()); | |
902 | |
903 // Speculative that all functions in the SCC return only nonnull | |
904 // pointers. We may refute this as we analyze functions. | |
905 bool SCCReturnsNonNull = true; | |
906 | |
907 bool MadeChange = false; | |
908 | |
909 // Check each function in turn, determining which functions return nonnull | |
910 // pointers. | |
911 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { | |
912 Function *F = (*I)->getFunction(); | |
913 | |
914 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) | |
915 // External node or node we don't want to optimize - skip it; | |
916 return false; | |
917 | |
918 // Already nonnull. | |
919 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, | |
920 Attribute::NonNull)) | |
921 continue; | |
922 | |
923 // Definitions with weak linkage may be overridden at linktime, so | |
924 // treat them like declarations. | |
925 if (F->isDeclaration() || F->mayBeOverridden()) | |
926 return false; | |
927 | |
928 // We annotate nonnull return values, which are only applicable to | |
929 // pointer types. | |
930 if (!F->getReturnType()->isPointerTy()) | |
931 continue; | |
932 | |
933 bool Speculative = false; | |
934 if (isReturnNonNull(F, SCCNodes, *TLI, Speculative)) { | |
935 if (!Speculative) { | |
936 // Mark the function eagerly since we may discover a function | |
937 // which prevents us from speculating about the entire SCC | |
938 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); | |
939 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); | |
940 ++NumNonNullReturn; | |
941 MadeChange = true; | |
942 } | |
943 continue; | |
944 } | |
945 // At least one function returns something which could be null, can't | |
946 // speculate any more. | |
947 SCCReturnsNonNull = false; | |
948 } | |
949 | |
950 if (SCCReturnsNonNull) { | |
951 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { | |
952 Function *F = (*I)->getFunction(); | |
953 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, | |
954 Attribute::NonNull) || | |
955 !F->getReturnType()->isPointerTy()) | |
956 continue; | |
957 | |
958 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); | |
959 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); | |
960 ++NumNonNullReturn; | |
961 MadeChange = true; | |
962 } | |
963 } | |
964 | |
965 return MadeChange; | |
966 } | |
967 | |
968 static void setDoesNotAccessMemory(Function &F) { | |
969 if (!F.doesNotAccessMemory()) { | |
970 F.setDoesNotAccessMemory(); | |
971 ++NumAnnotated; | |
972 } | |
973 } | |
974 | |
975 static void setOnlyReadsMemory(Function &F) { | |
976 if (!F.onlyReadsMemory()) { | |
977 F.setOnlyReadsMemory(); | |
978 ++NumAnnotated; | |
979 } | |
980 } | |
981 | |
982 static void setDoesNotThrow(Function &F) { | |
983 if (!F.doesNotThrow()) { | |
984 F.setDoesNotThrow(); | |
985 ++NumAnnotated; | |
986 } | |
987 } | |
988 | |
989 static void setDoesNotCapture(Function &F, unsigned n) { | |
990 if (!F.doesNotCapture(n)) { | |
991 F.setDoesNotCapture(n); | |
992 ++NumAnnotated; | |
993 } | |
994 } | |
995 | |
996 static void setOnlyReadsMemory(Function &F, unsigned n) { | |
997 if (!F.onlyReadsMemory(n)) { | |
998 F.setOnlyReadsMemory(n); | |
999 ++NumAnnotated; | |
1000 } | |
1001 } | |
1002 | |
1003 static void setDoesNotAlias(Function &F, unsigned n) { | |
1004 if (!F.doesNotAlias(n)) { | |
1005 F.setDoesNotAlias(n); | |
1006 ++NumAnnotated; | |
1007 } | |
1008 } | |
1009 | |
1010 /// Analyze the name and prototype of the given function and set any applicable | |
1011 /// attributes. | |
1012 /// | |
1013 /// Returns true if any attributes were set and false otherwise. | |
1014 static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) { | |
840 if (F.hasFnAttribute(Attribute::OptimizeNone)) | 1015 if (F.hasFnAttribute(Attribute::OptimizeNone)) |
841 return false; | 1016 return false; |
842 | 1017 |
843 FunctionType *FTy = F.getFunctionType(); | 1018 FunctionType *FTy = F.getFunctionType(); |
844 LibFunc::Func TheLibFunc; | 1019 LibFunc::Func TheLibFunc; |
845 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc))) | 1020 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc))) |
846 return false; | 1021 return false; |
847 | 1022 |
848 switch (TheLibFunc) { | 1023 switch (TheLibFunc) { |
849 case LibFunc::strlen: | 1024 case LibFunc::strlen: |
850 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) | 1025 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
853 setDoesNotThrow(F); | 1028 setDoesNotThrow(F); |
854 setDoesNotCapture(F, 1); | 1029 setDoesNotCapture(F, 1); |
855 break; | 1030 break; |
856 case LibFunc::strchr: | 1031 case LibFunc::strchr: |
857 case LibFunc::strrchr: | 1032 case LibFunc::strrchr: |
858 if (FTy->getNumParams() != 2 || | 1033 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
859 !FTy->getParamType(0)->isPointerTy() || | |
860 !FTy->getParamType(1)->isIntegerTy()) | 1034 !FTy->getParamType(1)->isIntegerTy()) |
861 return false; | 1035 return false; |
862 setOnlyReadsMemory(F); | 1036 setOnlyReadsMemory(F); |
863 setDoesNotThrow(F); | 1037 setDoesNotThrow(F); |
864 break; | 1038 break; |
867 case LibFunc::strtof: | 1041 case LibFunc::strtof: |
868 case LibFunc::strtoul: | 1042 case LibFunc::strtoul: |
869 case LibFunc::strtoll: | 1043 case LibFunc::strtoll: |
870 case LibFunc::strtold: | 1044 case LibFunc::strtold: |
871 case LibFunc::strtoull: | 1045 case LibFunc::strtoull: |
872 if (FTy->getNumParams() < 2 || | 1046 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
873 !FTy->getParamType(1)->isPointerTy()) | |
874 return false; | 1047 return false; |
875 setDoesNotThrow(F); | 1048 setDoesNotThrow(F); |
876 setDoesNotCapture(F, 2); | 1049 setDoesNotCapture(F, 2); |
877 setOnlyReadsMemory(F, 1); | 1050 setOnlyReadsMemory(F, 1); |
878 break; | 1051 break; |
880 case LibFunc::stpcpy: | 1053 case LibFunc::stpcpy: |
881 case LibFunc::strcat: | 1054 case LibFunc::strcat: |
882 case LibFunc::strncat: | 1055 case LibFunc::strncat: |
883 case LibFunc::strncpy: | 1056 case LibFunc::strncpy: |
884 case LibFunc::stpncpy: | 1057 case LibFunc::stpncpy: |
885 if (FTy->getNumParams() < 2 || | 1058 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
886 !FTy->getParamType(1)->isPointerTy()) | |
887 return false; | 1059 return false; |
888 setDoesNotThrow(F); | 1060 setDoesNotThrow(F); |
889 setDoesNotCapture(F, 2); | 1061 setDoesNotCapture(F, 2); |
890 setOnlyReadsMemory(F, 2); | 1062 setOnlyReadsMemory(F, 2); |
891 break; | 1063 break; |
892 case LibFunc::strxfrm: | 1064 case LibFunc::strxfrm: |
893 if (FTy->getNumParams() != 3 || | 1065 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
894 !FTy->getParamType(0)->isPointerTy() || | 1066 !FTy->getParamType(1)->isPointerTy()) |
895 !FTy->getParamType(1)->isPointerTy()) | 1067 return false; |
896 return false; | 1068 setDoesNotThrow(F); |
897 setDoesNotThrow(F); | 1069 setDoesNotCapture(F, 1); |
898 setDoesNotCapture(F, 1); | 1070 setDoesNotCapture(F, 2); |
899 setDoesNotCapture(F, 2); | 1071 setOnlyReadsMemory(F, 2); |
900 setOnlyReadsMemory(F, 2); | 1072 break; |
901 break; | 1073 case LibFunc::strcmp: // 0,1 |
902 case LibFunc::strcmp: //0,1 | 1074 case LibFunc::strspn: // 0,1 |
903 case LibFunc::strspn: // 0,1 | 1075 case LibFunc::strncmp: // 0,1 |
904 case LibFunc::strncmp: // 0,1 | 1076 case LibFunc::strcspn: // 0,1 |
905 case LibFunc::strcspn: //0,1 | 1077 case LibFunc::strcoll: // 0,1 |
906 case LibFunc::strcoll: //0,1 | 1078 case LibFunc::strcasecmp: // 0,1 |
907 case LibFunc::strcasecmp: // 0,1 | 1079 case LibFunc::strncasecmp: // |
908 case LibFunc::strncasecmp: // | 1080 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
909 if (FTy->getNumParams() < 2 || | |
910 !FTy->getParamType(0)->isPointerTy() || | |
911 !FTy->getParamType(1)->isPointerTy()) | 1081 !FTy->getParamType(1)->isPointerTy()) |
912 return false; | 1082 return false; |
913 setOnlyReadsMemory(F); | 1083 setOnlyReadsMemory(F); |
914 setDoesNotThrow(F); | 1084 setDoesNotThrow(F); |
915 setDoesNotCapture(F, 1); | 1085 setDoesNotCapture(F, 1); |
955 setDoesNotCapture(F, 1); | 1125 setDoesNotCapture(F, 1); |
956 setOnlyReadsMemory(F, 1); | 1126 setOnlyReadsMemory(F, 1); |
957 break; | 1127 break; |
958 case LibFunc::stat: | 1128 case LibFunc::stat: |
959 case LibFunc::statvfs: | 1129 case LibFunc::statvfs: |
960 if (FTy->getNumParams() < 2 || | 1130 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
961 !FTy->getParamType(0)->isPointerTy() || | |
962 !FTy->getParamType(1)->isPointerTy()) | 1131 !FTy->getParamType(1)->isPointerTy()) |
963 return false; | 1132 return false; |
964 setDoesNotThrow(F); | 1133 setDoesNotThrow(F); |
965 setDoesNotCapture(F, 1); | 1134 setDoesNotCapture(F, 1); |
966 setDoesNotCapture(F, 2); | 1135 setDoesNotCapture(F, 2); |
967 setOnlyReadsMemory(F, 1); | 1136 setOnlyReadsMemory(F, 1); |
968 break; | 1137 break; |
969 case LibFunc::sscanf: | 1138 case LibFunc::sscanf: |
970 if (FTy->getNumParams() < 2 || | 1139 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
971 !FTy->getParamType(0)->isPointerTy() || | |
972 !FTy->getParamType(1)->isPointerTy()) | 1140 !FTy->getParamType(1)->isPointerTy()) |
973 return false; | 1141 return false; |
974 setDoesNotThrow(F); | 1142 setDoesNotThrow(F); |
975 setDoesNotCapture(F, 1); | 1143 setDoesNotCapture(F, 1); |
976 setDoesNotCapture(F, 2); | 1144 setDoesNotCapture(F, 2); |
977 setOnlyReadsMemory(F, 1); | 1145 setOnlyReadsMemory(F, 1); |
978 setOnlyReadsMemory(F, 2); | 1146 setOnlyReadsMemory(F, 2); |
979 break; | 1147 break; |
980 case LibFunc::sprintf: | 1148 case LibFunc::sprintf: |
981 if (FTy->getNumParams() < 2 || | 1149 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
982 !FTy->getParamType(0)->isPointerTy() || | |
983 !FTy->getParamType(1)->isPointerTy()) | 1150 !FTy->getParamType(1)->isPointerTy()) |
984 return false; | 1151 return false; |
985 setDoesNotThrow(F); | 1152 setDoesNotThrow(F); |
986 setDoesNotCapture(F, 1); | 1153 setDoesNotCapture(F, 1); |
987 setDoesNotCapture(F, 2); | 1154 setDoesNotCapture(F, 2); |
988 setOnlyReadsMemory(F, 2); | 1155 setOnlyReadsMemory(F, 2); |
989 break; | 1156 break; |
990 case LibFunc::snprintf: | 1157 case LibFunc::snprintf: |
991 if (FTy->getNumParams() != 3 || | 1158 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
992 !FTy->getParamType(0)->isPointerTy() || | |
993 !FTy->getParamType(2)->isPointerTy()) | 1159 !FTy->getParamType(2)->isPointerTy()) |
994 return false; | 1160 return false; |
995 setDoesNotThrow(F); | 1161 setDoesNotThrow(F); |
996 setDoesNotCapture(F, 1); | 1162 setDoesNotCapture(F, 1); |
997 setDoesNotCapture(F, 3); | 1163 setDoesNotCapture(F, 3); |
998 setOnlyReadsMemory(F, 3); | 1164 setOnlyReadsMemory(F, 3); |
999 break; | 1165 break; |
1000 case LibFunc::setitimer: | 1166 case LibFunc::setitimer: |
1001 if (FTy->getNumParams() != 3 || | 1167 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || |
1002 !FTy->getParamType(1)->isPointerTy() || | |
1003 !FTy->getParamType(2)->isPointerTy()) | 1168 !FTy->getParamType(2)->isPointerTy()) |
1004 return false; | 1169 return false; |
1005 setDoesNotThrow(F); | 1170 setDoesNotThrow(F); |
1006 setDoesNotCapture(F, 2); | 1171 setDoesNotCapture(F, 2); |
1007 setDoesNotCapture(F, 3); | 1172 setDoesNotCapture(F, 3); |
1008 setOnlyReadsMemory(F, 2); | 1173 setOnlyReadsMemory(F, 2); |
1009 break; | 1174 break; |
1010 case LibFunc::system: | 1175 case LibFunc::system: |
1011 if (FTy->getNumParams() != 1 || | 1176 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) |
1012 !FTy->getParamType(0)->isPointerTy()) | |
1013 return false; | 1177 return false; |
1014 // May throw; "system" is a valid pthread cancellation point. | 1178 // May throw; "system" is a valid pthread cancellation point. |
1015 setDoesNotCapture(F, 1); | 1179 setDoesNotCapture(F, 1); |
1016 setOnlyReadsMemory(F, 1); | 1180 setOnlyReadsMemory(F, 1); |
1017 break; | 1181 break; |
1018 case LibFunc::malloc: | 1182 case LibFunc::malloc: |
1019 if (FTy->getNumParams() != 1 || | 1183 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy()) |
1020 !FTy->getReturnType()->isPointerTy()) | |
1021 return false; | 1184 return false; |
1022 setDoesNotThrow(F); | 1185 setDoesNotThrow(F); |
1023 setDoesNotAlias(F, 0); | 1186 setDoesNotAlias(F, 0); |
1024 break; | 1187 break; |
1025 case LibFunc::memcmp: | 1188 case LibFunc::memcmp: |
1026 if (FTy->getNumParams() != 3 || | 1189 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
1027 !FTy->getParamType(0)->isPointerTy() || | |
1028 !FTy->getParamType(1)->isPointerTy()) | 1190 !FTy->getParamType(1)->isPointerTy()) |
1029 return false; | 1191 return false; |
1030 setOnlyReadsMemory(F); | 1192 setOnlyReadsMemory(F); |
1031 setDoesNotThrow(F); | 1193 setDoesNotThrow(F); |
1032 setDoesNotCapture(F, 1); | 1194 setDoesNotCapture(F, 1); |
1040 setDoesNotThrow(F); | 1202 setDoesNotThrow(F); |
1041 break; | 1203 break; |
1042 case LibFunc::modf: | 1204 case LibFunc::modf: |
1043 case LibFunc::modff: | 1205 case LibFunc::modff: |
1044 case LibFunc::modfl: | 1206 case LibFunc::modfl: |
1045 if (FTy->getNumParams() < 2 || | 1207 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
1046 !FTy->getParamType(1)->isPointerTy()) | |
1047 return false; | 1208 return false; |
1048 setDoesNotThrow(F); | 1209 setDoesNotThrow(F); |
1049 setDoesNotCapture(F, 2); | 1210 setDoesNotCapture(F, 2); |
1050 break; | 1211 break; |
1051 case LibFunc::memcpy: | 1212 case LibFunc::memcpy: |
1052 case LibFunc::memccpy: | 1213 case LibFunc::memccpy: |
1053 case LibFunc::memmove: | 1214 case LibFunc::memmove: |
1054 if (FTy->getNumParams() < 2 || | 1215 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) |
1055 !FTy->getParamType(1)->isPointerTy()) | |
1056 return false; | 1216 return false; |
1057 setDoesNotThrow(F); | 1217 setDoesNotThrow(F); |
1058 setDoesNotCapture(F, 2); | 1218 setDoesNotCapture(F, 2); |
1059 setOnlyReadsMemory(F, 2); | 1219 setOnlyReadsMemory(F, 2); |
1060 break; | 1220 break; |
1062 if (!FTy->getReturnType()->isPointerTy()) | 1222 if (!FTy->getReturnType()->isPointerTy()) |
1063 return false; | 1223 return false; |
1064 setDoesNotAlias(F, 0); | 1224 setDoesNotAlias(F, 0); |
1065 break; | 1225 break; |
1066 case LibFunc::mkdir: | 1226 case LibFunc::mkdir: |
1067 if (FTy->getNumParams() == 0 || | 1227 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
1068 !FTy->getParamType(0)->isPointerTy()) | |
1069 return false; | 1228 return false; |
1070 setDoesNotThrow(F); | 1229 setDoesNotThrow(F); |
1071 setDoesNotCapture(F, 1); | 1230 setDoesNotCapture(F, 1); |
1072 setOnlyReadsMemory(F, 1); | 1231 setOnlyReadsMemory(F, 1); |
1073 break; | 1232 break; |
1074 case LibFunc::mktime: | 1233 case LibFunc::mktime: |
1075 if (FTy->getNumParams() == 0 || | 1234 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) |
1076 !FTy->getParamType(0)->isPointerTy()) | |
1077 return false; | 1235 return false; |
1078 setDoesNotThrow(F); | 1236 setDoesNotThrow(F); |
1079 setDoesNotCapture(F, 1); | 1237 setDoesNotCapture(F, 1); |
1080 break; | 1238 break; |
1081 case LibFunc::realloc: | 1239 case LibFunc::realloc: |
1082 if (FTy->getNumParams() != 2 || | 1240 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
1083 !FTy->getParamType(0)->isPointerTy() || | |
1084 !FTy->getReturnType()->isPointerTy()) | 1241 !FTy->getReturnType()->isPointerTy()) |
1085 return false; | 1242 return false; |
1086 setDoesNotThrow(F); | 1243 setDoesNotThrow(F); |
1087 setDoesNotAlias(F, 0); | 1244 setDoesNotAlias(F, 0); |
1088 setDoesNotCapture(F, 1); | 1245 setDoesNotCapture(F, 1); |
1089 break; | 1246 break; |
1090 case LibFunc::read: | 1247 case LibFunc::read: |
1091 if (FTy->getNumParams() != 3 || | 1248 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) |
1092 !FTy->getParamType(1)->isPointerTy()) | |
1093 return false; | 1249 return false; |
1094 // May throw; "read" is a valid pthread cancellation point. | 1250 // May throw; "read" is a valid pthread cancellation point. |
1095 setDoesNotCapture(F, 2); | 1251 setDoesNotCapture(F, 2); |
1096 break; | 1252 break; |
1097 case LibFunc::rewind: | 1253 case LibFunc::rewind: |
1098 if (FTy->getNumParams() < 1 || | 1254 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
1099 !FTy->getParamType(0)->isPointerTy()) | |
1100 return false; | 1255 return false; |
1101 setDoesNotThrow(F); | 1256 setDoesNotThrow(F); |
1102 setDoesNotCapture(F, 1); | 1257 setDoesNotCapture(F, 1); |
1103 break; | 1258 break; |
1104 case LibFunc::rmdir: | 1259 case LibFunc::rmdir: |
1105 case LibFunc::remove: | 1260 case LibFunc::remove: |
1106 case LibFunc::realpath: | 1261 case LibFunc::realpath: |
1107 if (FTy->getNumParams() < 1 || | 1262 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
1108 !FTy->getParamType(0)->isPointerTy()) | |
1109 return false; | 1263 return false; |
1110 setDoesNotThrow(F); | 1264 setDoesNotThrow(F); |
1111 setDoesNotCapture(F, 1); | 1265 setDoesNotCapture(F, 1); |
1112 setOnlyReadsMemory(F, 1); | 1266 setOnlyReadsMemory(F, 1); |
1113 break; | 1267 break; |
1114 case LibFunc::rename: | 1268 case LibFunc::rename: |
1115 if (FTy->getNumParams() < 2 || | 1269 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
1116 !FTy->getParamType(0)->isPointerTy() || | |
1117 !FTy->getParamType(1)->isPointerTy()) | 1270 !FTy->getParamType(1)->isPointerTy()) |
1118 return false; | 1271 return false; |
1119 setDoesNotThrow(F); | 1272 setDoesNotThrow(F); |
1120 setDoesNotCapture(F, 1); | 1273 setDoesNotCapture(F, 1); |
1121 setDoesNotCapture(F, 2); | 1274 setDoesNotCapture(F, 2); |
1122 setOnlyReadsMemory(F, 1); | 1275 setOnlyReadsMemory(F, 1); |
1123 setOnlyReadsMemory(F, 2); | 1276 setOnlyReadsMemory(F, 2); |
1124 break; | 1277 break; |
1125 case LibFunc::readlink: | 1278 case LibFunc::readlink: |
1126 if (FTy->getNumParams() < 2 || | 1279 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
1127 !FTy->getParamType(0)->isPointerTy() || | |
1128 !FTy->getParamType(1)->isPointerTy()) | 1280 !FTy->getParamType(1)->isPointerTy()) |
1129 return false; | 1281 return false; |
1130 setDoesNotThrow(F); | 1282 setDoesNotThrow(F); |
1131 setDoesNotCapture(F, 1); | 1283 setDoesNotCapture(F, 1); |
1132 setDoesNotCapture(F, 2); | 1284 setDoesNotCapture(F, 2); |
1138 // May throw; "write" is a valid pthread cancellation point. | 1290 // May throw; "write" is a valid pthread cancellation point. |
1139 setDoesNotCapture(F, 2); | 1291 setDoesNotCapture(F, 2); |
1140 setOnlyReadsMemory(F, 2); | 1292 setOnlyReadsMemory(F, 2); |
1141 break; | 1293 break; |
1142 case LibFunc::bcopy: | 1294 case LibFunc::bcopy: |
1143 if (FTy->getNumParams() != 3 || | 1295 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
1144 !FTy->getParamType(0)->isPointerTy() || | |
1145 !FTy->getParamType(1)->isPointerTy()) | 1296 !FTy->getParamType(1)->isPointerTy()) |
1146 return false; | 1297 return false; |
1147 setDoesNotThrow(F); | 1298 setDoesNotThrow(F); |
1148 setDoesNotCapture(F, 1); | 1299 setDoesNotCapture(F, 1); |
1149 setDoesNotCapture(F, 2); | 1300 setDoesNotCapture(F, 2); |
1150 setOnlyReadsMemory(F, 1); | 1301 setOnlyReadsMemory(F, 1); |
1151 break; | 1302 break; |
1152 case LibFunc::bcmp: | 1303 case LibFunc::bcmp: |
1153 if (FTy->getNumParams() != 3 || | 1304 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
1154 !FTy->getParamType(0)->isPointerTy() || | |
1155 !FTy->getParamType(1)->isPointerTy()) | 1305 !FTy->getParamType(1)->isPointerTy()) |
1156 return false; | 1306 return false; |
1157 setDoesNotThrow(F); | 1307 setDoesNotThrow(F); |
1158 setOnlyReadsMemory(F); | 1308 setOnlyReadsMemory(F); |
1159 setDoesNotCapture(F, 1); | 1309 setDoesNotCapture(F, 1); |
1164 return false; | 1314 return false; |
1165 setDoesNotThrow(F); | 1315 setDoesNotThrow(F); |
1166 setDoesNotCapture(F, 1); | 1316 setDoesNotCapture(F, 1); |
1167 break; | 1317 break; |
1168 case LibFunc::calloc: | 1318 case LibFunc::calloc: |
1169 if (FTy->getNumParams() != 2 || | 1319 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy()) |
1170 !FTy->getReturnType()->isPointerTy()) | |
1171 return false; | 1320 return false; |
1172 setDoesNotThrow(F); | 1321 setDoesNotThrow(F); |
1173 setDoesNotAlias(F, 0); | 1322 setDoesNotAlias(F, 0); |
1174 break; | 1323 break; |
1175 case LibFunc::chmod: | 1324 case LibFunc::chmod: |
1204 setDoesNotThrow(F); | 1353 setDoesNotThrow(F); |
1205 setDoesNotCapture(F, 1); | 1354 setDoesNotCapture(F, 1); |
1206 setOnlyReadsMemory(F, 1); | 1355 setOnlyReadsMemory(F, 1); |
1207 break; | 1356 break; |
1208 case LibFunc::fopen: | 1357 case LibFunc::fopen: |
1209 if (FTy->getNumParams() != 2 || | 1358 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
1210 !FTy->getReturnType()->isPointerTy() || | |
1211 !FTy->getParamType(0)->isPointerTy() || | 1359 !FTy->getParamType(0)->isPointerTy() || |
1212 !FTy->getParamType(1)->isPointerTy()) | 1360 !FTy->getParamType(1)->isPointerTy()) |
1213 return false; | 1361 return false; |
1214 setDoesNotThrow(F); | 1362 setDoesNotThrow(F); |
1215 setDoesNotAlias(F, 0); | 1363 setDoesNotAlias(F, 0); |
1217 setDoesNotCapture(F, 2); | 1365 setDoesNotCapture(F, 2); |
1218 setOnlyReadsMemory(F, 1); | 1366 setOnlyReadsMemory(F, 1); |
1219 setOnlyReadsMemory(F, 2); | 1367 setOnlyReadsMemory(F, 2); |
1220 break; | 1368 break; |
1221 case LibFunc::fdopen: | 1369 case LibFunc::fdopen: |
1222 if (FTy->getNumParams() != 2 || | 1370 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
1223 !FTy->getReturnType()->isPointerTy() || | |
1224 !FTy->getParamType(1)->isPointerTy()) | 1371 !FTy->getParamType(1)->isPointerTy()) |
1225 return false; | 1372 return false; |
1226 setDoesNotThrow(F); | 1373 setDoesNotThrow(F); |
1227 setDoesNotAlias(F, 0); | 1374 setDoesNotAlias(F, 0); |
1228 setDoesNotCapture(F, 2); | 1375 setDoesNotCapture(F, 2); |
1264 return false; | 1411 return false; |
1265 setDoesNotThrow(F); | 1412 setDoesNotThrow(F); |
1266 setDoesNotCapture(F, 2); | 1413 setDoesNotCapture(F, 2); |
1267 break; | 1414 break; |
1268 case LibFunc::fgets: | 1415 case LibFunc::fgets: |
1269 if (FTy->getNumParams() != 3 || | 1416 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
1270 !FTy->getParamType(0)->isPointerTy() || | |
1271 !FTy->getParamType(2)->isPointerTy()) | 1417 !FTy->getParamType(2)->isPointerTy()) |
1272 return false; | 1418 return false; |
1273 setDoesNotThrow(F); | 1419 setDoesNotThrow(F); |
1274 setDoesNotCapture(F, 3); | 1420 setDoesNotCapture(F, 3); |
1275 break; | 1421 break; |
1276 case LibFunc::fread: | 1422 case LibFunc::fread: |
1277 if (FTy->getNumParams() != 4 || | 1423 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || |
1278 !FTy->getParamType(0)->isPointerTy() || | |
1279 !FTy->getParamType(3)->isPointerTy()) | 1424 !FTy->getParamType(3)->isPointerTy()) |
1280 return false; | 1425 return false; |
1281 setDoesNotThrow(F); | 1426 setDoesNotThrow(F); |
1282 setDoesNotCapture(F, 1); | 1427 setDoesNotCapture(F, 1); |
1283 setDoesNotCapture(F, 4); | 1428 setDoesNotCapture(F, 4); |
1284 break; | 1429 break; |
1285 case LibFunc::fwrite: | 1430 case LibFunc::fwrite: |
1286 if (FTy->getNumParams() != 4 || | 1431 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || |
1287 !FTy->getParamType(0)->isPointerTy() || | |
1288 !FTy->getParamType(3)->isPointerTy()) | 1432 !FTy->getParamType(3)->isPointerTy()) |
1289 return false; | 1433 return false; |
1290 setDoesNotThrow(F); | 1434 setDoesNotThrow(F); |
1291 setDoesNotCapture(F, 1); | 1435 setDoesNotCapture(F, 1); |
1292 setDoesNotCapture(F, 4); | 1436 setDoesNotCapture(F, 4); |
1293 break; | 1437 break; |
1294 case LibFunc::fputs: | 1438 case LibFunc::fputs: |
1295 if (FTy->getNumParams() < 2 || | 1439 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
1296 !FTy->getParamType(0)->isPointerTy() || | |
1297 !FTy->getParamType(1)->isPointerTy()) | 1440 !FTy->getParamType(1)->isPointerTy()) |
1298 return false; | 1441 return false; |
1299 setDoesNotThrow(F); | 1442 setDoesNotThrow(F); |
1300 setDoesNotCapture(F, 1); | 1443 setDoesNotCapture(F, 1); |
1301 setDoesNotCapture(F, 2); | 1444 setDoesNotCapture(F, 2); |
1302 setOnlyReadsMemory(F, 1); | 1445 setOnlyReadsMemory(F, 1); |
1303 break; | 1446 break; |
1304 case LibFunc::fscanf: | 1447 case LibFunc::fscanf: |
1305 case LibFunc::fprintf: | 1448 case LibFunc::fprintf: |
1306 if (FTy->getNumParams() < 2 || | 1449 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
1307 !FTy->getParamType(0)->isPointerTy() || | |
1308 !FTy->getParamType(1)->isPointerTy()) | 1450 !FTy->getParamType(1)->isPointerTy()) |
1309 return false; | 1451 return false; |
1310 setDoesNotThrow(F); | 1452 setDoesNotThrow(F); |
1311 setDoesNotCapture(F, 1); | 1453 setDoesNotCapture(F, 1); |
1312 setDoesNotCapture(F, 2); | 1454 setDoesNotCapture(F, 2); |
1313 setOnlyReadsMemory(F, 2); | 1455 setOnlyReadsMemory(F, 2); |
1314 break; | 1456 break; |
1315 case LibFunc::fgetpos: | 1457 case LibFunc::fgetpos: |
1316 if (FTy->getNumParams() < 2 || | 1458 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || |
1317 !FTy->getParamType(0)->isPointerTy() || | |
1318 !FTy->getParamType(1)->isPointerTy()) | 1459 !FTy->getParamType(1)->isPointerTy()) |
1319 return false; | 1460 return false; |
1320 setDoesNotThrow(F); | 1461 setDoesNotThrow(F); |
1321 setDoesNotCapture(F, 1); | 1462 setDoesNotCapture(F, 1); |
1322 setDoesNotCapture(F, 2); | 1463 setDoesNotCapture(F, 2); |
1379 setDoesNotCapture(F, 1); | 1520 setDoesNotCapture(F, 1); |
1380 setOnlyReadsMemory(F, 1); | 1521 setOnlyReadsMemory(F, 1); |
1381 break; | 1522 break; |
1382 case LibFunc::utime: | 1523 case LibFunc::utime: |
1383 case LibFunc::utimes: | 1524 case LibFunc::utimes: |
1384 if (FTy->getNumParams() != 2 || | 1525 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
1385 !FTy->getParamType(0)->isPointerTy() || | |
1386 !FTy->getParamType(1)->isPointerTy()) | 1526 !FTy->getParamType(1)->isPointerTy()) |
1387 return false; | 1527 return false; |
1388 setDoesNotThrow(F); | 1528 setDoesNotThrow(F); |
1389 setDoesNotCapture(F, 1); | 1529 setDoesNotCapture(F, 1); |
1390 setDoesNotCapture(F, 2); | 1530 setDoesNotCapture(F, 2); |
1421 break; | 1561 break; |
1422 case LibFunc::putchar: | 1562 case LibFunc::putchar: |
1423 setDoesNotThrow(F); | 1563 setDoesNotThrow(F); |
1424 break; | 1564 break; |
1425 case LibFunc::popen: | 1565 case LibFunc::popen: |
1426 if (FTy->getNumParams() != 2 || | 1566 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
1427 !FTy->getReturnType()->isPointerTy() || | |
1428 !FTy->getParamType(0)->isPointerTy() || | 1567 !FTy->getParamType(0)->isPointerTy() || |
1429 !FTy->getParamType(1)->isPointerTy()) | 1568 !FTy->getParamType(1)->isPointerTy()) |
1430 return false; | 1569 return false; |
1431 setDoesNotThrow(F); | 1570 setDoesNotThrow(F); |
1432 setDoesNotAlias(F, 0); | 1571 setDoesNotAlias(F, 0); |
1447 setDoesNotThrow(F); | 1586 setDoesNotThrow(F); |
1448 setDoesNotCapture(F, 1); | 1587 setDoesNotCapture(F, 1); |
1449 setOnlyReadsMemory(F, 1); | 1588 setOnlyReadsMemory(F, 1); |
1450 break; | 1589 break; |
1451 case LibFunc::vsscanf: | 1590 case LibFunc::vsscanf: |
1452 if (FTy->getNumParams() != 3 || | 1591 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || |
1453 !FTy->getParamType(1)->isPointerTy() || | |
1454 !FTy->getParamType(2)->isPointerTy()) | 1592 !FTy->getParamType(2)->isPointerTy()) |
1455 return false; | 1593 return false; |
1456 setDoesNotThrow(F); | 1594 setDoesNotThrow(F); |
1457 setDoesNotCapture(F, 1); | 1595 setDoesNotCapture(F, 1); |
1458 setDoesNotCapture(F, 2); | 1596 setDoesNotCapture(F, 2); |
1459 setOnlyReadsMemory(F, 1); | 1597 setOnlyReadsMemory(F, 1); |
1460 setOnlyReadsMemory(F, 2); | 1598 setOnlyReadsMemory(F, 2); |
1461 break; | 1599 break; |
1462 case LibFunc::vfscanf: | 1600 case LibFunc::vfscanf: |
1463 if (FTy->getNumParams() != 3 || | 1601 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || |
1464 !FTy->getParamType(1)->isPointerTy() || | |
1465 !FTy->getParamType(2)->isPointerTy()) | 1602 !FTy->getParamType(2)->isPointerTy()) |
1466 return false; | 1603 return false; |
1467 setDoesNotThrow(F); | 1604 setDoesNotThrow(F); |
1468 setDoesNotCapture(F, 1); | 1605 setDoesNotCapture(F, 1); |
1469 setDoesNotCapture(F, 2); | 1606 setDoesNotCapture(F, 2); |
1482 setDoesNotCapture(F, 1); | 1619 setDoesNotCapture(F, 1); |
1483 setOnlyReadsMemory(F, 1); | 1620 setOnlyReadsMemory(F, 1); |
1484 break; | 1621 break; |
1485 case LibFunc::vfprintf: | 1622 case LibFunc::vfprintf: |
1486 case LibFunc::vsprintf: | 1623 case LibFunc::vsprintf: |
1487 if (FTy->getNumParams() != 3 || | 1624 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || |
1488 !FTy->getParamType(0)->isPointerTy() || | |
1489 !FTy->getParamType(1)->isPointerTy()) | 1625 !FTy->getParamType(1)->isPointerTy()) |
1490 return false; | 1626 return false; |
1491 setDoesNotThrow(F); | 1627 setDoesNotThrow(F); |
1492 setDoesNotCapture(F, 1); | 1628 setDoesNotCapture(F, 1); |
1493 setDoesNotCapture(F, 2); | 1629 setDoesNotCapture(F, 2); |
1494 setOnlyReadsMemory(F, 2); | 1630 setOnlyReadsMemory(F, 2); |
1495 break; | 1631 break; |
1496 case LibFunc::vsnprintf: | 1632 case LibFunc::vsnprintf: |
1497 if (FTy->getNumParams() != 4 || | 1633 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || |
1498 !FTy->getParamType(0)->isPointerTy() || | |
1499 !FTy->getParamType(2)->isPointerTy()) | 1634 !FTy->getParamType(2)->isPointerTy()) |
1500 return false; | 1635 return false; |
1501 setDoesNotThrow(F); | 1636 setDoesNotThrow(F); |
1502 setDoesNotCapture(F, 1); | 1637 setDoesNotCapture(F, 1); |
1503 setDoesNotCapture(F, 3); | 1638 setDoesNotCapture(F, 3); |
1509 // May throw; "open" is a valid pthread cancellation point. | 1644 // May throw; "open" is a valid pthread cancellation point. |
1510 setDoesNotCapture(F, 1); | 1645 setDoesNotCapture(F, 1); |
1511 setOnlyReadsMemory(F, 1); | 1646 setOnlyReadsMemory(F, 1); |
1512 break; | 1647 break; |
1513 case LibFunc::opendir: | 1648 case LibFunc::opendir: |
1514 if (FTy->getNumParams() != 1 || | 1649 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() || |
1515 !FTy->getReturnType()->isPointerTy() || | |
1516 !FTy->getParamType(0)->isPointerTy()) | 1650 !FTy->getParamType(0)->isPointerTy()) |
1517 return false; | 1651 return false; |
1518 setDoesNotThrow(F); | 1652 setDoesNotThrow(F); |
1519 setDoesNotAlias(F, 0); | 1653 setDoesNotAlias(F, 0); |
1520 setDoesNotCapture(F, 1); | 1654 setDoesNotCapture(F, 1); |
1538 case LibFunc::ntohs: | 1672 case LibFunc::ntohs: |
1539 setDoesNotThrow(F); | 1673 setDoesNotThrow(F); |
1540 setDoesNotAccessMemory(F); | 1674 setDoesNotAccessMemory(F); |
1541 break; | 1675 break; |
1542 case LibFunc::lstat: | 1676 case LibFunc::lstat: |
1543 if (FTy->getNumParams() != 2 || | 1677 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || |
1544 !FTy->getParamType(0)->isPointerTy() || | |
1545 !FTy->getParamType(1)->isPointerTy()) | 1678 !FTy->getParamType(1)->isPointerTy()) |
1546 return false; | 1679 return false; |
1547 setDoesNotThrow(F); | 1680 setDoesNotThrow(F); |
1548 setDoesNotCapture(F, 1); | 1681 setDoesNotCapture(F, 1); |
1549 setDoesNotCapture(F, 2); | 1682 setDoesNotCapture(F, 2); |
1562 // May throw; places call through function pointer. | 1695 // May throw; places call through function pointer. |
1563 setDoesNotCapture(F, 4); | 1696 setDoesNotCapture(F, 4); |
1564 break; | 1697 break; |
1565 case LibFunc::dunder_strdup: | 1698 case LibFunc::dunder_strdup: |
1566 case LibFunc::dunder_strndup: | 1699 case LibFunc::dunder_strndup: |
1567 if (FTy->getNumParams() < 1 || | 1700 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() || |
1568 !FTy->getReturnType()->isPointerTy() || | |
1569 !FTy->getParamType(0)->isPointerTy()) | 1701 !FTy->getParamType(0)->isPointerTy()) |
1570 return false; | 1702 return false; |
1571 setDoesNotThrow(F); | 1703 setDoesNotThrow(F); |
1572 setDoesNotAlias(F, 0); | 1704 setDoesNotAlias(F, 0); |
1573 setDoesNotCapture(F, 1); | 1705 setDoesNotCapture(F, 1); |
1574 setOnlyReadsMemory(F, 1); | 1706 setOnlyReadsMemory(F, 1); |
1575 break; | 1707 break; |
1576 case LibFunc::dunder_strtok_r: | 1708 case LibFunc::dunder_strtok_r: |
1577 if (FTy->getNumParams() != 3 || | 1709 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) |
1578 !FTy->getParamType(1)->isPointerTy()) | |
1579 return false; | 1710 return false; |
1580 setDoesNotThrow(F); | 1711 setDoesNotThrow(F); |
1581 setDoesNotCapture(F, 2); | 1712 setDoesNotCapture(F, 2); |
1582 setOnlyReadsMemory(F, 2); | 1713 setOnlyReadsMemory(F, 2); |
1583 break; | 1714 break; |
1592 return false; | 1723 return false; |
1593 setDoesNotThrow(F); | 1724 setDoesNotThrow(F); |
1594 setDoesNotCapture(F, 2); | 1725 setDoesNotCapture(F, 2); |
1595 break; | 1726 break; |
1596 case LibFunc::dunder_isoc99_scanf: | 1727 case LibFunc::dunder_isoc99_scanf: |
1597 if (FTy->getNumParams() < 1 || | 1728 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) |
1598 !FTy->getParamType(0)->isPointerTy()) | |
1599 return false; | 1729 return false; |
1600 setDoesNotThrow(F); | 1730 setDoesNotThrow(F); |
1601 setDoesNotCapture(F, 1); | 1731 setDoesNotCapture(F, 1); |
1602 setOnlyReadsMemory(F, 1); | 1732 setOnlyReadsMemory(F, 1); |
1603 break; | 1733 break; |
1604 case LibFunc::stat64: | 1734 case LibFunc::stat64: |
1605 case LibFunc::lstat64: | 1735 case LibFunc::lstat64: |
1606 case LibFunc::statvfs64: | 1736 case LibFunc::statvfs64: |
1607 if (FTy->getNumParams() < 1 || | 1737 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() || |
1608 !FTy->getParamType(0)->isPointerTy() || | |
1609 !FTy->getParamType(1)->isPointerTy()) | 1738 !FTy->getParamType(1)->isPointerTy()) |
1610 return false; | 1739 return false; |
1611 setDoesNotThrow(F); | 1740 setDoesNotThrow(F); |
1612 setDoesNotCapture(F, 1); | 1741 setDoesNotCapture(F, 1); |
1613 setDoesNotCapture(F, 2); | 1742 setDoesNotCapture(F, 2); |
1614 setOnlyReadsMemory(F, 1); | 1743 setOnlyReadsMemory(F, 1); |
1615 break; | 1744 break; |
1616 case LibFunc::dunder_isoc99_sscanf: | 1745 case LibFunc::dunder_isoc99_sscanf: |
1617 if (FTy->getNumParams() < 1 || | 1746 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() || |
1618 !FTy->getParamType(0)->isPointerTy() || | |
1619 !FTy->getParamType(1)->isPointerTy()) | 1747 !FTy->getParamType(1)->isPointerTy()) |
1620 return false; | 1748 return false; |
1621 setDoesNotThrow(F); | 1749 setDoesNotThrow(F); |
1622 setDoesNotCapture(F, 1); | 1750 setDoesNotCapture(F, 1); |
1623 setDoesNotCapture(F, 2); | 1751 setDoesNotCapture(F, 2); |
1624 setOnlyReadsMemory(F, 1); | 1752 setOnlyReadsMemory(F, 1); |
1625 setOnlyReadsMemory(F, 2); | 1753 setOnlyReadsMemory(F, 2); |
1626 break; | 1754 break; |
1627 case LibFunc::fopen64: | 1755 case LibFunc::fopen64: |
1628 if (FTy->getNumParams() != 2 || | 1756 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || |
1629 !FTy->getReturnType()->isPointerTy() || | |
1630 !FTy->getParamType(0)->isPointerTy() || | 1757 !FTy->getParamType(0)->isPointerTy() || |
1631 !FTy->getParamType(1)->isPointerTy()) | 1758 !FTy->getParamType(1)->isPointerTy()) |
1632 return false; | 1759 return false; |
1633 setDoesNotThrow(F); | 1760 setDoesNotThrow(F); |
1634 setDoesNotAlias(F, 0); | 1761 setDoesNotAlias(F, 0); |
1681 } | 1808 } |
1682 | 1809 |
1683 return true; | 1810 return true; |
1684 } | 1811 } |
1685 | 1812 |
1686 /// annotateLibraryCalls - Adds attributes to well-known standard library | 1813 /// Adds attributes to well-known standard library call declarations. |
1687 /// call declarations. | |
1688 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) { | 1814 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) { |
1689 bool MadeChange = false; | 1815 bool MadeChange = false; |
1690 | 1816 |
1691 // Check each function in turn annotating well-known library function | 1817 // Check each function in turn annotating well-known library function |
1692 // declarations with attributes. | 1818 // declarations with attributes. |
1693 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { | 1819 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
1694 Function *F = (*I)->getFunction(); | 1820 Function *F = (*I)->getFunction(); |
1695 | 1821 |
1696 if (F && F->isDeclaration()) | 1822 if (F && F->isDeclaration()) |
1697 MadeChange |= inferPrototypeAttributes(*F); | 1823 MadeChange |= inferPrototypeAttributes(*F, *TLI); |
1698 } | 1824 } |
1699 | 1825 |
1700 return MadeChange; | 1826 return MadeChange; |
1701 } | 1827 } |
1702 | 1828 |
1703 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { | 1829 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { |
1704 AA = &getAnalysis<AliasAnalysis>(); | |
1705 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); | 1830 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
1706 | 1831 |
1707 bool Changed = annotateLibraryCalls(SCC); | 1832 bool Changed = annotateLibraryCalls(SCC); |
1708 Changed |= AddReadAttrs(SCC); | 1833 Changed |= AddReadAttrs(SCC); |
1709 Changed |= AddArgumentAttrs(SCC); | 1834 Changed |= AddArgumentAttrs(SCC); |
1710 Changed |= AddNoAliasAttrs(SCC); | 1835 Changed |= AddNoAliasAttrs(SCC); |
1836 Changed |= AddNonNullAttrs(SCC); | |
1711 return Changed; | 1837 return Changed; |
1712 } | 1838 } |