Skip to content

Commit a1ca0d1

Browse files
[TLI] Add support for reallocarray
From the man page (e.g. https://man.archlinux.org/man/reallocarray.3.en): SYNOPSIS reallocarray(): Since glibc 2.29: _DEFAULT_SOURCE glibc 2.28 and earlier: _GNU_SOURCE STANDARDS reallocarray() None. HISTORY reallocarray() glibc 2.26. OpenBSD 5.6, FreeBSD 11.0.
1 parent 795b4ef commit a1ca0d1

File tree

7 files changed

+32
-2
lines changed

7 files changed

+32
-2
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3224,6 +3224,13 @@ def AllocA : GNULibBuiltin<"stdlib.h"> {
32243224
let AddBuiltinPrefixedAlias = 1;
32253225
}
32263226

3227+
// Available in glibc by default since since 2.29 and in GNU mode before.
3228+
def ReallocArray : GNULibBuiltin<"stdlib.h"> {
3229+
let Spellings = ["reallocarray"];
3230+
let Prototype = "void*(void*, size_t, size_t)";
3231+
}
3232+
3233+
32273234
// POSIX malloc.h
32283235

32293236
def MemAlign : GNULibBuiltin<"malloc.h"> {

clang/test/Sema/builtins-gnu-mode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ int stpncpy;
88
int strdup;
99
int strndup;
1010
int index;
11+
int reallocarray;
1112
int rindex;
1213
int bzero;
1314
int strcasecmp;

llvm/include/llvm/Analysis/TargetLibraryInfo.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,11 @@ TLI_DEFINE_ENUM_INTERNAL(reallocf)
20772077
TLI_DEFINE_STRING_INTERNAL("reallocf")
20782078
TLI_DEFINE_SIG_INTERNAL(Ptr, Ptr, SizeT)
20792079

2080+
/// void *reallocarray(void *ptr, size_t nmemb, size_t size);
2081+
TLI_DEFINE_ENUM_INTERNAL(reallocarray)
2082+
TLI_DEFINE_STRING_INTERNAL("reallocarray")
2083+
TLI_DEFINE_SIG_INTERNAL(Ptr, Ptr, SizeT, SizeT)
2084+
20802085
/// char *realpath(const char *file_name, char *resolved_name);
20812086
TLI_DEFINE_ENUM_INTERNAL(realpath)
20822087
TLI_DEFINE_STRING_INTERNAL("realpath")

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ static void initializeLibCalls(TargetLibraryInfoImpl &TLI, const Triple &T,
852852
TLI.setUnavailable(LibFunc_memrchr);
853853
TLI.setUnavailable(LibFunc_ntohl);
854854
TLI.setUnavailable(LibFunc_ntohs);
855+
TLI.setUnavailable(LibFunc_reallocarray);
855856
TLI.setUnavailable(LibFunc_reallocf);
856857
TLI.setUnavailable(LibFunc_roundeven);
857858
TLI.setUnavailable(LibFunc_roundevenf);

llvm/lib/Transforms/Utils/BuildLibCalls.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,21 @@ bool llvm::inferNonMandatoryLibFuncAttrs(Function &F,
577577
Changed |= setDoesNotCapture(F, 0);
578578
Changed |= setArgNoUndef(F, 1);
579579
break;
580+
case LibFunc_reallocarray:
581+
Changed |= setAllocFamily(F, "malloc");
582+
Changed |= setAllocKind(F, AllocFnKind::Realloc);
583+
Changed |= setAllocatedPointerParam(F, 0);
584+
Changed |= setAllocSize(F, 1, std::nullopt);
585+
Changed |= setAllocSize(F, 2, std::nullopt);
586+
Changed |= setOnlyAccessesInaccessibleMemOrArgMem(F);
587+
Changed |= setRetNoUndef(F);
588+
Changed |= setDoesNotThrow(F);
589+
Changed |= setRetDoesNotAlias(F);
590+
Changed |= setWillReturn(F);
591+
Changed |= setDoesNotCapture(F, 0);
592+
Changed |= setArgNoUndef(F, 1);
593+
Changed |= setArgNoUndef(F, 2);
594+
break;
580595
case LibFunc_read:
581596
// May throw; "read" is a valid pthread cancellation point.
582597
Changed |= setRetAndArgsNoUndef(F);

llvm/test/tools/llvm-tli-checker/ps4-tli-check.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
## the exact count first; the two directives should add up to that.
5555
## Yes, this means additions to TLI will fail this test, but the argument
5656
## to -COUNT can't be an expression.
57-
# AVAIL: TLI knows 522 symbols, 289 available
57+
# AVAIL: TLI knows 523 symbols, 289 available
5858
# AVAIL-COUNT-289: {{^}} available
5959
# AVAIL-NOT: {{^}} available
60-
# UNAVAIL-COUNT-233: not available
60+
# UNAVAIL-COUNT-234: not available
6161
# UNAVAIL-NOT: not available
6262

6363
## This is a large file so it's worth telling lit to stop here.

llvm/unittests/Analysis/TargetLibraryInfoTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
318318
"declare void @qsort(i8*, i64, i64, i32 (i8*, i8*)*)\n"
319319
"declare i64 @readlink(i8*, i8*, i64)\n"
320320
"declare i8* @realloc(i8*, i64)\n"
321+
"declare i8* @reallocarray(i8*, i64, i64)\n"
321322
"declare i8* @reallocf(i8*, i64)\n"
322323
"declare double @remainder(double, double)\n"
323324
"declare float @remainderf(float, float)\n"

0 commit comments

Comments
 (0)