Skip to content

Commit fd6ebea

Browse files
committed
[MemLoc] Support memcmp in MemoryLocation::getForArgument.
This patch adds support for memcmp in MemoryLocation::getForArgument. memcmp reads from the first 2 arguments up to the number of bytes of the third argument. Reviewed By: efriedma Differential Revision: https://reviews.llvm.org/D86725
1 parent 85dacca commit fd6ebea

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

llvm/lib/Analysis/MemoryLocation.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,29 @@ MemoryLocation MemoryLocation::getForArgument(const CallBase *Call,
210210
// LoopIdiomRecognizer likes to turn loops into calls to memset_pattern16
211211
// whenever possible.
212212
LibFunc F;
213-
if (TLI && Call->getCalledFunction() &&
214-
TLI->getLibFunc(*Call->getCalledFunction(), F) &&
215-
F == LibFunc_memset_pattern16 && TLI->has(F)) {
216-
assert((ArgIdx == 0 || ArgIdx == 1) &&
217-
"Invalid argument index for memset_pattern16");
218-
if (ArgIdx == 1)
219-
return MemoryLocation(Arg, LocationSize::precise(16), AATags);
220-
if (const ConstantInt *LenCI =
221-
dyn_cast<ConstantInt>(Call->getArgOperand(2)))
222-
return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
223-
AATags);
213+
if (TLI && TLI->getLibFunc(*Call, F) && TLI->has(F)) {
214+
switch (F) {
215+
case LibFunc_memset_pattern16:
216+
assert((ArgIdx == 0 || ArgIdx == 1) &&
217+
"Invalid argument index for memset_pattern16");
218+
if (ArgIdx == 1)
219+
return MemoryLocation(Arg, LocationSize::precise(16), AATags);
220+
if (const ConstantInt *LenCI =
221+
dyn_cast<ConstantInt>(Call->getArgOperand(2)))
222+
return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
223+
AATags);
224+
break;
225+
case LibFunc_memcmp:
226+
assert((ArgIdx == 0 || ArgIdx == 1) &&
227+
"Invalid argument index for memcmp");
228+
if (const ConstantInt *LenCI =
229+
dyn_cast<ConstantInt>(Call->getArgOperand(2)))
230+
return MemoryLocation(Arg, LocationSize::precise(LenCI->getZExtValue()),
231+
AATags);
232+
break;
233+
default:
234+
break;
235+
};
224236
}
225237
// FIXME: Handle memset_pattern4 and memset_pattern8 also.
226238

llvm/test/Analysis/BasicAA/libfuncs.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ target triple = "x86_64-apple-macosx10.7"
77
; CHECK: Just Ref: Ptr: i8* %a <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
88
; CHECK-NEXT: Just Ref: Ptr: i8* %b <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
99
; CHECK-NEXT: Just Ref: Ptr: i8* %a.gep.1 <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
10-
; CHECK-NEXT: Just Ref: Ptr: i8* %a.gep.5 <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
10+
; CHECK-NEXT: NoModRef: Ptr: i8* %a.gep.5 <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
1111
; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.1 <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
12-
; CHECK-NEXT: Just Ref: Ptr: i8* %b.gep.5 <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
12+
; CHECK-NEXT: NoModRef: Ptr: i8* %b.gep.5 <-> %res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)
1313
define i32 @test_memcmp_const_size(i8* noalias %a, i8* noalias %b) {
1414
entry:
1515
%res = tail call i32 @memcmp(i8* %a, i8* %b, i64 4)

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ define i32 @test_memcmp_const_size(i8* noalias %foo) {
8585
; CHECK-NEXT: store i8 49, i8* [[STACK_PTR]], align 1
8686
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr i8, i8* [[STACK_PTR]], i64 1
8787
; CHECK-NEXT: store i8 50, i8* [[GEP_1]], align 1
88-
; CHECK-NEXT: [[GEP_2:%.*]] = getelementptr i8, i8* [[STACK_PTR]], i64 2
89-
; CHECK-NEXT: store i8 51, i8* [[GEP_2]], align 1
90-
; CHECK-NEXT: [[GEP_3:%.*]] = getelementptr i8, i8* [[STACK_PTR]], i64 3
91-
; CHECK-NEXT: store i8 52, i8* [[GEP_3]], align 1
9288
; CHECK-NEXT: [[RES:%.*]] = call i32 @memcmp(i8* nonnull dereferenceable(2) [[FOO:%.*]], i8* nonnull dereferenceable(2) [[STACK_PTR]], i64 2)
9389
; CHECK-NEXT: ret i32 [[RES]]
9490
;

0 commit comments

Comments
 (0)