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