[FunctionAttrs] Make the per-function attribute inference a boring
[oota-llvm.git] / lib / Transforms / IPO / FunctionAttrs.cpp
index 931a54e09f680a854e13681ea7a44a2c5bec6d16..3ea54687365c1fb44c6a85eccdc60ad33e70079b 100644 (file)
@@ -58,82 +58,8 @@ struct FunctionAttrs : public CallGraphSCCPass {
     initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
   }
 
     initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
   }
 
-  // runOnSCC - Analyze the SCC, performing the transformation if possible.
   bool runOnSCC(CallGraphSCC &SCC) override;
 
   bool runOnSCC(CallGraphSCC &SCC) override;
 
-  // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
-  bool AddReadAttrs(const CallGraphSCC &SCC);
-
-  // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
-  bool AddArgumentAttrs(const CallGraphSCC &SCC);
-
-  // IsFunctionMallocLike - Does this function allocate new memory?
-  bool IsFunctionMallocLike(Function *F, SmallPtrSet<Function *, 8> &) const;
-
-  // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
-  bool AddNoAliasAttrs(const CallGraphSCC &SCC);
-
-  /// \brief Does this function return null?
-  bool ReturnsNonNull(Function *F, SmallPtrSet<Function *, 8> &,
-                      bool &Speculative) const;
-
-  /// \brief Deduce nonnull attributes for the SCC.
-  bool AddNonNullAttrs(const CallGraphSCC &SCC);
-
-  // Utility methods used by inferPrototypeAttributes to add attributes
-  // and maintain annotation statistics.
-
-  void setDoesNotAccessMemory(Function &F) {
-    if (!F.doesNotAccessMemory()) {
-      F.setDoesNotAccessMemory();
-      ++NumAnnotated;
-    }
-  }
-
-  void setOnlyReadsMemory(Function &F) {
-    if (!F.onlyReadsMemory()) {
-      F.setOnlyReadsMemory();
-      ++NumAnnotated;
-    }
-  }
-
-  void setDoesNotThrow(Function &F) {
-    if (!F.doesNotThrow()) {
-      F.setDoesNotThrow();
-      ++NumAnnotated;
-    }
-  }
-
-  void setDoesNotCapture(Function &F, unsigned n) {
-    if (!F.doesNotCapture(n)) {
-      F.setDoesNotCapture(n);
-      ++NumAnnotated;
-    }
-  }
-
-  void setOnlyReadsMemory(Function &F, unsigned n) {
-    if (!F.onlyReadsMemory(n)) {
-      F.setOnlyReadsMemory(n);
-      ++NumAnnotated;
-    }
-  }
-
-  void setDoesNotAlias(Function &F, unsigned n) {
-    if (!F.doesNotAlias(n)) {
-      F.setDoesNotAlias(n);
-      ++NumAnnotated;
-    }
-  }
-
-  // inferPrototypeAttributes - Analyze the name and prototype of the
-  // given function and set any applicable attributes.  Returns true
-  // if any attributes were set and false otherwise.
-  bool inferPrototypeAttributes(Function &F);
-
-  // annotateLibraryCalls - Adds attributes to well-known standard library
-  // call declarations.
-  bool annotateLibraryCalls(const CallGraphSCC &SCC);
-
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesCFG();
     AU.addRequired<AssumptionCacheTracker>();
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesCFG();
     AU.addRequired<AssumptionCacheTracker>();
@@ -143,6 +69,15 @@ struct FunctionAttrs : public CallGraphSCCPass {
 
 private:
   TargetLibraryInfo *TLI;
 
 private:
   TargetLibraryInfo *TLI;
+
+  bool AddReadAttrs(const CallGraphSCC &SCC);
+  bool AddArgumentAttrs(const CallGraphSCC &SCC);
+  bool IsFunctionMallocLike(Function *F, SmallPtrSet<Function *, 8> &) const;
+  bool AddNoAliasAttrs(const CallGraphSCC &SCC);
+  bool ReturnsNonNull(Function *F, SmallPtrSet<Function *, 8> &,
+                      bool &Speculative) const;
+  bool AddNonNullAttrs(const CallGraphSCC &SCC);
+  bool annotateLibraryCalls(const CallGraphSCC &SCC);
 };
 }
 
 };
 }
 
