Skip to content

Commit d716f16

Browse files
[MemLoc] Support bcmp in MemoryLocation::getForArgument
Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D87964
1 parent 534e913 commit d716f16

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

llvm/lib/Analysis/MemoryLocation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,10 @@ MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
237237
return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
238238
AATags);
239239
break;
240+
case LibFunc_bcmp:
240241
case LibFunc_memcmp:
241242
assert((ArgIdx == 0 || ArgIdx == 1) &&
242-
"Invalid argument index for memcmp");
243+
"Invalid argument index for memcmp/bcmp");
243244
if (const ConstantInt *LenCI =
244245
dyn_cast<ConstantInt>(Call->getArgOperand(2)))
245246
return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),

llvm/test/Analysis/BasicAA/libfuncs.ll

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
; RUN: opt -inferattrs -basic-aa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 %s | FileCheck %s
2-
3-
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
4-
target triple = "x86_64-apple-macosx10.7"
1+
; RUN: opt -mtriple=i386-pc-linux-gnu -inferattrs -basic-aa -aa-eval -print-all-alias-modref-info -disable-output 2>&1 %s | FileCheck %s
52

63
; CHECK-LABEL: Function: test_memcmp_const_size
74
; CHECK: Just Ref: Ptr: i8* %a <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
@@ -45,5 +42,47 @@ entry:
4542
ret i32 %res
4643
}
4744

48-
4945
declare i32 @memcmp(i8*, i8*, i64)
46+
declare i32 @bcmp(i8*, i8*, i64)
47+
48+
; CHECK-LABEL: Function: test_bcmp_const_size
49+
; CHECK: Just Ref: Ptr: i8* %a <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 4)
50+
; CHECK-NEXT: Just Ref: Ptr: i8* %b <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 4)
51+
; CHECK-NEXT: Just Ref: Ptr: i8* %a.gep.1 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 4)
52+
; CHECK-NEXT: NoModRef: Ptr: i8* %a.gep.5 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 4)
53+
; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.1 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 4)
54+
; CHECK-NEXT: NoModRef: Ptr: i8* %b.gep.5 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 4)
55+
define i32 @test_bcmp_const_size(i8* noalias %a, i8* noalias %b) {
56+
entry:
57+
%res = tail call i32 @bcmp(i8* %a, i8* %b, i64 4)
58+
%a.gep.1 = getelementptr i8, i8* %a, i32 1
59+
store i8 0, i8* %a.gep.1
60+
%a.gep.5 = getelementptr i8, i8* %a, i32 5
61+
store i8 1, i8* %a.gep.5
62+
%b.gep.1 = getelementptr i8, i8* %b, i32 1
63+
store i8 2, i8* %b.gep.1
64+
%b.gep.5 = getelementptr i8, i8* %b, i32 5
65+
store i8 3, i8* %b.gep.5
66+
ret i32 %res
67+
}
68+
69+
; CHECK-LABEL: Function: test_bcmp_variable_size
70+
; CHECK: Just Ref: Ptr: i8* %a <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 %n)
71+
; CHECK-NEXT: Just Ref: Ptr: i8* %b <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 %n)
72+
; CHECK-NEXT: Just Ref: Ptr: i8* %a.gep.1 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 %n)
73+
; CHECK-NEXT: Just Ref: Ptr: i8* %a.gep.5 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 %n)
74+
; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.1 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 %n)
75+
; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.5 <-> %res = tail call i32 @bcmp(i8* %a, i8* %b, i64 %n)
76+
define i32 @test_bcmp_variable_size(i8* noalias %a, i8* noalias %b, i64 %n) {
77+
entry:
78+
%res = tail call i32 @bcmp(i8* %a, i8* %b, i64 %n)
79+
%a.gep.1 = getelementptr i8, i8* %a, i32 1
80+
store i8 0, i8* %a.gep.1
81+
%a.gep.5 = getelementptr i8, i8* %a, i32 5
82+
store i8 1, i8* %a.gep.5
83+
%b.gep.1 = getelementptr i8, i8* %b, i32 1
84+
store i8 2, i8* %b.gep.1
85+
%b.gep.5 = getelementptr i8, i8* %b, i32 5
86+
store i8 3, i8* %b.gep.5
87+
ret i32 %res
88+
}

