[llvm] r372098 - [InstCombine] Annotate strdup with deref_or_null

David Bolvansky via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 17 03:12:48 PDT 2019


Author: xbolva00
Date: Tue Sep 17 03:12:48 2019
New Revision: 372098

URL: http://llvm.org/viewvc/llvm-project?rev=372098&view=rev
Log:
[InstCombine] Annotate strdup with deref_or_null

Modified:
    llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h
    llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/deref-alloc-fns.ll
    llvm/trunk/test/Transforms/InstCombine/objsize.ll

Modified: llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h?rev=372098&r1=372097&r2=372098&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h (original)
+++ llvm/trunk/include/llvm/Analysis/MemoryBuiltins.h Tue Sep 17 03:12:48 2019
@@ -104,6 +104,11 @@ bool isReallocLikeFn(const Function *F,
 bool isOpNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
                      bool LookThroughBitCast = false);
 
+/// Tests if a value is a call or invoke to a library function that
+/// allocates memory (strdup, strndup).
+bool isStrdupLikeFn(const Value *V, const TargetLibraryInfo *TLI,
+                     bool LookThroughBitCast = false);
+
 //===----------------------------------------------------------------------===//
 //  malloc Call Utility Functions.
 //

Modified: llvm/trunk/lib/Analysis/MemoryBuiltins.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemoryBuiltins.cpp?rev=372098&r1=372097&r2=372098&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/MemoryBuiltins.cpp (original)
+++ llvm/trunk/lib/Analysis/MemoryBuiltins.cpp Tue Sep 17 03:12:48 2019
@@ -307,6 +307,13 @@ bool llvm::isOpNewLikeFn(const Value *V,
   return getAllocationData(V, OpNewLike, TLI, LookThroughBitCast).hasValue();
 }
 
+/// Tests if a value is a call or invoke to a library function that
+/// allocates memory (strdup, strndup).
+bool llvm::isStrdupLikeFn(const Value *V, const TargetLibraryInfo *TLI,
+                          bool LookThroughBitCast) {
+  return getAllocationData(V, StrDupLike, TLI, LookThroughBitCast).hasValue();
+}
+
 /// extractMallocCall - Returns the corresponding CallInst if the instruction
 /// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
 /// ignore InvokeInst here.

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=372098&r1=372097&r2=372098&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Tue Sep 17 03:12:48 2019
@@ -4208,6 +4208,12 @@ static void annotateAnyAllocSite(CallBas
       Call.addAttribute(AttributeList::ReturnIndex,
                         Attribute::getWithDereferenceableOrNullBytes(
                             Call.getContext(), Size.getZExtValue()));
+  } else if (isStrdupLikeFn(&Call, TLI) && Call.getNumArgOperands() == 1) {
+    // TODO: handle strndup
+    if (uint64_t Len = GetStringLength(Call.getOperand(0)))
+      Call.addAttribute(
+          AttributeList::ReturnIndex,
+          Attribute::getWithDereferenceableOrNullBytes(Call.getContext(), Len));
   }
 }
 

Modified: llvm/trunk/test/Transforms/InstCombine/deref-alloc-fns.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/deref-alloc-fns.ll?rev=372098&r1=372097&r2=372098&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/deref-alloc-fns.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/deref-alloc-fns.ll Tue Sep 17 03:12:48 2019
@@ -6,6 +6,9 @@ declare noalias i8* @calloc(i64, i64)
 declare noalias i8* @realloc(i8* nocapture, i64)
 declare noalias nonnull i8* @_Znam(i64) ; throwing version of 'new'
 declare noalias nonnull i8* @_Znwm(i64) ; throwing version of 'new'
+declare noalias i8* @strdup(i8*)
+
+ at .str = private unnamed_addr constant [6 x i8] c"hello\00", align 1
 
 define noalias i8* @malloc_nonconstant_size(i64 %n) {
 ; CHECK-LABEL: @malloc_nonconstant_size(
@@ -206,3 +209,21 @@ define noalias i8* @op_new_constant_zero
   %call = tail call i8* @_Znam(i64 0)
   ret i8* %call
 }
+
+define noalias i8* @strdup_constant_str() {
+; CHECK-LABEL: @strdup_constant_str(
+; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias dereferenceable_or_null(6) i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
+; CHECK-NEXT:    ret i8* [[CALL]]
+;
+  %call = tail call noalias i8* @strdup(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
+  ret i8* %call
+}
+
+define noalias i8* @strdup_notconstant_str(i8 * %str) {
+; CHECK-LABEL: @strdup_notconstant_str(
+; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @strdup(i8* [[STR:%.*]])
+; CHECK-NEXT:    ret i8* [[CALL]]
+;
+  %call = tail call noalias i8* @strdup(i8* %str)
+  ret i8* %call
+}

Modified: llvm/trunk/test/Transforms/InstCombine/objsize.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/objsize.ll?rev=372098&r1=372097&r2=372098&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/objsize.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/objsize.ll Tue Sep 17 03:12:48 2019
@@ -141,7 +141,7 @@ define i32 @test4(i8** %esc) nounwind ss
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TMP0:%.*]] = alloca [[STRUCT_DATA:%.*]], align 8
 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast %struct.data* [[TMP0]] to i8*
-; CHECK-NEXT:    call void @llvm.memset.p0i8.i32(i8* nonnull align 8 dereferenceable(1824) [[TMP1]], i8 0, i32 1824, i1 false)
+; CHECK-NEXT:    call void @llvm.memset.p0i8.i32(i8* nonnull align 8 dereferenceable(1824) [[TMP1]], i8 0, i32 1824, i1 false) #0
 ; CHECK-NEXT:    [[TMP2:%.*]] = bitcast i8** [[ESC:%.*]] to %struct.data**
 ; CHECK-NEXT:    store %struct.data* [[TMP0]], %struct.data** [[TMP2]], align 4
 ; CHECK-NEXT:    ret i32 0
@@ -163,7 +163,7 @@ define i8* @test5(i32 %n) nounwind ssp {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[TMP0:%.*]] = tail call noalias dereferenceable_or_null(20) i8* @malloc(i32 20) #0
 ; CHECK-NEXT:    [[TMP1:%.*]] = load i8*, i8** @s, align 8
-; CHECK-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(10) [[TMP0]], i8* nonnull align 1 dereferenceable(10) [[TMP1]], i32 10, i1 false)
+; CHECK-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i32(i8* nonnull align 1 dereferenceable(10) [[TMP0]], i8* nonnull align 1 dereferenceable(10) [[TMP1]], i32 10, i1 false) #0
 ; CHECK-NEXT:    ret i8* [[TMP0]]
 ;
 entry:
@@ -227,7 +227,7 @@ declare noalias i8* @strndup(i8* nocaptu
 
 define i32 @test9(i8** %esc) {
 ; CHECK-LABEL: @test9(
-; CHECK-NEXT:    [[CALL:%.*]] = tail call i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0)) #0
+; CHECK-NEXT:    [[CALL:%.*]] = tail call dereferenceable_or_null(8) i8* @strdup(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0)) #0
 ; CHECK-NEXT:    store i8* [[CALL]], i8** [[ESC:%.*]], align 8
 ; CHECK-NEXT:    ret i32 8
 ;




More information about the llvm-commits mailing list