Skip to content

Commit 1241e76

Browse files
authored
[AggressiveInstCombine] Fix strncmp inlining (#91204)
Fix the issue that `char` constants are converted to `uint64_t` in the wrong way when doing the inlining.
1 parent 1b22eca commit 1241e76

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,8 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
10731073
B.CreateZExt(B.CreateLoad(B.getInt8Ty(),
10741074
B.CreateInBoundsPtrAdd(Base, B.getInt64(i))),
10751075
CI->getType());
1076-
Value *VR = ConstantInt::get(CI->getType(), RHS[i]);
1076+
Value *VR =
1077+
ConstantInt::get(CI->getType(), static_cast<unsigned char>(RHS[i]));
10771078
Value *Sub = Swapped ? B.CreateSub(VR, VL) : B.CreateSub(VL, VR);
10781079
if (i < N - 1)
10791080
B.CreateCondBr(B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)),

llvm/test/Transforms/AggressiveInstCombine/strncmp-1.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare i32 @strcmp(ptr nocapture, ptr nocapture)
88

99
@s2 = constant [2 x i8] c"a\00"
1010
@s3 = constant [3 x i8] c"ab\00"
11+
@s3ff = constant [3 x i8] c"\FE\FF\00"
1112

1213
define i1 @test_strncmp_1(ptr %s) {
1314
; CHECK-LABEL: define i1 @test_strncmp_1(
@@ -214,3 +215,40 @@ entry:
214215
%cmp = icmp sle i32 %call, 0
215216
ret i1 %cmp
216217
}
218+
219+
define i1 @test_strcmp_4(ptr %s) {
220+
; CHECK-LABEL: define i1 @test_strcmp_4(
221+
; CHECK-SAME: ptr [[S:%.*]]) {
222+
; CHECK-NEXT: entry:
223+
; CHECK-NEXT: br label [[SUB_0:%.*]]
224+
; CHECK: sub_0:
225+
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[S]], align 1
226+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[TMP0]] to i32
227+
; CHECK-NEXT: [[TMP2:%.*]] = sub i32 254, [[TMP1]]
228+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0
229+
; CHECK-NEXT: br i1 [[TMP3]], label [[NE:%.*]], label [[SUB_1:%.*]]
230+
; CHECK: sub_1:
231+
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i8, ptr [[S]], i64 1
232+
; CHECK-NEXT: [[TMP5:%.*]] = load i8, ptr [[TMP4]], align 1
233+
; CHECK-NEXT: [[TMP6:%.*]] = zext i8 [[TMP5]] to i32
234+
; CHECK-NEXT: [[TMP7:%.*]] = sub i32 255, [[TMP6]]
235+
; CHECK-NEXT: [[TMP8:%.*]] = icmp ne i32 [[TMP7]], 0
236+
; CHECK-NEXT: br i1 [[TMP8]], label [[NE]], label [[SUB_2:%.*]]
237+
; CHECK: sub_2:
238+
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds i8, ptr [[S]], i64 2
239+
; CHECK-NEXT: [[TMP10:%.*]] = load i8, ptr [[TMP9]], align 1
240+
; CHECK-NEXT: [[TMP11:%.*]] = zext i8 [[TMP10]] to i32
241+
; CHECK-NEXT: [[TMP12:%.*]] = sub i32 0, [[TMP11]]
242+
; CHECK-NEXT: br label [[NE]]
243+
; CHECK: ne:
244+
; CHECK-NEXT: [[TMP13:%.*]] = phi i32 [ [[TMP2]], [[SUB_0]] ], [ [[TMP7]], [[SUB_1]] ], [ [[TMP12]], [[SUB_2]] ]
245+
; CHECK-NEXT: br label [[ENTRY_TAIL:%.*]]
246+
; CHECK: entry.tail:
247+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP13]], 0
248+
; CHECK-NEXT: ret i1 [[CMP]]
249+
;
250+
entry:
251+
%call = tail call i32 @strcmp(ptr nonnull dereferenceable(3) @s3ff, ptr nonnull dereferenceable(1) %s)
252+
%cmp = icmp eq i32 %call, 0
253+
ret i1 %cmp
254+
}

0 commit comments

Comments
 (0)