1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements a simple interprocedural pass which walks the 11 // call-graph, looking for functions which do not access or only read 12 // non-local memory, and marking them readnone/readonly. It does the 13 // same with function arguments independently, marking them readonly/ 14 // readnone/nocapture. Finally, well-known library call declarations 15 // are marked with all attributes that are consistent with the 16 // function's standard definition. This pass is implemented as a 17 // bottom-up traversal of the call-graph. 18 // 19 //===----------------------------------------------------------------------===// 20 21 #include "llvm/Transforms/IPO.h" 22 #include "llvm/ADT/SCCIterator.h" 23 #include "llvm/ADT/SetVector.h" 24 #include "llvm/ADT/SmallSet.h" 25 #include "llvm/ADT/Statistic.h" 26 #include "llvm/Analysis/AliasAnalysis.h" 27 #include "llvm/Analysis/AssumptionCache.h" 28 #include "llvm/Analysis/BasicAliasAnalysis.h" 29 #include "llvm/Analysis/CallGraph.h" 30 #include "llvm/Analysis/CallGraphSCCPass.h" 31 #include "llvm/Analysis/CaptureTracking.h" 32 #include "llvm/Analysis/TargetLibraryInfo.h" 33 #include "llvm/Analysis/ValueTracking.h" 34 #include "llvm/IR/GlobalVariable.h" 35 #include "llvm/IR/InstIterator.h" 36 #include "llvm/IR/IntrinsicInst.h" 37 #include "llvm/IR/LLVMContext.h" 38 #include "llvm/Support/Debug.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include "llvm/Analysis/TargetLibraryInfo.h" 41 using namespace llvm; 42 43 #define DEBUG_TYPE "functionattrs" 44 45 STATISTIC(NumReadNone, "Number of functions marked readnone"); 46 STATISTIC(NumReadOnly, "Number of functions marked readonly"); 47 STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); 48 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone"); 49 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly"); 50 STATISTIC(NumNoAlias, "Number of function returns marked noalias"); 51 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull"); 52 STATISTIC(NumAnnotated, "Number of attributes added to library functions"); 53 54 namespace { 55 struct FunctionAttrs : public CallGraphSCCPass { 56 static char ID; // Pass identification, replacement for typeid 57 FunctionAttrs() : CallGraphSCCPass(ID) { 58 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); 59 } 60 61 bool runOnSCC(CallGraphSCC &SCC) override; 62 63 void getAnalysisUsage(AnalysisUsage &AU) const override { 64 AU.setPreservesCFG(); 65 AU.addRequired<AssumptionCacheTracker>(); 66 AU.addRequired<TargetLibraryInfoWrapperPass>(); 67 CallGraphSCCPass::getAnalysisUsage(AU); 68 } 69 70 private: 71 TargetLibraryInfo *TLI; 72 73 bool AddReadAttrs(const CallGraphSCC &SCC); 74 bool AddArgumentAttrs(const CallGraphSCC &SCC); 75 bool AddNoAliasAttrs(const CallGraphSCC &SCC); 76 bool AddNonNullAttrs(const CallGraphSCC &SCC); 77 bool annotateLibraryCalls(const CallGraphSCC &SCC); 78 }; 79 } 80 81 char FunctionAttrs::ID = 0; 82 INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", 83 "Deduce function attributes", false, false) 84 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 85 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 86 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 87 INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", 88 "Deduce function attributes", false, false) 89 90 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } 91 92 namespace { 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 134 // If the call doesn't access memory, we're done. 135 if (!(MRB & MRI_ModRef)) 136 continue; 137 138 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) { 139 // The call could access any memory. If that includes writes, give up. 140 if (MRB & MRI_Mod) 141 return MAK_MayWrite; 142 // If it reads, note it. 143 if (MRB & MRI_Ref) 144 ReadsMemory = true; 145 continue; 146 } 147 148 // Check whether all pointer arguments point to local memory, and 149 // ignore calls that only access local memory. 150 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); 151 CI != CE; ++CI) { 152 Value *Arg = *CI; 153 if (!Arg->getType()->isPointerTy()) 154 continue; 155 156 AAMDNodes AAInfo; 157 I->getAAMetadata(AAInfo); 158 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo); 159 160 // Skip accesses to local or constant memory as they don't impact the 161 // externally visible mod/ref behavior. 162 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 163 continue; 164 165 if (MRB & MRI_Mod) 166 // Writes non-local memory. Give up. 167 return MAK_MayWrite; 168 if (MRB & MRI_Ref) 169 // Ok, it reads non-local memory. 170 ReadsMemory = true; 171 } 172 continue; 173 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 174 // Ignore non-volatile loads from local memory. (Atomic is okay here.) 175 if (!LI->isVolatile()) { 176 MemoryLocation Loc = MemoryLocation::get(LI); 177 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 178 continue; 179 } 180 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 181 // Ignore non-volatile stores to local memory. (Atomic is okay here.) 182 if (!SI->isVolatile()) { 183 MemoryLocation Loc = MemoryLocation::get(SI); 184 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 185 continue; 186 } 187 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { 188 // Ignore vaargs on local memory. 189 MemoryLocation Loc = MemoryLocation::get(VI); 190 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) 191 continue; 192 } 193 194 // Any remaining instructions need to be taken seriously! Check if they 195 // read or write memory. 196 if (I->mayWriteToMemory()) 197 // Writes memory. Just give up. 198 return MAK_MayWrite; 199 200 // If this instruction may read memory, remember that. 201 ReadsMemory |= I->mayReadFromMemory(); 202 } 203 204 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone; 205 } 206 207 /// Deduce readonly/readnone attributes for the SCC. 208 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { 209 SmallPtrSet<Function *, 8> SCCNodes; 210 211 // Fill SCCNodes with the elements of the SCC. Used for quickly 212 // looking up whether a given CallGraphNode is in this SCC. 213 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 214 SCCNodes.insert((*I)->getFunction()); 215 216 // Check if any of the functions in the SCC read or write memory. If they 217 // write memory then they can't be marked readnone or readonly. 218 bool ReadsMemory = false; 219 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 220 Function *F = (*I)->getFunction(); 221 222 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) 223 // External node or node we don't want to optimize - assume it may write 224 // memory and give up. 225 return false; 226 227 // We need to manually construct BasicAA directly in order to disable its 228 // use of other function analyses. 229 BasicAAResult BAR(createLegacyPMBasicAAResult(*this, *F)); 230 231 // Construct our own AA results for this function. We do this manually to 232 // work around the limitations of the legacy pass manager. 233 AAResults AAR(createLegacyPMAAResults(*this, *F, BAR)); 234 235 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) { 236 case MAK_MayWrite: 237 return false; 238 case MAK_ReadOnly: 239 ReadsMemory = true; 240 break; 241 case MAK_ReadNone: 242 // Nothing to do! 243 break; 244 } 245 } 246 247 // Success! Functions in this SCC do not access memory, or only read memory. 248 // Give them the appropriate attribute. 249 bool MadeChange = false; 250 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 251 Function *F = (*I)->getFunction(); 252 253 if (F->doesNotAccessMemory()) 254 // Already perfect! 255 continue; 256 257 if (F->onlyReadsMemory() && ReadsMemory) 258 // No change. 259 continue; 260 261 MadeChange = true; 262 263 // Clear out any existing attributes. 264 AttrBuilder B; 265 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); 266 F->removeAttributes( 267 AttributeSet::FunctionIndex, 268 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B)); 269 270 // Add in the new attribute. 271 F->addAttribute(AttributeSet::FunctionIndex, 272 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone); 273 274 if (ReadsMemory) 275 ++NumReadOnly; 276 else 277 ++NumReadNone; 278 } 279 280 return MadeChange; 281 } 282 283 namespace { 284 /// For a given pointer Argument, this retains a list of Arguments of functions 285 /// in the same SCC that the pointer data flows into. We use this to build an 286 /// SCC of the arguments. 287 struct ArgumentGraphNode { 288 Argument *Definition; 289 SmallVector<ArgumentGraphNode *, 4> Uses; 290 }; 291 292 class ArgumentGraph { 293 // We store pointers to ArgumentGraphNode objects, so it's important that 294 // that they not move around upon insert. 295 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy; 296 297 ArgumentMapTy ArgumentMap; 298 299 // There is no root node for the argument graph, in fact: 300 // void f(int *x, int *y) { if (...) f(x, y); } 301 // is an example where the graph is disconnected. The SCCIterator requires a 302 // single entry point, so we maintain a fake ("synthetic") root node that 303 // uses every node. Because the graph is directed and nothing points into 304 // the root, it will not participate in any SCCs (except for its own). 305 ArgumentGraphNode SyntheticRoot; 306 307 public: 308 ArgumentGraph() { SyntheticRoot.Definition = nullptr; } 309 310 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator; 311 312 iterator begin() { return SyntheticRoot.Uses.begin(); } 313 iterator end() { return SyntheticRoot.Uses.end(); } 314 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } 315 316 ArgumentGraphNode *operator[](Argument *A) { 317 ArgumentGraphNode &Node = ArgumentMap[A]; 318 Node.Definition = A; 319 SyntheticRoot.Uses.push_back(&Node); 320 return &Node; 321 } 322 }; 323 324 /// This tracker checks whether callees are in the SCC, and if so it does not 325 /// consider that a capture, instead adding it to the "Uses" list and 326 /// continuing with the analysis. 327 struct ArgumentUsesTracker : public CaptureTracker { 328 ArgumentUsesTracker(const SmallPtrSet<Function *, 8> &SCCNodes) 329 : Captured(false), SCCNodes(SCCNodes) {} 330 331 void tooManyUses() override { Captured = true; } 332 333 bool captured(const Use *U) override { 334 CallSite CS(U->getUser()); 335 if (!CS.getInstruction()) { 336 Captured = true; 337 return true; 338 } 339 340 Function *F = CS.getCalledFunction(); 341 if (!F || !SCCNodes.count(F)) { 342 Captured = true; 343 return true; 344 } 345 346 bool Found = false; 347 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 348 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end(); 349 PI != PE; ++PI, ++AI) { 350 if (AI == AE) { 351 assert(F->isVarArg() && "More params than args in non-varargs call"); 352 Captured = true; 353 return true; 354 } 355 if (PI == U) { 356 Uses.push_back(&*AI); 357 Found = true; 358 break; 359 } 360 } 361 assert(Found && "Capturing call-site captured nothing?"); 362 (void)Found; 363 return false; 364 } 365 366 bool Captured; // True only if certainly captured (used outside our SCC). 367 SmallVector<Argument *, 4> Uses; // Uses within our SCC. 368 369 const SmallPtrSet<Function *, 8> &SCCNodes; 370 }; 371 } 372 373 namespace llvm { 374 template <> struct GraphTraits<ArgumentGraphNode *> { 375 typedef ArgumentGraphNode NodeType; 376 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType; 377 378 static inline NodeType *getEntryNode(NodeType *A) { return A; } 379 static inline ChildIteratorType child_begin(NodeType *N) { 380 return N->Uses.begin(); 381 } 382 static inline ChildIteratorType child_end(NodeType *N) { 383 return N->Uses.end(); 384 } 385 }; 386 template <> 387 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> { 388 static NodeType *getEntryNode(ArgumentGraph *AG) { 389 return AG->getEntryNode(); 390 } 391 static ChildIteratorType nodes_begin(ArgumentGraph *AG) { 392 return AG->begin(); 393 } 394 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); } 395 }; 396 } 397 398 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone. 399 static Attribute::AttrKind 400 determinePointerReadAttrs(Argument *A, 401 const SmallPtrSet<Argument *, 8> &SCCNodes) { 402 403 SmallVector<Use *, 32> Worklist; 404 SmallSet<Use *, 32> Visited; 405 406 // inalloca arguments are always clobbered by the call. 407 if (A->hasInAllocaAttr()) 408 return Attribute::None; 409 410 bool IsRead = false; 411 // We don't need to track IsWritten. If A is written to, return immediately. 412 413 for (Use &U : A->uses()) { 414 Visited.insert(&U); 415 Worklist.push_back(&U); 416 } 417 418 while (!Worklist.empty()) { 419 Use *U = Worklist.pop_back_val(); 420 Instruction *I = cast<Instruction>(U->getUser()); 421 Value *V = U->get(); 422 423 switch (I->getOpcode()) { 424 case Instruction::BitCast: 425 case Instruction::GetElementPtr: 426 case Instruction::PHI: 427 case Instruction::Select: 428 case Instruction::AddrSpaceCast: 429 // The original value is not read/written via this if the new value isn't. 430 for (Use &UU : I->uses()) 431 if (Visited.insert(&UU).second) 432 Worklist.push_back(&UU); 433 break; 434 435 case Instruction::Call: 436 case Instruction::Invoke: { 437 bool Captures = true; 438 439 if (I->getType()->isVoidTy()) 440 Captures = false; 441 442 auto AddUsersToWorklistIfCapturing = [&] { 443 if (Captures) 444 for (Use &UU : I->uses()) 445 if (Visited.insert(&UU).second) 446 Worklist.push_back(&UU); 447 }; 448 449 CallSite CS(I); 450 if (CS.doesNotAccessMemory()) { 451 AddUsersToWorklistIfCapturing(); 452 continue; 453 } 454 455 Function *F = CS.getCalledFunction(); 456 if (!F) { 457 if (CS.onlyReadsMemory()) { 458 IsRead = true; 459 AddUsersToWorklistIfCapturing(); 460 continue; 461 } 462 return Attribute::None; 463 } 464 465 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); 466 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); 467 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) { 468 if (A->get() == V) { 469 if (AI == AE) { 470 assert(F->isVarArg() && 471 "More params than args in non-varargs call."); 472 return Attribute::None; 473 } 474 Captures &= !CS.doesNotCapture(A - B); 475 if (SCCNodes.count(&*AI)) 476 continue; 477 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B)) 478 return Attribute::None; 479 if (!CS.doesNotAccessMemory(A - B)) 480 IsRead = true; 481 } 482 } 483 AddUsersToWorklistIfCapturing(); 484 break; 485 } 486 487 case Instruction::Load: 488 IsRead = true; 489 break; 490 491 case Instruction::ICmp: 492 case Instruction::Ret: 493 break; 494 495 default: 496 return Attribute::None; 497 } 498 } 499 500 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone; 501 } 502 503 /// Deduce nocapture attributes for the SCC. 504 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) { 505 bool Changed = false; 506 507 SmallPtrSet<Function *, 8> SCCNodes; 508 509 // Fill SCCNodes with the elements of the SCC. Used for quickly 510 // looking up whether a given CallGraphNode is in this SCC. 511 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 512 Function *F = (*I)->getFunction(); 513 if (F && !F->isDeclaration() && !F->mayBeOverridden() && 514 !F->hasFnAttribute(Attribute::OptimizeNone)) 515 SCCNodes.insert(F); 516 } 517 518 ArgumentGraph AG; 519 520 AttrBuilder B; 521 B.addAttribute(Attribute::NoCapture); 522 523 // Check each function in turn, determining which pointer arguments are not 524 // captured. 525 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 526 Function *F = (*I)->getFunction(); 527 528 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) 529 // External node or function we're trying not to optimize - only a problem 530 // for arguments that we pass to it. 531 continue; 532 533 // Definitions with weak linkage may be overridden at linktime with 534 // something that captures pointers, so treat them like declarations. 535 if (F->isDeclaration() || F->mayBeOverridden()) 536 continue; 537 538 // Functions that are readonly (or readnone) and nounwind and don't return 539 // a value can't capture arguments. Don't analyze them. 540 if (F->onlyReadsMemory() && F->doesNotThrow() && 541 F->getReturnType()->isVoidTy()) { 542 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; 543 ++A) { 544 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { 545 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); 546 ++NumNoCapture; 547 Changed = true; 548 } 549 } 550 continue; 551 } 552 553 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E; 554 ++A) { 555 if (!A->getType()->isPointerTy()) 556 continue; 557 bool HasNonLocalUses = false; 558 if (!A->hasNoCaptureAttr()) { 559 ArgumentUsesTracker Tracker(SCCNodes); 560 PointerMayBeCaptured(&*A, &Tracker); 561 if (!Tracker.Captured) { 562 if (Tracker.Uses.empty()) { 563 // If it's trivially not captured, mark it nocapture now. 564 A->addAttr( 565 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B)); 566 ++NumNoCapture; 567 Changed = true; 568 } else { 569 // If it's not trivially captured and not trivially not captured, 570 // then it must be calling into another function in our SCC. Save 571 // its particulars for Argument-SCC analysis later. 572 ArgumentGraphNode *Node = AG[&*A]; 573 for (SmallVectorImpl<Argument *>::iterator 574 UI = Tracker.Uses.begin(), 575 UE = Tracker.Uses.end(); 576 UI != UE; ++UI) { 577 Node->Uses.push_back(AG[*UI]); 578 if (*UI != A) 579 HasNonLocalUses = true; 580 } 581 } 582 } 583 // Otherwise, it's captured. Don't bother doing SCC analysis on it. 584 } 585 if (!HasNonLocalUses && !A->onlyReadsMemory()) { 586 // Can we determine that it's readonly/readnone without doing an SCC? 587 // Note that we don't allow any calls at all here, or else our result 588 // will be dependent on the iteration order through the functions in the 589 // SCC. 590 SmallPtrSet<Argument *, 8> Self; 591 Self.insert(&*A); 592 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self); 593 if (R != Attribute::None) { 594 AttrBuilder B; 595 B.addAttribute(R); 596 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 597 Changed = true; 598 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 599 } 600 } 601 } 602 } 603 604 // The graph we've collected is partial because we stopped scanning for 605 // argument uses once we solved the argument trivially. These partial nodes 606 // show up as ArgumentGraphNode objects with an empty Uses list, and for 607 // these nodes the final decision about whether they capture has already been 608 // made. If the definition doesn't have a 'nocapture' attribute by now, it 609 // captures. 610 611 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) { 612 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I; 613 if (ArgumentSCC.size() == 1) { 614 if (!ArgumentSCC[0]->Definition) 615 continue; // synthetic root node 616 617 // eg. "void f(int* x) { if (...) f(x); }" 618 if (ArgumentSCC[0]->Uses.size() == 1 && 619 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { 620 Argument *A = ArgumentSCC[0]->Definition; 621 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 622 ++NumNoCapture; 623 Changed = true; 624 } 625 continue; 626 } 627 628 bool SCCCaptured = false; 629 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); 630 I != E && !SCCCaptured; ++I) { 631 ArgumentGraphNode *Node = *I; 632 if (Node->Uses.empty()) { 633 if (!Node->Definition->hasNoCaptureAttr()) 634 SCCCaptured = true; 635 } 636 } 637 if (SCCCaptured) 638 continue; 639 640 SmallPtrSet<Argument *, 8> ArgumentSCCNodes; 641 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for 642 // quickly looking up whether a given Argument is in this ArgumentSCC. 643 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) { 644 ArgumentSCCNodes.insert((*I)->Definition); 645 } 646 647 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); 648 I != E && !SCCCaptured; ++I) { 649 ArgumentGraphNode *N = *I; 650 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(), 651 UE = N->Uses.end(); 652 UI != UE; ++UI) { 653 Argument *A = (*UI)->Definition; 654 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) 655 continue; 656 SCCCaptured = true; 657 break; 658 } 659 } 660 if (SCCCaptured) 661 continue; 662 663 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 664 Argument *A = ArgumentSCC[i]->Definition; 665 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 666 ++NumNoCapture; 667 Changed = true; 668 } 669 670 // We also want to compute readonly/readnone. With a small number of false 671 // negatives, we can assume that any pointer which is captured isn't going 672 // to be provably readonly or readnone, since by definition we can't 673 // analyze all uses of a captured pointer. 674 // 675 // The false negatives happen when the pointer is captured by a function 676 // that promises readonly/readnone behaviour on the pointer, then the 677 // pointer's lifetime ends before anything that writes to arbitrary memory. 678 // Also, a readonly/readnone pointer may be returned, but returning a 679 // pointer is capturing it. 680 681 Attribute::AttrKind ReadAttr = Attribute::ReadNone; 682 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 683 Argument *A = ArgumentSCC[i]->Definition; 684 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes); 685 if (K == Attribute::ReadNone) 686 continue; 687 if (K == Attribute::ReadOnly) { 688 ReadAttr = Attribute::ReadOnly; 689 continue; 690 } 691 ReadAttr = K; 692 break; 693 } 694 695 if (ReadAttr != Attribute::None) { 696 AttrBuilder B, R; 697 B.addAttribute(ReadAttr); 698 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone); 699 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { 700 Argument *A = ArgumentSCC[i]->Definition; 701 // Clear out existing readonly/readnone attributes 702 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R)); 703 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 704 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg; 705 Changed = true; 706 } 707 } 708 } 709 710 return Changed; 711 } 712 713 /// Tests whether a function is "malloc-like". 714 /// 715 /// A function is "malloc-like" if it returns either null or a pointer that 716 /// doesn't alias any other pointer visible to the caller. 717 static bool isFunctionMallocLike(Function *F, 718 SmallPtrSet<Function *, 8> &SCCNodes) { 719 SmallSetVector<Value *, 8> FlowsToReturn; 720 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) 721 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) 722 FlowsToReturn.insert(Ret->getReturnValue()); 723 724 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { 725 Value *RetVal = FlowsToReturn[i]; 726 727 if (Constant *C = dyn_cast<Constant>(RetVal)) { 728 if (!C->isNullValue() && !isa<UndefValue>(C)) 729 return false; 730 731 continue; 732 } 733 734 if (isa<Argument>(RetVal)) 735 return false; 736 737 if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) 738 switch (RVI->getOpcode()) { 739 // Extend the analysis by looking upwards. 740 case Instruction::BitCast: 741 case Instruction::GetElementPtr: 742 case Instruction::AddrSpaceCast: 743 FlowsToReturn.insert(RVI->getOperand(0)); 744 continue; 745 case Instruction::Select: { 746 SelectInst *SI = cast<SelectInst>(RVI); 747 FlowsToReturn.insert(SI->getTrueValue()); 748 FlowsToReturn.insert(SI->getFalseValue()); 749 continue; 750 } 751 case Instruction::PHI: { 752 PHINode *PN = cast<PHINode>(RVI); 753 for (Value *IncValue : PN->incoming_values()) 754 FlowsToReturn.insert(IncValue); 755 continue; 756 } 757 758 // Check whether the pointer came from an allocation. 759 case Instruction::Alloca: 760 break; 761 case Instruction::Call: 762 case Instruction::Invoke: { 763 CallSite CS(RVI); 764 if (CS.paramHasAttr(0, Attribute::NoAlias)) 765 break; 766 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) 767 break; 768 } // fall-through 769 default: 770 return false; // Did not come from an allocation. 771 } 772 773 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) 774 return false; 775 } 776 777 return true; 778 } 779 780 /// Deduce noalias attributes for the SCC. 781 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { 782 SmallPtrSet<Function *, 8> SCCNodes; 783 784 // Fill SCCNodes with the elements of the SCC. Used for quickly 785 // looking up whether a given CallGraphNode is in this SCC. 786 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 787 SCCNodes.insert((*I)->getFunction()); 788 789 // Check each function in turn, determining which functions return noalias 790 // pointers. 791 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 792 Function *F = (*I)->getFunction(); 793 794 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) 795 // External node or node we don't want to optimize - skip it; 796 return false; 797 798 // Already noalias. 799 if (F->doesNotAlias(0)) 800 continue; 801 802 // Definitions with weak linkage may be overridden at linktime, so 803 // treat them like declarations. 804 if (F->isDeclaration() || F->mayBeOverridden()) 805 return false; 806 807 // We annotate noalias return values, which are only applicable to 808 // pointer types. 809 if (!F->getReturnType()->isPointerTy()) 810 continue; 811 812 if (!isFunctionMallocLike(F, SCCNodes)) 813 return false; 814 } 815 816 bool MadeChange = false; 817 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 818 Function *F = (*I)->getFunction(); 819 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) 820 continue; 821 822 F->setDoesNotAlias(0); 823 ++NumNoAlias; 824 MadeChange = true; 825 } 826 827 return MadeChange; 828 } 829 830 /// Tests whether this function is known to not return null. 831 /// 832 /// Requires that the function returns a pointer. 833 /// 834 /// Returns true if it believes the function will not return a null, and sets 835 /// \p Speculative based on whether the returned conclusion is a speculative 836 /// conclusion due to SCC calls. 837 static bool isReturnNonNull(Function *F, SmallPtrSet<Function *, 8> &SCCNodes, 838 const TargetLibraryInfo &TLI, bool &Speculative) { 839 assert(F->getReturnType()->isPointerTy() && 840 "nonnull only meaningful on pointer types"); 841 Speculative = false; 842 843 SmallSetVector<Value *, 8> FlowsToReturn; 844 for (BasicBlock &BB : *F) 845 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) 846 FlowsToReturn.insert(Ret->getReturnValue()); 847 848 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { 849 Value *RetVal = FlowsToReturn[i]; 850 851 // If this value is locally known to be non-null, we're good 852 if (isKnownNonNull(RetVal, &TLI)) 853 continue; 854 855 // Otherwise, we need to look upwards since we can't make any local 856 // conclusions. 857 Instruction *RVI = dyn_cast<Instruction>(RetVal); 858 if (!RVI) 859 return false; 860 switch (RVI->getOpcode()) { 861 // Extend the analysis by looking upwards. 862 case Instruction::BitCast: 863 case Instruction::GetElementPtr: 864 case Instruction::AddrSpaceCast: 865 FlowsToReturn.insert(RVI->getOperand(0)); 866 continue; 867 case Instruction::Select: { 868 SelectInst *SI = cast<SelectInst>(RVI); 869 FlowsToReturn.insert(SI->getTrueValue()); 870 FlowsToReturn.insert(SI->getFalseValue()); 871 continue; 872 } 873 case Instruction::PHI: { 874 PHINode *PN = cast<PHINode>(RVI); 875 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 876 FlowsToReturn.insert(PN->getIncomingValue(i)); 877 continue; 878 } 879 case Instruction::Call: 880 case Instruction::Invoke: { 881 CallSite CS(RVI); 882 Function *Callee = CS.getCalledFunction(); 883 // A call to a node within the SCC is assumed to return null until 884 // proven otherwise 885 if (Callee && SCCNodes.count(Callee)) { 886 Speculative = true; 887 continue; 888 } 889 return false; 890 } 891 default: 892 return false; // Unknown source, may be null 893 }; 894 llvm_unreachable("should have either continued or returned"); 895 } 896 897 return true; 898 } 899 900 /// Deduce nonnull attributes for the SCC. 901 bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) { 902 SmallPtrSet<Function *, 8> SCCNodes; 903 904 // Fill SCCNodes with the elements of the SCC. Used for quickly 905 // looking up whether a given CallGraphNode is in this SCC. 906 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) 907 SCCNodes.insert((*I)->getFunction()); 908 909 // Speculative that all functions in the SCC return only nonnull 910 // pointers. We may refute this as we analyze functions. 911 bool SCCReturnsNonNull = true; 912 913 bool MadeChange = false; 914 915 // Check each function in turn, determining which functions return nonnull 916 // pointers. 917 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 918 Function *F = (*I)->getFunction(); 919 920 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) 921 // External node or node we don't want to optimize - skip it; 922 return false; 923 924 // Already nonnull. 925 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, 926 Attribute::NonNull)) 927 continue; 928 929 // Definitions with weak linkage may be overridden at linktime, so 930 // treat them like declarations. 931 if (F->isDeclaration() || F->mayBeOverridden()) 932 return false; 933 934 // We annotate nonnull return values, which are only applicable to 935 // pointer types. 936 if (!F->getReturnType()->isPointerTy()) 937 continue; 938 939 bool Speculative = false; 940 if (isReturnNonNull(F, SCCNodes, *TLI, Speculative)) { 941 if (!Speculative) { 942 // Mark the function eagerly since we may discover a function 943 // which prevents us from speculating about the entire SCC 944 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n"); 945 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); 946 ++NumNonNullReturn; 947 MadeChange = true; 948 } 949 continue; 950 } 951 // At least one function returns something which could be null, can't 952 // speculate any more. 953 SCCReturnsNonNull = false; 954 } 955 956 if (SCCReturnsNonNull) { 957 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 958 Function *F = (*I)->getFunction(); 959 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex, 960 Attribute::NonNull) || 961 !F->getReturnType()->isPointerTy()) 962 continue; 963 964 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n"); 965 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull); 966 ++NumNonNullReturn; 967 MadeChange = true; 968 } 969 } 970 971 return MadeChange; 972 } 973 974 static void setDoesNotAccessMemory(Function &F) { 975 if (!F.doesNotAccessMemory()) { 976 F.setDoesNotAccessMemory(); 977 ++NumAnnotated; 978 } 979 } 980 981 static void setOnlyReadsMemory(Function &F) { 982 if (!F.onlyReadsMemory()) { 983 F.setOnlyReadsMemory(); 984 ++NumAnnotated; 985 } 986 } 987 988 static void setDoesNotThrow(Function &F) { 989 if (!F.doesNotThrow()) { 990 F.setDoesNotThrow(); 991 ++NumAnnotated; 992 } 993 } 994 995 static void setDoesNotCapture(Function &F, unsigned n) { 996 if (!F.doesNotCapture(n)) { 997 F.setDoesNotCapture(n); 998 ++NumAnnotated; 999 } 1000 } 1001 1002 static void setOnlyReadsMemory(Function &F, unsigned n) { 1003 if (!F.onlyReadsMemory(n)) { 1004 F.setOnlyReadsMemory(n); 1005 ++NumAnnotated; 1006 } 1007 } 1008 1009 static void setDoesNotAlias(Function &F, unsigned n) { 1010 if (!F.doesNotAlias(n)) { 1011 F.setDoesNotAlias(n); 1012 ++NumAnnotated; 1013 } 1014 } 1015 1016 /// Analyze the name and prototype of the given function and set any applicable 1017 /// attributes. 1018 /// 1019 /// Returns true if any attributes were set and false otherwise. 1020 static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) { 1021 if (F.hasFnAttribute(Attribute::OptimizeNone)) 1022 return false; 1023 1024 FunctionType *FTy = F.getFunctionType(); 1025 LibFunc::Func TheLibFunc; 1026 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc))) 1027 return false; 1028 1029 switch (TheLibFunc) { 1030 case LibFunc::strlen: 1031 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1032 return false; 1033 setOnlyReadsMemory(F); 1034 setDoesNotThrow(F); 1035 setDoesNotCapture(F, 1); 1036 break; 1037 case LibFunc::strchr: 1038 case LibFunc::strrchr: 1039 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || 1040 !FTy->getParamType(1)->isIntegerTy()) 1041 return false; 1042 setOnlyReadsMemory(F); 1043 setDoesNotThrow(F); 1044 break; 1045 case LibFunc::strtol: 1046 case LibFunc::strtod: 1047 case LibFunc::strtof: 1048 case LibFunc::strtoul: 1049 case LibFunc::strtoll: 1050 case LibFunc::strtold: 1051 case LibFunc::strtoull: 1052 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) 1053 return false; 1054 setDoesNotThrow(F); 1055 setDoesNotCapture(F, 2); 1056 setOnlyReadsMemory(F, 1); 1057 break; 1058 case LibFunc::strcpy: 1059 case LibFunc::stpcpy: 1060 case LibFunc::strcat: 1061 case LibFunc::strncat: 1062 case LibFunc::strncpy: 1063 case LibFunc::stpncpy: 1064 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) 1065 return false; 1066 setDoesNotThrow(F); 1067 setDoesNotCapture(F, 2); 1068 setOnlyReadsMemory(F, 2); 1069 break; 1070 case LibFunc::strxfrm: 1071 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || 1072 !FTy->getParamType(1)->isPointerTy()) 1073 return false; 1074 setDoesNotThrow(F); 1075 setDoesNotCapture(F, 1); 1076 setDoesNotCapture(F, 2); 1077 setOnlyReadsMemory(F, 2); 1078 break; 1079 case LibFunc::strcmp: // 0,1 1080 case LibFunc::strspn: // 0,1 1081 case LibFunc::strncmp: // 0,1 1082 case LibFunc::strcspn: // 0,1 1083 case LibFunc::strcoll: // 0,1 1084 case LibFunc::strcasecmp: // 0,1 1085 case LibFunc::strncasecmp: // 1086 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1087 !FTy->getParamType(1)->isPointerTy()) 1088 return false; 1089 setOnlyReadsMemory(F); 1090 setDoesNotThrow(F); 1091 setDoesNotCapture(F, 1); 1092 setDoesNotCapture(F, 2); 1093 break; 1094 case LibFunc::strstr: 1095 case LibFunc::strpbrk: 1096 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1097 return false; 1098 setOnlyReadsMemory(F); 1099 setDoesNotThrow(F); 1100 setDoesNotCapture(F, 2); 1101 break; 1102 case LibFunc::strtok: 1103 case LibFunc::strtok_r: 1104 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) 1105 return false; 1106 setDoesNotThrow(F); 1107 setDoesNotCapture(F, 2); 1108 setOnlyReadsMemory(F, 2); 1109 break; 1110 case LibFunc::scanf: 1111 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) 1112 return false; 1113 setDoesNotThrow(F); 1114 setDoesNotCapture(F, 1); 1115 setOnlyReadsMemory(F, 1); 1116 break; 1117 case LibFunc::setbuf: 1118 case LibFunc::setvbuf: 1119 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) 1120 return false; 1121 setDoesNotThrow(F); 1122 setDoesNotCapture(F, 1); 1123 break; 1124 case LibFunc::strdup: 1125 case LibFunc::strndup: 1126 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() || 1127 !FTy->getParamType(0)->isPointerTy()) 1128 return false; 1129 setDoesNotThrow(F); 1130 setDoesNotAlias(F, 0); 1131 setDoesNotCapture(F, 1); 1132 setOnlyReadsMemory(F, 1); 1133 break; 1134 case LibFunc::stat: 1135 case LibFunc::statvfs: 1136 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1137 !FTy->getParamType(1)->isPointerTy()) 1138 return false; 1139 setDoesNotThrow(F); 1140 setDoesNotCapture(F, 1); 1141 setDoesNotCapture(F, 2); 1142 setOnlyReadsMemory(F, 1); 1143 break; 1144 case LibFunc::sscanf: 1145 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1146 !FTy->getParamType(1)->isPointerTy()) 1147 return false; 1148 setDoesNotThrow(F); 1149 setDoesNotCapture(F, 1); 1150 setDoesNotCapture(F, 2); 1151 setOnlyReadsMemory(F, 1); 1152 setOnlyReadsMemory(F, 2); 1153 break; 1154 case LibFunc::sprintf: 1155 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1156 !FTy->getParamType(1)->isPointerTy()) 1157 return false; 1158 setDoesNotThrow(F); 1159 setDoesNotCapture(F, 1); 1160 setDoesNotCapture(F, 2); 1161 setOnlyReadsMemory(F, 2); 1162 break; 1163 case LibFunc::snprintf: 1164 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || 1165 !FTy->getParamType(2)->isPointerTy()) 1166 return false; 1167 setDoesNotThrow(F); 1168 setDoesNotCapture(F, 1); 1169 setDoesNotCapture(F, 3); 1170 setOnlyReadsMemory(F, 3); 1171 break; 1172 case LibFunc::setitimer: 1173 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || 1174 !FTy->getParamType(2)->isPointerTy()) 1175 return false; 1176 setDoesNotThrow(F); 1177 setDoesNotCapture(F, 2); 1178 setDoesNotCapture(F, 3); 1179 setOnlyReadsMemory(F, 2); 1180 break; 1181 case LibFunc::system: 1182 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1183 return false; 1184 // May throw; "system" is a valid pthread cancellation point. 1185 setDoesNotCapture(F, 1); 1186 setOnlyReadsMemory(F, 1); 1187 break; 1188 case LibFunc::malloc: 1189 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy()) 1190 return false; 1191 setDoesNotThrow(F); 1192 setDoesNotAlias(F, 0); 1193 break; 1194 case LibFunc::memcmp: 1195 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || 1196 !FTy->getParamType(1)->isPointerTy()) 1197 return false; 1198 setOnlyReadsMemory(F); 1199 setDoesNotThrow(F); 1200 setDoesNotCapture(F, 1); 1201 setDoesNotCapture(F, 2); 1202 break; 1203 case LibFunc::memchr: 1204 case LibFunc::memrchr: 1205 if (FTy->getNumParams() != 3) 1206 return false; 1207 setOnlyReadsMemory(F); 1208 setDoesNotThrow(F); 1209 break; 1210 case LibFunc::modf: 1211 case LibFunc::modff: 1212 case LibFunc::modfl: 1213 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) 1214 return false; 1215 setDoesNotThrow(F); 1216 setDoesNotCapture(F, 2); 1217 break; 1218 case LibFunc::memcpy: 1219 case LibFunc::memccpy: 1220 case LibFunc::memmove: 1221 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy()) 1222 return false; 1223 setDoesNotThrow(F); 1224 setDoesNotCapture(F, 2); 1225 setOnlyReadsMemory(F, 2); 1226 break; 1227 case LibFunc::memalign: 1228 if (!FTy->getReturnType()->isPointerTy()) 1229 return false; 1230 setDoesNotAlias(F, 0); 1231 break; 1232 case LibFunc::mkdir: 1233 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 1234 return false; 1235 setDoesNotThrow(F); 1236 setDoesNotCapture(F, 1); 1237 setOnlyReadsMemory(F, 1); 1238 break; 1239 case LibFunc::mktime: 1240 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 1241 return false; 1242 setDoesNotThrow(F); 1243 setDoesNotCapture(F, 1); 1244 break; 1245 case LibFunc::realloc: 1246 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || 1247 !FTy->getReturnType()->isPointerTy()) 1248 return false; 1249 setDoesNotThrow(F); 1250 setDoesNotAlias(F, 0); 1251 setDoesNotCapture(F, 1); 1252 break; 1253 case LibFunc::read: 1254 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) 1255 return false; 1256 // May throw; "read" is a valid pthread cancellation point. 1257 setDoesNotCapture(F, 2); 1258 break; 1259 case LibFunc::rewind: 1260 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) 1261 return false; 1262 setDoesNotThrow(F); 1263 setDoesNotCapture(F, 1); 1264 break; 1265 case LibFunc::rmdir: 1266 case LibFunc::remove: 1267 case LibFunc::realpath: 1268 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) 1269 return false; 1270 setDoesNotThrow(F); 1271 setDoesNotCapture(F, 1); 1272 setOnlyReadsMemory(F, 1); 1273 break; 1274 case LibFunc::rename: 1275 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1276 !FTy->getParamType(1)->isPointerTy()) 1277 return false; 1278 setDoesNotThrow(F); 1279 setDoesNotCapture(F, 1); 1280 setDoesNotCapture(F, 2); 1281 setOnlyReadsMemory(F, 1); 1282 setOnlyReadsMemory(F, 2); 1283 break; 1284 case LibFunc::readlink: 1285 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1286 !FTy->getParamType(1)->isPointerTy()) 1287 return false; 1288 setDoesNotThrow(F); 1289 setDoesNotCapture(F, 1); 1290 setDoesNotCapture(F, 2); 1291 setOnlyReadsMemory(F, 1); 1292 break; 1293 case LibFunc::write: 1294 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) 1295 return false; 1296 // May throw; "write" is a valid pthread cancellation point. 1297 setDoesNotCapture(F, 2); 1298 setOnlyReadsMemory(F, 2); 1299 break; 1300 case LibFunc::bcopy: 1301 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || 1302 !FTy->getParamType(1)->isPointerTy()) 1303 return false; 1304 setDoesNotThrow(F); 1305 setDoesNotCapture(F, 1); 1306 setDoesNotCapture(F, 2); 1307 setOnlyReadsMemory(F, 1); 1308 break; 1309 case LibFunc::bcmp: 1310 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || 1311 !FTy->getParamType(1)->isPointerTy()) 1312 return false; 1313 setDoesNotThrow(F); 1314 setOnlyReadsMemory(F); 1315 setDoesNotCapture(F, 1); 1316 setDoesNotCapture(F, 2); 1317 break; 1318 case LibFunc::bzero: 1319 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) 1320 return false; 1321 setDoesNotThrow(F); 1322 setDoesNotCapture(F, 1); 1323 break; 1324 case LibFunc::calloc: 1325 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy()) 1326 return false; 1327 setDoesNotThrow(F); 1328 setDoesNotAlias(F, 0); 1329 break; 1330 case LibFunc::chmod: 1331 case LibFunc::chown: 1332 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 1333 return false; 1334 setDoesNotThrow(F); 1335 setDoesNotCapture(F, 1); 1336 setOnlyReadsMemory(F, 1); 1337 break; 1338 case LibFunc::ctermid: 1339 case LibFunc::clearerr: 1340 case LibFunc::closedir: 1341 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 1342 return false; 1343 setDoesNotThrow(F); 1344 setDoesNotCapture(F, 1); 1345 break; 1346 case LibFunc::atoi: 1347 case LibFunc::atol: 1348 case LibFunc::atof: 1349 case LibFunc::atoll: 1350 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1351 return false; 1352 setDoesNotThrow(F); 1353 setOnlyReadsMemory(F); 1354 setDoesNotCapture(F, 1); 1355 break; 1356 case LibFunc::access: 1357 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) 1358 return false; 1359 setDoesNotThrow(F); 1360 setDoesNotCapture(F, 1); 1361 setOnlyReadsMemory(F, 1); 1362 break; 1363 case LibFunc::fopen: 1364 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || 1365 !FTy->getParamType(0)->isPointerTy() || 1366 !FTy->getParamType(1)->isPointerTy()) 1367 return false; 1368 setDoesNotThrow(F); 1369 setDoesNotAlias(F, 0); 1370 setDoesNotCapture(F, 1); 1371 setDoesNotCapture(F, 2); 1372 setOnlyReadsMemory(F, 1); 1373 setOnlyReadsMemory(F, 2); 1374 break; 1375 case LibFunc::fdopen: 1376 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || 1377 !FTy->getParamType(1)->isPointerTy()) 1378 return false; 1379 setDoesNotThrow(F); 1380 setDoesNotAlias(F, 0); 1381 setDoesNotCapture(F, 2); 1382 setOnlyReadsMemory(F, 2); 1383 break; 1384 case LibFunc::feof: 1385 case LibFunc::free: 1386 case LibFunc::fseek: 1387 case LibFunc::ftell: 1388 case LibFunc::fgetc: 1389 case LibFunc::fseeko: 1390 case LibFunc::ftello: 1391 case LibFunc::fileno: 1392 case LibFunc::fflush: 1393 case LibFunc::fclose: 1394 case LibFunc::fsetpos: 1395 case LibFunc::flockfile: 1396 case LibFunc::funlockfile: 1397 case LibFunc::ftrylockfile: 1398 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 1399 return false; 1400 setDoesNotThrow(F); 1401 setDoesNotCapture(F, 1); 1402 break; 1403 case LibFunc::ferror: 1404 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1405 return false; 1406 setDoesNotThrow(F); 1407 setDoesNotCapture(F, 1); 1408 setOnlyReadsMemory(F); 1409 break; 1410 case LibFunc::fputc: 1411 case LibFunc::fstat: 1412 case LibFunc::frexp: 1413 case LibFunc::frexpf: 1414 case LibFunc::frexpl: 1415 case LibFunc::fstatvfs: 1416 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1417 return false; 1418 setDoesNotThrow(F); 1419 setDoesNotCapture(F, 2); 1420 break; 1421 case LibFunc::fgets: 1422 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || 1423 !FTy->getParamType(2)->isPointerTy()) 1424 return false; 1425 setDoesNotThrow(F); 1426 setDoesNotCapture(F, 3); 1427 break; 1428 case LibFunc::fread: 1429 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || 1430 !FTy->getParamType(3)->isPointerTy()) 1431 return false; 1432 setDoesNotThrow(F); 1433 setDoesNotCapture(F, 1); 1434 setDoesNotCapture(F, 4); 1435 break; 1436 case LibFunc::fwrite: 1437 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || 1438 !FTy->getParamType(3)->isPointerTy()) 1439 return false; 1440 setDoesNotThrow(F); 1441 setDoesNotCapture(F, 1); 1442 setDoesNotCapture(F, 4); 1443 break; 1444 case LibFunc::fputs: 1445 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1446 !FTy->getParamType(1)->isPointerTy()) 1447 return false; 1448 setDoesNotThrow(F); 1449 setDoesNotCapture(F, 1); 1450 setDoesNotCapture(F, 2); 1451 setOnlyReadsMemory(F, 1); 1452 break; 1453 case LibFunc::fscanf: 1454 case LibFunc::fprintf: 1455 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1456 !FTy->getParamType(1)->isPointerTy()) 1457 return false; 1458 setDoesNotThrow(F); 1459 setDoesNotCapture(F, 1); 1460 setDoesNotCapture(F, 2); 1461 setOnlyReadsMemory(F, 2); 1462 break; 1463 case LibFunc::fgetpos: 1464 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() || 1465 !FTy->getParamType(1)->isPointerTy()) 1466 return false; 1467 setDoesNotThrow(F); 1468 setDoesNotCapture(F, 1); 1469 setDoesNotCapture(F, 2); 1470 break; 1471 case LibFunc::getc: 1472 case LibFunc::getlogin_r: 1473 case LibFunc::getc_unlocked: 1474 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 1475 return false; 1476 setDoesNotThrow(F); 1477 setDoesNotCapture(F, 1); 1478 break; 1479 case LibFunc::getenv: 1480 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1481 return false; 1482 setDoesNotThrow(F); 1483 setOnlyReadsMemory(F); 1484 setDoesNotCapture(F, 1); 1485 break; 1486 case LibFunc::gets: 1487 case LibFunc::getchar: 1488 setDoesNotThrow(F); 1489 break; 1490 case LibFunc::getitimer: 1491 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1492 return false; 1493 setDoesNotThrow(F); 1494 setDoesNotCapture(F, 2); 1495 break; 1496 case LibFunc::getpwnam: 1497 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1498 return false; 1499 setDoesNotThrow(F); 1500 setDoesNotCapture(F, 1); 1501 setOnlyReadsMemory(F, 1); 1502 break; 1503 case LibFunc::ungetc: 1504 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1505 return false; 1506 setDoesNotThrow(F); 1507 setDoesNotCapture(F, 2); 1508 break; 1509 case LibFunc::uname: 1510 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1511 return false; 1512 setDoesNotThrow(F); 1513 setDoesNotCapture(F, 1); 1514 break; 1515 case LibFunc::unlink: 1516 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1517 return false; 1518 setDoesNotThrow(F); 1519 setDoesNotCapture(F, 1); 1520 setOnlyReadsMemory(F, 1); 1521 break; 1522 case LibFunc::unsetenv: 1523 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1524 return false; 1525 setDoesNotThrow(F); 1526 setDoesNotCapture(F, 1); 1527 setOnlyReadsMemory(F, 1); 1528 break; 1529 case LibFunc::utime: 1530 case LibFunc::utimes: 1531 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || 1532 !FTy->getParamType(1)->isPointerTy()) 1533 return false; 1534 setDoesNotThrow(F); 1535 setDoesNotCapture(F, 1); 1536 setDoesNotCapture(F, 2); 1537 setOnlyReadsMemory(F, 1); 1538 setOnlyReadsMemory(F, 2); 1539 break; 1540 case LibFunc::putc: 1541 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1542 return false; 1543 setDoesNotThrow(F); 1544 setDoesNotCapture(F, 2); 1545 break; 1546 case LibFunc::puts: 1547 case LibFunc::printf: 1548 case LibFunc::perror: 1549 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1550 return false; 1551 setDoesNotThrow(F); 1552 setDoesNotCapture(F, 1); 1553 setOnlyReadsMemory(F, 1); 1554 break; 1555 case LibFunc::pread: 1556 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy()) 1557 return false; 1558 // May throw; "pread" is a valid pthread cancellation point. 1559 setDoesNotCapture(F, 2); 1560 break; 1561 case LibFunc::pwrite: 1562 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy()) 1563 return false; 1564 // May throw; "pwrite" is a valid pthread cancellation point. 1565 setDoesNotCapture(F, 2); 1566 setOnlyReadsMemory(F, 2); 1567 break; 1568 case LibFunc::putchar: 1569 setDoesNotThrow(F); 1570 break; 1571 case LibFunc::popen: 1572 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || 1573 !FTy->getParamType(0)->isPointerTy() || 1574 !FTy->getParamType(1)->isPointerTy()) 1575 return false; 1576 setDoesNotThrow(F); 1577 setDoesNotAlias(F, 0); 1578 setDoesNotCapture(F, 1); 1579 setDoesNotCapture(F, 2); 1580 setOnlyReadsMemory(F, 1); 1581 setOnlyReadsMemory(F, 2); 1582 break; 1583 case LibFunc::pclose: 1584 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1585 return false; 1586 setDoesNotThrow(F); 1587 setDoesNotCapture(F, 1); 1588 break; 1589 case LibFunc::vscanf: 1590 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1591 return false; 1592 setDoesNotThrow(F); 1593 setDoesNotCapture(F, 1); 1594 setOnlyReadsMemory(F, 1); 1595 break; 1596 case LibFunc::vsscanf: 1597 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || 1598 !FTy->getParamType(2)->isPointerTy()) 1599 return false; 1600 setDoesNotThrow(F); 1601 setDoesNotCapture(F, 1); 1602 setDoesNotCapture(F, 2); 1603 setOnlyReadsMemory(F, 1); 1604 setOnlyReadsMemory(F, 2); 1605 break; 1606 case LibFunc::vfscanf: 1607 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() || 1608 !FTy->getParamType(2)->isPointerTy()) 1609 return false; 1610 setDoesNotThrow(F); 1611 setDoesNotCapture(F, 1); 1612 setDoesNotCapture(F, 2); 1613 setOnlyReadsMemory(F, 2); 1614 break; 1615 case LibFunc::valloc: 1616 if (!FTy->getReturnType()->isPointerTy()) 1617 return false; 1618 setDoesNotThrow(F); 1619 setDoesNotAlias(F, 0); 1620 break; 1621 case LibFunc::vprintf: 1622 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy()) 1623 return false; 1624 setDoesNotThrow(F); 1625 setDoesNotCapture(F, 1); 1626 setOnlyReadsMemory(F, 1); 1627 break; 1628 case LibFunc::vfprintf: 1629 case LibFunc::vsprintf: 1630 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() || 1631 !FTy->getParamType(1)->isPointerTy()) 1632 return false; 1633 setDoesNotThrow(F); 1634 setDoesNotCapture(F, 1); 1635 setDoesNotCapture(F, 2); 1636 setOnlyReadsMemory(F, 2); 1637 break; 1638 case LibFunc::vsnprintf: 1639 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() || 1640 !FTy->getParamType(2)->isPointerTy()) 1641 return false; 1642 setDoesNotThrow(F); 1643 setDoesNotCapture(F, 1); 1644 setDoesNotCapture(F, 3); 1645 setOnlyReadsMemory(F, 3); 1646 break; 1647 case LibFunc::open: 1648 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy()) 1649 return false; 1650 // May throw; "open" is a valid pthread cancellation point. 1651 setDoesNotCapture(F, 1); 1652 setOnlyReadsMemory(F, 1); 1653 break; 1654 case LibFunc::opendir: 1655 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() || 1656 !FTy->getParamType(0)->isPointerTy()) 1657 return false; 1658 setDoesNotThrow(F); 1659 setDoesNotAlias(F, 0); 1660 setDoesNotCapture(F, 1); 1661 setOnlyReadsMemory(F, 1); 1662 break; 1663 case LibFunc::tmpfile: 1664 if (!FTy->getReturnType()->isPointerTy()) 1665 return false; 1666 setDoesNotThrow(F); 1667 setDoesNotAlias(F, 0); 1668 break; 1669 case LibFunc::times: 1670 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1671 return false; 1672 setDoesNotThrow(F); 1673 setDoesNotCapture(F, 1); 1674 break; 1675 case LibFunc::htonl: 1676 case LibFunc::htons: 1677 case LibFunc::ntohl: 1678 case LibFunc::ntohs: 1679 setDoesNotThrow(F); 1680 setDoesNotAccessMemory(F); 1681 break; 1682 case LibFunc::lstat: 1683 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || 1684 !FTy->getParamType(1)->isPointerTy()) 1685 return false; 1686 setDoesNotThrow(F); 1687 setDoesNotCapture(F, 1); 1688 setDoesNotCapture(F, 2); 1689 setOnlyReadsMemory(F, 1); 1690 break; 1691 case LibFunc::lchown: 1692 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy()) 1693 return false; 1694 setDoesNotThrow(F); 1695 setDoesNotCapture(F, 1); 1696 setOnlyReadsMemory(F, 1); 1697 break; 1698 case LibFunc::qsort: 1699 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy()) 1700 return false; 1701 // May throw; places call through function pointer. 1702 setDoesNotCapture(F, 4); 1703 break; 1704 case LibFunc::dunder_strdup: 1705 case LibFunc::dunder_strndup: 1706 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() || 1707 !FTy->getParamType(0)->isPointerTy()) 1708 return false; 1709 setDoesNotThrow(F); 1710 setDoesNotAlias(F, 0); 1711 setDoesNotCapture(F, 1); 1712 setOnlyReadsMemory(F, 1); 1713 break; 1714 case LibFunc::dunder_strtok_r: 1715 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy()) 1716 return false; 1717 setDoesNotThrow(F); 1718 setDoesNotCapture(F, 2); 1719 setOnlyReadsMemory(F, 2); 1720 break; 1721 case LibFunc::under_IO_getc: 1722 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy()) 1723 return false; 1724 setDoesNotThrow(F); 1725 setDoesNotCapture(F, 1); 1726 break; 1727 case LibFunc::under_IO_putc: 1728 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1729 return false; 1730 setDoesNotThrow(F); 1731 setDoesNotCapture(F, 2); 1732 break; 1733 case LibFunc::dunder_isoc99_scanf: 1734 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy()) 1735 return false; 1736 setDoesNotThrow(F); 1737 setDoesNotCapture(F, 1); 1738 setOnlyReadsMemory(F, 1); 1739 break; 1740 case LibFunc::stat64: 1741 case LibFunc::lstat64: 1742 case LibFunc::statvfs64: 1743 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() || 1744 !FTy->getParamType(1)->isPointerTy()) 1745 return false; 1746 setDoesNotThrow(F); 1747 setDoesNotCapture(F, 1); 1748 setDoesNotCapture(F, 2); 1749 setOnlyReadsMemory(F, 1); 1750 break; 1751 case LibFunc::dunder_isoc99_sscanf: 1752 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() || 1753 !FTy->getParamType(1)->isPointerTy()) 1754 return false; 1755 setDoesNotThrow(F); 1756 setDoesNotCapture(F, 1); 1757 setDoesNotCapture(F, 2); 1758 setOnlyReadsMemory(F, 1); 1759 setOnlyReadsMemory(F, 2); 1760 break; 1761 case LibFunc::fopen64: 1762 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() || 1763 !FTy->getParamType(0)->isPointerTy() || 1764 !FTy->getParamType(1)->isPointerTy()) 1765 return false; 1766 setDoesNotThrow(F); 1767 setDoesNotAlias(F, 0); 1768 setDoesNotCapture(F, 1); 1769 setDoesNotCapture(F, 2); 1770 setOnlyReadsMemory(F, 1); 1771 setOnlyReadsMemory(F, 2); 1772 break; 1773 case LibFunc::fseeko64: 1774 case LibFunc::ftello64: 1775 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy()) 1776 return false; 1777 setDoesNotThrow(F); 1778 setDoesNotCapture(F, 1); 1779 break; 1780 case LibFunc::tmpfile64: 1781 if (!FTy->getReturnType()->isPointerTy()) 1782 return false; 1783 setDoesNotThrow(F); 1784 setDoesNotAlias(F, 0); 1785 break; 1786 case LibFunc::fstat64: 1787 case LibFunc::fstatvfs64: 1788 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy()) 1789 return false; 1790 setDoesNotThrow(F); 1791 setDoesNotCapture(F, 2); 1792 break; 1793 case LibFunc::open64: 1794 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy()) 1795 return false; 1796 // May throw; "open" is a valid pthread cancellation point. 1797 setDoesNotCapture(F, 1); 1798 setOnlyReadsMemory(F, 1); 1799 break; 1800 case LibFunc::gettimeofday: 1801 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() || 1802 !FTy->getParamType(1)->isPointerTy()) 1803 return false; 1804 // Currently some platforms have the restrict keyword on the arguments to 1805 // gettimeofday. To be conservative, do not add noalias to gettimeofday's 1806 // arguments. 1807 setDoesNotThrow(F); 1808 setDoesNotCapture(F, 1); 1809 setDoesNotCapture(F, 2); 1810 break; 1811 default: 1812 // Didn't mark any attributes. 1813 return false; 1814 } 1815 1816 return true; 1817 } 1818 1819 /// Adds attributes to well-known standard library call declarations. 1820 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) { 1821 bool MadeChange = false; 1822 1823 // Check each function in turn annotating well-known library function 1824 // declarations with attributes. 1825 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { 1826 Function *F = (*I)->getFunction(); 1827 1828 if (F && F->isDeclaration()) 1829 MadeChange |= inferPrototypeAttributes(*F, *TLI); 1830 } 1831 1832 return MadeChange; 1833 } 1834 1835 bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { 1836 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 1837 1838 bool Changed = annotateLibraryCalls(SCC); 1839 Changed |= AddReadAttrs(SCC); 1840 Changed |= AddArgumentAttrs(SCC); 1841 Changed |= AddNoAliasAttrs(SCC); 1842 Changed |= AddNonNullAttrs(SCC); 1843 return Changed; 1844 } 1845