[llvm] r290588 - [Analysis] Ignore `nobuiltin` on `allocsize` function calls.

George Burgess IV via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 26 22:32:14 PST 2016


Author: gbiv
Date: Tue Dec 27 00:32:14 2016
New Revision: 290588

URL: http://llvm.org/viewvc/llvm-project?rev=290588&view=rev
Log:
[Analysis] Ignore `nobuiltin` on `allocsize` function calls.

We currently ignore the `allocsize` attribute on functions calls with
the `nobuiltin` attribute when trying to lower `@llvm.objectsize`. We
shouldn't care about `nobuiltin` here: `allocsize` is explicitly added
by the user, not inferred based on a function's symbol.

Modified:
    llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
    llvm/trunk/test/Transforms/InstCombine/allocsize.ll

Modified: llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryBuiltins.cpp?rev=290588&r1=290587&r2=290588&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/MemoryBuiltins.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryBuiltins.cpp Tue Dec 27 00:32:14 2016
@@ -77,8 +77,8 @@ static const std::pair<LibFunc::Func, Al
   // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
 };
 
-
-static Function *getCalledFunction(const Value *V, bool LookThroughBitCast) {
+static Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
+                                   bool &IsNoBuiltin) {
   // Don't care about intrinsics in this case.
   if (isa<IntrinsicInst>(V))
     return nullptr;
@@ -90,8 +90,7 @@ static Function *getCalledFunction(const
   if (!CS.getInstruction())
     return nullptr;
 
-  if (CS.isNoBuiltin())
-    return nullptr;
+  IsNoBuiltin = CS.isNoBuiltin();
 
   Function *Callee = CS.getCalledFunction();
   if (!Callee || !Callee->isDeclaration())
@@ -143,22 +142,28 @@ getAllocationDataForFunction(const Funct
 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);
+  bool IsNoBuiltinCall;
+  if (const Function *Callee =
+          getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
+    if (!IsNoBuiltinCall)
+      return getAllocationDataForFunction(Callee, AllocTy, TLI);
   return None;
 }
 
 static Optional<AllocFnsTy> getAllocationSize(const Value *V,
                                               const TargetLibraryInfo *TLI) {
-  const Function *Callee = getCalledFunction(V, /*LookThroughBitCast=*/false);
+  bool IsNoBuiltinCall;
+  const Function *Callee =
+      getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
   if (!Callee)
     return None;
 
   // Prefer to use existing information over allocsize. This will give us an
   // accurate AllocTy.
-  if (Optional<AllocFnsTy> Data =
-          getAllocationDataForFunction(Callee, AnyAlloc, TLI))
-    return Data;
+  if (!IsNoBuiltinCall)
+    if (Optional<AllocFnsTy> Data =
+            getAllocationDataForFunction(Callee, AnyAlloc, TLI))
+      return Data;
 
   Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
   if (Attr == Attribute())

Modified: llvm/trunk/test/Transforms/InstCombine/allocsize.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/allocsize.ll?rev=290588&r1=290587&r2=290588&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/allocsize.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/allocsize.ll Tue Dec 27 00:32:14 2016
@@ -134,6 +134,19 @@ define void @test_overflow(i8** %p, i32*
   ret void
 }
 
+; CHECK-LABEL: define void @test_nobuiltin
+; We had a bug where `nobuiltin` would cause `allocsize` to be ignored in
+; @llvm.objectsize calculations.
+define void @test_nobuiltin(i8** %p, i64* %r) {
+  %1 = call i8* @my_malloc(i8* null, i32 100) nobuiltin
+  store i8* %1, i8** %p, align 8
+
+  %2 = call i64 @llvm.objectsize.i64.p0i8(i8* %1, i1 false)
+  ; CHECK: store i64 100
+  store i64 %2, i64* %r, align 8
+  ret void
+}
+
 attributes #0 = { allocsize(1) }
 attributes #1 = { allocsize(2, 3) }
 




More information about the llvm-commits mailing list