]> granicus.if.org Git - llvm/commitdiff
Add argmononly attribute to strlen and wcslen, i.e. they only read memory (string...
authorXin Tong <trent.xin.tong@gmail.com>
Sun, 18 Jun 2017 03:10:26 +0000 (03:10 +0000)
committerXin Tong <trent.xin.tong@gmail.com>
Sun, 18 Jun 2017 03:10:26 +0000 (03:10 +0000)
Summary:
This allows strlen to be moved out of the loop in case its argument is
not modified in the loop in LICM.

Reviewers: hfinkel, davide, sanjoy, dberlin

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34323

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305641 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/BuildLibCalls.cpp
test/Transforms/InferFunctionAttrs/annotate.ll
test/Transforms/LICM/strlen.ll [new file with mode: 0644]

index ebde1f9a17dd6b0e459c795a5f967843ee2b32a2..b60dfb4f3541ddc73d7747f73fcf1fc03c216169 100644 (file)
@@ -116,6 +116,7 @@ bool llvm::inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI) {
   case LibFunc_wcslen:
     Changed |= setOnlyReadsMemory(F);
     Changed |= setDoesNotThrow(F);
+    Changed |= setOnlyAccessesArgMemory(F);
     Changed |= setDoesNotCapture(F, 0);
     return Changed;
   case LibFunc_strchr:
index cb4b5cdd1e8cf830c2387bc3f82898a77b8bd32e..80ac8f99edc0460eeadf75ae15afce0c904505c7 100644 (file)
@@ -844,7 +844,7 @@ declare i64 @strcspn(i8*, i8*)
 ; CHECK: declare noalias i8* @strdup(i8* nocapture readonly) [[G0]]
 declare i8* @strdup(i8*)
 
-; CHECK: declare i64 @strlen(i8* nocapture) [[G1]]
+; CHECK: declare i64 @strlen(i8* nocapture) [[G2:#[0-9]+]]
 declare i64 @strlen(i8*)
 
 ; CHECK: declare i32 @strncasecmp(i8* nocapture, i8* nocapture, i64) [[G1]]
@@ -996,10 +996,11 @@ declare i64 @write(i32, i8*, i64)
 
 
 ; memset_pattern16 isn't available everywhere.
-; CHECK-DARWIN: declare void @memset_pattern16(i8* nocapture, i8* nocapture readonly, i64) [[G2:#[0-9]+]]
+; CHECK-DARWIN: declare void @memset_pattern16(i8* nocapture, i8* nocapture readonly, i64) [[G3:#[0-9]+]]
 declare void @memset_pattern16(i8*, i8*, i64)
 
 
 ; CHECK: attributes [[G0]] = { nounwind }
 ; CHECK: attributes [[G1]] = { nounwind readonly }
-; CHECK-DARWIN: attributes [[G2]] = { argmemonly }
+; CHECK: attributes [[G2]] = { argmemonly nounwind readonly }
+; CHECK-DARWIN: attributes [[G3]] = { argmemonly }
diff --git a/test/Transforms/LICM/strlen.ll b/test/Transforms/LICM/strlen.ll
new file mode 100644 (file)
index 0000000..27d51c8
--- /dev/null
@@ -0,0 +1,19 @@
+; RUN: opt -S -inferattrs -basicaa -licm < %s | FileCheck %s
+
+define void @test(i64* noalias %loc, i8* noalias %a) {
+; CHECK-LABEL: @test
+; CHECK: @strlen
+; CHECK-LABEL: loop:
+  br label %loop
+
+loop:
+  %res = call i64 @strlen(i8* %a)
+  store i64 %res, i64* %loc
+  br label %loop
+}
+
+; CHECK: declare i64 @strlen(i8* nocapture) #0
+; CHECK: attributes #0 = { argmemonly nounwind readonly }
+declare i64 @strlen(i8*)
+
+