Skip to content

Commit 8aeb63e

Browse files
committed
Fix bug when memcmp had size equal three
1 parent 27809e1 commit 8aeb63e

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

llvm/lib/CodeGen/ExpandMemCmp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ Value *MemCmpExpansion::getMemCmpOneBlock() {
648648

649649
// The i8 and i16 cases don't need compares. We zext the loaded values and
650650
// subtract them to get the suitable negative, zero, or positive i32 result.
651-
if (Size < 4) {
651+
if (Size == 1 || Size == 2) {
652652
const LoadPair Loads = getLoadPair(LoadSizeType, BSwapSizeType,
653653
Builder.getInt32Ty(), /*Offset*/ 0);
654654
return Builder.CreateSub(Loads.Lhs, Loads.Rhs);

llvm/test/CodeGen/AArch64/memcmp.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ define i32 @length3(ptr %X, ptr %Y) nounwind {
160160
; CHECK-NEXT: orr w9, w11, w10, lsl #16
161161
; CHECK-NEXT: rev w8, w8
162162
; CHECK-NEXT: rev w9, w9
163+
; CHECK-NEXT: cmp w8, w9
164+
; CHECK-NEXT: cset w8, hi
165+
; CHECK-NEXT: cset w9, lo
163166
; CHECK-NEXT: sub w0, w8, w9
164167
; CHECK-NEXT: ret
165168
%m = tail call i32 @memcmp(ptr %X, ptr %Y, i64 3) nounwind

llvm/test/Transforms/ExpandMemCmp/AArch64/memcmp.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ define i32 @cmp3(ptr nocapture readonly %x, ptr nocapture readonly %y) {
4444
; CHECK-NEXT: [[TMP4:%.*]] = zext i24 [[TMP2]] to i32
4545
; CHECK-NEXT: [[TMP5:%.*]] = call i32 @llvm.bswap.i32(i32 [[TMP3]])
4646
; CHECK-NEXT: [[TMP6:%.*]] = call i32 @llvm.bswap.i32(i32 [[TMP4]])
47-
; CHECK-NEXT: [[TMP7:%.*]] = sub i32 [[TMP5]], [[TMP6]]
48-
; CHECK-NEXT: ret i32 [[TMP7]]
47+
; CHECK-NEXT: [[TMP7:%.*]] = icmp ugt i32 [[TMP5]], [[TMP6]]
48+
; CHECK-NEXT: [[TMP8:%.*]] = icmp ult i32 [[TMP5]], [[TMP6]]
49+
; CHECK-NEXT: [[TMP9:%.*]] = zext i1 [[TMP7]] to i32
50+
; CHECK-NEXT: [[TMP10:%.*]] = zext i1 [[TMP8]] to i32
51+
; CHECK-NEXT: [[TMP11:%.*]] = sub i32 [[TMP9]], [[TMP10]]
52+
; CHECK-NEXT: ret i32 [[TMP11]]
4953
;
5054
%call = tail call i32 @memcmp(ptr %x, ptr %y, i64 3)
5155
ret i32 %call

0 commit comments

Comments
 (0)