llvm/test/Transforms/DeadStoreElimination/MSSA/libcalls.ll

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,63 @@ entry:
130130
%res = call i32 @memcmp(i8* nonnull %foo, i8* nonnull %stack.ptr, i64 %n)
131131
ret i32 %res
132132
}
133+
134+
declare i32 @bcmp(i8*, i8*, i64)
135+
136+
define i1 @test_bcmp_const_size(i8* noalias %foo) {
137+
; CHECK-LABEL: @test_bcmp_const_size(
138+
; CHECK-NEXT: entry:
139+
; CHECK-NEXT: [[STACK:%.*]] = alloca [10 x i8], align 1
140+
; CHECK-NEXT: [[STACK_PTR:%.*]] = bitcast [10 x i8]* [[STACK]] to i8*
141+
; CHECK-NEXT: store i8 49, i8* [[STACK_PTR]], align 1
142+
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr i8, i8* [[STACK_PTR]], i64 1
143+
; CHECK-NEXT: store i8 50, i8* [[GEP_1]], align 1
144+
; CHECK-NEXT: [[CALL:%.*]] = call i32 @bcmp(i8* nonnull dereferenceable(2) [[FOO:%.*]], i8* nonnull dereferenceable(2) [[STACK_PTR]], i64 2)
145+
; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[CALL]], 0
146+
; CHECK-NEXT: ret i1 [[RES]]
147+
;
148+
entry:
149+
%stack = alloca [10 x i8]
150+
%stack.ptr = bitcast [10 x i8]* %stack to i8*
151+
store i8 49, i8* %stack.ptr, align 1
152+
%gep.1 = getelementptr i8, i8* %stack.ptr, i64 1
153+
store i8 50, i8* %gep.1, align 1
154+
%gep.2 = getelementptr i8, i8* %stack.ptr, i64 2
155+
store i8 51, i8* %gep.2, align 1
156+
%gep.3 = getelementptr i8, i8* %stack.ptr, i64 3
157+
store i8 52, i8* %gep.3, align 1
158+
%call = call i32 @bcmp(i8* nonnull dereferenceable(2) %foo, i8* nonnull dereferenceable(2) %stack.ptr, i64 2)
159+
%res = icmp eq i32 %call, 0
160+
ret i1 %res
161+
}
162+
163+
define i1 @test_bcmp_variable_size(i8* noalias %foo, i64 %n) {
164+
; CHECK-LABEL: @test_bcmp_variable_size(
165+
; CHECK-NEXT: entry:
166+
; CHECK-NEXT: [[STACK:%.*]] = alloca [10 x i8], align 1
167+
; CHECK-NEXT: [[STACK_PTR:%.*]] = bitcast [10 x i8]* [[STACK]] to i8*
168+
; CHECK-NEXT: store i8 49, i8* [[STACK_PTR]], align 1
169+
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr i8, i8* [[STACK_PTR]], i64 1
170+
; CHECK-NEXT: store i8 50, i8* [[GEP_1]], align 1
171+
; CHECK-NEXT: [[GEP_2:%.*]] = getelementptr i8, i8* [[STACK_PTR]], i64 2
172+
; CHECK-NEXT: store i8 51, i8* [[GEP_2]], align 1
173+
; CHECK-NEXT: [[GEP_3:%.*]] = getelementptr i8, i8* [[STACK_PTR]], i64 3
174+
; CHECK-NEXT: store i8 52, i8* [[GEP_3]], align 1
175+
; CHECK-NEXT: [[CALL:%.*]] = call i32 @bcmp(i8* nonnull [[FOO:%.*]], i8* nonnull [[STACK_PTR]], i64 [[N:%.*]])
176+
; CHECK-NEXT: [[RES:%.*]] = icmp eq i32 [[CALL]], 0
177+
; CHECK-NEXT: ret i1 [[RES]]
178+
;
179+
entry:
180+
%stack = alloca [10 x i8]
181+
%stack.ptr = bitcast [10 x i8]* %stack to i8*
182+
store i8 49, i8* %stack.ptr, align 1
183+
%gep.1 = getelementptr i8, i8* %stack.ptr, i64 1
184+
store i8 50, i8* %gep.1, align 1
185+
%gep.2 = getelementptr i8, i8* %stack.ptr, i64 2
186+
store i8 51, i8* %gep.2, align 1
187+
%gep.3 = getelementptr i8, i8* %stack.ptr, i64 3
188+
store i8 52, i8* %gep.3, align 1
189+
%call = call i32 @bcmp(i8* nonnull %foo, i8* nonnull %stack.ptr, i64 %n)
190+
%res = icmp eq i32 %call, 0
191+
ret i1 %res
192+
}

0 commit comments

Comments
 (0)