Skip to content

Commit

Permalink
[AggressiveInstCombine] Fix strncmp inlining (llvm#91204)
Browse files Browse the repository at this point in the history
Fix the issue that `char` constants are converted to `uint64_t` in the
wrong way when doing the inlining.
  • Loading branch information
FLZ101 authored May 6, 2024
1 parent 1b22eca commit 1241e76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,8 @@ void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
B.CreateZExt(B.CreateLoad(B.getInt8Ty(),
B.CreateInBoundsPtrAdd(Base, B.getInt64(i))),
CI->getType());
Value *VR = ConstantInt::get(CI->getType(), RHS[i]);
Value *VR =
ConstantInt::get(CI->getType(), static_cast<unsigned char>(RHS[i]));
Value *Sub = Swapped ? B.CreateSub(VR, VL) : B.CreateSub(VL, VR);
if (i < N - 1)
B.CreateCondBr(B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)),
Expand Down
38 changes: 38 additions & 0 deletions llvm/test/Transforms/AggressiveInstCombine/strncmp-1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ declare i32 @strcmp(ptr nocapture, ptr nocapture)

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

define i1 @test_strncmp_1(ptr %s) {
; CHECK-LABEL: define i1 @test_strncmp_1(
Expand Down Expand Up @@ -214,3 +215,40 @@ entry:
%cmp = icmp sle i32 %call, 0
ret i1 %cmp
}

define i1 @test_strcmp_4(ptr %s) {
; CHECK-LABEL: define i1 @test_strcmp_4(
; CHECK-SAME: ptr [[S:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[SUB_0:%.*]]
; CHECK: sub_0:
; CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[S]], align 1
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[TMP0]] to i32
; CHECK-NEXT: [[TMP2:%.*]] = sub i32 254, [[TMP1]]
; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0
; CHECK-NEXT: br i1 [[TMP3]], label [[NE:%.*]], label [[SUB_1:%.*]]
; CHECK: sub_1:
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i8, ptr [[S]], i64 1
; CHECK-NEXT: [[TMP5:%.*]] = load i8, ptr [[TMP4]], align 1
; CHECK-NEXT: [[TMP6:%.*]] = zext i8 [[TMP5]] to i32
; CHECK-NEXT: [[TMP7:%.*]] = sub i32 255, [[TMP6]]
; CHECK-NEXT: [[TMP8:%.*]] = icmp ne i32 [[TMP7]], 0
; CHECK-NEXT: br i1 [[TMP8]], label [[NE]], label [[SUB_2:%.*]]
; CHECK: sub_2:
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds i8, ptr [[S]], i64 2
; CHECK-NEXT: [[TMP10:%.*]] = load i8, ptr [[TMP9]], align 1
; CHECK-NEXT: [[TMP11:%.*]] = zext i8 [[TMP10]] to i32
; CHECK-NEXT: [[TMP12:%.*]] = sub i32 0, [[TMP11]]
; CHECK-NEXT: br label [[NE]]
; CHECK: ne:
; CHECK-NEXT: [[TMP13:%.*]] = phi i32 [ [[TMP2]], [[SUB_0]] ], [ [[TMP7]], [[SUB_1]] ], [ [[TMP12]], [[SUB_2]] ]
; CHECK-NEXT: br label [[ENTRY_TAIL:%.*]]
; CHECK: entry.tail:
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP13]], 0
; CHECK-NEXT: ret i1 [[CMP]]
;
entry:
%call = tail call i32 @strcmp(ptr nonnull dereferenceable(3) @s3ff, ptr nonnull dereferenceable(1) %s)
%cmp = icmp eq i32 %call, 0
ret i1 %cmp
}

0 comments on commit 1241e76

Please sign in to comment.