@@ -157,7 +92,7 @@ INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
 
 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
 
 
 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
 
-/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
+/// Deduce readonly/readnone attributes for the SCC.
 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
   SmallPtrSet<Function *, 8> SCCNodes;
 
 bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
   SmallPtrSet<Function *, 8> SCCNodes;
 
@@ -316,9 +251,9 @@ bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
 }
 
 namespace {
 }
 
 namespace {
-// For a given pointer Argument, this retains a list of Arguments of functions
-// in the same SCC that the pointer data flows into. We use this to build an
-// SCC of the arguments.
+/// For a given pointer Argument, this retains a list of Arguments of functions
+/// in the same SCC that the pointer data flows into. We use this to build an
+/// SCC of the arguments.
 struct ArgumentGraphNode {
   Argument *Definition;
   SmallVector<ArgumentGraphNode *, 4> Uses;
 struct ArgumentGraphNode {
   Argument *Definition;
   SmallVector<ArgumentGraphNode *, 4> Uses;
@@ -356,9 +291,9 @@ public:
   }
 };
 
   }
 };
 
-// This tracker checks whether callees are in the SCC, and if so it does not
-// consider that a capture, instead adding it to the "Uses" list and
-// continuing with the analysis.
+/// This tracker checks whether callees are in the SCC, and if so it does not
+/// consider that a capture, instead adding it to the "Uses" list and
+/// continuing with the analysis.
 struct ArgumentUsesTracker : public CaptureTracker {
   ArgumentUsesTracker(const SmallPtrSet<Function *, 8> &SCCNodes)
       : Captured(false), SCCNodes(SCCNodes) {}
 struct ArgumentUsesTracker : public CaptureTracker {
   ArgumentUsesTracker(const SmallPtrSet<Function *, 8> &SCCNodes)
       : Captured(false), SCCNodes(SCCNodes) {}
@@ -430,7 +365,7 @@ struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
 };
 }
 
 };
 }
 
-// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
+/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
 static Attribute::AttrKind
 determinePointerReadAttrs(Argument *A,
                           const SmallPtrSet<Argument *, 8> &SCCNodes) {
 static Attribute::AttrKind
 determinePointerReadAttrs(Argument *A,
                           const SmallPtrSet<Argument *, 8> &SCCNodes) {
@@ -535,7 +470,7 @@ determinePointerReadAttrs(Argument *A,
   return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
 }
 
   return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
 }
 
-/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
+/// Deduce nocapture attributes for the SCC.
 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
   bool Changed = false;
 
 bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
   bool Changed = false;
 
@@ -745,8 +680,10 @@ bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
   return Changed;
 }
 
   return Changed;
 }
 
-/// IsFunctionMallocLike - A function is malloc-like if it returns either null
-/// or a pointer that doesn't alias any other pointer visible to the caller.
+/// Tests whether a function is "malloc-like".
+///
+/// A function is "malloc-like" if it returns either null or a pointer that
+/// doesn't alias any other pointer visible to the caller.
 bool FunctionAttrs::IsFunctionMallocLike(
     Function *F, SmallPtrSet<Function *, 8> &SCCNodes) const {
   SmallSetVector<Value *, 8> FlowsToReturn;
 bool FunctionAttrs::IsFunctionMallocLike(
     Function *F, SmallPtrSet<Function *, 8> &SCCNodes) const {
   SmallSetVector<Value *, 8> FlowsToReturn;
@@ -810,7 +747,7 @@ bool FunctionAttrs::IsFunctionMallocLike(
   return true;
 }
 
   return true;
 }
 
-/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
+/// Deduce noalias attributes for the SCC.
 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
   SmallPtrSet<Function *, 8> SCCNodes;
 
 bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
   SmallPtrSet<Function *, 8> SCCNodes;
 
@@ -860,6 +797,7 @@ bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
   return MadeChange;
 }
 
   return MadeChange;
 }
 
