LLVM API Documentation
00001 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a simple interprocedural pass which walks the 00011 // call-graph, looking for functions which do not access or only read 00012 // non-local memory, and marking them readnone/readonly. It does the 00013 // same with function arguments independently, marking them readonly/ 00014 // readnone/nocapture. Finally, well-known library call declarations 00015 // are marked with all attributes that are consistent with the 00016 // function's standard definition. This pass is implemented as a 00017 // bottom-up traversal of the call-graph. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #include "llvm/Transforms/IPO.h" 00022 #include "llvm/ADT/SCCIterator.h" 00023 #include "llvm/ADT/SetVector.h" 00024 #include "llvm/ADT/SmallSet.h" 00025 #include "llvm/ADT/Statistic.h" 00026 #include "llvm/Analysis/AliasAnalysis.h" 00027 #include "llvm/Analysis/CallGraph.h" 00028 #include "llvm/Analysis/CallGraphSCCPass.h" 00029 #include "llvm/Analysis/CaptureTracking.h" 00030 #include "llvm/IR/GlobalVariable.h" 00031 #include "llvm/IR/InstIterator.h" 00032 #include "llvm/IR/IntrinsicInst.h" 00033 #include "llvm/IR/LLVMContext.h" 00034 #include "llvm/Target/TargetLibraryInfo.h" 00035 using namespace llvm; 00036 00037 #define DEBUG_TYPE "functionattrs" 00038 00039 STATISTIC(NumReadNone, "Number of functions marked readnone"); 00040 STATISTIC(NumReadOnly, "Number of functions marked readonly"); 00041 STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); 00042 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); 00043 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); 00044 STATISTIC(NumNoAlias, "Number of function returns marked noalias"); 00045 STATISTIC(NumAnnotated, "Number of attributes added to library functions"); 00046 00047 namespace { 00048 struct FunctionAttrs : public CallGraphSCCPass { 00049 static char ID; // Pass identification, replacement for typeid 00050 FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) { 00051 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); 00052 } 00053 00054 // runOnSCC - Analyze the SCC, performing the transformation if possible. 00055 bool runOnSCC(CallGraphSCC &SCC) override; 00056 00057 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. 00058 bool AddReadAttrs(const CallGraphSCC &SCC); 00059 00060 // AddArgumentAttrs - Deduce nocapture attributes for the SCC. 00061 bool AddArgumentAttrs(const CallGraphSCC &SCC); 00062 00063 // IsFunctionMallocLike - Does this function allocate new memory? 00064 bool IsFunctionMallocLike(Function *F, 00065 SmallPtrSet<Function*, 8> &) const; 00066 00067 // AddNoAliasAttrs - Deduce noalias attributes for the SCC. 00068 bool AddNoAliasAttrs(const CallGraphSCC &SCC); 00069 00070 // Utility methods used by inferPrototypeAttributes to add attributes 00071 // and maintain annotation statistics. 00072 00073 void setDoesNotAccessMemory(Function &F) { 00074 if (!F.doesNotAccessMemory()) { 00075 F.setDoesNotAccessMemory(); 00076 ++NumAnnotated; 00077 } 00078 } 00079 00080 void setOnlyReadsMemory(Function &F) { 00081 if (!F.onlyReadsMemory()) { 00082 F.setOnlyReadsMemory(); 00083 ++NumAnnotated; 00084 } 00085 } 00086 00087 void setDoesNotThrow(Function &F) { 00088 if (!F.doesNotThrow()) { 00089 F.setDoesNotThrow(); 00090 ++NumAnnotated; 00091 } 00092 } 00093 00094 void setDoesNotCapture(Function &F, unsigned n) { 00095 if (!F.doesNotCapture(n)) { 00096 F.setDoesNotCapture(n); 00097 ++NumAnnotated; 00098 } 00099 } 00100 00101 void setOnlyReadsMemory(Function &F, unsigned n) { 00102 if (!F.onlyReadsMemory(n)) { 00103 F.setOnlyReadsMemory(n); 00104 ++NumAnnotated; 00105 } 00106 } 00107 00108 void setDoesNotAlias(Function &F, unsigned n) { 00109 if (!F.doesNotAlias(n)) { 00110 F.setDoesNotAlias(n); 00111 ++NumAnnotated; 00112 } 00113 } 00114 00115 // inferPrototypeAttributes - Analyze the name and prototype of the 00116 // given function and set any applicable attributes. Returns true 00117 // if any attributes were set and false otherwise. 00118 bool inferPrototypeAttributes(Function &F); 00119 00120 // annotateLibraryCalls - Adds attributes to well-known standard library 00121 // call declarations. 00122 bool annotateLibraryCalls(const CallGraphSCC &SCC); 00123 00124 void getAnalysisUsage(AnalysisUsage &AU) const override { 00125 AU.setPreservesCFG(); 00126 AU.addRequired<AliasAnalysis>(); 00127 AU.addRequired<TargetLibraryInfo>(); 00128 CallGraphSCCPass::getAnalysisUsage(AU); 00129 } 00130 00131 private: 00132 AliasAnalysis *AA; 00133 TargetLibraryInfo *TLI; 00134 }; 00135 } 00136 00137 char FunctionAttrs::ID = 0; 00138 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", 00139 "Deduce function attributes", false, false) 00140 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 00141 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 00142 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) 00143 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", 00144 "Deduce function attributes", false, false) 00145 00146 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } 00147 00148 00149 /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. 00150 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { 00151 SmallPtrSet<Function*, 8> SCCNodes; 00152 00153 // Fill SCCNodes with the elements of the SCC. Used for quickly 00154 // looking up whether a given CallGraphNode is in this SCC. 00155 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 00156 SCCNodes.insert((*I)->getFunction()); 00157 00158 // Check if any of the functions in the SCC read or write memory. If they 00159 // write memory then they can't be marked readnone or readonly. 00160 bool ReadsMemory = false; 00161 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00162 Function *F = (*I)->getFunction(); 00163 00164 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) 00165 // External node or node we don't want to optimize - assume it may write 00166 // memory and give up. 00167 return false; 00168 00169 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F); 00170 if (MRB == AliasAnalysis::DoesNotAccessMemory) 00171 // Already perfect! 00172 continue; 00173 00174 // Definitions with weak linkage may be overridden at linktime with 00175 // something that writes memory, so treat them like declarations. 00176 if (F->isDeclaration() || F->mayBeOverridden()) { 00177 if (!AliasAnalysis::onlyReadsMemory(MRB)) 00178 // May write memory. Just give up. 00179 return false; 00180 00181 ReadsMemory = true; 00182 continue; 00183 } 00184 00185 // Scan the function body for instructions that may read or write memory. 00186 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { 00187 Instruction *I = &*II; 00188 00189 // Some instructions can be ignored even if they read or write memory. 00190 // Detect these now, skipping to the next instruction if one is found. 00191 CallSite CS(cast<Value>(I)); 00192 if (CS) { 00193 // Ignore calls to functions in the same SCC. 00194 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) 00195 continue; 00196 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS); 00197 // If the call doesn't access arbitrary memory, we may be able to 00198 // figure out something. 00199 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { 00200 // If the call does access argument pointees, check each argument. 00201 if (AliasAnalysis::doesAccessArgPointees(MRB)) 00202 // Check whether all pointer arguments point to local memory, and 00203 // ignore calls that only access local memory. 00204 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); 00205 CI != CE; ++CI) { 00206 Value *Arg = *CI; 00207 if (Arg->getType()->isPointerTy()) { 00208 AAMDNodes AAInfo; 00209 I->getAAMetadata(AAInfo); 00210 00211 AliasAnalysis::Location Loc(Arg, 00212 AliasAnalysis::UnknownSize, AAInfo); 00213 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) { 00214 if (MRB & AliasAnalysis::Mod) 00215 // Writes non-local memory. Give up. 00216 return false; 00217 if (MRB & AliasAnalysis::Ref) 00218 // Ok, it reads non-local memory. 00219 ReadsMemory = true; 00220 } 00221 } 00222 } 00223 continue; 00224 } 00225 // The call could access any memory. If that includes writes, give up. 00226 if (MRB & AliasAnalysis::Mod) 00227 return false; 00228 // If it reads, note it. 00229 if (MRB & AliasAnalysis::Ref) 00230 ReadsMemory = true; 00231 continue; 00232 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 00233 // Ignore non-volatile loads from local memory. (Atomic is okay here.) 00234 if (!LI->isVolatile()) { 00235 AliasAnalysis::Location Loc = AA->getLocation(LI); 00236 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) 00237 continue; 00238 } 00239 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 00240 // Ignore non-volatile stores to local memory. (Atomic is okay here.) 00241 if (!SI->isVolatile()) { 00242 AliasAnalysis::Location Loc = AA->getLocation(SI); 00243 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) 00244 continue; 00245 } 00246 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { 00247 // Ignore vaargs on local memory. 00248 AliasAnalysis::Location Loc = AA->getLocation(VI); 00249 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) 00250 continue; 00251 } 00252 00253 // Any remaining instructions need to be taken seriously! Check if they 00254 // read or write memory. 00255 if (I->mayWriteToMemory()) 00256 // Writes memory. Just give up. 00257 return false; 00258 00259 // If this instruction may read memory, remember that. 00260 ReadsMemory |= I->mayReadFromMemory(); 00261 } 00262 } 00263 00264 // Success! Functions in this SCC do not access memory, or only read memory. 00265 // Give them the appropriate attribute. 00266 bool MadeChange = false; 00267 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00268 Function *F = (*I)->getFunction(); 00269 00270 if (F->doesNotAccessMemory()) 00271 // Already perfect! 00272 continue; 00273 00274 if (F->onlyReadsMemory() && ReadsMemory) 00275 // No change. 00276 continue; 00277 00278 MadeChange = true; 00279 00280 // Clear out any existing attributes. 00281 AttrBuilder B; 00282 B.addAttribute(Attribute::ReadOnly) 00283 .addAttribute(Attribute::ReadNone); 00284 F->removeAttributes(AttributeSet::FunctionIndex, 00285 AttributeSet::get(F->getContext(), 00286 AttributeSet::FunctionIndex, B)); 00287 00288 // Add in the new attribute. 00289 F->addAttribute(AttributeSet::FunctionIndex, 00290 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); 00291 00292 if (ReadsMemory) 00293 ++NumReadOnly; 00294 else 00295 ++NumReadNone; 00296 } 00297 00298 return MadeChange; 00299 } 00300 00301 namespace { 00302 // For a given pointer Argument, this retains a list of Arguments of functions 00303 // in the same SCC that the pointer data flows into. We use this to build an 00304 // SCC of the arguments. 00305 struct ArgumentGraphNode { 00306 Argument *Definition; 00307 SmallVector<ArgumentGraphNode*, 4> Uses; 00308 }; 00309 00310 class ArgumentGraph { 00311 // We store pointers to ArgumentGraphNode objects, so it's important that 00312 // that they not move around upon insert. 00313 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy; 00314 00315 ArgumentMapTy ArgumentMap; 00316 00317 // There is no root node for the argument graph, in fact: 00318 // void f(int *x, int *y) { if (...) f(x, y); } 00319 // is an example where the graph is disconnected. The SCCIterator requires a 00320 // single entry point, so we maintain a fake ("synthetic") root node that 00321 // uses every node. Because the graph is directed and nothing points into 00322 // the root, it will not participate in any SCCs (except for its own). 00323 ArgumentGraphNode SyntheticRoot; 00324 00325 public: 00326 ArgumentGraph() { SyntheticRoot.Definition = nullptr; } 00327 00328 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator; 00329 00330 iterator begin() { return SyntheticRoot.Uses.begin(); } 00331 iterator end() { return SyntheticRoot.Uses.end(); } 00332 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } 00333 00334 ArgumentGraphNode *operator[](Argument *A) { 00335 ArgumentGraphNode &Node = ArgumentMap[A]; 00336 Node.Definition = A; 00337 SyntheticRoot.Uses.push_back(&Node); 00338 return &Node; 00339 } 00340 }; 00341 00342 // This tracker checks whether callees are in the SCC, and if so it does not 00343 // consider that a capture, instead adding it to the "Uses" list and 00344 // continuing with the analysis. 00345 struct ArgumentUsesTracker : public CaptureTracker { 00346 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes) 00347 : Captured(false), SCCNodes(SCCNodes) {} 00348 00349 void tooManyUses() override { Captured = true; } 00350 00351 bool captured(const Use *U) override { 00352 CallSite CS(U->getUser()); 00353 if (!CS.getInstruction()) { Captured = true; return true; } 00354 00355 Function *F = CS.getCalledFunction(); 00356 if (!F || !SCCNodes.count(F)) { Captured = true; return true; } 00357 00358 bool Found = false; 00359 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 00360 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end(); 00361 PI != PE; ++PI, ++AI) { 00362 if (AI == AE) { 00363 assert(F->isVarArg() && "More params than args in non-varargs call"); 00364 Captured = true; 00365 return true; 00366 } 00367 if (PI == U) { 00368 Uses.push_back(AI); 00369 Found = true; 00370 break; 00371 } 00372 } 00373 assert(Found && "Capturing call-site captured nothing?"); 00374 (void)Found; 00375 return false; 00376 } 00377 00378 bool Captured; // True only if certainly captured (used outside our SCC). 00379 SmallVector<Argument*, 4> Uses; // Uses within our SCC. 00380 00381 const SmallPtrSet<Function*, 8> &SCCNodes; 00382 }; 00383 } 00384 00385 namespace llvm { 00386 template<> struct GraphTraits<ArgumentGraphNode*> { 00387 typedef ArgumentGraphNode NodeType; 00388 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType; 00389 00390 static inline NodeType *getEntryNode(NodeType *A) { return A; } 00391 static inline ChildIteratorType child_begin(NodeType *N) { 00392 return N->Uses.begin(); 00393 } 00394 static inline ChildIteratorType child_end(NodeType *N) { 00395 return N->Uses.end(); 00396 } 00397 }; 00398 template<> struct GraphTraits<ArgumentGraph*> 00399 : public GraphTraits<ArgumentGraphNode*> { 00400 static NodeType *getEntryNode(ArgumentGraph *AG) { 00401 return AG->getEntryNode(); 00402 } 00403 static ChildIteratorType nodes_begin(ArgumentGraph *AG) { 00404 return AG->begin(); 00405 } 00406 static ChildIteratorType nodes_end(ArgumentGraph *AG) { 00407 return AG->end(); 00408 } 00409 }; 00410 } 00411 00412 // Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. 00413 static Attribute::AttrKind 00414 determinePointerReadAttrs(Argument *A, 00415 const SmallPtrSet<Argument*, 8> &SCCNodes) { 00416 00417 SmallVector<Use*, 32> Worklist; 00418 SmallSet<Use*, 32> Visited; 00419 int Count = 0; 00420 00421 // inalloca arguments are always clobbered by the call. 00422 if (A->hasInAllocaAttr()) 00423 return Attribute::None; 00424 00425 bool IsRead = false; 00426 // We don't need to track IsWritten. If A is written to, return immediately. 00427 00428 for (Use &U : A->uses()) { 00429 if (Count++ >= 20) 00430 return Attribute::None; 00431 00432 Visited.insert(&U); 00433 Worklist.push_back(&U); 00434 } 00435 00436 while (!Worklist.empty()) { 00437 Use *U = Worklist.pop_back_val(); 00438 Instruction *I = cast<Instruction>(U->getUser()); 00439 Value *V = U->get(); 00440 00441 switch (I->getOpcode()) { 00442 case Instruction::BitCast: 00443 case Instruction::GetElementPtr: 00444 case Instruction::PHI: 00445 case Instruction::Select: 00446 case Instruction::AddrSpaceCast: 00447 // The original value is not read/written via this if the new value isn't. 00448 for (Use &UU : I->uses()) 00449 if (Visited.insert(&UU)) 00450 Worklist.push_back(&UU); 00451 break; 00452 00453 case Instruction::Call: 00454 case Instruction::Invoke: { 00455 bool Captures = true; 00456 00457 if (I->getType()->isVoidTy()) 00458 Captures = false; 00459 00460 auto AddUsersToWorklistIfCapturing = [&] { 00461 if (Captures) 00462 for (Use &UU : I->uses()) 00463 if (Visited.insert(&UU)) 00464 Worklist.push_back(&UU); 00465 }; 00466 00467 CallSite CS(I); 00468 if (CS.doesNotAccessMemory()) { 00469 AddUsersToWorklistIfCapturing(); 00470 continue; 00471 } 00472 00473 Function *F = CS.getCalledFunction(); 00474 if (!F) { 00475 if (CS.onlyReadsMemory()) { 00476 IsRead = true; 00477 AddUsersToWorklistIfCapturing(); 00478 continue; 00479 } 00480 return Attribute::None; 00481 } 00482 00483 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 00484 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); 00485 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) { 00486 if (A->get() == V) { 00487 if (AI == AE) { 00488 assert(F->isVarArg() && 00489 "More params than args in non-varargs call."); 00490 return Attribute::None; 00491 } 00492 Captures &= !CS.doesNotCapture(A - B); 00493 if (SCCNodes.count(AI)) 00494 continue; 00495 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B)) 00496 return Attribute::None; 00497 if (!CS.doesNotAccessMemory(A - B)) 00498 IsRead = true; 00499 } 00500 } 00501 AddUsersToWorklistIfCapturing(); 00502 break; 00503 } 00504 00505 case Instruction::Load: 00506 IsRead = true; 00507 break; 00508 00509 case Instruction::ICmp: 00510 case Instruction::Ret: 00511 break; 00512 00513 default: 00514 return Attribute::None; 00515 } 00516 } 00517 00518 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; 00519 } 00520 00521 /// AddArgumentAttrs - Deduce nocapture attributes for the SCC. 00522 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) { 00523 bool Changed = false; 00524 00525 SmallPtrSet<Function*, 8> SCCNodes; 00526 00527 // Fill SCCNodes with the elements of the SCC. Used for quickly 00528 // looking up whether a given CallGraphNode is in this SCC. 00529 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00530 Function *F = (*I)->getFunction(); 00531 if (F && !F->isDeclaration() && !F->mayBeOverridden() && 00532 !F->hasFnAttribute(Attribute::OptimizeNone)) 00533 SCCNodes.insert(F); 00534 } 00535 00536 ArgumentGraph AG; 00537 00538 AttrBuilder B; 00539 B.addAttribute(Attribute::NoCapture); 00540 00541 // Check each function in turn, determining which pointer arguments are not 00542 // captured. 00543 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00544 Function *F = (*I)->getFunction(); 00545 00546 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) 00547 // External node or function we're trying not to optimize - only a problem 00548 // for arguments that we pass to it. 00549 continue; 00550 00551 // Definitions with weak linkage may be overridden at linktime with 00552 // something that captures pointers, so treat them like declarations. 00553 if (F->isDeclaration() || F->mayBeOverridden()) 00554 continue; 00555 00556 // Functions that are readonly (or readnone) and nounwind and don't return 00557 // a value can't capture arguments. Don't analyze them. 00558 if (F->onlyReadsMemory() && F->doesNotThrow() && 00559 F->getReturnType()->isVoidTy()) { 00560 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); 00561 A != E; ++A) { 00562 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { 00563 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); 00564 ++NumNoCapture; 00565 Changed = true; 00566 } 00567 } 00568 continue; 00569 } 00570 00571 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); 00572 A != E; ++A) { 00573 if (!A->getType()->isPointerTy()) continue; 00574 bool HasNonLocalUses = false; 00575 if (!A->hasNoCaptureAttr()) { 00576 ArgumentUsesTracker Tracker(SCCNodes); 00577 PointerMayBeCaptured(A, &Tracker); 00578 if (!Tracker.Captured) { 00579 if (Tracker.Uses.empty()) { 00580 // If it's trivially not captured, mark it nocapture now. 00581 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B)); 00582 ++NumNoCapture; 00583 Changed = true; 00584 } else { 00585 // If it's not trivially captured and not trivially not captured, 00586 // then it must be calling into another function in our SCC. Save 00587 // its particulars for Argument-SCC analysis later. 00588 ArgumentGraphNode *Node = AG[A]; 00589 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(), 00590 UE = Tracker.Uses.end(); UI != UE; ++UI) { 00591 Node->Uses.push_back(AG[*UI]); 00592 if (*UI != A) 00593 HasNonLocalUses = true; 00594 } 00595 } 00596 } 00597 // Otherwise, it's captured. Don't bother doing SCC analysis on it. 00598 } 00599 if (!HasNonLocalUses && !A->onlyReadsMemory()) { 00600 // Can we determine that it's readonly/readnone without doing an SCC? 00601 // Note that we don't allow any calls at all here, or else our result 00602 // will be dependent on the iteration order through the functions in the 00603 // SCC. 00604 SmallPtrSet<Argument*, 8> Self; 00605 Self.insert(A); 00606 Attribute::AttrKind R = determinePointerReadAttrs(A, Self); 00607 if (R != Attribute::None) { 00608 AttrBuilder B; 00609 B.addAttribute(R); 00610 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 00611 Changed = true; 00612 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 00613 } 00614 } 00615 } 00616 } 00617 00618 // The graph we've collected is partial because we stopped scanning for 00619 // argument uses once we solved the argument trivially. These partial nodes 00620 // show up as ArgumentGraphNode objects with an empty Uses list, and for 00621 // these nodes the final decision about whether they capture has already been 00622 // made. If the definition doesn't have a 'nocapture' attribute by now, it 00623 // captures. 00624 00625 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) { 00626 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; 00627 if (ArgumentSCC.size() == 1) { 00628 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node 00629 00630 // eg. "void f(int* x) { if (...) f(x); }" 00631 if (ArgumentSCC[0]->Uses.size() == 1 && 00632 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { 00633 Argument *A = ArgumentSCC[0]->Definition; 00634 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 00635 ++NumNoCapture; 00636 Changed = true; 00637 } 00638 continue; 00639 } 00640 00641 bool SCCCaptured = false; 00642 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); 00643 I != E && !SCCCaptured; ++I) { 00644 ArgumentGraphNode *Node = *I; 00645 if (Node->Uses.empty()) { 00646 if (!Node->Definition->hasNoCaptureAttr()) 00647 SCCCaptured = true; 00648 } 00649 } 00650 if (SCCCaptured) continue; 00651 00652 SmallPtrSet<Argument*, 8> ArgumentSCCNodes; 00653 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for 00654 // quickly looking up whether a given Argument is in this ArgumentSCC. 00655 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) { 00656 ArgumentSCCNodes.insert((*I)->Definition); 00657 } 00658 00659 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); 00660 I != E && !SCCCaptured; ++I) { 00661 ArgumentGraphNode *N = *I; 00662 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(), 00663 UE = N->Uses.end(); UI != UE; ++UI) { 00664 Argument *A = (*UI)->Definition; 00665 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) 00666 continue; 00667 SCCCaptured = true; 00668 break; 00669 } 00670 } 00671 if (SCCCaptured) continue; 00672 00673 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 00674 Argument *A = ArgumentSCC[i]->Definition; 00675 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 00676 ++NumNoCapture; 00677 Changed = true; 00678 } 00679 00680 // We also want to compute readonly/readnone. With a small number of false 00681 // negatives, we can assume that any pointer which is captured isn't going 00682 // to be provably readonly or readnone, since by definition we can't 00683 // analyze all uses of a captured pointer. 00684 // 00685 // The false negatives happen when the pointer is captured by a function 00686 // that promises readonly/readnone behaviour on the pointer, then the 00687 // pointer's lifetime ends before anything that writes to arbitrary memory. 00688 // Also, a readonly/readnone pointer may be returned, but returning a 00689 // pointer is capturing it. 00690 00691 Attribute::AttrKind ReadAttr = Attribute::ReadNone; 00692 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 00693 Argument *A = ArgumentSCC[i]->Definition; 00694 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes); 00695 if (K == Attribute::ReadNone) 00696 continue; 00697 if (K == Attribute::ReadOnly) { 00698 ReadAttr = Attribute::ReadOnly; 00699 continue; 00700 } 00701 ReadAttr = K; 00702 break; 00703 } 00704 00705 if (ReadAttr != Attribute::None) { 00706 AttrBuilder B; 00707 B.addAttribute(ReadAttr); 00708 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 00709 Argument *A = ArgumentSCC[i]->Definition; 00710 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 00711 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 00712 Changed = true; 00713 } 00714 } 00715 } 00716 00717 return Changed; 00718 } 00719 00720 /// IsFunctionMallocLike - A function is malloc-like if it returns either null 00721 /// or a pointer that doesn't alias any other pointer visible to the caller. 00722 bool FunctionAttrs::IsFunctionMallocLike(Function *F, 00723 SmallPtrSet<Function*, 8> &SCCNodes) const { 00724 SmallSetVector<Value *, 8> FlowsToReturn; 00725 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) 00726 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) 00727 FlowsToReturn.insert(Ret->getReturnValue()); 00728 00729 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { 00730 Value *RetVal = FlowsToReturn[i]; 00731 00732 if (Constant *C = dyn_cast<Constant>(RetVal)) { 00733 if (!C->isNullValue() && !isa<UndefValue>(C)) 00734 return false; 00735 00736 continue; 00737 } 00738 00739 if (isa<Argument>(RetVal)) 00740 return false; 00741 00742 if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) 00743 switch (RVI->getOpcode()) { 00744 // Extend the analysis by looking upwards. 00745 case Instruction::BitCast: 00746 case Instruction::GetElementPtr: 00747 case Instruction::AddrSpaceCast: 00748 FlowsToReturn.insert(RVI->getOperand(0)); 00749 continue; 00750 case Instruction::Select: { 00751 SelectInst *SI = cast<SelectInst>(RVI); 00752 FlowsToReturn.insert(SI->getTrueValue()); 00753 FlowsToReturn.insert(SI->getFalseValue()); 00754 continue; 00755 } 00756 case Instruction::PHI: { 00757 PHINode *PN = cast<PHINode>(RVI); 00758 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 00759 FlowsToReturn.insert(PN->getIncomingValue(i)); 00760 continue; 00761 } 00762 00763 // Check whether the pointer came from an allocation. 00764 case Instruction::Alloca: 00765 break; 00766 case Instruction::Call: 00767 case Instruction::Invoke: { 00768 CallSite CS(RVI); 00769 if (CS.paramHasAttr(0, Attribute::NoAlias)) 00770 break; 00771 if (CS.getCalledFunction() && 00772 SCCNodes.count(CS.getCalledFunction())) 00773 break; 00774 } // fall-through 00775 default: 00776 return false; // Did not come from an allocation. 00777 } 00778 00779 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) 00780 return false; 00781 } 00782 00783 return true; 00784 } 00785 00786 /// AddNoAliasAttrs - Deduce noalias attributes for the SCC. 00787 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { 00788 SmallPtrSet<Function*, 8> SCCNodes; 00789 00790 // Fill SCCNodes with the elements of the SCC. Used for quickly 00791 // looking up whether a given CallGraphNode is in this SCC. 00792 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 00793 SCCNodes.insert((*I)->getFunction()); 00794 00795 // Check each function in turn, determining which functions return noalias 00796 // pointers. 00797 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00798 Function *F = (*I)->getFunction(); 00799 00800 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) 00801 // External node or node we don't want to optimize - skip it; 00802 return false; 00803 00804 // Already noalias. 00805 if (F->doesNotAlias(0)) 00806 continue; 00807 00808 // Definitions with weak linkage may be overridden at linktime, so 00809 // treat them like declarations. 00810 if (F->isDeclaration() || F->mayBeOverridden()) 00811 return false; 00812 00813 // We annotate noalias return values, which are only applicable to 00814 // pointer types. 00815 if (!F->getReturnType()->isPointerTy()) 00816 continue; 00817 00818 if (!IsFunctionMallocLike(F, SCCNodes)) 00819 return false; 00820 } 00821 00822 bool MadeChange = false; 00823 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 00824 Function *F = (*I)->getFunction(); 00825 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) 00826 continue; 00827 00828 F->setDoesNotAlias(0); 00829 ++NumNoAlias; 00830 MadeChange = true; 00831 } 00832 00833 return MadeChange; 00834 } 00835 00836 /// inferPrototypeAttributes - Analyze the name and prototype of the 00837 /// given function and set any applicable attributes. Returns true 00838 /// if any attributes were set and false otherwise. 00839 bool FunctionAttrs::inferPrototypeAttributes(Function &F) { 00840 if (F.hasFnAttribute(Attribute::OptimizeNone)) 00841 return false; 00842 00843 FunctionType *FTy = F.getFunctionType(); 00844 LibFunc::Func TheLibFunc; 00845 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc))) 00846 return false; 00847 00848 switch (TheLibFunc) { 00849 case LibFunc::strlen: 00850 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 00851 return false; 00852 setOnlyReadsMemory(F); 00853 setDoesNotThrow(F); 00854 setDoesNotCapture(F, 1); 00855 break; 00856 case LibFunc::strchr: 00857 case LibFunc::strrchr: 00858 if (FTy->getNumParams() != 2 || 00859 !FTy->getParamType(0)->isPointerTy() || 00860 !FTy->getParamType(1)->isIntegerTy()) 00861 return false; 00862 setOnlyReadsMemory(F); 00863 setDoesNotThrow(F); 00864 break; 00865 case LibFunc::strtol: 00866 case LibFunc::strtod: 00867 case LibFunc::strtof: 00868 case LibFunc::strtoul: 00869 case LibFunc::strtoll: 00870 case LibFunc::strtold: 00871 case LibFunc::strtoull: 00872 if (FTy->getNumParams() < 2 || 00873 !FTy->getParamType(1)->isPointerTy()) 00874 return false; 00875 setDoesNotThrow(F); 00876 setDoesNotCapture(F, 2); 00877 setOnlyReadsMemory(F, 1); 00878 break; 00879 case LibFunc::strcpy: 00880 case LibFunc::stpcpy: 00881 case LibFunc::strcat: 00882 case LibFunc::strncat: 00883 case LibFunc::strncpy: 00884 case LibFunc::stpncpy: 00885 if (FTy->getNumParams() < 2 || 00886 !FTy->getParamType(1)->isPointerTy()) 00887 return false; 00888 setDoesNotThrow(F); 00889 setDoesNotCapture(F, 2); 00890 setOnlyReadsMemory(F, 2); 00891 break; 00892 case LibFunc::strxfrm: 00893 if (FTy->getNumParams() != 3 || 00894 !FTy->getParamType(0)->isPointerTy() || 00895 !FTy->getParamType(1)->isPointerTy()) 00896 return false; 00897 setDoesNotThrow(F); 00898 setDoesNotCapture(F, 1); 00899 setDoesNotCapture(F, 2); 00900 setOnlyReadsMemory(F, 2); 00901 break; 00902 case LibFunc::strcmp: //0,1 00903 case LibFunc::strspn: // 0,1 00904 case LibFunc::strncmp: // 0,1 00905 case LibFunc::strcspn: //0,1 00906 case LibFunc::strcoll: //0,1 00907 case LibFunc::strcasecmp: // 0,1 00908 case LibFunc::strncasecmp: // 00909 if (FTy->getNumParams() < 2 || 00910 !FTy->getParamType(0)->isPointerTy() || 00911 !FTy->getParamType(1)->isPointerTy()) 00912 return false; 00913 setOnlyReadsMemory(F); 00914 setDoesNotThrow(F); 00915 setDoesNotCapture(F, 1); 00916 setDoesNotCapture(F, 2); 00917 break; 00918 case LibFunc::strstr: 00919 case LibFunc::strpbrk: 00920 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 00921 return false; 00922 setOnlyReadsMemory(F); 00923 setDoesNotThrow(F); 00924 setDoesNotCapture(F, 2); 00925 break; 00926 case LibFunc::strtok: 00927 case LibFunc::strtok_r: 00928 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) 00929 return false; 00930 setDoesNotThrow(F); 00931 setDoesNotCapture(F, 2); 00932 setOnlyReadsMemory(F, 2); 00933 break; 00934 case LibFunc::scanf: 00935 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) 00936 return false; 00937 setDoesNotThrow(F); 00938 setDoesNotCapture(F, 1); 00939 setOnlyReadsMemory(F, 1); 00940 break; 00941 case LibFunc::setbuf: 00942 case LibFunc::setvbuf: 00943 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) 00944 return false; 00945 setDoesNotThrow(F); 00946 setDoesNotCapture(F, 1); 00947 break; 00948 case LibFunc::strdup: 00949 case LibFunc::strndup: 00950 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() || 00951 !FTy->getParamType(0)->isPointerTy()) 00952 return false; 00953 setDoesNotThrow(F); 00954 setDoesNotAlias(F, 0); 00955 setDoesNotCapture(F, 1); 00956 setOnlyReadsMemory(F, 1); 00957 break; 00958 case LibFunc::stat: 00959 case LibFunc::statvfs: 00960 if (FTy->getNumParams() < 2 || 00961 !FTy->getParamType(0)->isPointerTy() || 00962 !FTy->getParamType(1)->isPointerTy()) 00963 return false; 00964 setDoesNotThrow(F); 00965 setDoesNotCapture(F, 1); 00966 setDoesNotCapture(F, 2); 00967 setOnlyReadsMemory(F, 1); 00968 break; 00969 case LibFunc::sscanf: 00970 if (FTy->getNumParams() < 2 || 00971 !FTy->getParamType(0)->isPointerTy() || 00972 !FTy->getParamType(1)->isPointerTy()) 00973 return false; 00974 setDoesNotThrow(F); 00975 setDoesNotCapture(F, 1); 00976 setDoesNotCapture(F, 2); 00977 setOnlyReadsMemory(F, 1); 00978 setOnlyReadsMemory(F, 2); 00979 break; 00980 case LibFunc::sprintf: 00981 if (FTy->getNumParams() < 2 || 00982 !FTy->getParamType(0)->isPointerTy() || 00983 !FTy->getParamType(1)->isPointerTy()) 00984 return false; 00985 setDoesNotThrow(F); 00986 setDoesNotCapture(F, 1); 00987 setDoesNotCapture(F, 2); 00988 setOnlyReadsMemory(F, 2); 00989 break; 00990 case LibFunc::snprintf: 00991 if (FTy->getNumParams() != 3 || 00992 !FTy->getParamType(0)->isPointerTy() || 00993 !FTy->getParamType(2)->isPointerTy()) 00994 return false; 00995 setDoesNotThrow(F); 00996 setDoesNotCapture(F, 1); 00997 setDoesNotCapture(F, 3); 00998 setOnlyReadsMemory(F, 3); 00999 break; 01000 case LibFunc::setitimer: 01001 if (FTy->getNumParams() != 3 || 01002 !FTy->getParamType(1)->isPointerTy() || 01003 !FTy->getParamType(2)->isPointerTy()) 01004 return false; 01005 setDoesNotThrow(F); 01006 setDoesNotCapture(F, 2); 01007 setDoesNotCapture(F, 3); 01008 setOnlyReadsMemory(F, 2); 01009 break; 01010 case LibFunc::system: 01011 if (FTy->getNumParams() != 1 || 01012 !FTy->getParamType(0)->isPointerTy()) 01013 return false; 01014 // May throw; "system" is a valid pthread cancellation point. 01015 setDoesNotCapture(F, 1); 01016 setOnlyReadsMemory(F, 1); 01017 break; 01018 case LibFunc::malloc: 01019 if (FTy->getNumParams() != 1 || 01020 !FTy->getReturnType()->isPointerTy()) 01021 return false; 01022 setDoesNotThrow(F); 01023 setDoesNotAlias(F, 0); 01024 break; 01025 case LibFunc::memcmp: 01026 if (FTy->getNumParams() != 3 || 01027 !FTy->getParamType(0)->isPointerTy() || 01028 !FTy->getParamType(1)->isPointerTy()) 01029 return false; 01030 setOnlyReadsMemory(F); 01031 setDoesNotThrow(F); 01032 setDoesNotCapture(F, 1); 01033 setDoesNotCapture(F, 2); 01034 break; 01035 case LibFunc::memchr: 01036 case LibFunc::memrchr: 01037 if (FTy->getNumParams() != 3) 01038 return false; 01039 setOnlyReadsMemory(F); 01040 setDoesNotThrow(F); 01041 break; 01042 case LibFunc::modf: 01043 case LibFunc::modff: 01044 case LibFunc::modfl: 01045 if (FTy->getNumParams() < 2 || 01046 !FTy->getParamType(1)->isPointerTy()) 01047 return false; 01048 setDoesNotThrow(F); 01049 setDoesNotCapture(F, 2); 01050 break; 01051 case LibFunc::memcpy: 01052 case LibFunc::memccpy: 01053 case LibFunc::memmove: 01054 if (FTy->getNumParams() < 2 || 01055 !FTy->getParamType(1)->isPointerTy()) 01056 return false; 01057 setDoesNotThrow(F); 01058 setDoesNotCapture(F, 2); 01059 setOnlyReadsMemory(F, 2); 01060 break; 01061 case LibFunc::memalign: 01062 if (!FTy->getReturnType()->isPointerTy()) 01063 return false; 01064 setDoesNotAlias(F, 0); 01065 break; 01066 case LibFunc::mkdir: 01067 if (FTy->getNumParams() == 0 || 01068 !FTy->getParamType(0)->isPointerTy()) 01069 return false; 01070 setDoesNotThrow(F); 01071 setDoesNotCapture(F, 1); 01072 setOnlyReadsMemory(F, 1); 01073 break; 01074 case LibFunc::mktime: 01075 if (FTy->getNumParams() == 0 || 01076 !FTy->getParamType(0)->isPointerTy()) 01077 return false; 01078 setDoesNotThrow(F); 01079 setDoesNotCapture(F, 1); 01080 break; 01081 case LibFunc::realloc: 01082 if (FTy->getNumParams() != 2 || 01083 !FTy->getParamType(0)->isPointerTy() || 01084 !FTy->getReturnType()->isPointerTy()) 01085 return false; 01086 setDoesNotThrow(F); 01087 setDoesNotAlias(F, 0); 01088 setDoesNotCapture(F, 1); 01089 break; 01090 case LibFunc::read: 01091 if (FTy->getNumParams() != 3 || 01092 !FTy->getParamType(1)->isPointerTy()) 01093 return false; 01094 // May throw; "read" is a valid pthread cancellation point. 01095 setDoesNotCapture(F, 2); 01096 break; 01097 case LibFunc::rewind: 01098 if (FTy->getNumParams() < 1 || 01099 !FTy->getParamType(0)->isPointerTy()) 01100 return false; 01101 setDoesNotThrow(F); 01102 setDoesNotCapture(F, 1); 01103 break; 01104 case LibFunc::rmdir: 01105 case LibFunc::remove: 01106 case LibFunc::realpath: 01107 if (FTy->getNumParams() < 1 || 01108 !FTy->getParamType(0)->isPointerTy()) 01109 return false; 01110 setDoesNotThrow(F); 01111 setDoesNotCapture(F, 1); 01112 setOnlyReadsMemory(F, 1); 01113 break; 01114 case LibFunc::rename: 01115 if (FTy->getNumParams() < 2 || 01116 !FTy->getParamType(0)->isPointerTy() || 01117 !FTy->getParamType(1)->isPointerTy()) 01118 return false; 01119 setDoesNotThrow(F); 01120 setDoesNotCapture(F, 1); 01121 setDoesNotCapture(F, 2); 01122 setOnlyReadsMemory(F, 1); 01123 setOnlyReadsMemory(F, 2); 01124 break; 01125 case LibFunc::readlink: 01126 if (FTy->getNumParams() < 2 || 01127 !FTy->getParamType(0)->isPointerTy() || 01128 !FTy->getParamType(1)->isPointerTy()) 01129 return false; 01130 setDoesNotThrow(F); 01131 setDoesNotCapture(F, 1); 01132 setDoesNotCapture(F, 2); 01133 setOnlyReadsMemory(F, 1); 01134 break; 01135 case LibFunc::write: 01136 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) 01137 return false; 01138 // May throw; "write" is a valid pthread cancellation point. 01139 setDoesNotCapture(F, 2); 01140 setOnlyReadsMemory(F, 2); 01141 break; 01142 case LibFunc::bcopy: 01143 if (FTy->getNumParams() != 3 || 01144 !FTy->getParamType(0)->isPointerTy() || 01145 !FTy->getParamType(1)->isPointerTy()) 01146 return false; 01147 setDoesNotThrow(F); 01148 setDoesNotCapture(F, 1); 01149 setDoesNotCapture(F, 2); 01150 setOnlyReadsMemory(F, 1); 01151 break; 01152 case LibFunc::bcmp: 01153 if (FTy->getNumParams() != 3 || 01154 !FTy->getParamType(0)->isPointerTy() || 01155 !FTy->getParamType(1)->isPointerTy()) 01156 return false; 01157 setDoesNotThrow(F); 01158 setOnlyReadsMemory(F); 01159 setDoesNotCapture(F, 1); 01160 setDoesNotCapture(F, 2); 01161 break; 01162 case LibFunc::bzero: 01163 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) 01164 return false; 01165 setDoesNotThrow(F); 01166 setDoesNotCapture(F, 1); 01167 break; 01168 case LibFunc::calloc: 01169 if (FTy->getNumParams() != 2 || 01170 !FTy->getReturnType()->isPointerTy()) 01171 return false; 01172 setDoesNotThrow(F); 01173 setDoesNotAlias(F, 0); 01174 break; 01175 case LibFunc::chmod: 01176 case LibFunc::chown: 01177 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 01178 return false; 01179 setDoesNotThrow(F); 01180 setDoesNotCapture(F, 1); 01181 setOnlyReadsMemory(F, 1); 01182 break; 01183 case LibFunc::ctermid: 01184 case LibFunc::clearerr: 01185 case LibFunc::closedir: 01186 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 01187 return false; 01188 setDoesNotThrow(F); 01189 setDoesNotCapture(F, 1); 01190 break; 01191 case LibFunc::atoi: 01192 case LibFunc::atol: 01193 case LibFunc::atof: 01194 case LibFunc::atoll: 01195 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01196 return false; 01197 setDoesNotThrow(F); 01198 setOnlyReadsMemory(F); 01199 setDoesNotCapture(F, 1); 01200 break; 01201 case LibFunc::access: 01202 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) 01203 return false; 01204 setDoesNotThrow(F); 01205 setDoesNotCapture(F, 1); 01206 setOnlyReadsMemory(F, 1); 01207 break; 01208 case LibFunc::fopen: 01209 if (FTy->getNumParams() != 2 || 01210 !FTy->getReturnType()->isPointerTy() || 01211 !FTy->getParamType(0)->isPointerTy() || 01212 !FTy->getParamType(1)->isPointerTy()) 01213 return false; 01214 setDoesNotThrow(F); 01215 setDoesNotAlias(F, 0); 01216 setDoesNotCapture(F, 1); 01217 setDoesNotCapture(F, 2); 01218 setOnlyReadsMemory(F, 1); 01219 setOnlyReadsMemory(F, 2); 01220 break; 01221 case LibFunc::fdopen: 01222 if (FTy->getNumParams() != 2 || 01223 !FTy->getReturnType()->isPointerTy() || 01224 !FTy->getParamType(1)->isPointerTy()) 01225 return false; 01226 setDoesNotThrow(F); 01227 setDoesNotAlias(F, 0); 01228 setDoesNotCapture(F, 2); 01229 setOnlyReadsMemory(F, 2); 01230 break; 01231 case LibFunc::feof: 01232 case LibFunc::free: 01233 case LibFunc::fseek: 01234 case LibFunc::ftell: 01235 case LibFunc::fgetc: 01236 case LibFunc::fseeko: 01237 case LibFunc::ftello: 01238 case LibFunc::fileno: 01239 case LibFunc::fflush: 01240 case LibFunc::fclose: 01241 case LibFunc::fsetpos: 01242 case LibFunc::flockfile: 01243 case LibFunc::funlockfile: 01244 case LibFunc::ftrylockfile: 01245 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 01246 return false; 01247 setDoesNotThrow(F); 01248 setDoesNotCapture(F, 1); 01249 break; 01250 case LibFunc::ferror: 01251 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01252 return false; 01253 setDoesNotThrow(F); 01254 setDoesNotCapture(F, 1); 01255 setOnlyReadsMemory(F); 01256 break; 01257 case LibFunc::fputc: 01258 case LibFunc::fstat: 01259 case LibFunc::frexp: 01260 case LibFunc::frexpf: 01261 case LibFunc::frexpl: 01262 case LibFunc::fstatvfs: 01263 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 01264 return false; 01265 setDoesNotThrow(F); 01266 setDoesNotCapture(F, 2); 01267 break; 01268 case LibFunc::fgets: 01269 if (FTy->getNumParams() != 3 || 01270 !FTy->getParamType(0)->isPointerTy() || 01271 !FTy->getParamType(2)->isPointerTy()) 01272 return false; 01273 setDoesNotThrow(F); 01274 setDoesNotCapture(F, 3); 01275 break; 01276 case LibFunc::fread: 01277 if (FTy->getNumParams() != 4 || 01278 !FTy->getParamType(0)->isPointerTy() || 01279 !FTy->getParamType(3)->isPointerTy()) 01280 return false; 01281 setDoesNotThrow(F); 01282 setDoesNotCapture(F, 1); 01283 setDoesNotCapture(F, 4); 01284 break; 01285 case LibFunc::fwrite: 01286 if (FTy->getNumParams() != 4 || 01287 !FTy->getParamType(0)->isPointerTy() || 01288 !FTy->getParamType(3)->isPointerTy()) 01289 return false; 01290 setDoesNotThrow(F); 01291 setDoesNotCapture(F, 1); 01292 setDoesNotCapture(F, 4); 01293 break; 01294 case LibFunc::fputs: 01295 if (FTy->getNumParams() < 2 || 01296 !FTy->getParamType(0)->isPointerTy() || 01297 !FTy->getParamType(1)->isPointerTy()) 01298 return false; 01299 setDoesNotThrow(F); 01300 setDoesNotCapture(F, 1); 01301 setDoesNotCapture(F, 2); 01302 setOnlyReadsMemory(F, 1); 01303 break; 01304 case LibFunc::fscanf: 01305 case LibFunc::fprintf: 01306 if (FTy->getNumParams() < 2 || 01307 !FTy->getParamType(0)->isPointerTy() || 01308 !FTy->getParamType(1)->isPointerTy()) 01309 return false; 01310 setDoesNotThrow(F); 01311 setDoesNotCapture(F, 1); 01312 setDoesNotCapture(F, 2); 01313 setOnlyReadsMemory(F, 2); 01314 break; 01315 case LibFunc::fgetpos: 01316 if (FTy->getNumParams() < 2 || 01317 !FTy->getParamType(0)->isPointerTy() || 01318 !FTy->getParamType(1)->isPointerTy()) 01319 return false; 01320 setDoesNotThrow(F); 01321 setDoesNotCapture(F, 1); 01322 setDoesNotCapture(F, 2); 01323 break; 01324 case LibFunc::getc: 01325 case LibFunc::getlogin_r: 01326 case LibFunc::getc_unlocked: 01327 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 01328 return false; 01329 setDoesNotThrow(F); 01330 setDoesNotCapture(F, 1); 01331 break; 01332 case LibFunc::getenv: 01333 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01334 return false; 01335 setDoesNotThrow(F); 01336 setOnlyReadsMemory(F); 01337 setDoesNotCapture(F, 1); 01338 break; 01339 case LibFunc::gets: 01340 case LibFunc::getchar: 01341 setDoesNotThrow(F); 01342 break; 01343 case LibFunc::getitimer: 01344 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 01345 return false; 01346 setDoesNotThrow(F); 01347 setDoesNotCapture(F, 2); 01348 break; 01349 case LibFunc::getpwnam: 01350 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01351 return false; 01352 setDoesNotThrow(F); 01353 setDoesNotCapture(F, 1); 01354 setOnlyReadsMemory(F, 1); 01355 break; 01356 case LibFunc::ungetc: 01357 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 01358 return false; 01359 setDoesNotThrow(F); 01360 setDoesNotCapture(F, 2); 01361 break; 01362 case LibFunc::uname: 01363 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01364 return false; 01365 setDoesNotThrow(F); 01366 setDoesNotCapture(F, 1); 01367 break; 01368 case LibFunc::unlink: 01369 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01370 return false; 01371 setDoesNotThrow(F); 01372 setDoesNotCapture(F, 1); 01373 setOnlyReadsMemory(F, 1); 01374 break; 01375 case LibFunc::unsetenv: 01376 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01377 return false; 01378 setDoesNotThrow(F); 01379 setDoesNotCapture(F, 1); 01380 setOnlyReadsMemory(F, 1); 01381 break; 01382 case LibFunc::utime: 01383 case LibFunc::utimes: 01384 if (FTy->getNumParams() != 2 || 01385 !FTy->getParamType(0)->isPointerTy() || 01386 !FTy->getParamType(1)->isPointerTy()) 01387 return false; 01388 setDoesNotThrow(F); 01389 setDoesNotCapture(F, 1); 01390 setDoesNotCapture(F, 2); 01391 setOnlyReadsMemory(F, 1); 01392 setOnlyReadsMemory(F, 2); 01393 break; 01394 case LibFunc::putc: 01395 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 01396 return false; 01397 setDoesNotThrow(F); 01398 setDoesNotCapture(F, 2); 01399 break; 01400 case LibFunc::puts: 01401 case LibFunc::printf: 01402 case LibFunc::perror: 01403 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01404 return false; 01405 setDoesNotThrow(F); 01406 setDoesNotCapture(F, 1); 01407 setOnlyReadsMemory(F, 1); 01408 break; 01409 case LibFunc::pread: 01410 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy()) 01411 return false; 01412 // May throw; "pread" is a valid pthread cancellation point. 01413 setDoesNotCapture(F, 2); 01414 break; 01415 case LibFunc::pwrite: 01416 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy()) 01417 return false; 01418 // May throw; "pwrite" is a valid pthread cancellation point. 01419 setDoesNotCapture(F, 2); 01420 setOnlyReadsMemory(F, 2); 01421 break; 01422 case LibFunc::putchar: 01423 setDoesNotThrow(F); 01424 break; 01425 case LibFunc::popen: 01426 if (FTy->getNumParams() != 2 || 01427 !FTy->getReturnType()->isPointerTy() || 01428 !FTy->getParamType(0)->isPointerTy() || 01429 !FTy->getParamType(1)->isPointerTy()) 01430 return false; 01431 setDoesNotThrow(F); 01432 setDoesNotAlias(F, 0); 01433 setDoesNotCapture(F, 1); 01434 setDoesNotCapture(F, 2); 01435 setOnlyReadsMemory(F, 1); 01436 setOnlyReadsMemory(F, 2); 01437 break; 01438 case LibFunc::pclose: 01439 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01440 return false; 01441 setDoesNotThrow(F); 01442 setDoesNotCapture(F, 1); 01443 break; 01444 case LibFunc::vscanf: 01445 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 01446 return false; 01447 setDoesNotThrow(F); 01448 setDoesNotCapture(F, 1); 01449 setOnlyReadsMemory(F, 1); 01450 break; 01451 case LibFunc::vsscanf: 01452 if (FTy->getNumParams() != 3 || 01453 !FTy->getParamType(1)->isPointerTy() || 01454 !FTy->getParamType(2)->isPointerTy()) 01455 return false; 01456 setDoesNotThrow(F); 01457 setDoesNotCapture(F, 1); 01458 setDoesNotCapture(F, 2); 01459 setOnlyReadsMemory(F, 1); 01460 setOnlyReadsMemory(F, 2); 01461 break; 01462 case LibFunc::vfscanf: 01463 if (FTy->getNumParams() != 3 || 01464 !FTy->getParamType(1)->isPointerTy() || 01465 !FTy->getParamType(2)->isPointerTy()) 01466 return false; 01467 setDoesNotThrow(F); 01468 setDoesNotCapture(F, 1); 01469 setDoesNotCapture(F, 2); 01470 setOnlyReadsMemory(F, 2); 01471 break; 01472 case LibFunc::valloc: 01473 if (!FTy->getReturnType()->isPointerTy()) 01474 return false; 01475 setDoesNotThrow(F); 01476 setDoesNotAlias(F, 0); 01477 break; 01478 case LibFunc::vprintf: 01479 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) 01480 return false; 01481 setDoesNotThrow(F); 01482 setDoesNotCapture(F, 1); 01483 setOnlyReadsMemory(F, 1); 01484 break; 01485 case LibFunc::vfprintf: 01486 case LibFunc::vsprintf: 01487 if (FTy->getNumParams() != 3 || 01488 !FTy->getParamType(0)->isPointerTy() || 01489 !FTy->getParamType(1)->isPointerTy()) 01490 return false; 01491 setDoesNotThrow(F); 01492 setDoesNotCapture(F, 1); 01493 setDoesNotCapture(F, 2); 01494 setOnlyReadsMemory(F, 2); 01495 break; 01496 case LibFunc::vsnprintf: 01497 if (FTy->getNumParams() != 4 || 01498 !FTy->getParamType(0)->isPointerTy() || 01499 !FTy->getParamType(2)->isPointerTy()) 01500 return false; 01501 setDoesNotThrow(F); 01502 setDoesNotCapture(F, 1); 01503 setDoesNotCapture(F, 3); 01504 setOnlyReadsMemory(F, 3); 01505 break; 01506 case LibFunc::open: 01507 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy()) 01508 return false; 01509 // May throw; "open" is a valid pthread cancellation point. 01510 setDoesNotCapture(F, 1); 01511 setOnlyReadsMemory(F, 1); 01512 break; 01513 case LibFunc::opendir: 01514 if (FTy->getNumParams() != 1 || 01515 !FTy->getReturnType()->isPointerTy() || 01516 !FTy->getParamType(0)->isPointerTy()) 01517 return false; 01518 setDoesNotThrow(F); 01519 setDoesNotAlias(F, 0); 01520 setDoesNotCapture(F, 1); 01521 setOnlyReadsMemory(F, 1); 01522 break; 01523 case LibFunc::tmpfile: 01524 if (!FTy->getReturnType()->isPointerTy()) 01525 return false; 01526 setDoesNotThrow(F); 01527 setDoesNotAlias(F, 0); 01528 break; 01529 case LibFunc::times: 01530 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01531 return false; 01532 setDoesNotThrow(F); 01533 setDoesNotCapture(F, 1); 01534 break; 01535 case LibFunc::htonl: 01536 case LibFunc::htons: 01537 case LibFunc::ntohl: 01538 case LibFunc::ntohs: 01539 setDoesNotThrow(F); 01540 setDoesNotAccessMemory(F); 01541 break; 01542 case LibFunc::lstat: 01543 if (FTy->getNumParams() != 2 || 01544 !FTy->getParamType(0)->isPointerTy() || 01545 !FTy->getParamType(1)->isPointerTy()) 01546 return false; 01547 setDoesNotThrow(F); 01548 setDoesNotCapture(F, 1); 01549 setDoesNotCapture(F, 2); 01550 setOnlyReadsMemory(F, 1); 01551 break; 01552 case LibFunc::lchown: 01553 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy()) 01554 return false; 01555 setDoesNotThrow(F); 01556 setDoesNotCapture(F, 1); 01557 setOnlyReadsMemory(F, 1); 01558 break; 01559 case LibFunc::qsort: 01560 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy()) 01561 return false; 01562 // May throw; places call through function pointer. 01563 setDoesNotCapture(F, 4); 01564 break; 01565 case LibFunc::dunder_strdup: 01566 case LibFunc::dunder_strndup: 01567 if (FTy->getNumParams() < 1 || 01568 !FTy->getReturnType()->isPointerTy() || 01569 !FTy->getParamType(0)->isPointerTy()) 01570 return false; 01571 setDoesNotThrow(F); 01572 setDoesNotAlias(F, 0); 01573 setDoesNotCapture(F, 1); 01574 setOnlyReadsMemory(F, 1); 01575 break; 01576 case LibFunc::dunder_strtok_r: 01577 if (FTy->getNumParams() != 3 || 01578 !FTy->getParamType(1)->isPointerTy()) 01579 return false; 01580 setDoesNotThrow(F); 01581 setDoesNotCapture(F, 2); 01582 setOnlyReadsMemory(F, 2); 01583 break; 01584 case LibFunc::under_IO_getc: 01585 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 01586 return false; 01587 setDoesNotThrow(F); 01588 setDoesNotCapture(F, 1); 01589 break; 01590 case LibFunc::under_IO_putc: 01591 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 01592 return false; 01593 setDoesNotThrow(F); 01594 setDoesNotCapture(F, 2); 01595 break; 01596 case LibFunc::dunder_isoc99_scanf: 01597 if (FTy->getNumParams() < 1 || 01598 !FTy->getParamType(0)->isPointerTy()) 01599 return false; 01600 setDoesNotThrow(F); 01601 setDoesNotCapture(F, 1); 01602 setOnlyReadsMemory(F, 1); 01603 break; 01604 case LibFunc::stat64: 01605 case LibFunc::lstat64: 01606 case LibFunc::statvfs64: 01607 if (FTy->getNumParams() < 1 || 01608 !FTy->getParamType(0)->isPointerTy() || 01609 !FTy->getParamType(1)->isPointerTy()) 01610 return false; 01611 setDoesNotThrow(F); 01612 setDoesNotCapture(F, 1); 01613 setDoesNotCapture(F, 2); 01614 setOnlyReadsMemory(F, 1); 01615 break; 01616 case LibFunc::dunder_isoc99_sscanf: 01617 if (FTy->getNumParams() < 1 || 01618 !FTy->getParamType(0)->isPointerTy() || 01619 !FTy->getParamType(1)->isPointerTy()) 01620 return false; 01621 setDoesNotThrow(F); 01622 setDoesNotCapture(F, 1); 01623 setDoesNotCapture(F, 2); 01624 setOnlyReadsMemory(F, 1); 01625 setOnlyReadsMemory(F, 2); 01626 break; 01627 case LibFunc::fopen64: 01628 if (FTy->getNumParams() != 2 || 01629 !FTy->getReturnType()->isPointerTy() || 01630 !FTy->getParamType(0)->isPointerTy() || 01631 !FTy->getParamType(1)->isPointerTy()) 01632 return false; 01633 setDoesNotThrow(F); 01634 setDoesNotAlias(F, 0); 01635 setDoesNotCapture(F, 1); 01636 setDoesNotCapture(F, 2); 01637 setOnlyReadsMemory(F, 1); 01638 setOnlyReadsMemory(F, 2); 01639 break; 01640 case LibFunc::fseeko64: 01641 case LibFunc::ftello64: 01642 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 01643 return false; 01644 setDoesNotThrow(F); 01645 setDoesNotCapture(F, 1); 01646 break; 01647 case LibFunc::tmpfile64: 01648 if (!FTy->getReturnType()->isPointerTy()) 01649 return false; 01650 setDoesNotThrow(F); 01651 setDoesNotAlias(F, 0); 01652 break; 01653 case LibFunc::fstat64: 01654 case LibFunc::fstatvfs64: 01655 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 01656 return false; 01657 setDoesNotThrow(F); 01658 setDoesNotCapture(F, 2); 01659 break; 01660 case LibFunc::open64: 01661 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy()) 01662 return false; 01663 // May throw; "open" is a valid pthread cancellation point. 01664 setDoesNotCapture(F, 1); 01665 setOnlyReadsMemory(F, 1); 01666 break; 01667 case LibFunc::gettimeofday: 01668 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || 01669 !FTy->getParamType(1)->isPointerTy()) 01670 return false; 01671 // Currently some platforms have the restrict keyword on the arguments to 01672 // gettimeofday. To be conservative, do not add noalias to gettimeofday's 01673 // arguments. 01674 setDoesNotThrow(F); 01675 setDoesNotCapture(F, 1); 01676 setDoesNotCapture(F, 2); 01677 break; 01678 default: 01679 // Didn't mark any attributes. 01680 return false; 01681 } 01682 01683 return true; 01684 } 01685 01686 /// annotateLibraryCalls - Adds attributes to well-known standard library 01687 /// call declarations. 01688 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) { 01689 bool MadeChange = false; 01690 01691 // Check each function in turn annotating well-known library function 01692 // declarations with attributes. 01693 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 01694 Function *F = (*I)->getFunction(); 01695 01696 if (F && F->isDeclaration()) 01697 MadeChange |= inferPrototypeAttributes(*F); 01698 } 01699 01700 return MadeChange; 01701 } 01702 01703 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { 01704 AA = &getAnalysis<AliasAnalysis>(); 01705 TLI = &getAnalysis<TargetLibraryInfo>(); 01706 01707 bool Changed = annotateLibraryCalls(SCC); 01708 Changed |= AddReadAttrs(SCC); 01709 Changed |= AddArgumentAttrs(SCC); 01710 Changed |= AddNoAliasAttrs(SCC); 01711 return Changed; 01712 }