Skip to content

Commit 083fc6c

Browse files
committed
dont transform idiom named strlen
1 parent 93c425e commit 083fc6c

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ bool LoopIdiomRecognize::runOnLoop(Loop *L) {
313313

314314
// Disable loop idiom recognition if the function's name is a common idiom.
315315
StringRef Name = L->getHeader()->getParent()->getName();
316-
if (Name == "memset" || Name == "memcpy")
316+
if (Name == "memset" || Name == "memcpy" || Name == "strlen" ||
317+
Name == "wcslen")
317318
return false;
318319

319320
// Determine if code size heuristics need to be applied.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
; RUN: opt -passes='loop(loop-idiom)' < %s -S | FileCheck %s
2+
3+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
4+
target triple = "x86_64-unknown-linux-gnu"
5+
6+
; CHECK-LABEL: @strlen(
7+
; CHECK-NOT: call{{.*}} strlen
8+
; CHECK-LABEL: @wcslen(
9+
; CHECK-NOT: call{{.*}} wcslen
10+
11+
define i64 @strlen(ptr %str) {
12+
entry:
13+
br label %while.cond
14+
15+
while.cond:
16+
%str.addr.0 = phi ptr [ %str, %entry ], [ %incdec.ptr, %while.cond ]
17+
%0 = load i8, ptr %str.addr.0, align 1
18+
%cmp.not = icmp eq i8 %0, 0
19+
%incdec.ptr = getelementptr i8, ptr %str.addr.0, i64 1
20+
br i1 %cmp.not, label %while.end, label %while.cond
21+
22+
while.end:
23+
%sub.ptr.lhs.cast = ptrtoint ptr %str.addr.0 to i64
24+
%sub.ptr.rhs.cast = ptrtoint ptr %str to i64
25+
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
26+
ret i64 %sub.ptr.sub
27+
}
28+
29+
define i64 @wcslen(ptr %src) {
30+
entry:
31+
%cmp = icmp eq ptr %src, null
32+
br i1 %cmp, label %return, label %lor.lhs.false
33+
34+
lor.lhs.false: ; preds = %entry
35+
%0 = load i32, ptr %src, align 4
36+
%cmp1 = icmp eq i32 %0, 0
37+
br i1 %cmp1, label %return, label %while.cond.preheader
38+
39+
while.cond.preheader: ; preds = %lor.lhs.false
40+
br label %while.cond
41+
42+
while.cond: ; preds = %while.cond.preheader, %while.cond
43+
%src.pn = phi ptr [ %curr.0, %while.cond ], [ %src, %while.cond.preheader ]
44+
%curr.0 = getelementptr inbounds i8, ptr %src.pn, i64 4
45+
%1 = load i32, ptr %curr.0, align 4
46+
%tobool.not = icmp eq i32 %1, 0
47+
br i1 %tobool.not, label %while.end, label %while.cond
48+
49+
while.end: ; preds = %while.cond
50+
%curr.0.lcssa = phi ptr [ %curr.0, %while.cond ]
51+
%sub.ptr.lhs.cast = ptrtoint ptr %curr.0.lcssa to i64
52+
%sub.ptr.rhs.cast = ptrtoint ptr %src to i64
53+
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
54+
%sub.ptr.div = ashr exact i64 %sub.ptr.sub, 2
55+
br label %return
56+
57+
return: ; preds = %entry, %lor.lhs.false, %while.end
58+
%retval.0 = phi i64 [ %sub.ptr.div, %while.end ], [ 0, %lor.lhs.false ], [ 0, %entry ]
59+
ret i64 %retval.0
60+
}
61+
62+
63+
!llvm.module.flags = !{!0}
64+
!0 = !{i32 1, !"wchar_size", i32 4}

0 commit comments

Comments
 (0)