Skip to content

Commit 7523550

Browse files
authored
[Clang][CodeGen] Add __builtin_bcopy (#67130)
Add __builtin_bcopy to the list of GNU builtins. This was causing a series of test failures in glibc. Adjust the tests to reflect the changes in codegen. Fixes #51409. Fixes #63065.
1 parent bc6e7f0 commit 7523550

File tree

6 files changed

+28
-8
lines changed

6 files changed

+28
-8
lines changed

clang/include/clang/Basic/Builtins.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ BUILTIN(__builtin_va_copy, "vAA", "n")
565565
BUILTIN(__builtin_stdarg_start, "vA.", "nt")
566566
BUILTIN(__builtin_assume_aligned, "v*vC*z.", "nctE")
567567
BUILTIN(__builtin_bcmp, "ivC*vC*z", "FnE")
568-
BUILTIN(__builtin_bcopy, "vv*v*z", "n")
568+
BUILTIN(__builtin_bcopy, "vvC*v*z", "nF")
569569
BUILTIN(__builtin_bzero, "vv*z", "nF")
570570
BUILTIN(__builtin_free, "vv*", "nF")
571571
BUILTIN(__builtin_malloc, "v*z", "nF")
@@ -1161,6 +1161,7 @@ LIBBUILTIN(strndup, "c*cC*z", "f", STRING_H, ALL_GNU_LANGUAGES)
11611161
LIBBUILTIN(index, "c*cC*i", "f", STRINGS_H, ALL_GNU_LANGUAGES)
11621162
LIBBUILTIN(rindex, "c*cC*i", "f", STRINGS_H, ALL_GNU_LANGUAGES)
11631163
LIBBUILTIN(bzero, "vv*z", "f", STRINGS_H, ALL_GNU_LANGUAGES)
1164+
LIBBUILTIN(bcopy, "vvC*v*z", "f", STRINGS_H, ALL_GNU_LANGUAGES)
11641165
LIBBUILTIN(bcmp, "ivC*vC*z", "fE", STRINGS_H, ALL_GNU_LANGUAGES)
11651166
// In some systems str[n]casejmp is a macro that expands to _str[n]icmp.
11661167
// We undefine then here to avoid wrong name.

clang/lib/AST/Decl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,6 +4356,10 @@ unsigned FunctionDecl::getMemoryFunctionKind() const {
43564356
case Builtin::BIbzero:
43574357
return Builtin::BIbzero;
43584358

4359+
case Builtin::BI__builtin_bcopy:
4360+
case Builtin::BIbcopy:
4361+
return Builtin::BIbcopy;
4362+
43594363
case Builtin::BIfree:
43604364
return Builtin::BIfree;
43614365

@@ -4387,6 +4391,8 @@ unsigned FunctionDecl::getMemoryFunctionKind() const {
43874391
return Builtin::BIstrlen;
43884392
if (FnInfo->isStr("bzero"))
43894393
return Builtin::BIbzero;
4394+
if (FnInfo->isStr("bcopy"))
4395+
return Builtin::BIbcopy;
43904396
} else if (isInStdNamespace()) {
43914397
if (FnInfo->isStr("free"))
43924398
return Builtin::BIfree;

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,20 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
37033703
Builder.CreateMemSet(Dest, Builder.getInt8(0), SizeVal, false);
37043704
return RValue::get(nullptr);
37053705
}
3706+
3707+
case Builtin::BIbcopy:
3708+
case Builtin::BI__builtin_bcopy: {
3709+
Address Src = EmitPointerWithAlignment(E->getArg(0));
3710+
Address Dest = EmitPointerWithAlignment(E->getArg(1));
3711+
Value *SizeVal = EmitScalarExpr(E->getArg(2));
3712+
EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(0)->getType(),
3713+
E->getArg(0)->getExprLoc(), FD, 0);
3714+
EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(1)->getType(),
3715+
E->getArg(1)->getExprLoc(), FD, 0);
3716+
Builder.CreateMemMove(Dest, Src, SizeVal, false);
3717+
return RValue::get(Dest.getPointer());
3718+
}
3719+
37063720
case Builtin::BImemcpy:
37073721
case Builtin::BI__builtin_memcpy:
37083722
case Builtin::BImempcpy:

clang/test/Analysis/bstring.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@ int memcmp8(char *a, size_t n) {
483483
//===----------------------------------------------------------------------===
484484

485485
#define bcopy BUILTIN(bcopy)
486-
// __builtin_bcopy is not defined with const in Builtins.def.
487-
void bcopy(/*const*/ void *s1, void *s2, size_t n);
486+
void bcopy(const void *s1, void *s2, size_t n);
488487

489488

490489
void bcopy0 (void) {

clang/test/Analysis/security-syntax-checks.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ int test_bcmp(void *a, void *b, size_t n) {
5757
}
5858

5959
// Obsolete function bcopy
60-
void bcopy(void *, void *, size_t);
60+
void bcopy(const void *, void *, size_t);
6161

62-
void test_bcopy(void *a, void *b, size_t n) {
62+
void test_bcopy(const void *a, void *b, size_t n) {
6363
bcopy(a, b, n); // expected-warning{{The bcopy() function is obsoleted by memcpy() or memmove(}}
6464
}
6565

clang/test/CodeGen/PowerPC/builtins-ppc-xlcompat-macros.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ void testalignx(const void *pointer) {
167167
}
168168

169169
// 64BIT-LABEL: @testbcopy(
170-
// 64BIT: call void @bcopy(ptr noundef {{%.*}}, ptr noundef {{%.*}}, i64 noundef {{%.*}})
170+
// 64BIT: call void @llvm.memmove.p0.p0.i64(ptr align 1 {{%.*}}, ptr align 1 {{%.*}}, i64 {{%.*}}, i1 false)
171171
// 64BIT-NEXT: ret void
172172
//
173173
// 32BIT-LABEL: @testbcopy(
174-
// 32BIT: call void @bcopy(ptr noundef {{%.*}}, ptr noundef {{%.*}}, i32 noundef {{%.*}})
174+
// 32BIT: call void @llvm.memmove.p0.p0.i32(ptr align 1 {{%.*}}, ptr align 1 {{%.*}}, i32 {{%.*}}, i1 false)
175175
// 32BIT-NEXT: ret void
176176
//
177177
void testbcopy(const void *src, void *dest, size_t n) {
178-
__bcopy(src, dest, n);
178+
bcopy(src, dest, n);
179179
}
180180

181181
// 64BIT-LABEL: @testbzero(

0 commit comments

Comments
 (0)