[Analysis] Refactor as promised in r290397.

This also makes us no longer check for `allocsize` on intrinsic calls.
This shouldn't matter, since intrinsics should provide the information
we get from `allocsize` on their own.

llvm-svn: 290585
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp
index 3e464f0..d41af65 100644
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -79,6 +79,10 @@
 
 
 static Function *getCalledFunction(const Value *V, bool LookThroughBitCast) {
+  // Don't care about intrinsics in this case.
+  if (isa<IntrinsicInst>(V))
+    return nullptr;
+
   if (LookThroughBitCast)
     V = V->stripPointerCasts();
 
@@ -98,17 +102,9 @@
 /// Returns the allocation data for the given value if it's either a call to a
 /// known allocation function, or a call to a function with the allocsize
 /// attribute.
-static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
-                                              const TargetLibraryInfo *TLI,
-                                              bool LookThroughBitCast = false) {
-  // Skip intrinsics
-  if (isa<IntrinsicInst>(V))
-    return None;
-
-  const Function *Callee = getCalledFunction(V, LookThroughBitCast);
-  if (!Callee)
-    return None;
-
+static Optional<AllocFnsTy>
+getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
+                             const TargetLibraryInfo *TLI) {
   // Make sure that the function is available.
   StringRef FnName = Callee->getName();
   LibFunc::Func TLIFn;
@@ -144,20 +140,30 @@
   return None;
 }
 
+static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
+                                              const TargetLibraryInfo *TLI,
+                                              bool LookThroughBitCast = false) {
+  if (const Function *Callee = getCalledFunction(V, LookThroughBitCast))
+    return getAllocationDataForFunction(Callee, AllocTy, TLI);
+  return None;
+}
+
 static Optional<AllocFnsTy> getAllocationSize(const Value *V,
                                               const TargetLibraryInfo *TLI) {
+  const Function *Callee = getCalledFunction(V, /*LookThroughBitCast=*/false);
+  if (!Callee)
+    return None;
+
   // Prefer to use existing information over allocsize. This will give us an
   // accurate AllocTy.
   if (Optional<AllocFnsTy> Data =
-          getAllocationData(V, AnyAlloc, TLI, /*LookThroughBitCast=*/false))
+          getAllocationDataForFunction(Callee, AnyAlloc, TLI))
     return Data;
 
-  // FIXME: Not calling getCalledFunction twice would be nice.
-  const Function *Callee = getCalledFunction(V, /*LookThroughBitCast=*/false);
-  if (!Callee || !Callee->hasFnAttribute(Attribute::AllocSize))
+  Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
+  if (Attr == Attribute())
     return None;
 
-  Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
   std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
 
   AllocFnsTy Result;