Skip to content

Commit

Permalink
[MemoryBuiltins] Added support for memalign
Browse files Browse the repository at this point in the history
memalign is older aligned_alloc.
  • Loading branch information
davidbolvansky committed Apr 20, 2021
1 parent 5ed8cea commit 319c9f6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions llvm/lib/Analysis/MemoryBuiltins.cpp
Expand Up @@ -103,6 +103,7 @@ static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
{LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
{LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
{LibFunc_aligned_alloc, {AlignedAllocLike, 2, 1, -1}},
{LibFunc_memalign, {AlignedAllocLike, 2, 1, -1}},
{LibFunc_calloc, {CallocLike, 2, 0, 1}},
{LibFunc_vec_calloc, {CallocLike, 2, 0, 1}},
{LibFunc_realloc, {ReallocLike, 2, 1, -1}},
Expand Down
52 changes: 52 additions & 0 deletions llvm/test/Transforms/InstCombine/deref-alloc-fns.ll
@@ -1,5 +1,7 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -instcombine -S < %s | FileCheck %s
; RUN: opt -mtriple=x86_64-unknown-linux-gnu < %s -instcombine -S | FileCheck %s --check-prefixes=CHECK,GNU


declare noalias i8* @malloc(i64)
declare noalias i8* @calloc(i64, i64)
Expand All @@ -8,6 +10,7 @@ declare noalias nonnull i8* @_Znam(i64) ; throwing version of 'new'
declare noalias nonnull i8* @_Znwm(i64) ; throwing version of 'new'
declare noalias i8* @strdup(i8*)
declare noalias i8* @aligned_alloc(i64, i64)
declare noalias i8* @memalign(i64, i64)

@.str = private unnamed_addr constant [6 x i8] c"hello\00", align 1

Expand Down Expand Up @@ -60,6 +63,15 @@ define noalias i8* @aligned_alloc_unknown_size_possibly_zero(i1 %c) {
ret i8* %call
}

define noalias i8* @aligned_alloc_unknown_align(i64 %align) {
; CHECK-LABEL: @aligned_alloc_unknown_align(
; CHECK-NEXT: [[CALL:%.*]] = tail call noalias dereferenceable_or_null(128) i8* @aligned_alloc(i64 [[ALIGN:%.*]], i64 128)
; CHECK-NEXT: ret i8* [[CALL]]
;
%call = tail call noalias i8* @aligned_alloc(i64 %align, i64 128)
ret i8* %call
}

declare noalias i8* @foo(i8*, i8*, i8*)

define noalias i8* @aligned_alloc_dynamic_args(i64 %align, i64 %size) {
Expand All @@ -78,6 +90,46 @@ define noalias i8* @aligned_alloc_dynamic_args(i64 %align, i64 %size) {
ret i8* %call
}

define noalias i8* @memalign_constant_size() {
; GNU-LABEL: @memalign_constant_size(
; GNU-NEXT: [[CALL:%.*]] = tail call noalias align 32 dereferenceable_or_null(512) i8* @memalign(i64 32, i64 512)
; GNU-NEXT: ret i8* [[CALL]]
;
%call = tail call noalias i8* @memalign(i64 32, i64 512)
ret i8* %call
}

define noalias i8* @memalign_unknown_size_nonzero(i1 %c) {
; GNU-LABEL: @memalign_unknown_size_nonzero(
; GNU-NEXT: [[SIZE:%.*]] = select i1 [[C:%.*]], i64 64, i64 128
; GNU-NEXT: [[CALL:%.*]] = tail call noalias align 32 i8* @memalign(i64 32, i64 [[SIZE]])
; GNU-NEXT: ret i8* [[CALL]]
;
%size = select i1 %c, i64 64, i64 128
%call = tail call noalias i8* @memalign(i64 32, i64 %size)
ret i8* %call
}

define noalias i8* @memalign_unknown_size_possibly_zero(i1 %c) {
; CHECK-LABEL: @memalign_unknown_size_possibly_zero(
; CHECK-NEXT: [[SIZE:%.*]] = select i1 [[C:%.*]], i64 64, i64 0
; CHECK-NEXT: [[CALL:%.*]] = tail call noalias i8* @memalign(i64 32, i64 [[SIZE]])
; CHECK-NEXT: ret i8* [[CALL]]
;
%size = select i1 %c, i64 64, i64 0
%call = tail call noalias i8* @memalign(i64 32, i64 %size)
ret i8* %call
}

define noalias i8* @memalign_unknown_align(i64 %align) {
; GNU-LABEL: @memalign_unknown_align(
; GNU-NEXT: [[CALL:%.*]] = tail call noalias dereferenceable_or_null(128) i8* @memalign(i64 [[ALIGN:%.*]], i64 128)
; GNU-NEXT: ret i8* [[CALL]]
;
%call = tail call noalias i8* @memalign(i64 %align, i64 128)
ret i8* %call
}

define noalias i8* @malloc_constant_size2() {
; CHECK-LABEL: @malloc_constant_size2(
; CHECK-NEXT: [[CALL:%.*]] = tail call noalias dereferenceable_or_null(80) i8* @malloc(i64 40)
Expand Down

0 comments on commit 319c9f6

Please sign in to comment.