+/// Tests whether this function is known to not return null.
 bool FunctionAttrs::ReturnsNonNull(Function *F,
                                    SmallPtrSet<Function *, 8> &SCCNodes,
                                    bool &Speculative) const {
 bool FunctionAttrs::ReturnsNonNull(Function *F,
                                    SmallPtrSet<Function *, 8> &SCCNodes,
                                    bool &Speculative) const {
@@ -924,6 +862,7 @@ bool FunctionAttrs::ReturnsNonNull(Function *F,
   return true;
 }
 
   return true;
 }
 
+/// Deduce nonnull attributes for the SCC.
 bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
   SmallPtrSet<Function *, 8> SCCNodes;
 
 bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
   SmallPtrSet<Function *, 8> SCCNodes;
 
@@ -997,16 +936,59 @@ bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
   return MadeChange;
 }
 
   return MadeChange;
 }
 
-/// inferPrototypeAttributes - Analyze the name and prototype of the
-/// given function and set any applicable attributes.  Returns true
-/// if any attributes were set and false otherwise.
-bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
+static void setDoesNotAccessMemory(Function &F) {
+  if (!F.doesNotAccessMemory()) {
+    F.setDoesNotAccessMemory();
+    ++NumAnnotated;
+  }
+}
+
+static void setOnlyReadsMemory(Function &F) {
+  if (!F.onlyReadsMemory()) {
+    F.setOnlyReadsMemory();
+    ++NumAnnotated;
+  }
+}
+
+static void setDoesNotThrow(Function &F) {
+  if (!F.doesNotThrow()) {
+    F.setDoesNotThrow();
+    ++NumAnnotated;
+  }
+}
+
+static void setDoesNotCapture(Function &F, unsigned n) {
+  if (!F.doesNotCapture(n)) {
+    F.setDoesNotCapture(n);
+    ++NumAnnotated;
+  }
+}
+
+static void setOnlyReadsMemory(Function &F, unsigned n) {
+  if (!F.onlyReadsMemory(n)) {
+    F.setOnlyReadsMemory(n);
+    ++NumAnnotated;
+  }
+}
+
+static void setDoesNotAlias(Function &F, unsigned n) {
+  if (!F.doesNotAlias(n)) {
+    F.setDoesNotAlias(n);
+    ++NumAnnotated;
+  }
+}
+
+/// Analyze the name and prototype of the given function and set any applicable
+/// attributes.
+///
+/// Returns true if any attributes were set and false otherwise.
+static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
   if (F.hasFnAttribute(Attribute::OptimizeNone))
     return false;
 
   FunctionType *FTy = F.getFunctionType();
   LibFunc::Func TheLibFunc;
   if (F.hasFnAttribute(Attribute::OptimizeNone))
     return false;
 
   FunctionType *FTy = F.getFunctionType();
   LibFunc::Func TheLibFunc;
-  if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
+  if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
     return false;
 
   switch (TheLibFunc) {
     return false;
 
   switch (TheLibFunc) {
@@ -1799,8 +1781,7 @@ bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
   return true;
 }
 
   return true;
 }
 
-/// annotateLibraryCalls - Adds attributes to well-known standard library
-/// call declarations.
+/// Adds attributes to well-known standard library call declarations.
 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
   bool MadeChange = false;
 
 bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
   bool MadeChange = false;
 
@@ -1810,7 +1791,7 @@ bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
     Function *F = (*I)->getFunction();
 
     if (F && F->isDeclaration())
     Function *F = (*I)->getFunction();
 
     if (F && F->isDeclaration())
-      MadeChange |= inferPrototypeAttributes(*F);
+      MadeChange |= inferPrototypeAttributes(*F, *TLI);
   }
 
   return MadeChange;
   }
 
   return MadeChange;