[FunctionAttrs] Move the malloc-like test to a static helper function
[oota-llvm.git] / lib / Transforms / IPO / FunctionAttrs.cpp
index 3ea54687365c1fb44c6a85eccdc60ad33e70079b..109ef16872abc1e8cb870b1a0d208ef20ae5354b 100644 (file)
@@ -72,10 +72,7 @@ private:
 
   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);
 };
@@ -684,8 +681,8 @@ bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
 ///
 /// 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 {
+static bool isFunctionMallocLike(Function *F,
+                                 SmallPtrSet<Function *, 8> &SCCNodes) {
   SmallSetVector<Value *, 8> FlowsToReturn;
   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
     if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
@@ -779,7 +776,7 @@ bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
     if (!F->getReturnType()->isPointerTy())
       continue;
 
-    if (!IsFunctionMallocLike(F, SCCNodes))
+    if (!isFunctionMallocLike(F, SCCNodes))
       return false;
   }
 
@@ -798,9 +795,14 @@ bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
 }
 
 /// Tests whether this function is known to not return null.
-bool FunctionAttrs::ReturnsNonNull(Function *F,
-                                   SmallPtrSet<Function *, 8> &SCCNodes,
-                                   bool &Speculative) const {
+///
+/// Requires that the function returns a pointer.
+///
+/// Returns true if it believes the function will not return a null, and sets
+/// \p Speculative based on whether the returned conclusion is a speculative
+/// conclusion due to SCC calls.
+static bool isReturnNonNull(Function *F, SmallPtrSet<Function *, 8> &SCCNodes,
+                            const TargetLibraryInfo &TLI, bool &Speculative) {
   assert(F->getReturnType()->isPointerTy() &&
          "nonnull only meaningful on pointer types");
   Speculative = false;
@@ -814,7 +816,7 @@ bool FunctionAttrs::ReturnsNonNull(Function *F,
     Value *RetVal = FlowsToReturn[i];
 
     // If this value is locally known to be non-null, we're good
-    if (isKnownNonNull(RetVal, TLI))
+    if (isKnownNonNull(RetVal, &TLI))
       continue;
 
     // Otherwise, we need to look upwards since we can't make any local
@@ -902,7 +904,7 @@ bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
       continue;
 
     bool Speculative = false;
-    if (ReturnsNonNull(F, SCCNodes, Speculative)) {
+    if (isReturnNonNull(F, SCCNodes, *TLI, Speculative)) {
       if (!Speculative) {
         // Mark the function eagerly since we may discover a function
         // which prevents us from speculating about the entire SCC