LLVM API Documentation

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