Skip to content

Commit f0091d8

Browse files
committed
Fix isLibFuncEmittable logic error
1 parent 528daea commit f0091d8

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,8 +1780,9 @@ bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
17801780
return false;
17811781
StrLenFunc = emitStrLen(MaterialzedBase, Builder, *DL, TLI);
17821782
} else {
1783-
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen) &&
1784-
!DisableLIRP::Wcslen)
1783+
if (DisableLIRP::Wcslen)
1784+
return false;
1785+
if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
17851786
return false;
17861787
StrLenFunc = emitWcsLen(MaterialzedBase, Builder, *DL, TLI);
17871788
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
; RUN: opt -passes='loop(loop-idiom),verify' -enable-loop-idiom-wcslen < %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+
; invalid libcall prototype
7+
declare void @strlen(i32)
8+
declare void @wcslen(i32)
9+
10+
define i64 @valid_wcslen32(ptr %src) {
11+
; CHECK-LABEL: valid_wcslen32
12+
; CHECK-NOT: call {{.*}} @wcslen
13+
entry:
14+
%cmp = icmp eq ptr %src, null
15+
br i1 %cmp, label %return, label %lor.lhs.false
16+
17+
lor.lhs.false: ; preds = %entry
18+
%0 = load i32, ptr %src, align 4
19+
%cmp1 = icmp eq i32 %0, 0
20+
br i1 %cmp1, label %return, label %while.cond.preheader
21+
22+
while.cond.preheader: ; preds = %lor.lhs.false
23+
br label %while.cond
24+
25+
while.cond: ; preds = %while.cond.preheader, %while.cond
26+
%src.pn = phi ptr [ %curr.0, %while.cond ], [ %src, %while.cond.preheader ]
27+
%curr.0 = getelementptr inbounds i8, ptr %src.pn, i64 4
28+
%1 = load i32, ptr %curr.0, align 4
29+
%tobool.not = icmp eq i32 %1, 0
30+
br i1 %tobool.not, label %while.end, label %while.cond
31+
32+
while.end: ; preds = %while.cond
33+
%curr.0.lcssa = phi ptr [ %curr.0, %while.cond ]
34+
%sub.ptr.lhs.cast = ptrtoint ptr %curr.0.lcssa to i64
35+
%sub.ptr.rhs.cast = ptrtoint ptr %src to i64
36+
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
37+
%sub.ptr.div = ashr exact i64 %sub.ptr.sub, 2
38+
br label %return
39+
40+
return: ; preds = %entry, %lor.lhs.false, %while.end
41+
%retval.0 = phi i64 [ %sub.ptr.div, %while.end ], [ 0, %lor.lhs.false ], [ 0, %entry ]
42+
ret i64 %retval.0
43+
}
44+
45+
define i64 @valid_strlen(ptr %str) {
46+
; CHECK-LABEL: valid_strlen
47+
; CHECK-NOT: call {{.*}} @strlen
48+
entry:
49+
br label %while.cond
50+
51+
while.cond:
52+
%str.addr.0 = phi ptr [ %str, %entry ], [ %incdec.ptr, %while.cond ]
53+
%0 = load i8, ptr %str.addr.0, align 1
54+
%cmp.not = icmp eq i8 %0, 0
55+
%incdec.ptr = getelementptr i8, ptr %str.addr.0, i64 1
56+
br i1 %cmp.not, label %while.end, label %while.cond
57+
58+
while.end:
59+
%sub.ptr.lhs.cast = ptrtoint ptr %str.addr.0 to i64
60+
%sub.ptr.rhs.cast = ptrtoint ptr %str to i64
61+
%sub.ptr.sub = sub i64 %sub.ptr.lhs.cast, %sub.ptr.rhs.cast
62+
ret i64 %sub.ptr.sub
63+
}
64+
65+
!llvm.module.flags = !{!0}
66+
!0 = !{i32 1, !"wchar_size", i32 4}

0 commit comments

Comments
 (0)