InstCombine: If we call llvm.objectsize on a malloc call we can replace it with the size passed to malloc.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@122959 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/InstCombine/InstCombineCalls.cpp b/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 7068cb6..603a010 100644
--- a/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -298,12 +298,16 @@
         }
       }
     } else if (CallInst *MI = extractMallocCall(Op1)) {
-      // Get alloca size.
+      // Get allocation size.
       const Type* MallocType = getMallocAllocatedType(MI);
       if (MallocType && MallocType->isSized())
         if (Value *NElems = getMallocArraySize(MI, TD, true))
           if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
             Size = NElements->getZExtValue() * TD->getTypeAllocSize(MallocType);
+
+      // If there is no offset we can just return the size passed to malloc.
+      if (Offset == 0)
+        return ReplaceInstUsesWith(CI, MI->getArgOperand(0));
     }
 
     // Do not return "I don't know" here. Later optimization passes could