[InstCombine] Don't check for alloc fn before fetching object size
authorNikita Popov <npopov@redhat.com>
Thu, 21 Jul 2022 08:05:48 +0000 (10:05 +0200)
committerNikita Popov <npopov@redhat.com>
Thu, 21 Jul 2022 08:45:03 +0000 (10:45 +0200)
This code is just interested in the allocsize, not any other
allocator properties.

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/deref-alloc-fns.ll

index 6a2cfd63573a4c9bbc8198d8ec0c20879af8bb38..b1106d655535c97c297d3fd8ec9dd7b7adaa9a80 100644 (file)
@@ -2885,7 +2885,7 @@ bool InstCombinerImpl::annotateAnyAllocSite(CallBase &Call,
   // of the respective allocator declaration with generic attributes.
   bool Changed = false;
 
-  if (isAllocationFn(&Call, TLI)) {
+  if (Call.getType()->isPointerTy()) {
     uint64_t Size;
     ObjectSizeOpts Opts;
     if (getObjectSize(&Call, Size, DL, TLI, Opts) && Size > 0) {
index 0b529358f6bee1d4933098040af26f157d8954ec..41a2427b683d3e38d270be2456a957f05114093e 100644 (file)
@@ -13,6 +13,8 @@ declare noalias i8* @aligned_alloc(i64, i64)
 declare noalias align 16 i8* @memalign(i64, i64)
 ; new[](unsigned int, align_val_t)
 declare noalias i8* @_ZnajSt11align_val_t(i64 %size, i64 %align)
+declare i8* @my_malloc(i64) allocsize(0)
+declare i8* @my_calloc(i64, i64) allocsize(0, 1)
 
 @.str = private unnamed_addr constant [6 x i8] c"hello\00", align 1
 
@@ -354,3 +356,20 @@ define noalias i8* @op_new_align() {
   ret i8* %call
 }
 
+define i8* @my_malloc_constant_size() {
+; CHECK-LABEL: @my_malloc_constant_size(
+; CHECK-NEXT:    [[CALL:%.*]] = call dereferenceable_or_null(32) i8* @my_malloc(i64 32)
+; CHECK-NEXT:    ret i8* [[CALL]]
+;
+  %call = call i8* @my_malloc(i64 32)
+  ret i8* %call
+}
+
+define i8* @my_calloc_constant_size() {
+; CHECK-LABEL: @my_calloc_constant_size(
+; CHECK-NEXT:    [[CALL:%.*]] = call dereferenceable_or_null(128) i8* @my_calloc(i64 32, i64 4)
+; CHECK-NEXT:    ret i8* [[CALL]]
+;
+  %call = call i8* @my_calloc(i64 32, i64 4)
+  ret i8* %call
+}