llvm.org GIT mirror llvm / 137f7df
[SimplifyLibCalls] Simplify bcmp too. Summary: Fixes PR40699. Reviewers: gchatelet Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61585 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360021 91177308-0d34-0410-b5e6-96231b3b80d8 Clement Courbet 1 year, 5 months ago
3 changed file(s) with 165 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
137137 Value *optimizeStrStr(CallInst *CI, IRBuilder<> &B);
138138 Value *optimizeMemChr(CallInst *CI, IRBuilder<> &B);
139139 Value *optimizeMemCmp(CallInst *CI, IRBuilder<> &B);
140 Value *optimizeBCmp(CallInst *CI, IRBuilder<> &B);
141 Value *optimizeMemCmpBCmpCommon(CallInst *CI, IRBuilder<> &B);
140142 Value *optimizeMemCpy(CallInst *CI, IRBuilder<> &B);
141143 Value *optimizeMemMove(CallInst *CI, IRBuilder<> &B);
142144 Value *optimizeMemSet(CallInst *CI, IRBuilder<> &B);
915915 return nullptr;
916916 }
917917
918 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
918 // Most simplifications for memcmp also apply to bcmp.
919 Value *LibCallSimplifier::optimizeMemCmpBCmpCommon(CallInst *CI,
920 IRBuilder<> &B) {
919921 Value *LHS = CI->getArgOperand(0), *RHS = CI->getArgOperand(1);
920922 Value *Size = CI->getArgOperand(2);
921923
928930 LenC->getZExtValue(), B, DL))
929931 return Res;
930932
933 return nullptr;
934 }
935
936 Value *LibCallSimplifier::optimizeMemCmp(CallInst *CI, IRBuilder<> &B) {
937 if (Value *V = optimizeMemCmpBCmpCommon(CI, B))
938 return V;
939
931940 // memcmp(x, y, Len) == 0 -> bcmp(x, y, Len) == 0
932941 // `bcmp` can be more efficient than memcmp because it only has to know that
933942 // there is a difference, not where it is.
934943 if (isOnlyUsedInZeroEqualityComparison(CI) && TLI->has(LibFunc_bcmp)) {
944 Value *LHS = CI->getArgOperand(0);
945 Value *RHS = CI->getArgOperand(1);
946 Value *Size = CI->getArgOperand(2);
935947 return emitBCmp(LHS, RHS, Size, B, DL, TLI);
936948 }
937949
938950 return nullptr;
951 }
952
953 Value *LibCallSimplifier::optimizeBCmp(CallInst *CI, IRBuilder<> &B) {
954 return optimizeMemCmpBCmpCommon(CI, B);
939955 }
940956
941957 Value *LibCallSimplifier::optimizeMemCpy(CallInst *CI, IRBuilder<> &B) {
25112527 return optimizeStrStr(CI, Builder);
25122528 case LibFunc_memchr:
25132529 return optimizeMemChr(CI, Builder);
2530 case LibFunc_bcmp:
2531 return optimizeBCmp(CI, Builder);
25142532 case LibFunc_memcmp:
25152533 return optimizeMemCmp(CI, Builder);
25162534 case LibFunc_memcpy:
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; Test that the bcmp library call simplifier works correctly.
2 ;
3 ; RUN: opt < %s -instcombine -mtriple=x86_64-unknown-linux-gnu -S | FileCheck %s
4
5 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32:64"
6
7 @foo = constant [4 x i8] c"foo\00"
8 @hel = constant [4 x i8] c"hel\00"
9 @hello_u = constant [8 x i8] c"hello_u\00"
10
11 declare i32 @bcmp(i8*, i8*, i32)
12
13 ; Check bcmp(mem, mem, size) -> 0.
14
15 define i32 @test_simplify1(i8* %mem, i32 %size) {
16 ; CHECK-LABEL: @test_simplify1(
17 ; CHECK-NEXT: ret i32 0
18 ;
19 %ret = call i32 @bcmp(i8* %mem, i8* %mem, i32 %size)
20 ret i32 %ret
21 }
22
23 ; Check bcmp(mem1, mem2, 0) -> 0.
24
25 define i32 @test_simplify2(i8* %mem1, i8* %mem2) {
26 ; CHECK-LABEL: @test_simplify2(
27 ; CHECK-NEXT: ret i32 0
28 ;
29 %ret = call i32 @bcmp(i8* %mem1, i8* %mem2, i32 0)
30 ret i32 %ret
31 }
32
33 ;; Check bcmp(mem1, mem2, 1) -> *(unsigned char*)mem1 - *(unsigned char*)mem2.
34
35 define i32 @test_simplify3(i8* %mem1, i8* %mem2) {
36 ; CHECK-LABEL: @test_simplify3(
37 ; CHECK-NEXT: [[LHSC:%.*]] = load i8, i8* [[MEM1:%.*]], align 1
38 ; CHECK-NEXT: [[LHSV:%.*]] = zext i8 [[LHSC]] to i32
39 ; CHECK-NEXT: [[RHSC:%.*]] = load i8, i8* [[MEM2:%.*]], align 1
40 ; CHECK-NEXT: [[RHSV:%.*]] = zext i8 [[RHSC]] to i32
41 ; CHECK-NEXT: [[CHARDIFF:%.*]] = sub nsw i32 [[LHSV]], [[RHSV]]
42 ; CHECK-NEXT: ret i32 [[CHARDIFF]]
43 ;
44 %ret = call i32 @bcmp(i8* %mem1, i8* %mem2, i32 1)
45 ret i32 %ret
46 }
47
48 ; Check bcmp(mem1, mem2, size) -> cnst, where all arguments are constants.
49
50 define i32 @test_simplify4() {
51 ; CHECK-LABEL: @test_simplify4(
52 ; CHECK-NEXT: ret i32 0
53 ;
54 %mem1 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
55 %mem2 = getelementptr [8 x i8], [8 x i8]* @hello_u, i32 0, i32 0
56 %ret = call i32 @bcmp(i8* %mem1, i8* %mem2, i32 3)
57 ret i32 %ret
58 }
59
60 define i32 @test_simplify5() {
61 ; CHECK-LABEL: @test_simplify5(
62 ; CHECK-NEXT: ret i32 1
63 ;
64 %mem1 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
65 %mem2 = getelementptr [4 x i8], [4 x i8]* @foo, i32 0, i32 0
66 %ret = call i32 @bcmp(i8* %mem1, i8* %mem2, i32 3)
67 ret i32 %ret
68 }
69
70 define i32 @test_simplify6() {
71 ; CHECK-LABEL: @test_simplify6(
72 ; CHECK-NEXT: ret i32 -1
73 ;
74 %mem1 = getelementptr [4 x i8], [4 x i8]* @foo, i32 0, i32 0
75 %mem2 = getelementptr [4 x i8], [4 x i8]* @hel, i32 0, i32 0
76 %ret = call i32 @bcmp(i8* %mem1, i8* %mem2, i32 3)
77 ret i32 %ret
78 }
79
80 ; Check bcmp(mem1, mem2, 8)==0 -> *(int64_t*)mem1 == *(int64_t*)mem2
81
82 define i1 @test_simplify7(i64 %x, i64 %y) {
83 ; CHECK-LABEL: @test_simplify7(
84 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i64 [[X:%.*]], [[Y:%.*]]
85 ; CHECK-NEXT: ret i1 [[TMP1]]
86 ;
87 %x.addr = alloca i64, align 8
88 %y.addr = alloca i64, align 8
89 store i64 %x, i64* %x.addr, align 8
90 store i64 %y, i64* %y.addr, align 8
91 %xptr = bitcast i64* %x.addr to i8*
92 %yptr = bitcast i64* %y.addr to i8*
93 %call = call i32 @bcmp(i8* %xptr, i8* %yptr, i32 8)
94 %cmp = icmp eq i32 %call, 0
95 ret i1 %cmp
96 }
97
98 ; Check bcmp(mem1, mem2, 4)==0 -> *(int32_t*)mem1 == *(int32_t*)mem2
99
100 define i1 @test_simplify8(i32 %x, i32 %y) {
101 ; CHECK-LABEL: @test_simplify8(
102 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
103 ; CHECK-NEXT: ret i1 [[TMP1]]
104 ;
105 %x.addr = alloca i32, align 4
106 %y.addr = alloca i32, align 4
107 store i32 %x, i32* %x.addr, align 4
108 store i32 %y, i32* %y.addr, align 4
109 %xptr = bitcast i32* %x.addr to i8*
110 %yptr = bitcast i32* %y.addr to i8*
111 %call = call i32 @bcmp(i8* %xptr, i8* %yptr, i32 4)
112 %cmp = icmp eq i32 %call, 0
113 ret i1 %cmp
114 }
115
116 ; Check bcmp(mem1, mem2, 2)==0 -> *(int16_t*)mem1 == *(int16_t*)mem2
117
118 define i1 @test_simplify9(i16 %x, i16 %y) {
119 ; CHECK-LABEL: @test_simplify9(
120 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i16 [[X:%.*]], [[Y:%.*]]
121 ; CHECK-NEXT: ret i1 [[TMP1]]
122 ;
123 %x.addr = alloca i16, align 2
124 %y.addr = alloca i16, align 2
125 store i16 %x, i16* %x.addr, align 2
126 store i16 %y, i16* %y.addr, align 2
127 %xptr = bitcast i16* %x.addr to i8*
128 %yptr = bitcast i16* %y.addr to i8*
129 %call = call i32 @bcmp(i8* %xptr, i8* %yptr, i32 2)
130 %cmp = icmp eq i32 %call, 0
131 ret i1 %cmp
132 }
133
134 define i1 @test_simplify10(i8* %mem1, i8* %mem2, i32 %size) {
135 ; CHECK-LABEL: @test_simplify10(
136 ; CHECK-NEXT: [[CALL:%.*]] = call i32 @bcmp(i8* [[MEM1:%.*]], i8* [[MEM2:%.*]], i32 [[SIZE:%.*]])
137 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[CALL]], 0
138 ; CHECK-NEXT: ret i1 [[CMP]]
139 ;
140 %call = call i32 @bcmp(i8* %mem1, i8* %mem2, i32 %size)
141 %cmp = icmp eq i32 %call, 0
142 ret i1 %cmp
143 }